How To Add Payment In Quickbooks Online Using C#

In this blog, we will learn how to add a payment in Quickbooks online from .NET MVC web application using SDK.

Before using any Quickbooks online API we need access token, if you don’t know how to get access token then you can find it here.

CustomerRef and TotalAmt elements are required for creating a payment, we will use these required elements for creating a payment.

  • We have to give the customer reference in CustomerRef and the total amount of the transaction in the TotalAmt element.
  • Customers must be already added in Quickbooks before adding it to the payment.
  • CustomerRef:- Reference to a customer or job, Use Customer.Id and Customer.DisplayName from that object for CustomerRef.value and CustomerRef.name, respectively. Customer.Id” is required and “Customer.DisplayName”  is optional. if we give both then Quickbooks will ignore “Customer.DisplayName”.
  • TotalAmt:- Indicates the total amount of the transaction. it includes the total of all the charges, taxes, and allowances.

Let’s create a Payment.

  • First, we have to create a ServiceContext with Auth tokens and realmId.
  • For that, we need access token and realmId
  • Then, we have to create a Payment object and assign data into an object, the following are some required data information that we have to give.
    1. CustomerRef: we have to give Quickbooks online Customer ID as a reference for the customer.
    2. TotalAmt: we have to give a total of all the charges, taxes, and allowances.
  • After that, We have to create a DataService object by passing a ServiceContext object as a parameter.
  • Add Payment object in DataService.Add() for adding a Payment.
  • If a Payment created successfully, it will return the newly-created Payment object, from this object you can store details like Payment Id in the database if you want to.
  • The code is as below.
public ActionResult CreatePayment()
{
  try
  {
    OAuth2RequestValidator oauthValidator = new OAuth2RequestValidator(Access_token);

    // Create a ServiceContext with Auth tokens and realmId
    ServiceContext serviceContext = new ServiceContext(RealmId, IntuitServicesType.QBO, oauthValidator);
    serviceContext.IppConfiguration.MinorVersion.Qbo = "23";
    serviceContext.IppConfiguration.BaseUrl.Qbo = QboBaseUrl;

    Payment ObjPayment = new Payment();

    ObjPayment.TotalAmtSpecified = true;
    ObjPayment.TotalAmt = 100;
    ObjPayment.CustomerRef = new ReferenceType();
    ObjPayment.CustomerRef.Value = "58"; //Quickbooks online Customer Id

    DataService dataService = new DataService(serviceContext);

    Payment PaymentAdd = dataService.Add(ObjPayment);
    if (PaymentAdd != null && !string.IsNullOrEmpty(PaymentAdd.Id))
    {
        //you can write Database code here
        ViewBag.IsSuccess = true;
    }
    return View();
  }
  catch (IdsException ex)
  {
    ViewBag.IsSuccess = false;
    if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message))
    {
        ViewBag.Message = ex.InnerException.Message;
    }
    else if (!string.IsNullOrEmpty(ex.Message))
    {
        ViewBag.Message = ex.Message;
    }
    else
    {
        ViewBag.Message = "Something went wrong,IdsException occurs";
    }
    return View();
  }
  catch (Exception ex)
  {
    ViewBag.IsSuccess = false;
    if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message))
    {
        ViewBag.Message = ex.InnerException.Message;
    }
    else if (!string.IsNullOrEmpty(ex.Message))
    {
        ViewBag.Message = ex.Message;
    }
    else
    {
        ViewBag.Message = "Something went wrong,Exception occurs";
    }
    return View();
  }
}
  • View code is as below
@{
    ViewBag.Title = "CreatePayment";
}

<h2>Create Payment</h2>

@if (ViewBag.IsSuccess != null && ViewBag.IsSuccess == true)
{
    <div class="row">
        <label class="label label-success"> Payment Created Successfully </label>
    </div>
}
else if (ViewBag.IsSuccess != null && ViewBag.IsSuccess == false)
{
    <div class="row">
        <label class="label label-danger">@ViewBag.Message</label>
    </div>
}
  • The above code will add a Payment in Quickbooks Online.
  • Before Adding the Payment, we have to make sure one thing that the Customer ID we gave as a reference must exist on Quickbooks online.
  • You can also query/get the Customer before adding the Payment and then add it references. or you can store it in the database at the time of add Customer and use it accordingly.
  • So this way we can add a Payment in Quickbooks Online.

OUTPUT:(Payment preview on Quickbook online)

4 Comments

  1. nitesh

    How to add payment method

    0
    0
    Reply
  2. matt

    Hi Tabish-
    These are all great tutorials! Any chance you can go into detail on how to apply payments to multipal invoices?

    thanks
    matt

    0
    0
    Reply
    1. Thank you matt,
      you can refer below code for adding invoices in payment, here I have added only one invoice, you can add multiple invoices in the list.
      you just need to give the invoice ID of a particular invoice.

      Payment ObjPayment = new Payment();

      ObjPayment.TotalAmtSpecified = true;
      ObjPayment.TotalAmt = 100;
      ObjPayment.CustomerRef = new ReferenceType();
      ObjPayment.CustomerRef.Value = “58”; //Quickbooks online Customer Id

      List LineList = new List();
      Line line = new Line();
      List linkedTxnList = new List();
      LinkedTxn linkedTxn = new LinkedTxn();
      // Give type as invoice and Quickbook Invoice ID for invoice
      linkedTxn.TxnType = “Invoice”;
      linkedTxn.TxnId = “168”;
      linkedTxnList.Add(linkedTxn);
      line.AmountSpecified = true;
      line.Amount = 100;
      line.LinkedTxn = linkedTxnList.ToArray();
      LineList.Add(line);
      ObjPayment.Line = LineList.ToArray();

      DataService dataService = new DataService(serviceContext);

      Payment PaymentAdd = dataService.Add(ObjPayment);

      0
      0
      Reply
      1. Junaid

        how to send payment type from code

        0
        0
        Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories