Web Application

PayPal Express Checkout Integration with Laravel 5

What is PayPal?

PayPal is a company operating a worldwide online payment 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 is also called PayPal short.

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 is used provided 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 Integration in Laravel with Your App

First, we will install the PayPal SDK using composer.

composer require paypal/rest-api-sdk-php


Next, we need to login into PayPal developer mode, create a new sandbox account, and get some keys like client_id and secret for testing this PayPal integration in Laravel.

Login to developer.paypal.com to create a merchant account, so that you can sell or 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.

PAYPAL_CLIENT_ID=
PAYPAL_SECRET=
PAYPAL_MODE=sandbox

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

<?php
return [
       ‘client id' => env(‘PAYPAL_CLIENT_ID’ , ‘ ’),
       ‘secret' => env( 'PAYPAL_SECRET' , ‘ ’),
       ‘settings' => array(
             ‘mode' => env( 'PAYPAL_MODE' , sandbox'),
             ‘http.ConnectionTimeOut' => 30,
             ‘log . LogEnabled’ => true,
             ‘log. FileName' => storage_path( ) . ‘/logs/paypal.log’,
             ‘log . LogLevel => ‘ERROR’
        ),
];

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.

<form class=”w3-container w3-display-middle w3-card-4” method=”POST” ids=”payment-forms” action="/payment/add-funds/paypal”> 

{{ csrf_field() }} 
      <h2 class=w3-text-blue”>Payment Form</h2> 
      <p>Demo Paypal form - Integrating paypal in laravel</p> 
      <p> 
            <label class=”w3-text-blue”><b>Enter Amount</b></label> 
            <input class=”wg3-input w3-border” name=”amount" type=”text”> 
      </p> 
      <button class=”w3-btn w3-blue”>Pay with PayPal</button> 
</form> 
paypal payment form

PayPal Payment Controller

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

php artisan make:controller PaymentController 

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.

