0

I am trying to build an online form that a user can enter their invoice number, the webserver will then interface with the Xero API to GET the invoice details and populate some additional fields for payment.

I have already got the form logic built and in place however I have no clue of how to start with the Xero API.

I've created a private app and have the required keys but I cannot find any documentation on how to actually go about setting up the API call in PHP.

Any help would be much appreciated.

**** UPDATE ****

I was able to get the API working using the following:

function GetInvData ($InvNumber) {

    try {
        $config = [
            'oauth' => [
                'consumer_key' => 'REDACTED',
                'rsa_private_key' => 'REDACTED',
            ],
        ];

        $xero = new PrivateApplication($config);
        //$invoice =  $xero->load(\XeroPHP\Models\Accounting\Invoice::class)->where('InvoiceNumber',$InvNumber)->execute();
        $invoice =  $xero->load('Accounting\Invoice')
                    ->where('InvoiceNumber',$InvNumber)
                    ->execute();

    }
    catch(\Exception $ex) {
        var_dump($ex);
    }

    return ($invoice);
}

However what is returned is the most complex piece of JSON I have ever laid eyes on.... Probably not saying much.

All I need to be able to extract is the the Invoice Amount, The Customer name and Their email address, can anybody assist with this?

XeroPHP\Remote\Collection Object
(
    [_associated_objects:protected] => 
    [storage:ArrayObject:private] => Array
        (
            [0] => XeroPHP\Models\Accounting\Invoice Object
                (
                    [_data:protected] => Array
                        (
                            [Type] => ACCREC
                            [Contact] => XeroPHP\Models\Accounting\Contact Object
                                (
                                    [_data:protected] => Array
                                        (
                                            [ContactID] => cf6eef48-cda3-4862-8518-4d631ea54c1c
                                            [ContactNumber] => 
                                            [AccountNumber] => 
                                            [ContactStatus] => 
                                            [Name] => REDACTED
                                            [FirstName] => 
                                            [LastName] => 
                                            [EmailAddress] => 
                                            [SkypeUserName] => 
                                            [ContactPersons] => 
                                            [BankAccountDetails] => 
                                            [TaxNumber] => 
                                            [AccountsReceivableTaxType] => 
                                            [AccountsPayableTaxType] => 
                                            [Addresses] => 
                                            [Phones] => 
                                            [IsSupplier] => 
                                            [IsCustomer] => 
                                            [DefaultCurrency] => 
                                            [XeroNetworkKey] => 
                                            [SalesDefaultAccountCode] => 
                                            [PurchasesDefaultAccountCode] => 
                                            [SalesTrackingCategories] => 
                                            [PurchasesTrackingCategories] => 
                                            [TrackingCategoryName] => 
                                            [TrackingCategoryOption] => 
                                            [PaymentTerms] => 
                                            [UpdatedDateUTC] => 
                                            [ContactGroups] => 
                                            [Website] => 
                                            [BrandingTheme] => 
                                            [BatchPayments] => 
                                            [Discount] => 
                                            [Balances] => 
                                            [HasAttachments] => 
                                        )

                                    [_dirty:protected] => Array
                                        (
                                        )

                                    [_associated_objects:protected] => Array
                                        (
                                            [Contact] => XeroPHP\Models\Accounting\Invoice Object
 *RECURSION*
                                        )

                                    [_application:protected] => 
                                )

                            [LineItems] => 
                            [Date] => DateTime Object
                                (
                                    [date] => 2019-03-05 00:00:00.000000
                                    [timezone_type] => 3
                                    [timezone] => Australia/Melbourne
                                )

                            [DueDate] => DateTime Object
                                (
                                    [date] => 2019-03-12 00:00:00.000000
                                    [timezone_type] => 3
                                    [timezone] => Australia/Melbourne
                                )

                            [LineAmountTypes] => Exclusive
                            [InvoiceNumber] => Inv-1521
                            [Reference] => Feb 2019
                            [BrandingThemeID] => c560d364-0331-4677-a529-8ce702559165
                            [Url] => 
                            [CurrencyCode] => AUD
                            [CurrencyRate] => 1
                            [Status] => AUTHORISED
                            [SentToContact] => 1
                            [ExpectedPaymentDate] => 
                            [PlannedPaymentDate] => 
                            [SubTotal] => 2150
                            [TotalTax] => 215
                            [Total] => 2365
                            [TotalDiscount] => 
                            [InvoiceID] => f5cfaed4-db9b-41f3-94e5-a025c2bc898a
                            [HasAttachments] => 
                            [Payments] => 
                            [Prepayments] => 
                            [Overpayments] => 
                            [AmountDue] => 2365
                            [AmountPaid] => 0
                            [FullyPaidOnDate] => 
                            [AmountCredited] => 0
                            [UpdatedDateUTC] => DateTime Object
                                (
                                    [date] => 2019-03-05 00:32:01.813000
                                    [timezone_type] => 3
                                    [timezone] => UTC
                                )

                            [CreditNotes] => 
                        )

                    [_dirty:protected] => Array
                        (
                        )

                    [_associated_objects:protected] => Array
                        (
                        )

                    [_application:protected] => XeroPHP\Application\PrivateApplication Object
                        (
                            [config:protected] => Array
                                (
                                    [xero] => Array
                                        (
                                            [site] => https://api.xero.com
                                            [base_url] => https://api.xero.com
                                            [core_version] => 2.0
                                            [payroll_version] => 1.0
                                            [file_version] => 1.0
                                            [model_namespace] => \XeroPHP\Models
                                        )

                                    [oauth] => Array
                                        (
                                            [signature_method] => RSA-SHA1
                                            [signature_location] => header
                                            [authorize_url] => https://api.xero.com/oauth/Authorize
                                            [request_token_path] => oauth/RequestToken
                                            [access_token_path] => oauth/AccessToken
                                            [consumer_key] => REDACTED
                                            [rsa_private_key] => REDACTED
                                            [token] => REDACTED
                                        )

                                    [curl] => Array
                                        (
                                            [10018] => XeroPHP
                                            [78] => 30
                                            [13] => 20
                                            [64] => 2
                                            [81] => 2
                                            [52] => 
                                            [10004] => 
                                            [10006] => 
                                            [10102] => 
                                        )

                                )

                            [oauth_client:protected] => XeroPHP\Remote\OAuth\Client Object
                                (
                                    [config:XeroPHP\Remote\OAuth\Client:private] => Array
                                        (
                                            [signature_method] => RSA-SHA1
                                            [signature_location] => header
                                            [authorize_url] => https://api.xero.com/oauth/Authorize
                                            [request_token_path] => oauth/RequestToken
                                            [access_token_path] => oauth/AccessToken
                                            [consumer_key] => REDACTED
                                            [rsa_private_key] => REDACTED
                                            [token] => REDACTED
                                        )

                                    [token_secret:XeroPHP\Remote\OAuth\Client:private] => 
                                    [verifier:XeroPHP\Remote\OAuth\Client:private] => 
                                )

                        )

                )

        )

)
Lewis M Hackfath
  • 131
  • 5
  • 17
  • Have you checked out the PHP SDK they refer to in their documentation? https://github.com/calcinai/xero-php#usage has examples how to find objects by different criteria. – 04FS Mar 13 '19 at 10:19
  • @04FS yes i have looked at this but there's no real guide on how to implement it. I'm so lost it isn't funny. – Lewis M Hackfath Mar 13 '19 at 10:23
  • 1
    There’s a couple of examples, https://github.com/calcinai/xero-php/tree/master/examples, and also an “example app”, https://github.com/XeroAPI/xero-php-sample-app If that isn’t enough to get you started and try some things, then I don’t know how to possibly help you here in a SO context. – 04FS Mar 13 '19 at 10:26
  • It seems like everything hinges on being able to install with composer. This client is hosted on a shared server and thus I do not believe you can use composer to install the package... – Lewis M Hackfath Mar 13 '19 at 10:32
  • 1
    Then you do that in your local dev environment, and only upload the resulting folder structure to your web host afterwards. You don’t need composer to actually _run_ this stuff. – 04FS Mar 13 '19 at 10:39
  • @04FS I've added some additional info to the original question if you are able to assist? thanks in advance – Lewis M Hackfath Mar 15 '19 at 05:19

