Client Pay Portal
 integrating with paypal express and kentico

Integrating PayPal Express with Kentico 8 - Part 2

This is the second post in my series on integrating PayPal Express with Kentico CMS v8. In Part 1 I covered the basics of the integration and how data flows between the two systems. In this post I’ll dive into the code and explain what pieces are needed to make the whole thing work. So let’s get started!


Tip

You can check out Part 1 of this blog here.


Setting things up

You will need to set up a few things to before you get coding. These steps are common to any ecommerce configuration and are covered extensively in the v8 documentation. These include:
 
  • Adding a new Payment Method for PayPal Express
  • Assigning the payment method to a shipping option
  • Assign the Payment / Shipping methods to the site

Once you have those done you’re ready to do some actual coding!


Tip

All your wildest dreams about configuring ecommerce in Kentico can be fulfilled here.


Creating a cool PayPal Button

One of the first things you’ll want to do is make a PayPal button for users to click. This can be a standard Kentico Button or a styled PayPal-specific button (recommended to get the noticeability of PayPal). What is really important is what happens when the user clicks the button. I put this button right on the shopping cart page to make the process as easy as I could.
 
  

When user selects PayPal you’re going to need to set up the transaction and get ready to send them to PPE. You do this by creating a token on the PPE system. This token will associate the purchase information with their PayPal account. It will also be how you will actually process the order later.

To make the token, you will want to create a SetExpressCheckoutResponseType object and populate with your values.
// # SetExpressCheckoutReq
SetExpressCheckoutRequestDetailsType setExpressCheckoutRequestDetails = new SetExpressCheckoutRequestDetailsType();
setExpressCheckoutRequestDetails.ReturnURL = [Order Confirmation URL];
setExpressCheckoutRequestDetails.CancelURL = [Order Cancel URL];
List<PaymentDetailsType> paymentDetailsList = new List<PaymentDetailsType>();
PaymentDetailsType paymentDetails1 = new PaymentDetailsType();
paymentDetails1.PaymentAction = PaymentActionCodeType.ORDER;
SellerDetailsType sellerDetails1 = new SellerDetailsType();
sellerDetails1.PayPalAccountID = [Payer ID];
paymentDetails1.SellerDetails = sellerDetails1;


Note

There are several PaymentAction values for specific uses. Be sure you know which ones to use when.
You can choose how much of the order details to populate based on the user’s current order. In the code below I am populating the order total, item level info, and shipping.
 
ShoppingCartInfo sc = ECommerceContext.CurrentShoppingCart;
bool cartEmpty = (sc != null&& sc.IsEmpty;
 
if (!cartEmpty)
{
//Shipping
       if (sc.IsShippingNeeded)
       {
       	paymentDetails1.ShippingTotal = new BasicAmountType(CurrencyCodeType.USDCurrencyInfoProvider.GetFormattedPrice(sc.TotalShippingsc.Currency));
}
 
       BasicAmountType orderTotal1 = new BasicAmountType(CurrencyCodeType.USDCurrencyInfoProvider.GetFormattedPrice(sc.RoundedTotalPricesc.Currency));
paymentDetails1.OrderTotal = orderTotal1;
 
       foreach (ShoppingCartItemInfo scii in sc.CartItems)
       {
       	PaymentDetailsItemType pditItem = new PaymentDetailsItemType();
              pditItem.Name = scii.SKU.SKUName;
pditItem.Amount = new BasicAmountType(CurrencyCodeType.USDCurrencyInfoProvider.GetFormattedPrice(scii.UnitPricesc.Currency));
       	pditItem.Quantity = scii.CartItemUnits;
paymentDetails1.PaymentDetailsItem.Add(pditItem);
}
 
       //Item total
       paymentDetails1.ItemTotal = new BasicAmountType(CurrencyCodeType.USDCurrencyInfoProvider.GetFormattedPrice(sc.TotalItemsPrice - sc.TotalItemsTaxsc.Currency));
	….
}


Note

I am saving the token into session so I can validate it later.


Sending the User to PayPal

The next step of the process is send the user over to PayPal to enter their information. At this point you have all of the order details created in PPE and have a token to identify it. We will send this token with the user to PPE and all of the order information should be displayed.

This code is using a web part property to send the user over to PPE.
 
//Get PayPal Express token and redirect user to PayPal
if (PayPalExpressFunctions.GetPayPalExpressToken(this.OrderConfirmationURLthis.OrderCancelURLthis.PayPalAccountID))
{
    Response.Redirect(this.PayPalURL + ValidationHelper.GetString(SessionHelper.GetValue("PayPalToken"), ""));
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

The user is now redirected to PayPal along with the token. When they log in, PayPal will pull up all of their information. The idea here is that they will see what they are purchasing, enter / update their shipping / billing information, and provide a payment method. Whatever they put in here will be associated with the token so you can retrieve after they return to your site.


On to Part 3!

Check back soon for Part 3 to see how we handle the response from PayPal and automate some internal Kentico functions based on the user’s PayPal info!
 

Author

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

Director of Magic

Read more