QuickBooks PHP API添加付款错误

时间:2014-05-22 14:37:11

标签: php quickbooks quickbooks-online

我尝试使用Keith Palmer的PHP API通过付款更新QB发票。我正在做的是:

1)抓取发票信息(参见下面的代码)

2)添加付款

遵循以下代码示例:

https://github.com/consolibyte/quickbooks-php/blob/master/docs/partner_platform/example_app_ipp_v3/example_payment_add.php

我创造了这个:

function updateQBInvoice($invoiceNumber = null, $depositAmount = null) {
    $this->autoRender = false;
    Configure::write('debug', 2);

    include(APP."webroot".DS."quickbooks-php".DS."docs".DS."example_app_ipp_v3".DS."config.php");
    $IPP = new QuickBooks_IPP($dsn);
    $creds = $IntuitAnywhere->load($the_username, $the_tenant);
    $IPP->authMode(QuickBooks_IPP::AUTHMODE_OAUTH, $the_username, $creds);  
    $realm = $creds['qb_realm'];

    if ($Context = $IPP->context()) {
        $IPP->version(QuickBooks_IPP_IDS::VERSION_3);
        $InvoiceService = new QuickBooks_IPP_Service_Invoice();
        $invoices = $InvoiceService->query($Context, $realm, "SELECT * FROM Invoice WHERE DocNumber='".$invoiceNumber."'");
        $Invoice = $invoices[0];

        $docNumber = $Invoice->getDocNumber();
        $CustomerRef = $Invoice->getCustomerRef();
        $invoiceId = $Invoice->getId();

        $PaymentService = new QuickBooks_IPP_Service_Payment();

        $Payment = new QuickBooks_IPP_Object_Payment();
        $Payment->setPaymentRefNum($invoiceNumber);
        $Payment->setTxnDate(date('Y-m-d'));
        $Payment->setTotalAmt($depositAmount);

        $Line = new QuickBooks_IPP_Object_Line();
        $Line->setAmount($depositAmount);

        $LinkedTxn = new QuickBooks_IPP_Object_LinkedTxn();
        $LinkedTxn->setTxnId($invoiceId);
        $LinkedTxn->setTxnType('Invoice');

        $Line->setLinkedTxn($LinkedTxn);

        $Payment->addLine($Line);

        $Payment->setCustomerRef($CustomerRef);

        debug($Payment);

        if ($resp = $PaymentService->add($Context, $realm, $Payment)) {
            print('Our new Payment ID is: [' . $resp . ']');
        } else {
            print($PaymentService->lastError());
        }

        debug('Request [' . $PaymentService->lastRequest() . ']');
        debug('Response [' . $PaymentService->lastResponse() . ']');                
    } else {
        echo 'Unable to load a context...?';
    }
}

我注意到,在获取发票信息时,所有数字都是负数,如果我将它们分配给变量,则为{-123}。我的回答总是:

2030: [Invalid ID, Id should be a valid number. Supplied value:{-10643}]

我想我在某处设置了错误。有人会关心解释我做错了吗?谢谢!

更新:

请求:

    POST https://quickbooks.api.intuit.com/v3/company/210896252/payment HTTP/1.1
Content-Type: application/xml
Authorization: OAuth realm="", oauth_signature_method="HMAC-SHA1", oauth_signature="jxw2JuAGtvcg9ynRxXApi8VjcWk%3D",  oauth_nonce="uvJB5", oauth_timestamp="1400786567", oauth_token="xxx", oauth_consumer_key="xxx", oauth_version="1.0"
Content-Length: 414

<Payment xmlns="http://schema.intuit.com/finance/v3">
    <Line xmlns="http://schema.intuit.com/finance/v3">
        <Amount>45</Amount>
        <LinkedTxn xmlns="http://schema.intuit.com/finance/v3">
            <TxnId>{-10643}</TxnId>
            <TxnType>Invoice</TxnType>
        </LinkedTxn>
    </Line>
    <PaymentRefNum>2060</PaymentRefNum>
    <TxnDate>2014-05-22</TxnDate>
    <TotalAmt>45</TotalAmt>
    <CustomerRef>627</CustomerRef>
</Payment>

响应:

HTTP/1.1 400 Bad Request
Date: Thu, 22 May 2014 19:22:47 GMT
Content-Type: application/xml
Content-Length: 278
intuit_tid: c53e5e81-fd5e-4a24-b511-cd6b3449d012
Via: 1.1 ipp-gateway-.net
Vary: Accept-Encoding
Content-Encoding: gzip
Connection: close

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2014-05-22T12:22:47.834-07:00"><Fault type="ValidationFault"><Error code="2030" element="LinkedTxn.TxnId"><Message>Invalid ID</Message><Detail>Id should be a valid number. Supplied value:{-10643}</Detail></Error></Fault></IntuitResponse>

1 个答案:

答案 0 :(得分:1)

这突出了您的问题:

<LinkedTxn xmlns="http://schema.intuit.com/finance/v3">
            <TxnId>0</TxnId>
            <TxnType>Invoice</TxnType>
        </LinkedTxn>

由于某种原因,您没有为交易设置有效的Id值。

这可能是因为以下两个原因之一:

  • 您只是没有在代码中设置有效的TxnId(如果您打印出print($Invoice->getId());您看到了什么?)

OR

  • 您正在使用GitHub中过时的代码版本,其中包含与此相关的代码(请确保您使用的是GitHub中的最新代码,从今天开始)