/home/desid573/public_html/payment/xero-2
Edit: /home/desid573/public_html/payment/xero-2/rgtiSend.php (7940B)
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);
}
}
// echo json_encode($storage, true);
// echo "
";
// echo $storage->getRefreshToken();
// echo "
";
// echo $storage->getExpires();
// echo "
";
// if ($storage->getHasExpired()) {
// $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->getRefreshToken()
// ]);
// // Save my token, expiration and refresh token
// $storage->setToken(
// $newAccessToken->getToken(),
// $newAccessToken->getExpires(),
// $xeroTenantId,
// $newAccessToken->getRefreshToken());
// } else {
// }
error_log($token);
$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";
$action = "none";
$str = '';
// $inv = submitInvoice("Lewis Cann", "", strtotime("now"), "Greenwood to Fremantle", ["total"=>100, "levy"=>0], "12344523432", "DD");
// $payment = submitPayment($inv["id"], 100, date("Y-m-d", strtotime("now")));
function getContact($xeroTenantId,$apiInstance, $name) {
global $xeroTenantId, $apiInstance;
try {
$where = 'Name=="'.$name.'"';
$result2 = $apiInstance->getContacts($xeroTenantId, null, $where);
if(count($result2->getContacts()) > 0) {
//print_r($result2);
error_log($result2->getContacts()[0]->getContactId());
return $result2->getContacts()[0]->getContactId();
} else {
return false;
}
//[/Contacts:Read]
if(count($result2->getContacts()) > 0) {
//print_r($result2);
return $result2->getContacts()[0]->getContactId();
} else {
return false;
}
} catch (\XeroAPI\XeroPHP\ApiException $e) {
error_log($e);
$error = AccountingObjectSerializer::deserialize(
$e->getResponseBody(),
'\XeroAPI\XeroPHP\Models\Accounting\Error',
[]
);
error_log($error);
}
}
function submitBill($name, $date, $amount, $type, $fileName) {
global $xeroTenantId, $apiInstance;
$contactId = getContact($xeroTenantId,$apiInstance, $name);
$dueDate = date("c", strtotime($date." + 14 days"));
$accountCode = "52001";
if($type == "FC" OR $type == "FA") {
//$theme = "f6369498-a39b-4f8a-aceb-b603392b1327";
$code = "PC";
} else if($type == "GC" or $type == "GA" or $type == "BA" or $type == "BF") {
$code = "QC";
} else if ($type == "SF" or $type == "SA") {
$code = "SC";
} else if ($type == "MF" or $type == "MA") {
$code = "MC";
}
$total = $amount;
//[Invoices:Create]
$lineitems = [];
$lineitem = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
$lineitem->setDescription("Subcontractor Payments")
->setQuantity(1)
->setItemCode($code)
->setUnitAmount($total)
->setAccountCode($accountCode);
array_push($lineitems, $lineitem);
// print_r($lineitems);
$contact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
if($contactId != false) {
$contact->setContactId($contactId);
} else {
$contact->setName($name);
$contact->setEmailAddress($email);
}
$invoice = new XeroAPI\XeroPHP\Models\Accounting\Invoice;
$invoice->setReference($ref)
->setDate(new DateTime('NOW'))
->setDueDate(new DateTime('+1 day'))
->setContact($contact)
->setLineItems($lineitems)
->setStatus(XeroAPI\XeroPHP\Models\Accounting\Invoice::STATUS_AUTHORISED)
->setType(XeroAPI\XeroPHP\Models\Accounting\Invoice::TYPE_ACCPAY)
->setLineAmountTypes(\XeroAPI\XeroPHP\Models\Accounting\LineAmountTypes::INCLUSIVE);
$invoices = new XeroAPI\XeroPHP\Models\Accounting\Invoices;
$arr_invoices = [];
array_push($arr_invoices, $invoice);
$invoices->setInvoices($arr_invoices);
try {
$result = $apiInstance->createInvoices($xeroTenantId,$invoices,false);
//print_r($result);
} catch (Exception $e) {
echo("Exception when calling AccountingApi->createInvoice: ". $e->getResponseBody());
}
$invNum = "";
//[/Invoices:Create]
$invoiceID = $result->getInvoices()[0]->getInvoiceId();
error_log($invoiceID);
$invNum = $result->getInvoices()[0]->getInvoiceNumber();
// if ($invNum != "") {
// return ["invNum" => $invNum, "invoiceID" => $invID];
// } else {
// return false;
// }
$includeOnline = true;
$handle = fopen($fileName, "r");
$body = fread($handle, filesize($fileName));
fclose($handle);
try {
$result = $apiInstance->createInvoiceAttachmentByFileName($xeroTenantId, $invoiceID, "contractorBill.pdf", $body, $includeOnline);
} catch (Exception $e) {
error_log('Exception when calling AccountingApi->createInvoiceAttachmentByFileName: '.$e->getMessage());
}
}
?>