/home/desid573/public_html/payment/xero-2
Edit: /home/desid573/public_html/payment/xero-2/addBillingContact.php (5247B)
query($sql);
$storage = $result->fetch_assoc();
error_log(time()." > ".$storage["expires"]);
$xeroTenantId = $storage["tenant_id"];
$token = $storage['token'];
if(time() > $storage["expires"]) {
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => '792F5D6E7E10404EA0BD0DA3CFF525BC',
'clientSecret' => '_w1yk8UQTp5QV9CzzDD4G3daZXjANQ40hQfyUZf91jUxemGF',
'redirectUri' => 'https://desidrivers.com.au/payment/xero-2/callback.php',
'urlAuthorize' => 'https://login.xero.com/identity/connect/authorize',
'urlAccessToken' => 'https://identity.xero.com/connect/token',
'urlResourceOwnerDetails' => 'https://api.xero.com/api.xro/2.0/Organisation'
]);
$newAccessToken = $provider->getAccessToken('refresh_token', [
'refresh_token' => $storage["refresh_token"]
]);
$token = $newAccessToken->getToken();
$sql = "INSERT INTO `xero_token`(`id`, `token`, `expires`, `tenant_id`, `refresh_token`, `id_token`) VALUES (NULL,'".$newAccessToken->getToken()."',".$newAccessToken->getExpires().",'".$storage["tenant_id"]."','".$newAccessToken->getRefreshToken()."','".$newAccessToken->getValues()["id_token"]."')";
error_log($sql);
$query = $conn->query($sql);
if(!$query) {
error_log($conn->error);
}
}
$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken($token);
$config->setHost("https://api.xero.com/api.xro/2.0");
$apiInstance = new XeroAPI\XeroPHP\Api\AccountingApi(
new GuzzleHttp\Client(),
$config
);
$endpoint = "Accounts";
function checkName() {
global $xeroTenantId, $apiInstance;
try {
$where = 'Name=="'.$_POST['name'].'"';
$result2 = $apiInstance->getContacts($xeroTenantId, null, $where);
if(count($result2->getContacts()) > 0) {
//print_r($result2);
error_log($result2->getContacts()[0]->getContactId());
updateContact($result2->getContacts()[0]->getContactId());
} else {
createContact();
}
} catch (\XeroAPI\XeroPHP\ApiException $e) {
error_log($e);
$error = AccountingObjectSerializer::deserialize(
$e->getResponseBody(),
'\XeroAPI\XeroPHP\Models\Accounting\Error',
[]
);
error_log($error);
}
}
function updateContact($contactID) {
global $xeroTenantId, $apiInstance;
$contact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
$contact->setContactID($contactID);
if($_POST['name'] != "") {
$contact->setName($_POST["name"]);
}
if($_POST['email'] != "") {
$contact->setEmailAddress($_POST['email']);
}
if($_POST['line2'] != "") {
$address = new XeroAPI\XeroPHP\Models\Accounting\Address;
$address
->setAddressLine1($_POST['line1'])
->setCity($_POST['line2']);
$contact->setAddresses(array($address));
}
$contact->setTaxNumber($_POST["abn"]);
$contact->setBankAccountDetails($_POST["bsb"].$_POST["acc"]);
$contacts = new XeroAPI\XeroPHP\Models\Accounting\Contacts;
$arr_contacts = [];
array_push($arr_contacts, $contact);
$contacts->setContacts($arr_contacts);
try {
$result = $apiInstance->updateContact($xeroTenantId, $contactID, $contacts);
} catch (Exception $e) {
error_log('Exception when calling AccountingApi->updateContact: '.$e->getMessage());
}
}
function createContact() {
global $xeroTenantId, $apiInstance;
$contact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
if($_POST['name'] != "") {
$contact->setName($_POST["name"]);
}
if($_POST['email'] != "") {
$contact->setEmailAddress($_POST['email']);
}
if($_POST['line2'] != "") {
$address = new XeroAPI\XeroPHP\Models\Accounting\Address;
$address
->setAddressLine1($_POST['line1'])
->setCity($_POST['line2']);
$contact->setAddresses(array($address));
}
$contact->setTaxNumber($_POST["abn"]);
if($_POST["bsb"] != ""){
$contact->setBankAccountDetails($_POST["bsb"].$_POST["acc"]);
}
$contacts = new XeroAPI\XeroPHP\Models\Accounting\Contacts;
$arr_contacts = [];
array_push($arr_contacts, $contact);
$contacts->setContacts($arr_contacts);
try {
$result = $apiInstance->createContacts($xeroTenantId, $contacts, $summarizeErrors);
} catch (Exception $e) {
error_log('Exception when calling AccountingApi->createContacts: '.$e->getMessage());
}
}