We are creating overpayments via the PHP Xero API. In some cases, the overpayments are being duplicated. Here is the code we are using:
<?php
$endpoint = 'https://api.xero.com/api.xro/2.0/BankTransactions';
$headers = array(
"Content-Type: application/json",
"Xero-tenant-id: " . $xero_access['tenant_id'],
"Authorization: Bearer " . $xero_access['token'],
);
$postFields = array(
"Type" => "RECEIVE-OVERPAYMENT",
'Contact' => ['ContactID' => $contactID],
'BankAccount' => ['accountID' => $xero_settings['account_id']],
'LineAmountTypes' => 'NoTax',
'LineItems' => [0 => [
'Description' => 'Customer Credit',
'LineAmount' => $price
]]
);
try {
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_URL, $endpoint);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
@curl_setopt($ch, CURLOPT_POST, 1);
@curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields, JSON_PRETTY_PRINT));
$response = @curl_exec($ch);
$status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
error_log($status_code);
error_log($response);
} finally {
@curl_close($ch);
}
?>
I am not quite sure why the transactions are being duplicated or even if it is a network issue, or something in the code above.
Is there a way to make these API calls unique and make sure a request is not sent twice?
Thanks