How To Get Payment As PDF From Quickbooks Online Using C#

In this article, I will explain to you how to get “payment” as PDF and download it in .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.

We will download payment pdf by payment ID.

For that, we have to get a “payment” form Quickbooks Online and call GetPdf function. it will return a byte array of pdf, we have to convert byte array to a pdf file and return pdf file to a user.

Below are a few steps to get “payment” as PDF,

  • First, we have to create a ServiceContext with Auth tokens and realmId.
  • For that, we need access token and realmId
  • We have to get the payment from Quickbooks online by calling payment API.
  • We are querying a payment by ID.
  • For querying/get the payment we have to define QueryService
  • We need to pass the ServiceContext object into QueryService.
  • We will get payment details in objPaymentFound if there is a payment that exists with a particular ID.
  • After that, We have to create a DataService object by passing a ServiceContext object as a parameter.
  • Add objPaymentFound object in dataService.GetPdf<Payment>() for getting a payment pdf byte Array.
  • dataService.GetPdf<Payment>() will return byte Array of pdf, we need to convert it into a pdf file for returning the file to a user.
  • At the last, we return pdf File with a unique name.
  • code is as below,
public FileResult GetPaymentPdfById(string PaymentID)
{
  byte[] PaymentPdfByteArray;
  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;

    string EXISTING_PAYMENT_QUERYBYID = string.Format("select * from Payment where id = '{0}'", PaymentID);
    var queryService = new QueryService<Payment>(serviceContext);
    Payment objPaymentFound = queryService.ExecuteIdsQuery(EXISTING_PAYMENT_QUERYBYID).FirstOrDefault<Payment>();

    //If Payment found on Quickbooks online
    if (objPaymentFound != null)
    {
        DataService dataService = new DataService(serviceContext);
        //it will return Byte Array of pdf
        PaymentPdfByteArray = dataService.GetPdf<Payment>(objPaymentFound);
        if (PaymentPdfByteArray != null)
        {
            //This is for unique name format like-"INVOICE-148-11_05_2019114648".
            var CurrentDate = DateTime.Now;
            var FileName = string.Format("{0}-{1}-{2}{3}{4}{5}{6}", "PAYMENT", PaymentID, CurrentDate.ToString("MM/dd/yyyy"), CurrentDate.ToString("HH"), CurrentDate.ToString("mm"), CurrentDate.ToString("ss"), ".pdf");
            return File(PaymentPdfByteArray, "application/pdf", FileName);
        }
    }
    return null;
  }
  catch (IdsException ex)
  {
    return null;
  }
  catch (Exception ex)
  {
    return null;
  }
}
  • The above code will return the pdf file of the particular payment.

You can set a download link in the list view of the payments. if you don’t know how to get payments from Quickbooks Online you can find it here.

  • View Code(cshtml) of the payment list with the download link is as below.
@model List<Intuit.Ipp.Data.Payment>

@{
    ViewBag.Title = "GetAllPayment";
}

<h2>Quickbooks online Payment List</h2>

<div>
    <table class="table table-bordered">
        <tr>
            <th>QBO ID</th>
            <th>Date</th>
            <th>Customer Name</th>
            <th>Total Amount</th>
            <th style="text-align:center;">Download</th>
        </tr>

        @foreach (var PaymentItem in Model)
        {
            <tr>
                <td>@PaymentItem.Id</td>
                <td>@PaymentItem.TxnDate.ToShortDateString()</td>
                <td>@PaymentItem.CustomerRef.name</td>
                <td>@PaymentItem.TotalAmt</td>
                <td style="text-align:center;"><a href="@Url.Action("GetPaymentPdfById", "Home", new { PaymentID = PaymentItem.Id })" target="_blank"><img src="~/Content/Images/downloadICON.png" height="25" width="25" /></a></td>
            </tr>
        }

    </table>
</div>
  • In the above code, on click of download icon payment pdf will be downloaded.

OUTPUT:

  • Payment List

  • Payment Order PDF

 

Submit a Comment

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

Subscribe

Select Categories