Getting Started

Summary: This post refers to integrating a payment gateway in laravel using PayPal. The most commonly used payment method universally accepted across the globe is now available to take payments from your laravel application, just ahead.

Integrating a paypal payment gateway with laravel application


  1. Make sure laravel healthy distribution is already installed.
  2. Database is configured i.e. database credentials are added to the .env file

Install PayPal SDK

composer require paypal/rest-api-sdk-php

And Create a developer account or buyer account on Paypal by visiting here

the credential obtained from here must be included in the .env file for security.

Create a new file paypal.php at the \config directory and place the underneath there.

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'

Create a simple Pay with PayPal dialogue box which can take amount to be paid as input from the user.

Create a new controller to manage PayPal operations as

php artisan make:controller PaymentController

Initialize the constructor as

public function __construct()

/** PayPal api context **/
       $paypal_conf = \Config::get('paypal');
       $this->_api_context = new ApiContext(new OAuthTokenCredential(


Finally, all setup of payment with PayPal is completed and this underneath code will execute when a new payer starts its payment.

public function payWithpaypal(Request $request)

$payer = new Payer();

$item_1 = new Item();

$item_1->setName('Item 1') /** item name **/
           ->setPrice($request->get('amount')); /** unit price **/

$item_list = new ItemList();

$amount = new Amount();

$transaction = new Transaction();
           ->setDescription('Your transaction description');

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

$payment = new Payment();
       /** dd($payment->create($this->_api_context));exit; **/
       try {


} 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();



/** 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');


This completes the Payment with PayPal in laravel application integrated.

The approved status of the payment is thus saved in session as

if ($result->getState() == 'approved') {

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


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



Sign in to publish a comment


Be the first to comment on this post.