I am trying to set up a process in which an invoice is added to our Quickbooks account, and if the customer is flagged as paid, it then sets up payment to show that payment. The invoice setting up works as expected, except that when I try to get the Invoice Id to be used in setting up the payment, it does not seem to be setting it.
Here's the code:
if(isset($companyName) && $companyName == $customerName) {
$InvoiceService = new QuickBooks_IPP_Service_Invoice();
$Invoice = new QuickBooks_IPP_Object_Invoice();
$Invoice->setDocNumber(1000 + $transaction->salesHistoryId);
$Invoice->setTxnDate($transaction->dateOrdered);
$Line = new QuickBooks_IPP_Object_Line();
$Line->setDescription('Annual Payment');
$Line->setDetailType('SalesItemLineDetail');
$Line->setAmount($transaction->contractPrice);
$SalesItemLineDetail = new QuickBooks_IPP_Object_SalesItemLineDetail();
$SalesItemLineDetail->setItemRef('7');
$SalesItemLineDetail->setUnitPrice(money_format('%i',$transaction->contractPrice));
$SalesItemLineDetail->setQty(1);
$Line->addSalesItemLineDetail($SalesItemLineDetail);
$Invoice->addLine($Line);
$Invoice->setCustomerRef($customerId);
$invoiceId = $Invoice->getId();
echo ($invoiceId);
$resp = $InvoiceService->add($Context, $realm, $Invoice);
}
if($transaction->paid == '1'){
$PaymentService = new QuickBooks_IPP_Service_Payment();
$Payment = new QuickBooks_IPP_Object_Payment();
$Payment->setPaymentRefNum('Invoice #'. (1000 + $transaction->salesHistoryId));
$Payment->setTxnDate($transaction->dateOrdered);;
$Payment->setTotalAmt($transaction->contractPrice);
$Line = new QuickBooks_IPP_Object_Line();
$Line->setAmount($transaction->contractPrice);
$LinkedTxn = new QuickBooks_IPP_Object_LinkedTxn();
$LinkedTxn->setTxnId($invoiceId);
$LinkedTxn->setTxnType('Invoice');
$Line->setLinkedTxn($LinkedTxn);
$Payment->addLine($Line);
$Payment->setCustomerRef($customerId);
$resp = $PaymentService->add($Context, $realm, $Payment);
}
The results of my IPP->lastRequest():
<Payment xmlns="http://schema.intuit.com/finance/v3">
<Line xmlns="http://schema.intuit.com/finance/v3">
<Amount>1295</Amount>
<LinkedTxn xmlns="http://schema.intuit.com/finance/v3">
<TxnId></TxnId>
<TxnType>Invoice</TxnType>
</LinkedTxn>
</Line>
<PaymentRefNum>Invoice #3310</PaymentRefNum>
<TxnDate>2015-06-14 00:00:00</TxnDate>
<TotalAmt>1295</TotalAmt>
<CustomerRef>1930</CustomerRef>
</Payment>HTTP/1.1 400 Bad Request
Date: Mon, 15 Jun 2015 17:24:45 GMT
Content-Type: application/xml
Content-Length: 270
intuit_tid: 2b6330f0-3df5-4336-911d-0ed43acafdd0
Vary: Accept-Encoding
Via: 1.1 ipp-gateway-ap06
Content-Encoding: gzip
Connection: close
The result of IPP->lastResponse():
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2015-06-15T10:24:41.206-07:00"><Fault type="ValidationFault"><Error code="2030" element="LinkedTxn.TxnId"><Message>Invalid ID</Message><Detail>Id should be a valid number. Supplied value:</Detail></Error></Fault></IntuitResponse><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
I'm guessing that since:
<LinkedTxn xmlns="http://schema.intuit.com/finance/v3">
<TxnId></TxnId>
<TxnType>Invoice</TxnType>
</LinkedTxn>
shows no TxnId, that that is the problem, but I'm unsure of how to fix this. Any help is appreciated. Thank you!
New invoices don't have an
Id
value until after they are created. It's just like any normal auto-increment id value in an SQL database.So if you look at your code here:
The value of
$invoiceId
is always going to benull
, because you haven't created the invoice yet. You create it two lines later. So, it doesn't have anId
yet. It can't. It doesn't exist.$InvoiceService
returns the invoiceId
value, so just change your code to this:And you'll be all set.