2 Answers2

1

You're pretty much asking how to do the whole thing which people don't take kindly to here. Good job getting the API call done, it might take a while before you completely understand what goes on with each line but it's not required.

Now that you've got that part done you just need to work with the data that Xero gives you back, well rather what xero-php (which is the library you're using) gives you back. What you've posted is not JSON, it's just a dump (text representation) of the data as it exists as an object in your code.

That object is a Collection of Invoices, but it's just a singular invoice in your case. It will always return a Collection even if you have really specific search queries, but you can always make yourself a little check to make sure the Collection only has one invoice if you're only expecting one.

So, for what you actually want (Invoice Amount, The Customer name and Their email address), here's the next lines of code.

Invoice Amount:

$invoice->Invoices[0]->Total;

Customer Name:

$invoice->Invoices[0]->Contact->Name;

Email Address:

$invoice->Invoices[0]->Contact->EmailAddress;

Invoices[0] is used to grab the first Invoice in the Collection, so it assumes there's only one. Hopefully that points you in the right direction.

Tom Bryant
  • 165
  • 1
  • 12
0

Ended up Getting it working with the following:

function GetInvData ($InvNumber) {
global $xero;
$return = array('AmountDue' => null, 'Customer' => null, 'EmailAddress' => null);

try {
    //throw new Exception("Test");
    $invoices =  $xero->load(Accounting\Invoice::class)
                     ->where('InvoiceNumber',$InvNumber)
                     ->where('Status', 'AUTHORISED')
                     ->execute();

    $invoice = $invoices->first();

    if(empty($invoice)){
        $return = '';
    }
    else {                      
        $invoiceCount = count($invoice);
        if($invoiceCount > 1) {
            throw new \Exception("Managed to find [" . $invoiceCount . "] with Invoice ID [" . $InvNumber . "]. Expected this to be unique.");
        }
        $contact = $invoice->getContact();
        $return['AmountDue'] = $invoice->getAmountDue();
        $return['Customer'] = $contact->getName();
        $return['EmailAddress'] = $contact->getEmailAddress();  
    }   
}

catch(\Exception $ex) {
    $return = LogException($ex);
}

return $return;
}

Thanks everyone for the assistance.

Lewis M Hackfath
  • 131
  • 5
  • 17