Blog Article:

Integrating Paypal Express and Kentico

by Piyush Kothari on April 30, 2018

Introduction

In this post, we will learn how to integrate PayPal Express Checkout into Kentico. Implementing a payment gateway is like implementing any other third-party Rest/SOAP API- just follow the instructions provided in the documentation, and you’ll be all set. However, since payment APIs involve money, people tend to get a bit anxious when setting them up so we'll step through the process in a bit more detail here.

Prepping the Basics

First, you will need a PayPal business account. Alternatively, you can create a sandbox account for web development at https://developer.paypal.com/. Make a note of the following information to load onto Kentico: APIUserName, APIPassword, and APISignature. You should next set up the ReturnURL and CancelURL values.

Instructions

Payment Page

First, we will create a new user control interface under CMSModules/Custom/Checkout/PaypalForm.ascx. In the UI, we will add a label that informs users we are taking them to another site to complete the payment.

<cms:LocalizedHeading ResourceString="PayPalForm.Title" runat="server" EnableViewState="false" Level="4" />
<cms:LocalizedLabel ResourceString="PayPalForm.InfoMessage" runat="server" EnableViewState="false" CssClass="InfoLabel" />

This controls class inherits CMS.EcommerceProvider.CMSPaymentGatewayForm class. We will need to override the ProcessData method. Here, we will get the token we get back from Paypal from the query string and store it in PaymentGatewayCustomData, which will be available in the PaymentGatewayProvider class.

public override string ProcessData()
{
    if(!string.IsNullOrEmpty(QueryHelper.GetSTring("token", string.Empty)))
    {
        PaymentGatewayCustomData[GatewayParameters.PAYPAL_TOKEN] = QueryHelper.GetString("token", string.Empty);
    }
    return "";
​}

In this tutorial, we will use the same code for the payment and return pages. You can set it up to be different if you want.

After overriding ProcessData, create a copy of the user control CMSWebparts/ECommerce/Checkout/Forms/PaymentForm.ascx so we make the most of the code that comes with Kentico.

Now, add the following piece of code to the OnLoad method:

if (!Page.IsPostBack && PaymentGatewayProvider is IPaypalExpressGatewayProvider)
{
    IPaypalExpressGatewayProvider ecProvider = ((IPaypalExpressGatewayProvider)PaymentGatewayProvider);
    PaymentGatewayProvider.ProcessCustomData();
    if (!PaymentGatewayProvider.IsPaymentCompleted && ecProvider.CanProcessPaymentCapture())
    {
        ecProvider.ProcessPaymentCapture();
        ProcessPaymentResult();
    }
}

This basically checks if the user is coming from PayPal and if the query string has a token. If the conditions are true, the code proceeds to the processing step.

Payment Gateway

This is the most important piece of the puzzle. Here, we will send PayPal the details about the purchase in the cart, and when we get the token back, we will store the payment details in our records.

We will execute three PayPal methods here: SetExpressCheckout, GetExpressCheckoutDetails, and DoExpressCheckout.
SetExpressCheckout

To obtain a token from PayPal, this method sends it the order details and the merchant’s credentials through the API. Then, the user is redirected to PayPal, but remember that the money hasn’t been transferred to your account yet.

GetExpressCheckoutDetails

After the payment has been processed by PayPal, the user is redirected to the merchant’s website with a token for the transaction. GetExpressCheckoutDetails confirms that the token we have received is valid.

DoExpressCheckoutPayment

We then send the token and PayerID values back to PayPal using DoExpressCheckoutPayment, after which Paypal verifies the values and authorizes the transaction. After the payment has been processed successfully, you might want to store in a database the TransactionID you get from PayPal.

Conclusion

As we said in the introduction, configuring a payment API isn’t any more complex than implementing other APIs. I hope this tutorial helped you set up PayPal Express Checkout on your website. Please share this post if you found it useful.

Piyush Kothari

Piyush is a Kentico Certified Developer and adept with ASP.NET, C#, and Javascript . His experience developing within the Kentico platform since early versions and his keen business sense make him an invaluable asset to the Tusk team.