first time invoice of xero developer is getting approve very easily, but if i have edited the invoice then it is not approving using php
function approveInvoice($access_token, $tenant_id, $invoiceID) {
$client = new Client();
$url = 'https://api.xero.com/api.xro/2.0/Invoices/'; . $invoiceID;
try {
// Fetch the existing invoice details to get complete data
$invoiceResponse = $client->get($url, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $access_token,
'Xero-tenant-id' => $tenant_id,
],
]);
// Ensure that the required fields are present
if (!isset($invoiceData['Invoices'][0])) {
return ['error' => 'Invalid invoice data fetched from Xero.'];
}
// Prepare the payload with the current invoice data and set status to 'AUTHORISED'
$invoice = $invoiceData['Invoices'][0];
$invoice['Status'] = 'AUTHORISED'; // Update the status to AUTHORISED
// Create the payload retaining necessary fields
$payload = [
'InvoiceID' => $invoiceID,
'Status' => 'AUTHORISED',
'LineAmountTypes' => $invoice['LineAmountTypes'], // Ensure this is set correctly (e.g., 'Exclusive', 'Inclusive', 'NoTax')
'Date' => $invoice['DateString'], // Ensure this date is in 'YYYY-MM-DD' format
'DueDate' => $invoice['DueDateString'], // Ensure this date is in 'YYYY-MM-DD' format
'Contact' => [
'ContactID' => $invoice['Contact']['ContactID'] // Make sure this ID is accurate and matches an existing contact in Xero
],
'LineItems' => array_map(function($item) {
return [
'Description' => $item['Description'],
'UnitAmount' => $item['UnitAmount'],
'TaxType' => $item['TaxType'],
'LineAmount' => $item['LineAmount'],
'Quantity' => $item['Quantity'],
'AccountCode' => $item['AccountCode'] ?? '400', // Adjust with the proper account code
'LineItemID' => $item['LineItemID'] // Include this if updating existing line items
];
}, $invoice['LineItems'])
];
// Log the payload
error_log('Payload being sent to Xero: ' . json_encode($payload, JSON_PRETTY_PRINT));
first time invoice of xero developer is getting approve very easily, but if i have edited the invoice then it is not approving using php
function approveInvoice($access_token, $tenant_id, $invoiceID) {
$client = new Client();
$url = 'https://api.xero.com/api.xro/2.0/Invoices/'; . $invoiceID;
try {
// Fetch the existing invoice details to get complete data
$invoiceResponse = $client->get($url, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $access_token,
'Xero-tenant-id' => $tenant_id,
],
]);
$invoiceData = json_decode($invoiceResponse->getBody()->getContents(), true);
// Ensure that the required fields are present
if (!isset($invoiceData['Invoices'][0])) {
return ['error' => 'Invalid invoice data fetched from Xero.'];
}
// Prepare the payload with the current invoice data and set status to 'AUTHORISED'
$invoice = $invoiceData['Invoices'][0];
$invoice['Status'] = 'AUTHORISED'; // Update the status to AUTHORISED
// Create the payload retaining necessary fields
$payload = [
'InvoiceID' => $invoiceID,
'Status' => 'AUTHORISED',
'LineAmountTypes' => $invoice['LineAmountTypes'], // Ensure this is set correctly (e.g., 'Exclusive', 'Inclusive', 'NoTax')
'Date' => $invoice['DateString'], // Ensure this date is in 'YYYY-MM-DD' format
'DueDate' => $invoice['DueDateString'], // Ensure this date is in 'YYYY-MM-DD' format
'Contact' => [
'ContactID' => $invoice['Contact']['ContactID'] // Make sure this ID is accurate and matches an existing contact in Xero
],
'LineItems' => array_map(function($item) {
return [
'Description' => $item['Description'],
'UnitAmount' => $item['UnitAmount'],
'TaxType' => $item['TaxType'],
'LineAmount' => $item['LineAmount'],
'Quantity' => $item['Quantity'],
'AccountCode' => $item['AccountCode'] ?? '400', // Adjust with the proper account code
'LineItemID' => $item['LineItemID'] // Include this if updating existing line items
];
}, $invoice['LineItems'])
];
// Log the payload
error_log('Payload being sent to Xero: ' . json_encode($payload, JSON_PRETTY_PRINT));
// Send the updated invoice data
$response = $client->post($url, [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $access_token,
'Xero-tenant-id' => $tenant_id,
],
'json' => $payload,
]);
return json_decode($response->getBody()->getContents(), true);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$responseBody = $e->getResponse()->getBody()->getContents();
error_log('Error: ' . $responseBody);
error_log('Error: ' . $e->getMessage());
return ['error' => 'Failed to approve the invoice. Error: ' . $e->getMessage()];
}
}