Get a Quote


Paypal Express Checkout Integration with Laravel 5

oneclick, administrator/ April 9, 2019 7:05 pm 256 Views

Paypal Express Checkout

What is PayPal?

PayPal is a company operating a worldwide online payments system that supports online money transfers and serves as an electronic alternative to the traditional money exchange system. The company operates as a payment gateway for online sellers, auction sites, and many other commercial users, for that it charges a fee in exchange for benefits. PayPal’s payment system also called PayPal inshort.

What is the Payment Gateway?

A payment gateway is a merchant service provided by an any-commerce application service provider that authorizes credit card or direct payments processing fore-businesses, online retailers, bricks and clicks, or traditional brick and mortar. The payment gateway used to provide by a bank to its customers, but can be provided by a specialized financial service provider as a separate service, such as a payment service provider. A payment gateway facilitates a payment transaction by the transfer of information between a payment portal (such as a website or mobile phone) and the front end processor or acquiring the bank.

Configuring PayPal in your app

• First, we will install the PayPal SDK using composer.

Install Paypal SDK
• Next, we need to login to PayPal developer mode, create a new sandbox account and get some keys like client_id and secret for testing this integration.
• Login to developer.paypal.com to create a merchant account, so that you can sell or to credit funds to your account.
• You can also create an account, to buy from some merchant.

create account for merchant

• We have created both merchant and buyer test accounts.
• You will have to create a new app to get the client_id and secret keys.

create new app

• After creating your app click on the app it shows you client_id and secret keys.
• Copy CLIENT_ID, SECRET  and paste those in your .env file for security.

env file

• Next, we will create a new file paypal.php, at \ config directory.
• Place the following content in the file

paypal php file

PayPal payment form

You can add any form, the way you want, here for demo purposes, We just have 1 input field to enter an amount and a button to submit a form.

paypal payment form code

paypal payment form

PayPal Payment Controller

We will create a new controller to manage all the PayPal related PHP stuff.

paypal payment controller

The above command will create a new controller at /app/http/Controllerswith name PaymentController

First We will initialize the PayPal configuration and settings in the _construct function.
So now after the amount is entered in the form and when clicked on the ‘Pay with PayPal’ button, the following function executes.

pay with paypal button

paypal SDK

In the above function, the terminology like Payer, Item is all from the PayPal SDK, so let me explain those terminologies form PayPal official docs.

• Payer
A resource representing a Payer that funds a payment For PayPal account payments, set payment method to ‘paypal’.

payer

• Item information
(Optional) Lets you specify item wise information

item information

  • Amount
Lets you specify a payment amount. You can also specify additional charges such as shipping, tax.

amount

 • Transaction
A transaction defines the contract of payment — what is the payment for and who is fulfilling it.

transaction

 • Redirect URLs
Set the URLs that the buyer must be redirected to after payment approval/ cancellation.

redirect URLs

 • Payment
A Payment Resource; create one using the above types and intent set to ‘sale’

payment

    • Create Payment
Create a payment by calling the ‘create’ method passing it a valid API context. (See bootstrap.php for more on ApiContext) The returned object contains the state and the URL to which the buyer must be redirected for payment approval

create payment

PayPal Payment Status

So after the payment is made, we need to tell the user whether the payment is a success or a failure. The following function executes after payment is processed.

paypal payment status

Approval Status

Determine if the user approved the payment or not.
if (isset($_GET[‘success’]) && $_GET[‘success’] == ‘true’) {
Get the payment Object by passing paymentId payment id was previously stored in session.

approval status

Payment Execute

PaymentExecution object includes information necessary to execute a PayPal account payment. The payer_id is added to the request query parameters when the user is redirected from PayPal’s Website to your site.

payment execute

And finally, if the result of the payment is a success, we will show a success message and a failure message if the payment fails.

result of the payment

We will store the messages in the Session and show them in the view.

message in the session

Check out the images below,

Enter some amount,

paywith paypal

Logging with test buyer account,

login with test buyer

Paypal payment page,

paypal payment page

Success message,

payment success

Wants to learn more about Laravel? We are providing a complete solution related to Laravel for your Websites. Contact us to know more about the PayPal payment gateway integration in Laravel.