PayPal Payment Gateway Integration in Laravel

In the following guide, I will explain to you how you can integrate PayPal payment gateway on your Laravel site using PayPal Rest API.

Let’s Prepare some stuff :

  • Go to PayPal Developer account and login with your credentials. 
  • Search for ‘My Apps & Credentials’ menu and click on ‘Create Program’ button.
  • Find the client id and secret of from sandbox mode and copy it for later use.

It’s Time to Code : 

For accomplishing this task we need a table. 

Now, create a migration using the following command:

php artisan make:migration create_pay_table

Open the newly created migration file and add the below code in the (up) method.


public function up()


    Schema::create(‘pay’, function (Blueprint $table) {





        $table->float(‘amount’, 10, 2);






Just migration the above data using the below command.

php artisan migrate

This command will create a ‘pay’ table in the database. 

Now, create a model  for ‘pay’ table.

php artisan make:model Payment

Add the below Code in .env file and change credentials accordingly




php artisan config:cache

Let’s define our routes which we will require in the next steps.



Route::get(‘payment’, ‘PaymentControllers@index’);

Route::post(‘paynow’, ‘PaymentControllers@charge’);

Route::get(‘paymentsuccess’, ‘PaymentController@payment_success’);

Route::get(‘paymenterror’, ‘PaymentController@payment_error’);

Package for PayPal Payment Gateway Integration 

Omnipay is the most popular payment processing library for Laravel. That gives you an effortless code for integrating various types of payment gateway. 

Installing Omnipay library:

composer require league/omnipay omnipay/paypal

It’s time to create a controller PaymentControllers and define the all methods mentioned in our route file.

php artisan make:controller PaymentControllers

Paste the Below Code in PaymentControllers file



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Omnipay\Omnipay;

use App\Payment;

class PaymentControllers extends Controller


    public $gateway;

    public function __construct()


        $this->gateway = Omnipay::create(‘PayPal_Rest’);



        $this->gateway->setTestMode(true); //set it to ‘false’ when go live


    public function index()


        return view(‘payment’);


    public function charge(Request $request)




            try {

                $response = $this->gateway->purchase(array(

                    ‘amount’ => $request->input(‘amount’),

                    ‘currency’ => env(‘PAYPAL_CURRENCY’),

                    ‘returnUrl’ => url(‘paymentsuccess’),

                    ‘cancelUrl’ => url(‘paymenterror’),



                if ($response->isRedirect()) {

                    $response->redirect(); // this will automatically forward the customer

                } else {

                    // not successful

                    return $response->getMessage();


            } catch(Exception $e) {

                return $e->getMessage();




    public function payment_success(Request $request)


        // Once the transaction has been approved, we need to complete it.

        if ($request->input(‘paymentId’) && $request->input(‘PayerID’))


            $transaction = $this->gateway->completePurchase(array(

                ‘payer_id’ => $request->input(‘PayerID’),

                ‘transactionReference’ => $request->input(‘paymentId’),


            $response = $transaction->send();


            if ($response->isSuccessful())


                // The customer has successfully paid.

                $arr_body = $response->getData();


                // Insert transaction data into the database

                $isPaymentExist = Payment::where(‘payment_id’, $arr_body[‘id’])->first();




                    $payment = new Payment;

                    $payment->payment_id = $arr_body[‘id’];

                    $payment->payer_id = $arr_body[‘payer’][‘payer_info’][‘payer_id’];

                    $payment->payer_email = $arr_body[‘payer’][‘payer_info’][’email’];

                    $payment->amount = $arr_body[‘transactions’][0][‘amount’][‘total’];

                    $payment->currency = env(‘PAYPAL_CURRENCY’);

                    $payment->payment_status = $arr_body[‘state’];




                return “Payment done. transaction id is: “. $arr_body[‘id’];

            } else {

                return $response->getMessage();


        } else {

            return ‘Transaction is declined’;



    public function payment_error()


        return ‘User has cancelled the payment.’;



In the above code, we take the important credentials and after that, we send a user to PayPal for payment. If the payment gets successful, then we store all the details in the ‘pay’ table.

Some small tweaks

  • Create a new file named as (payment.blade.php) 
  • Paste the code below in it.

<form action=”{{ url(‘paynow’) }}” method=”post” class=”form-group”>

      {{ csrf_field() }}

<input type=”text” name=”amount” />

<input type=”submit”  value=”Pay Now”>


Final Words

Finally, we had done all the changes it’s time to submit the form. When the user submits this form, then the function called in the PaymentController for the payment process. I hope you get enough information regarding PayPal payment gateway integration in Laravel. If you get stuck between methods I’m happy to help you to fix that problem.

Posted in LaravelTags:

Write a comment