Thomas Jaeger
Thomas Jaeger

Reputation: 943

How to apply a payment to an invoice with the QBO API v3 in C#?

How do I apply a payment to an existing invoice with the QuickBooks Online API v3 in C#?

I have tried the following code:

    private void UpdateInvoice(Payment payment)
    {
        Invoice oldInvoice = cmbInvoices.SelectedItem as Invoice;
        if (oldInvoice == null) return;
        Invoice invoiceToUpdate = new Invoice();
        invoiceToUpdate.Id = oldInvoice.Id;
        invoiceToUpdate.SyncToken = oldInvoice.SyncToken;
        invoiceToUpdate.Deposit = payment.TotalAmt;
        invoiceToUpdate.DepositSpecified = true;
        invoiceToUpdate.sparse = true;
        invoiceToUpdate.sparseSpecified = true;
        invoiceToUpdate.PaymentType = PaymentTypeEnum.CreditCard;
        invoiceToUpdate.PaymentTypeSpecified = true;
        invoiceToUpdate.PaymentMethodRef = payment.PaymentMethodRef;
        invoiceToUpdate.PaymentRefNum = payment.PaymentRefNum;
        invoiceToUpdate.CustomerRef = oldInvoice.CustomerRef;
        QBO.DataService.Update(invoiceToUpdate);
    }

and it fails with this error:

{"ValidationException was thrown."}

I'm clearly missing something here or I must be doing something wrong. The error message is useless and wish Intuit would clean this up.

I'm getting pretty frustrated with the bad documentation that Intuit puts out for their QuickBooks Online API v3 and wish their would start cleaning it up and making it useful. Also, please remove those dead 404 pages as well while you are at it.

Any help or pointers would be awesome.

Upvotes: 4

Views: 3012

Answers (2)

SlaterCodes
SlaterCodes

Reputation: 1139

you need to just create a payment object and associate it with the invoice. It works successfully for my QBO v3 setup.

You are linking the Invoice in the "LinkedTxn" area, so my InvoiceId would be 1282 in this example. My ProccessPayment is false because we're handling payments manually and logging in QBO.

Payment p = new Payment
{
               PaymentRefNum = "73453",
               PaymentTypeSpecified = true,
               PaymentType = PaymentTypeEnum.CreditCard,
               UnappliedAmt = 0,
               UnappliedAmtSpecified = true,
               TotalAmt = amount,
               TotalAmtSpecified = true,
               ProcessPayment = false,
               ProcessPaymentSpecified = true,
               TxnDateSpecified = true,
               TxnDate = charge.Created,
               CurrencyRef = new ReferenceType { name = "US Dollar", Value = "USD" },
               Line = new Line[] { new Line
               {
                              Amount = amount,
                              AmountSpecified = true,
                              DetailType = LineDetailTypeEnum.PaymentLineDetail,
                              DetailTypeSpecified = true,
                              LinkedTxn = new LinkedTxn[] { new LinkedTxn { TxnId = "1282", TxnType = "Invoice" } } }
               },
               CustomerRef = new ReferenceType { Value = "123" }
};

Upvotes: 2

Ronnyek
Ronnyek

Reputation: 502

For what its worth trying this very thing was causing me the same problems. Ends up it doesnt like not specifying line items, even when specifying a sparse update. I found reference by a sdk engineer that said just take the originally saved invoice, modify and pass that up to the api. Ended up taking care of at least my problem.

Upvotes: 0

Related Questions