/home/desid573/public_html/payment/xero-2
Edit: /home/desid573/public_html/payment/xero-2/invoiceSend.php (11528B)
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 submitPayment($invID, $paymentCode, $date, $totalAmount, $reference) {
global $xeroTenantId, $apiInstance;
$invoice = new \XeroAPI\XeroPHP\Models\Accounting\Invoice;
$invoice->setInvoiceId($invID)->setType("ACCREC");
$account = new \XeroAPI\XeroPHP\Models\Accounting\Account;
$account->setCode(11111);
$payment = new \XeroAPI\XeroPHP\Models\Accounting\Payment;
$payment->setInvoice($invoice)->setAccount($account)->setDate(new DateTime('NOW'))->setAmount($totalAmount)->setReference($reference);
//print_r($payment);
$result = $apiInstance->createPayment($xeroTenantId,$payment, true);
//print_r($result);
return $result;
}
function submitInvoice($name, $email, $date, $invDesc, $amount, $ref, $type, $tollDesc, $driver_name) {
global $xeroTenantId, $apiInstance;
$contactId = getContact($xeroTenantId,$apiInstance, $name);
$dueDate = date("c", strtotime($date." + 14 days"));
if($type == "FC" OR $type == "FA" OR $type == "SA" OR $type == "SF" OR $type == "MA" OR $type == "MF" OR $type == "BA" OR $type == "BF" OR $type == "GC" OR $type == "GA" OR $type == "SC" OR $type == "YA") {
$theme = "f6369498-a39b-4f8a-aceb-b603392b1327";
$code = "FC";
$accountCode = "42000";
} else if($type == "DD") {
$theme = "ed5eea99-3c48-48e6-969d-3af875340a78";
$code = "DD";
$accountCode = "41000";
} else {
$theme = "1f1b02b8-1b00-4803-8b2e-e4ec4f578f27";
$code = $type;
if($type == "HK") {
$accountCode = "41600";
} else if ($type == "SI") {
$accountCode = "41500";
}
}
$total = $amount["total"];
$invDesc = str_replace( "\\r\\n", "
", $invDesc );
$itemCode = $type;
if($itemCode == "SI" or $itemCode == "HK" or $itemCode == "DD") {
//
} else {
$itemCode = "FC";
}
if($driver_name != "Shebah") {
if($type == "FC" OR $type == "FA" OR $type == "SA" OR $type == "SF" OR $type == "MA" OR $type == "MF" OR $type == "BA" OR $type == "BF" OR $type == "GC" OR $type == "GA" OR $type == "SC" OR $type == "YA") {
$total = $amount["total"] - $amount["levy"];
$levy = $amount["levy"];
$itemCode = "FC";
}
}
if($type == "DD" OR $type == "HK" OR $type == "SI") {
$total = $total = $amount["total"];
}
//[Invoices:Create]
$lineitems = [];
$lineitem = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
$lineitem->setDescription($invDesc)
->setQuantity(1)
->setItemCode($itemCode)
->setUnitAmount($total)
->setAccountCode($accountCode);
array_push($lineitems, $lineitem);
if($type == "FC" OR $type == "FA" AND $driver_name != "Shebah") {
} else if($type == "SA" OR $type == "SF" AND $driver_name != "Shebah") {
$lineitem = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
$lineitem->setDescription("NSW Passenger Service Levy")
->setQuantity(1)
->setItemCode("SL")
->setUnitAmount($levy)
->setAccountCode(57778);
array_push($lineitems, $lineitem);
} else if($type == "MA" OR $type == "MF" AND $driver_name != "Shebah") {
$lineitem = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
$lineitem->setDescription("Commercial Passenger Vehicle Service Levy")
->setQuantity(1)
->setItemCode("ML")
->setUnitAmount($levy)
->setAccountCode(57778);
array_push($lineitems, $lineitem);
}
if($amount["tolls"] != null) {
$lineitem = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
$tollsDesc = "Tolls:\r\n".$tollsDesc;
// $tollsDesc = str_replace( "\\r\\n", "
", $tollsDesc );
$lineitem->setDescription($tollsDesc)
->setQuantity(1)
->setItemCode("TT")
->setUnitAmount($amount["tolls"])
->setAccountCode(49999);
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('+2 weeks'))
->setContact($contact)
->setLineItems($lineitems)
->setStatus(XeroAPI\XeroPHP\Models\Accounting\Invoice::STATUS_AUTHORISED)
->setType(XeroAPI\XeroPHP\Models\Accounting\Invoice::TYPE_ACCREC)
->setLineAmountTypes(\XeroAPI\XeroPHP\Models\Accounting\LineAmountTypes::INCLUSIVE)
->setBrandingThemeID($theme);
$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]
$invID = $result->getInvoices()[0]->getInvoiceId();
$invNum = $result->getInvoices()[0]->getInvoiceNumber();
if ($invNum != "") {
return ["invNum" => $invNum, "invoiceID" => $invID];
} else {
return false;
}
}
?>