【问题标题】:Get invoice data with Xero API - Private App使用 Xero API - Private App 获取发票数据
【发布时间】:2019-03-13 10:10:18
【问题描述】:

我正在尝试构建一个在线表单,用户可以输入他们的发票号码,然后网络服务器将与 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] => 
                                )

                        )

                )

        )

)

【问题讨论】:

  • 您是否查看过他们在文档中引用的 PHP SDK? github.com/calcinai/xero-php#usage 提供了如何按不同条件查找对象的示例。
  • @04FS 是的,我看过这个,但没有关于如何实现它的真正指南。我迷路了,这不好笑。
  • 有几个示例,github.com/calcinai/xero-php/tree/master/examples,还有一个“示例应用”,github.com/XeroAPI/xero-php-sample-app 如果这还不足以让您开始尝试一些事情,那么我不知道如何在 SO 上下文中为您提供帮助。
  • 似乎一切都取决于能否使用 composer 进行安装。此客户端托管在共享服务器上,因此我不相信您可以使用 composer 来安装包...
  • 然后您在本地开发环境中执行此操作,然后仅将生成的文件夹结构上传到您的网络主机。你不需要 composer 来真正运行这些东西。

标签: php xero-api


【解决方案1】:

您几乎是在问如何做人们不喜欢这里的整个事情。很好地完成了 API 调用,您可能需要一段时间才能完全理解每一行的内容,但这不是必需的。

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

该对象是发票集合,但在您的情况下它只是一张单张发票。即使您有非常具体的搜索查询,它也会始终返回一个集合,但您可以随时检查一下,以确保集合只有一张发票,如果您只期待一张发票。

所以,对于您真正想要的(发票金额、客户姓名和他们的电子邮件地址),下面是代码行。

发票金额:

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

客户姓名:

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

电子邮件地址:

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

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

【讨论】:

    【解决方案2】:

    最终让它与以下内容一起工作:

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

    感谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多