Laravel Omnipay PayPal payment gateway.

The PayPal is the highly used payment gateway. the Laravel is the most popular PHP framework. So Let’s see how we can setup the PayPal payment gateway on Laravel using Omnipay package.

Let’s see how we can setup the PayPal payment gateway using the Omnipay package on Laravel step by step.

I hope you installed Laravel if not please install Laravel first.

Now, Let’s open the “composer.json” file and add the Omnipay package.

{
    "require": {
        "omnipay/paypal": "~2.0"
    }
}

After modify “composer.json” file update the composer using the command prompt.

composer update

After installing the Omnipay package Let’s start the setup of Paypal payment gateway.

So, first of all, we create a route for PayPal payment gateway So let’s open the “routes/web.php” file.

Route::get('paypal/purchase', 'PaypalController@purchase');
Route::get('paypal/complete', 'PaypalController@complete');
Route::get('paypal/cancel', 'PaypalController@cancel');

Now, Let’s create the PaypalController So create new file at “app/Http/Controllers/PaypalController.php” file.

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Omnipay\Omnipay;
use Illuminate\Http\Request;

class PaypalController extends Controller
{

    protected $gateway = "";

    public function __construct(){
         $this->gateway = Omnipay::create('PayPal_Rest');
         $this->gateway->setClientId('clientId');
         $this->gateway->setSecret('clientSecret');
         $this->gateway->setTestMode(true);
    }

    public function purchase(){

        $params = array(
            'cancelUrl' => url('paypal/cancel'),
            'returnUrl' => url('paypal/complete'),
            'description' => 'Your Description',
            'amount' => 6.00,
            'currency' => 'USD'
        );

        $resp = $this->gateway->purchase($params)->setItems([
            ['name' => 'Product 1', 'quantity' => 1, 'price' => 3.00],
            ['name' => 'Product 2', 'quantity' => 1, 'price' => 2.00],
            ['name' => 'Product 3', 'quantity' => 1, 'price' => 1.00],
        ])->send();
 
        $transactionReference = $resp->getTransactionReference();
        $params['transactionReference'] = $transactionReference;

        \Session::put('params', $params);
        \Session::save();

         if ($resp->isRedirect()) {
             // redirect to offsite payment gateway
             $resp->redirect();
         } else {
             // payment failed: display message to customer echo
             dd($resp->getMessage());
         }
    }

    public function complete(Request $request){
         $params = \Session::get('params');
         $payerId = $request->input('PayerID');
         $token = $request->input('token');
         $paymentId = $request->input('paymentId');
         $params['payerId'] = $payerId;

         $response = $this->gateway->completePurchase($params)->send();
         if ($response->isSuccessful()) {
             echo $payerId;             
             echo '<br>';
             echo $token;
             echo '<br>';
             echo $paymentId;
             echo '<br>';
             exit;
         }else{
             echo “bugger”;
             print_r($response);
             exit;
         }
    }
    public function cancel(){
         echo "Something went wrong please try again.";
     }
}

Above is the complete code of the Omnipay PayPal payment gateway. If you face any issue after using this code please write the comment below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s