Laravel 6 Paypal Payment Integrate Example
In this tutorial, we will tell you how to integrate the Paypal payment gateway in the Laravel Framework(Laravel 6 Paypal Payment Integrate Example).
The Paypal payment gateway is a popular payment gateway method and it is easily used for the project. so many developers prefer that payment gateway method.
PayPal is a secure payment method and it’s work-based using API. it provides two environments for the user like a sandbox and live.
Overview
Step 1: Install Laravel 6
Step 2: Setting Database Configuration
Step 3: Create Table using migration
Step 4: Install Package
Step 5: Get and Set Stripe API Key and SECRET
Step 6: Create Route
Step 7: Create a Model and Controller
Step 8: Create Blade File
Step 9: Run Our Laravel Application
Laravel 6 Paypal Payment Integrate Example
Step 1: Install Laravel 6
We are going to install laravel 6, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.
1 | composer create-project --prefer-dist laravel/laravel larave6_paypal |
Step 2: Setting Database Configuration
After the complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
1 2 3 4 5 6 | DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name(laravel6_paypal) DB_USERNAME=Enter_Your_Database_Username(root) DB_PASSWORD=Enter_Your_Database_Password(root) |
Step 3: Create Table using migration
Now, We need to create a migration. so we will below command using create the payments table migration.
1 | php artisan make:migration create_payments_table --create=payments |
After complete migration. we need below changes in the database/migrations/create_payments_table file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePaymentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('payments', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('payment_id'); $table->string('payer_id'); $table->string('payer_email'); $table->float('amount', 10, 2); $table->string('currency'); $table->string('payment_status'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('payments'); } } ?> |
Run the below command. after the changes above file.
1 | php artisan migrate |
Step 4: Install Package
Now, we are going to install the “omnipay/paypal” package using the below command.
1 | composer require league/omnipay omnipay/paypal |
Step 5: Get and Set Stripe API Key and SECRET
Now, we have to need PAYPAL_CLIENT_ID and PAYPAL_CLIENT_SECRET. so we will go to the official Paypal site and after then login gets PAYPAL_CLIENT_ID and PAYPAL_CLIENT_SECRET after then we will set in the .env file.
1 2 3 | PAYPAL_CLIENT_ID=PASTE_HERE_CLIENT_ID PAYPAL_CLIENT_SECRET=PASTE_HERE_CLIENT_SECRET PAYPAL_CURRENCY=USD |
Step 6: Create Route
Add the following route code in the “routes/web.php” file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('payment', 'PaymentController@index'); Route::post('charge', 'PaymentController@charge'); Route::get('paymentsuccess', 'PaymentController@payment_success'); Route::get('paymenterror', 'PaymentController@payment_error'); ?> |
Step 7: Create a Model and Controller
Now, We will create the controller using the below command and paste the below code in this controller.
1 | php artisan make:controller PaymentController --model=Payment |
PaymentController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Omnipay\Omnipay; use App\Payment; class PaymentController extends Controller { public $gateway; public function __construct() { $this->gateway = Omnipay::create('PayPal_Rest'); $this->gateway->setClientId(env('PAYPAL_CLIENT_ID')); $this->gateway->setSecret(env('PAYPAL_CLIENT_SECRET')); $this->gateway->setTestMode(true); } public function index() { return view('payment'); } public function charge(Request $request) { if($request->input('submit')) { try { $response = $this->gateway->purchase(array( 'amount' => $request->input('amount'), 'currency' => env('PAYPAL_CURRENCY'), 'returnUrl' => url('paymentsuccess'), 'cancelUrl' => url('paymenterror'), ))->send(); if ($response->isRedirect()) { $response->redirect(); } else { return $response->getMessage(); } } catch(Exception $e) { return $e->getMessage(); } } } public function payment_success(Request $request) { 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()) { $arr_body = $response->getData(); $isPaymentExist = Payment::where('payment_id', $arr_body['id'])->first(); if(!$isPaymentExist) { $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']; $payment->save(); } return "Payment is successful. Your transaction id is: ". $arr_body['id']; } else { return $response->getMessage(); } } else { return 'Transaction is declined'; } } public function payment_error() { return 'User is canceled the payment.'; } } |
Payment.php
1 2 3 4 5 6 7 8 9 10 11 12 | <?php namespace App; use Illuminate\Database\Eloquent\Model; class Payment extends Model { protected $fillable = [ 'payment_id','payer_id', 'payer_email','amount','currency','payment_status' ]; } ?> |
Step 6: Create Blade File
So finally, we will create the payment.tpl file in “resources/views/” directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <!DOCTYPE html> <html lang="en"> <head> <title>Laravel 6 Paypal Payment Integrate Example - XpertPhp</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="col-lg-12"> <div class="text-center"> <h2>Pay for Event</h2> <br> </div> </div> </div> <div class="row"> <div class="col-lg-3"> </div> <div class="col-lg-6"> <form action="{{ url('charge') }}" method="post"> {{ csrf_field() }} <div class="row"> <div class="col-lg-12 form-group"> <label>Amount</label> <input autocomplete="off" class="form-control" type="text" name="amount"> </div> </div> <div class="row"> <div class="col-lg-12 form-group"> <button class="form-control btn btn-success submit-button" type="submit" style="margin-top: 10px;">Pay »</button> </div> </div> </form> </div> </div> </body> </html> |
Step 9: Run Our Laravel Application
We can start the server and run this example using the below command.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/payment |