/home/desid573/public_html/angel/payment/xero
Edit: /home/desid573/public_html/angel/payment/xero/getInvoice.php (4557B)
";
echo $storage->getRefreshToken();
echo "
";
echo $storage->getExpires();
echo "
";
$xeroTenantId = (string)$storage->getSession()['tenant_id'];
if ($storage->getHasExpired()) {
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => '0019D1B665CC45AE9D0B78C189A265AC',
'clientSecret' => 'thiiEp0E7ZSZPmQW58Oe9x3Ek_hmmd9fggVtdUD0EpFlZVII',
'redirectUri' => 'https://desidrivers.com.au/angel/payment/xero/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 {
}
$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken( (string)$storage->getSession()['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", 100, "12344523432", "DD");
$payment = submitPayment($inv["id"], 100, date("Y-m-d", strtotime("now")));
function getContact($xeroTenantId,$apiInstance, $name) {
global $xeroTenantId, $apiInstance;
$where = 'Name=="'.$name.'"';
$result2 = $apiInstance->getContacts($xeroTenantId, null, $where);
//[/Contacts:Read]
if(count($result2->getContacts()) > 0) {
//print_r($result2);
return $result2->getContacts()[0]->getContactId();
} else {
return false;
}
}
function submitPayment($invID, $amount, $date) {
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($date)->setAmount($amount);
$result = $apiInstance->createPayment($xeroTenantId,$payment, true);
echo $result;
}
function submitInvoice($name, $email, $date, $invDesc, $amount, $ref, $type) {
global $xeroTenantId, $apiInstance;
$contactId = getContact($xeroTenantId,$apiInstance, $name);
//[Invoices:Create]
$lineitems = [];
$lineitem = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
$lineitem->setDescription($invDesc)
->setQuantity(1)
->setItemCode("DD")
->setUnitAmount($amount)
->setAccountCode("41000");
array_push($lineitems, $lineitem);
$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(date('Y-m-d',$date)))
->setDueDate(new DateTime(date('Y-m-d', $date + 14 * 24 * 60 * 60)))
->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);
$result = $apiInstance->createInvoice($xeroTenantId,$invoice, true);
$invNum = "";
//[/Invoices:Create]
$invID = $result->getInvoices()[0]->getInvoiceId();
$invNum = $result->getInvoices()[0]->getInvoiceNumber();
if ($invNum != "") {
return ["invNum" => $invNum, "id" => $invID];
} else {
return false;
}
}
?>