Handle Paypal Express Checkout PHP

Express Checkout is a fast, easy way for buyers to pay with PayPal. Express Checkout eliminates one of the major causes of checkout abandonment by giving buyers all the transaction details at once, including order details, shipping options, insurance choices, and tax totals.

Studies show that adding the Express Checkout button to your website can increase your sales up to 18 percent.

When should you use Express Checkout:

Use Express Checkout to:

  • Accept payments from any PayPal account.
  • Eliminate the need for customers to enter personal information, including shipping, billing, or payment information.
  • Keep customers on your site after completing the transaction.
  • Sign up customers to make payments at regular intervals.
  • See more at: https://www.paypal.com/webapps/mpp/express-checkout

How do we implement it on our site

sandboxecPersonally, i found that PayPal developer guide is very huge and many links can get you confused on what you were trying to achieve. Lets skip to a short and simple process.

First download the https://github.com/amirduran/duranius-paypal-rest-api-php-library paypal library from github.

After downloading you will find two folders. Inside the example folder you will find few example code that can be implemented to direct access the checkout page.

Step 1

In the folder called library you will find two files

1. DPayPal.php
2. cacert.pem

Copy both files in your project folder, and reference file DPayPal.php using require_once php command require_once “DPayPal.php” which is already done in the example.

Make sure that both files DPayPal.php and cacert.pem are located in the same folder.

If you want to keep the files DPayPal.php and cacert.pem in the different folders, then open the file DPayPal.php and edit the following line

from CURLOPT_CAINFO => dirname(__FILE__) . ‘/cacert.pem’, //CA cert file
to CURLOPT_CAINFO => “PATH TO YOUR cacert.pem file”

Step 2

Enter your paypal API Credentials by going to your buyer sandbox or paypal account.

You’ll need to create an API username for PayPal that’s different from your shopping cart’s username. You can access PayPal via API only if you have a PayPal Premier / Business / Website Payments Pro account.

Follow these steps:
a. Log in to your PayPal Business account.
b. Click the My Account tab.
c. Click the Profile tab. If you haven’t already done so, you need to verify your account before requesting API credentials.
d. Click Request API credentials under Account information.
e. Click Set up PayPal API credentials and permissions under Option 1.
f. Click Request API Credentials.
g. Click Request API signature.
h. Click Agree and Submit.
i. Copy and paste the API username, password, and signature into your shopping cart’s configuration or administration screen.

my-profile-paypal

api-access-paypal view-or-remove-api-signature-paypal

Complete the process by following your shopping cart’s final steps.

Diving into Code

Open your DPayPal.php file which looks like below:

$username is your username where you will get it from step 2 API Username. Similarly, same for $password and $apisignature. We are using version 94 here. You can check all versions from https://developer.paypal.com/docs/classic/release-notes/.

Now, next step to process the orders by listing them and sending them as post data into PayPal server. This will be more secure since we will be doing all transaction from PayPal itself and after the payment is done we captured the response and output a success message in our system.

Open payment.php and create a success.php file in same folder but we wont do anything for this file right now. As, in example; everything has been done we just need to do a few tweaks according to our needs.

payment.php

The above code is basically same as in the example. Few lines have been changed. Change return to the path where you had created your success.php file earlier. OrderParams are the parameter where you will pass your order details but not item details. Most related to your shop details and settings.

Similarly, the items array will hold your items details. You can know all methods from https://developer.paypal.com/docs/classic/api/merchant/SetExpressCheckout_API_Operation_NVP/. For now we are just sending 1 item as order.

 

Now open up your browser and browse to the payment.php file. It will redirect you PayPal. You should see screen like alongside where your items details are shown when clicked on the dropdown.

 

 

 

 

 

 

 

You will need to login using your sandbox account credentials. See https://developer.paypal.com/docs/classic/lifecycle/sb_create-accounts/.

Now for success.php file:

success.php

After you have successfully processed your ordered from paypal. You will be redirected to success.php page. You payment information is shown. You will need to store these information into database for future references.

ABOUT THE AUTHOR

In a world without walls and fences, who needs windows and gates ? No gates, no windows bt apache inside! #dev Love for #Unix #Linux #Apple. Interests in coding, music, creative design, Linux and Apple products.

You may also like...

Leave a Reply

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