使用Xero API获取发票数据-专用应用程序

时间:2019-03-13 10:10:18

标签: php xero-api

我正在尝试构建一个在线表单,用户可以输入其发票编号,然后网络服务器将与Xero API交互以获取发票详细信息并填充一些其他付款字段。

我已经构建好了表单逻辑,但是我不知道如何从Xero API开始。

我已经创建了一个私人应用程序并具有必需的密钥,但是我找不到任何有关如何实际在PHP中设置API调用的文档。

任何帮助将不胜感激。

****更新****

我可以使用以下方法使API正常工作:

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);
}

然而,返回的内容是我所看过的最复杂的JSON。...可能不多说。

我需要提取的是发票金额,客户名称和他们的电子邮件地址,有人可以帮忙吗?

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] => 
                                )

                        )

                )

        )

)

2 个答案:

答案 0 :(得分:1)

您几乎是在问怎么做整个事情,而这是人们不喜欢的。完成API调用的工作不错,可能需要一段时间才能完全了解每一行的内容,但这不是必需的。

现在您已经完成了这部分工作,您只需要使用Xero退还给您的数据,而不是xero-php(您正在使用的库)退还给您什么。您发布的不是JSON,而是数据的转储(文本表示形式),因为它作为代码中的对象存在。

该对象是发票的集合,但是在您的情况下,它只是一个发票。即使您确实有特定的搜索查询,它也始终会返回一个集合,但是如果您只希望有一个发票,则可以随时进行一点检查以确保该集合只有一张发票。

因此,对于您实际想要的内容(发票金额,客户名称及其电子邮件地址),下面是下面的代码行。

发票金额:

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

客户名称:

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

电子邮件地址:

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

发票[0]用于获取集合中的第一张发票,因此假定只有一个。希望这会为您指明正确的方向。

答案 1 :(得分:0)

最终完成了以下工作:

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;
}

感谢大家的帮助。