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.
GetExpressCheckoutDetailsReq getExpressCheckoutDetails = new GetExpressCheckoutDetailsReq();
GetExpressCheckoutDetailsRequestType getExpressCheckoutDetailsRequest = new GetExpressCheckoutDetailsRequestType(strTokenID);
getExpressCheckoutDetails.GetExpressCheckoutDetailsRequest = getExpressCheckoutDetailsRequest;
PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService();
responseGetExpressCheckoutDetailsResponseType = service.GetExpressCheckoutDetails(getExpressCheckoutDetails);
if (responseGetExpressCheckoutDetailsResponseType != null)
{
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
uinfo = new UserInfo();
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;
UserInfoProvider.SetUserInfo(uinfo);
Customer Account
cinfo = new CustomerInfo();
cinfo.CustomerFirstName = pinfo.PayerName.FirstName;
cinfo.CustomerLastName = pinfo.PayerName.LastName;
cinfo.CustomerEmail = pinfo.Payer;
cinfo.CustomerEnabled = true;
cinfo.CustomerSiteID = SiteContext.CurrentSiteID;
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
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
};
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.
DoExpressCheckoutPaymentReq doExpressCheckoutPayment = new DoExpressCheckoutPaymentReq();
DoExpressCheckoutPaymentRequestDetailsType doExpressCheckoutPaymentRequestDetails = new DoExpressCheckoutPaymentRequestDetailsType();
doExpressCheckoutPaymentRequestDetails.Token = strTokenID;
doExpressCheckoutPaymentRequestDetails.PayerID = strPayerID;
List<PaymentDetailsType> paymentDetailsList = new List<PaymentDetailsType>();
PaymentDetailsType paymentDetails1 = new PaymentDetailsType();
doExpressCheckoutPayment.DoExpressCheckoutPaymentRequest = doExpressCheckoutPaymentRequest;
PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService();
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)
{
OrderStatusInfo status = OrderStatusInfoProvider.GetOrderStatusInfo("PaymentReceived", SiteContext.CurrentSiteName);
if (status != null)
{
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!