phpinvoicexero-api

Xero Api how to mark an invoice has been paid


I can successfully create an invoice thorugh the following code

$invoice = new Invoice;
        $invoice->setReference('Ref-' . $this->getRandNum())
            ->setDate(new DateTime(\Carbon\Carbon::parse($payment->created_at)->format('Y-m-d')))
            ->setDueDate(new DateTime(\Carbon\Carbon::parse($payment->created_at)->format('Y-m-d')))
            ->setContact($contact)
            ->setLineItems($lineitems)
            ->setStatus(Invoice::STATUS_AUTHORISED)
            ->setType(Invoice::TYPE_ACCREC)
            ->setLineAmountTypes(LineAmountTypes::INCLUSIVE)
            ->setAmountPaid($payment->total / 100)
            ->setInvoiceNumber($payment->stripe_id)
            ->setFullyPaidOnDate(\Carbon\Carbon::parse($payment->created_at)->format('Y-m-d\TH:i:s'));

        return $this->accountingApi->createInvoices($this->getTenantId(), $invoice);

The invoice status is set to Awaiting payment, even through I have used the function

->setFullyPaidOnDate(\Carbon\Carbon::parse($payment->created_at)->format('Y-m-d\TH:i:s'));

Is there any other code that should be executed to mark the invoice as paid.

Thanks Danny

Update

As advised I have added the create payment code and I get the following exception:

[400] Client error: `POST https://api.xero.com/api.xro/2.0/Payments` resulted in a `400 Bad Request` response:
{
  "ErrorNumber": 10,
  "Type": "ValidationException",
  "Message": "A validation exception occurred",
  "Elements" (truncated...)
 {"userId":1,"exception":"[object] (XeroAPI\\XeroPHP\\ApiException(code: 400): [400] Client error: `POST https://api.xero.com/api.xro/2.0/Payments` resulted in a `400 Bad Request` response:
{
  \"ErrorNumber\": 10,
  \"Type\": \"ValidationException\",
  \"Message\": \"A validation exception occurred\",
  \"Elements\" (truncated...)

The error message gets truncated and there is no way of knowing what the issue is. Is there a console on Xero's end to view these errors?

The following is my code:

$invoice = $this->createXeroInvoice($payment, $contact);
$newAcct = $this->getBankAccount();
$accountId = $newAcct->getAccounts()[0]->getAccountId();
$arr_payments = [];

$bankaccount = new Account;
$bankaccount->setAccountID($accountId);

$xero_payment = new Payment;
$xero_payment->setInvoice($invoice)
    ->setAccount($bankaccount)
    ->setAmount($payment->total / 100);

array_push($arr_payments, $xero_payment);
$xero_payments = new Payments;
$xero_payments->setPayments($arr_payments);

$result = $this->accountingApi->createPayment($this->getTenantId(), $xero_payments);

       

Solution

  • To mark an invoice as paid you will need to create a payment and apply it to the invoice. https://developer.xero.com/documentation/api/payments

    I'm not a PHP developer so I can't offer much guidance on exactly how to do this in your program I'm afraid but I think you can find an example of how to do it here: https://github.com/XeroAPI/xero-php-oauth2-app/blob/0d30486d3eda5f8cff9276fe507daf05541744ed/example.php#L1966