XeroAPI / xero-php-oauth2

Xero PHP SDK for oAuth 2 generated from Xero API OpenAPI Spec 3.0
MIT License
90 stars 64 forks source link

Creating Credit Notes of Type ACCRECCREDIT missing CreditNoteId in response and not found in Xero UI #275

Closed rabrowne85 closed 2 years ago

rabrowne85 commented 2 years ago

SDK you're using (please complete the following information):

Describe the bug When using the SDK to create a credit note with type ACCRECCREDIT the request is processed as successful (200 status code), but the response is returning the CreditNoteId as all zeros (i.e. 00000000-0000-0000-0000-000000000000) and cannot be found in the Xero Demo Company. The status is confirmed in the API History of the developer area.

To Reproduce Generally following the example from: updateOrCreateCreditNotes.

$contact = new Contact;
$contact->setContactID($internalAccount->xero_id);

$lineItems = [];

// Add all the items to the credit note:
$event->invoice->items->each(function ($item) use (&$lineItems, $salesCode) {
    $lineItems[] = new LineItem([
        'item_code' => $item->xero_id,
        'quantity' => $item->qty,
        'account_code' => $salesCode,
        'unit_amount' => $item->cost,
        'tax_type' => $item->taxRate->xero_code,
        'description' => $item->description,
        'discount_rate' => (true === $item->percentage)
            ? $item->discount
            : null,
        'discount_amount' => (false === $item->percentage)
            ? $item->discount
            : null,
    ]);
});

$invoice = new CreditNote([
    'type' => CreditNote::TYPE_ACCRECCREDIT,
    'invoice_number' => $event->invoice->prefixedId,
    'contact' => $contact,
    'date' => $event->invoice->invoice_date,
    'line_items' => $lineItems,
    'reference' => $event->invoice->sale->ref,
    'status' => $event->invoice->state->xeroState(),
    'currency_code' => $event->invoice->currency->iso,
]);

// Add the Xero ID if we have it and are therefore updating:
if (null !== $event->invoice->xero_id) {
    $invoice->setCreditNoteId($event->invoice->xero_id);
}

$invoices = new CreditNotes;
$invoices->setCreditNotes([$invoice]);

$result = $xero->updateOrCreateCreditNotes($this->credentials->getTenantId(), $invoices, false, 4);
echo($result);

Expected behavior In the $result object, I would expect to get the unique CreditNoteId and be able to find the credit note in the Xero interface against the contact.

Screenshots Not applicable

Additional context I am using Laravel 9, but the $xero variable is resolved to: \XeroAPI\XeroPHP\Api\AccountingApi::class. I started with version 1.12.1 but updated to 1.13.0 to confirm the update hadn't corrected the issue and it has not.

Sallyhornet commented 2 years ago

It sounds like you may have been using SummarizeErrors=false. Using this paramater, you would get a code 200 but also a validation error if the credit note failed.

You can view your logs at https://developer.xero.com/myapps/

Once logged in, select your App and click on the "history" tab. Here you will be able to filter the logs by method, status, date and endpoint.

Successful calls will have the response redacted, but this is particularly useful for looking at your unsuccessful calls.

If you are still unclear as to why the credit note is not in Xero, please can you reach out to the support team using this link https://developer.xero.com/contact-xero-developer-platform-support/ with details of your app's client id and the timestamp of the call (with your timezone)

rabrowne85 commented 2 years ago

@Sallyhornet thanks for the information. I am indeed using the SummarizeErrors = false as I understood this to give me information on all items individually, as opposed to a general response, which it does. However, I did not realise that it also always gives a status 200, hence my confusion. You are right that there is a validation error, the line_amount does not tally to the expected discounted price. Again I understood this to be calculated based on the other information provided if not specifically set, but it is not.

As I now have a way to debug further, I will close this issue. Thanks again for your help.