public function __construct( )
{
        /** PayPal api context  **/
        $paypal_conf = \Config: :get (‘paypal’);
        $this ->_api_context = new ApiContext(new OAuthTokenCredential (
             $paypal_conf [‘client_id’], 
             $paypal_conf [‘secret’] )
        );
        $this >_api_context->setConfig($paypal_conf[ 'settings ' ] ); 
}
public function payWithpaypal(Request $request) 
{
            $payer = new Payer(); 
                           $payer->setPaymentmethod( 'paypal'); 
            $item_1 new Item(); 
            $item_1 -> setName('Item 1’) /** item name */ 
                                      ->setCurrency( 'USD') 
                                      ->setQuantity(1) 
                                      ->setPrice($request->get( 'amount ' )); /** unit price **/
            $item_list = new ItemList(); 
                          $item_list=setItems(array($item_1)); 
            $amount = new Amount(); 
                          $amount -> setCurrency ( ‘USD’ ) 
                                   ->setTotal($request -> get ( 'amount ' )); 
            $transaction = new Transaction(); 
                           $transaction -> setAmount($amount) 
                                 ->setItemList($Item_list) 
                                 ->setDescription('Your transaction description'); 
            $redirect_urls = new RedirectUrls(); 
                           $redirect_urls -> setReturnUrl (URL: : roote (‘status’)) /** Specify return URL **/
                                 ->setCancelUrl(URL : : route( ‘status’ )); 
            $payment = new Payment(); 
                           $payment -> setIntent( ‘Sale’ ) 
                                 ->setPayer($payer) 
                                 ->setRedirectUrls($redirect_urls) 
                                 ->setTransactions(array($transaction)); 

/** dd( $payment->create( $this->_api_context));exit; **/ 
try {
             $payment->create( $this->_api_context); 
} catch (\PayPal Exception\PPConnectionException $ex) { 
             
             if (\Config::get('app. debug')) {
                  \Session::put('error', 'Connection timeout');
                 return Redirect::route('paywithpaypal"); 
            } else {
                  \Session::put('error', 'Some error occur, sorry for inconvenient'); 
            return Redirect::route('paywithpaypal');
            }
}

foreach ($payment->getLinks() as $link) { 
           if ($link->getRel() == 'approval_url') {
                $redirect_url = $link->getHref(); 
                break;
           }
}

/** add payment ID to session **/ 
Session::put('paypal_payment_id', $payment->getId());

if (isset($redirect_url)) {
         /** redirect to paypal **/ 
         return Redirect:: away($redirect_url);
}

\Session::put('error' , 'Unknown error occurred'); 
return Redirect : : route('paywithpaypal');

}

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

See also  How to Build a Real-Time Web Notification in Laravel using Pusher?

Payer

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

$payer = new Payer(); 
$payer->setPaymentMethod("paypal");

Item information

(Optional) Lets you specify item-wise information

$item_1 = new Item(); 
$item_1->setName( 'Item 1') /** item name **/
                        ->setCurrency( 'USD') 
                        ->set Quantity(1)
                        ->setPrice($request->get('amount')); /** unit price **/ 
$item_list = new ItemList();
              $item_list->setItems(array($item_1));

Amount

Let’s you specify a payment amount. You can also specify additional charges such as shipping, tax.

$amount = new Amount(); 
$amount->setCurrency( 'USD')
                ->setTotal($request->get('amount'));

Transaction

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

$transaction = new Transaction(); 
$transaction->setAmount($amount)
               ->setItemList($item_list) 
               ->setDescription( 'Your transaction description');

Redirect URLs

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

$redirect_urls = new RedirectUrls(); 
$redirect_urls->setReturnurl(URL: : route('status')) /** Specify return URL **/

Payment

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

$payment = new Payment(); 
$payment = new Payment(); 
$payment->setIntent('Sale')
         ->setPayer($payer) 
         ->setRedirectUrls($redirect_urls) 
         ->setTransactions(array( $transaction));

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

$payment->create( $this->_api_context);

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.

public function getPaymentstatus() 
{ 
 /** Get the payment ID before session clear **/ 
$payment_id = Session::get('paypal_payment_id' ); 
/** clear the session payment ID **/ 
Session::forget('paypal_payment_id'); 
            if (empty(Input::get( 'PayerID' )) || empty(Input::get('token' ))) { 
            \Session::put('error', 'Payment failed'); 
            return Redirect::route('/'); 
            }
$payment = Payment::get($payment_id, $this->_api_context); 
$execution = new PaymentExecution; 
$execution->setPayerId(Input::get( 'PayerID')); 

/**Execute the payment **/ 
$result = $payment->execute( $execution, $this->_api_context); 
if ($result->getState() == 'approved') { 
     \Session::put('success', 'Payment success’); 
     return Redirect::route('/'); 
}
\Session::put('error', 'Payment failed'); 
return Redirect::route('/');
}

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.

$payment = Payment::get($payment_id, $this->_api_context);

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.

$execution = new PaymentExecution(); 
$execution->setPayerId(Input::get( 'PayerID')); 
/**Execute the payment **/ 
$result = $payment->execute($execution, $this->_api_context);

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.

if ($result->getState() == 'approved') {
    \Session::put('success', 'Payment success'); 
    return Redirect::route('/');
}

\Session::put('error', 'Payment failed'); 
return Redirect::route('/');

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

@if ($message = Session::get('success')) 
        <div class="w3-panel w3-green w3-display-container"> 
               <span onclick="this.parentElement.style.display='none' “
                           class="w3-button w3-green w3-large w3-display-topright">&times;</span>          
               <p>{!! $message !!}</p> 
        </div> 
        <?php Session:: forget('success'); ?>
        @endif 

@if ($message = Session::get('error')) 
       <div class="w3-panel w3-red w3-display-container"> 
             <span onclick="this.parentElement.style.display='none' "
                         class="w3-button w3-red w3-large w3-display-topright">&times;</span> 
            <p>{!! $message !!}</p> 
       </div>
<?php Session:: forget('error'); ?> 
@endif 

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 and You hire dedicated Laravel developer for your project. Contact us to learn more about the PayPal integration in Laravel.

    Need an IT Experts? Get a Free Consultation!

    lets start your project

    Related Articles