Integrating PayPal Express with Kentico 8 - Part 3

This blog is the final installment of my series on integrating PayPal Express with Kentico CMS 8. Hopefully you have checked out Part 1 and 2 and are ready for some more code to bring the whole process together. So let’s get on with it!

TIP

Handling the return from PayPal Express

After the user has logged in and validated their address / payment information they will be returned to your site. The response will the token in the query string so you can locate the record on your side. I created a “PayPal Order Process” web part and its job is to validate the response and a few other things.

When the user comes back to the site, the PPE response will contain all of the information about the user, their purchase, and whether or not they pre-authorized to make the purchase. The following code will check the response to make sure the user is authorized.

// Create the GetExpressCheckoutDetailsReq objectGetExpressCheckoutDetailsReqgetExpressCheckoutDetails=newGetExpressCheckoutDetailsReq();
// A timestamped token, the value of which was returned by `SetExpressCheckout` responseGetExpressCheckoutDetailsRequestTypegetExpressCheckoutDetailsRequest=newGetExpressCheckoutDetailsRequestType(strTokenID);
getExpressCheckoutDetails.GetExpressCheckoutDetailsRequest=getExpressCheckoutDetailsRequest;
// Create the service wrapper object to make the API callPayPalAPIInterfaceServiceServiceservice=newPayPalAPIInterfaceServiceService();
// # API call// Invoke the GetExpressCheckoutDetails method in service wrapper objectresponseGetExpressCheckoutDetailsResponseType=service.GetExpressCheckoutDetails(getExpressCheckoutDetails);
if (responseGetExpressCheckoutDetailsResponseType!=null)
{
// # Success valuesif (responseGetExpressCheckoutDetailsResponseType.Ack.ToString().Trim().ToUpper().Equals("SUCCESS"))
{ ....

Automating the accounts

Now that we know the purchase is ready to go, we will get the user information from the response. With this, we will automate the user and account creation within Kentico.

//Create a new user account based on the pinfo valuesuinfo=newUserInfo();
// Set the propertiesuinfo.FullName=pinfo.PayerName.FirstName+" "+pinfo.PayerName.LastName;
uinfo.FirstName=pinfo.PayerName.FirstName;
uinfo.LastName=pinfo.PayerName.LastName;
uinfo.UserName=pinfo.Payer;
uinfo.Email=pinfo.Payer;
uinfo.UserEnabled=true;
uinfo.UserIsGlobalAdministrator=false;
// Save the userUserInfoProvider.SetUserInfo(uinfo);

Creating a Custom Payment Gateway

Regardless of any step you do ahead of the actual purchase process, you will need to create a Custom Payment Gateway within Kentico to handle the actual transaction. Normally I would put this at the front of the integration, but integrating PayPal Express is a little different so I have saved it until now.

Kentico gives some step by step instructions of creating this class and implementing it in your site.

Tip

The main part of the gateway is the ProcessPayment method. When the gateway is registered within the site for a payment option, this is what will get called to actually submit the transaction. This method is a void, which doesn’t return anything. It can, however, set an “ErrorMessage” value. If this value is set to anything then Kentico thinks the process failed and treats it as a failed transaction.

For the PayPal-specific code, we need to actually process the transaction. Up to this point we have set up an order within PPE and created a token. We now need to process that order. This is done by calling the DoExpressCheckoutPayment method for the service.

// Create the DoExpressCheckoutPaymentReq objectDoExpressCheckoutPaymentReqdoExpressCheckoutPayment=newDoExpressCheckoutPaymentReq();
DoExpressCheckoutPaymentRequestDetailsTypedoExpressCheckoutPaymentRequestDetails=newDoExpressCheckoutPaymentRequestDetailsType();
// The timestamped token value that was returned in the// `SetExpressCheckout` response and passed in the// `GetExpressCheckoutDetails` request.doExpressCheckoutPaymentRequestDetails.Token=strTokenID;
// Unique paypal buyer account identification number as returned in// `GetExpressCheckoutDetails` ResponsedoExpressCheckoutPaymentRequestDetails.PayerID=strPayerID;
// # Payment Information// list of information about the paymentList<PaymentDetailsType>paymentDetailsList=newList<PaymentDetailsType>();
// information about the first paymentPaymentDetailsTypepaymentDetails1=newPaymentDetailsType();
doExpressCheckoutPayment.DoExpressCheckoutPaymentRequest=doExpressCheckoutPaymentRequest;
// Create the service wrapper object to make the API callPayPalAPIInterfaceServiceServiceservice=newPayPalAPIInterfaceServiceService();
// # API call// Invoke the DoExpressCheckoutPayment method in service wrapper objectresponseDoExpressCheckoutPaymentResponseType=service.DoExpressCheckoutPayment(doExpressCheckoutPayment);

Once we call that method we will get a response back from PPE. We need to look at this response and determine if it went through ok.

If it’s successful, we need to update our order and mark it as paid. In the code below I’m pulling some data out of the response to store with the record.

OrderInfooinfo=OrderInfoProvider.GetOrderInfo(intOrderID);
if (oinfo!=null)
{
//Update the order as paid// Get the order statusOrderStatusInfostatus=OrderStatusInfoProvider.GetOrderStatusInfo("PaymentReceived", SiteContext.CurrentSiteName);
if (status!=null)
{
//Set the trasnsaction to session to validate the confirmation pageIEnumerator<PaymentInfoType>paymentInfoIterator=responseDoExpressCheckoutPaymentResponseType.DoExpressCheckoutPaymentResponseDetails.PaymentInfo.GetEnumerator();
while (paymentInfoIterator.MoveNext())
{
PaymentInfoTypepaymentInfo=paymentInfoIterator.Current;
PaymentResult.PaymentIsCompleted=true;
PaymentResult.PaymentTransactionID=paymentInfo.TransactionID;
PaymentResult.PaymentDescription=paymentInfo.TransactionID;
PaymentResult.PaymentStatusName="{$PaymentGateway.Result.Status.Completed$}";
PaymentResult.PaymentStatusValue="completed";
UpdateOrderPaymentResult();
...

Collecting your sweet profits

Now that we have a good response and our order is updated, the process is complete. Our custom Payment Form will get this response and then redirect the user to the “Order Complete’ page. And you will count all of your sweet, sweet profits like Scrooge McDuck.

Wrapping it all up

Integrating PayPal Express into your site is a great way to increase your sales by allowing users to use their existing PP accounts. With a little custom code you can streamline the process to eliminate double data entry and create a fluid user experience. Hopefully this blog gets you on your way!

Locations

Wizard Life

Twitter

What Are the Hidden Costs of a Building Mobile App? This video dives deep. https://t.co/lQ1Yf6TX8p https://t.co/FfZJP8uBvO

About us

Founded in 2000 by Vince Mayfield and Louis Erickson, Bit-Wizards is the result of a vision to create a company where innovation and creativity flourish. We adhere to proven engineering methods, which has helped us become a leading software development, digital marketing, and cloud information technology agency in the Southeastern United States.