PayPal Direct Payment API integration into your website

PayPal Direct Payment API integration into your website

Direct Payment

Direct payment API provides the way to do the PayPal payment from your website. This payment action will happen in background at the time customer will stay in your site itself.

The simple screen shots that explain the work flow about direct payment,

Follow the below steps to integrate the PayPal into your site.

Step 1: Creating a PayPal Developer Account        

  1. To create a PayPal account, click the Sign Up link at the top of the main PayPal page, and follow the resulting instructions.
  2. Go to Developer website and log in using PayPal account.
  3. This log in will create a default Business test account in the Sandbox.

Step 2: You can find your Business account under Applications -> Sandbox accounts. Click on the business account and go to profile option, where you will get the API credential of that account. Use this Credential in your application.

Step 3: You should enable the Payment Pro to this business account and have to accept the CE agreements to this account. These two process will be done by PayPal side. You can create ticket to do this action at https://ppmts.custhelp.com.

Step 4: To avoid 3rd step, you can create an another new US business account in developer site, and also create Personal account. You can find API credential for new business account. Use this new API into you site.

Step 5: Add code in your website.

Write the PayPal account settings in web configuration file.

<configuration>
<configSections>
<section name="paypal" type="PayPal.Manager.SDKConfigHandler, PayPalCoreSDK"/>   
</configSections>
<paypal>
<settings>
<add name="mode" value="sandbox"/>
<add name="connectionTimeout" value="30000"/>
<add name="requestRetries" value="1"/>
<add name="IPAddress" value="127.0.0.1"/>
</settings>
<accounts>
<account apiUsername="jb-us-seller_api1.paypal.com" apiPassword="WX4WTU3S8MY44S7F" apiSignature="AFcWxV21C7fd0v3bYYYRCpSSRl31A7yDhhsPUU2XhtMoZXsWHFxu-RWy" applicationId="APP-80W284485P519543T" certificateSubject="" signatureSubject=""/>
</accounts>
</paypal>
</configuration>

Add the below assemblies in references.

  • PayPalCoreSDK.dll
  • PayPalMerchantSDK.dll
  • PayPalPermissionsSDK.dll
  • log4net

Design the cart page, and then billing page here you should get the below details,

  • Card Type
  • Card Number
  • Expire Date
  • CVV Number
  • First and Last Name
  • Street Name
  • City
  • State
  • Country
  • Postal code
  • Phone (Optional)
  • Email address (Optional)
  • Payment amount

Design the confirmation page to display user entered information from previous page. So that they can confirm their inputs. Add the payment button here.

In Payment button click post action, added the code to do direct payment.

public static DoDirectPaymentResponseType PayPalPaymentAction(PaymentDetails paymentDetails)
    {
        try
        {
            DoDirectPaymentRequestType paymentRequest = new DoDirectPaymentRequestType();
            paymentRequest.DoDirectPaymentRequestDetails = new DoDirectPaymentRequestDetailsType();
            paymentRequest.DoDirectPaymentRequestDetails.PaymentAction = PaymentActionCodeType.SALE;
            #region Card details
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard = new CreditCardDetailsType();
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CreditCardNumber = paymentDetails.CardNumber;
            switch (paymentDetails.CardType)
            {
                case "Visa":
                    paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CreditCardType = CreditCardTypeType.VISA;
                    break;
                case "Master Card":
                    paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CreditCardType = CreditCardTypeType.MASTERCARD;
                    break;
                case "Discover":
                    paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CreditCardType = CreditCardTypeType.DISCOVER;
                    break;
                case "American Express":
                    paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CreditCardType = CreditCardTypeType.AMEX;
                    break;
            }
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CVV2 = paymentDetails.CVVNumber;
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.ExpMonth = int.Parse(paymentDetails.ExpMonth);
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.ExpYear = int.Parse(paymentDetails.ExpYear);
            #endregion

            #region Cart Holder address details
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CardOwner = new PayerInfoType();
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address = new AddressType();
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.Street1 = paymentDetails.Address;
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.CityName = paymentDetails.City;
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.StateOrProvince = paymentDetails.State;
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.PostalCode = paymentDetails.PostalCode;
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.CountryName = paymentDetails.Country;
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CardOwner.PayerName = new PersonNameType();
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CardOwner.PayerName.FirstName = paymentDetails.FirstName;
            paymentRequest.DoDirectPaymentRequestDetails.CreditCard.CardOwner.PayerName.LastName = paymentDetails.LastName;
            #endregion
            #region Payment details
            paymentRequest.DoDirectPaymentRequestDetails.PaymentDetails = new PaymentDetailsType();
            paymentRequest.DoDirectPaymentRequestDetails.PaymentDetails.OrderTotal = new BasicAmountType();
            BasicAmountType paymentAmount = new BasicAmountType(CurrencyCodeType.USD, paymentDetails.TotalAmountDue);
            paymentRequest.DoDirectPaymentRequestDetails.PaymentDetails.OrderTotal = paymentAmount;
            #endregion

            DoDirectPaymentReq wrapper = new DoDirectPaymentReq();
            wrapper.DoDirectPaymentRequest = paymentRequest;
            PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService();
            DoDirectPaymentResponseType response = service.DoDirectPayment(wrapper);
            return response;
        }
        catch (Exception ex)
        { return null; }
    }
               
 if (response.Ack == AckCodeType.SUCCESS || response.Ack == AckCodeType.SUCCESSWITHWARNING)
    {
             //Do Business logic here to update database
    }

Redirect to Thank you page if response succeed, else redirect o failure page.

Step 6: Check the sample application here(http://paypal.github.io/#merchant)

Step 7: After transaction succeeded, you can login to you Sandbox account using your business account credential(get password by change it in account profile). Here you can find the Payment details. The each transaction that done in your website will be managed in sandbox account.

Step 8: The Fake Name Generator website would be a good place to get test card numbers you can use. Don’t use other commonly used card number. It will lead to error some times.

He is a product manager at a reputed software company and a freelance blog writer. He is experienced in different technologies, web securities, and web applications. He keeps learning and make himself up to date on the latest technologies, news, health, and fitness. This encouraged him to share his experiences by writing articles.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
%d bloggers like this: