I'm trying to update a QB invoice with a payment using the PHP API by Keith Palmer. What I'm doing is:
1) Grab the invoice information (See code bellow)
2) Add a payment
Following this code example:
I've created this:
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...?';
}
}
What I've noticed, when getting the invoice info is, all numbers are negatives, and if I assigned them to variable they are {-123}. My response is always:
2030: [Invalid ID, Id should be a valid number. Supplied value:{-10643}]
I imagine I'm setting something wrong somewhere. Would someone care to explain what I'm doing wrong please? Thanks!
UPDATE:
Request:
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>
Response:
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>
This highlights your problem here:
<LinkedTxn xmlns="http://schema.intuit.com/finance/v3">
<TxnId>0</TxnId>
<TxnType>Invoice</TxnType>
</LinkedTxn>
You're not setting a valid Id value for the transaction for some reason.
This is probably because of one of two reasons:
print($Invoice->getId());
what do you see?)OR