integrating with paypal express and kentico

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

You check out Part 1 and Part 2 to see the series up to this point.


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 object
GetExpressCheckoutDetailsReq getExpressCheckoutDetails = new GetExpressCheckoutDetailsReq();
 
// A timestamped token, the value of which was returned by `SetExpressCheckout` response
GetExpressCheckoutDetailsRequestType getExpressCheckoutDetailsRequest = new GetExpressCheckoutDetailsRequestType(strTokenID);
getExpressCheckoutDetails.GetExpressCheckoutDetailsRequest = getExpressCheckoutDetailsRequest;
 
// Create the service wrapper object to make the API call
PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService();
 
// # API call
// Invoke the GetExpressCheckoutDetails method in service wrapper object
responseGetExpressCheckoutDetailsResponseType = service.GetExpressCheckoutDetails(getExpressCheckoutDetails);
 
if (responseGetExpressCheckoutDetailsResponseType != null)
{
    // # Success values
    if (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.
 
PayerInfoType pinfo = responseGetExpressCheckoutDetailsResponseType.GetExpressCheckoutDetailsResponseDetails.PayerInfo;

User Account
 
//Create a new user account based on the pinfo values
uinfo = new UserInfo();
 
// Set the properties
uinfo.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 user
UserInfoProvider.SetUserInfo(uinfo);

Customer Account
 
// Create new customer object
cinfo = new CustomerInfo();
 
// Set the properties
cinfo.CustomerFirstName = pinfo.PayerName.FirstName;
cinfo.CustomerLastName = pinfo.PayerName.LastName;
cinfo.CustomerEmail = pinfo.Payer;
cinfo.CustomerEnabled = true;
cinfo.CustomerSiteID = SiteContext.CurrentSiteID;
 
// Create the customer
CustomerInfoProvider.SetCustomerInfo(cinfo);
The last important piece is to create the addresses based off of the PPE information. This will eliminate the user having to enter the data again on the site.

Address Info
 
// Create new address object
AddressInfo newAddress = new AddressInfo
{
    AddressName = "Address - " + pinfo.Address.Street1,
    AddressLine1 = pinfo.Address.Street1,
    AddressLine2 = pinfo.Address.Street2,
    AddressCity = pinfo.Address.CityName,
    AddressZip = pinfo.Address.PostalCode,
    AddressIsBilling = true,
    AddressIsShipping = true,
    AddressIsCompany = false,
    AddressEnabled = true,
    AddressPersonalName = cinfo.CustomerInfoName,
    AddressCustomerID = cinfo.CustomerID,
    AddressCountryID = country.CountryID,
    AddressStateID = state.StateID
};
 
// Create the address
AddressInfoProvider.SetAddressInfo(newAddress);
 

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

Check out the Custom Payment Gateway guide here.

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 object
DoExpressCheckoutPaymentReq doExpressCheckoutPayment = new DoExpressCheckoutPaymentReq();
 
DoExpressCheckoutPaymentRequestDetailsType doExpressCheckoutPaymentRequestDetails = new DoExpressCheckoutPaymentRequestDetailsType();
 
// 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` Response
doExpressCheckoutPaymentRequestDetails.PayerID = strPayerID;
 
// # Payment Information
// list of information about the payment
List<PaymentDetailsType> paymentDetailsList = new List<PaymentDetailsType>();
 
// information about the first payment
PaymentDetailsType paymentDetails1 = new PaymentDetailsType();
doExpressCheckoutPayment.DoExpressCheckoutPaymentRequest = doExpressCheckoutPaymentRequest;
 
// Create the service wrapper object to make the API call
PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService();
 
// # API call
// Invoke the DoExpressCheckoutPayment method in service wrapper object
responseDoExpressCheckoutPaymentResponseType = 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 (responseDoExpressCheckoutPaymentResponseType.Ack.ToString().Trim().ToUpper().Equals("SUCCESS"))
{ ...

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.
 
OrderInfo oinfo = OrderInfoProvider.GetOrderInfo(intOrderID);
if (oinfo != null)
{
    //Update the order as paid
    // Get the order status
    OrderStatusInfo status = OrderStatusInfoProvider.GetOrderStatusInfo("PaymentReceived"SiteContext.CurrentSiteName);
    if (status != null)
    {
        //Set the trasnsaction to session to validate the confirmation page
        IEnumerator<PaymentInfoType> paymentInfoIterator = responseDoExpressCheckoutPaymentResponseType.DoExpressCheckoutPaymentResponseDetails.PaymentInfo.GetEnumerator();
        while (paymentInfoIterator.MoveNext())
        {
            PaymentInfoType paymentInfo = 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!
 
Comments
 
comments powered by Disqus

Author

Wiz E. Wig, Mascot & Director of Magic
Wiz E. Wig

Director of Magic

Read more