Set up Paypal Payment Gateway Integration in Laravel PHP Example

Set up Paypal Payment Gateway Integration in Laravel PHP Example

PayPal standard payment gateway integration in Laravel PHP

In this tutorial, i will tell you to integrate Paypal payment gateway in standard way in Laravel PHP Framework.

Paypal payment gateway is going popular for all projects and easier to integrate in website for developers and here i define easy way to integrate paypal payment gateway in application.

There are two environments, one is sandbox which is used for testing purpose and second one is live.

Customer can easily make payment on Paypal payment gateway and they can also make secure payment by using their cards or paypal balance that means paypal payment gateway is secure for users to make online payment.

I have two tables products and payments and relatively models.

Here is the process to create table and model in Laravel.

Product Table

Run PHP artisan to create migration file for product table.

php artisan make:migration create_products_table

  1. use Illuminate\Database\Schema\Blueprint;
  2. use Illuminate\Database\Migrations\Migration;
  3. class CreateProductsTable extends Migration
  4. {
  5. public function up()
  6. {
  7. Schema::create('products', function (Blueprint $table) {
  8. $table->increments('id');
  9. $table->string('name');
  10. $table->text('details');
  11. $table->float('price');
  12. $table->timestamps();
  13. });
  14. }
  15. public function down()
  16. {
  17. Schema::drop("products");
  18. }
  19. }

Paste this code in your migration file for products and run php artisan migrate to create table in your database.

Product Model

  1. namespace App;
  2. use Illuminate\Database\Eloquent\Model;
  3. class Product extends Model
  4. {
  5. public $fillable = ['name','details','price'];
  6. }
Payment Table

Same as product, create a payment table using php artisan command.

php artisan make:migration create_payments_table

  1. use Illuminate\Database\Schema\Blueprint;
  2. use Illuminate\Database\Migrations\Migration;
  3. class CreatePaymentsTable extends Migration
  4. {
  5. public function up()
  6. {
  7. Schema::create('payments', function (Blueprint $table) {
  8. $table->increments('id');
  9. $table->string('item_number');
  10. $table->string('transaction_id');
  11. $table->string('currency_code');
  12. $table->string('payment_status');
  13. $table->timestamps();
  14. });
  15. }
  16. public function down()
  17. {
  18. Schema::drop("payments");
  19. }
  20. }
Payment Model

  1. namespace App;
  2. use Illuminate\Database\Eloquent\Model;
  3. class Payment extends Model
  4. {
  5. public $fillable = ['item_number','transaction_id','currency_code','payment_status'];
  6. }
routes.php

  1. Route::get('payment-status',array('as'=>'payment.status','uses'=>'PaymentController@paymentInfo'));
  2. Route::get('payment',array('as'=>'payment','uses'=>'PaymentController@payment'));
  3. Route::get('payment-cancel', function () { return 'Payment has been canceled'; });
Payment Controller

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Product;
  5. use App\Payment;
  6. class PaymentController extends Controller {
  7. public function payment(Request $request){
  8.     $product=Product::find($request->id);    
  9.      return view('payment',compact('product'));
  10.     }
  11.     public function paymentInfo(Request $request){        
  12.         if($request->tx){
  13.             if($payment=Payment::where('transaction_id',$request->tx)->first()){
  14.                 $payment_id=$payment->id;
  15.             }else{
  16.                 $payment=new Payment;
  17.                 $payment->item_number=$request->item_number;
  18.                 $payment->transaction_id=$request->tx;
  19.                 $payment->currency_code=$request->cc;
  20.                 $payment->payment_status=$request->st;
  21.                 $payment->save();
  22.                 $payment_id=$payment->id;
  23.             }
  24.         return 'Pyament has been done and your payment id is : '.$payment_id;
  25.         
  26.         }else{
  27.             return 'Payment has failed';
  28.         }
  29.     }
  30. }

Payment method are used to render form of payment gateway where selected product data is passed to buy.

After successfull redirection from paypal payment gateway we fetch response data in paymentInfo method and according that we update payment table with transaction id.

Payment form

In this form we bind the selected product details with paypal form.

  1. <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" name="frmTransaction" id="frmTransaction">
  2. <input type="hidden" name="business" value="{{$paypal_id}}">
  3. <input type="hidden" name="cmd" value="_xclick">
  4.     <input type="hidden" name="item_name" value="{{$product->name}}">
  5. <input type="hidden" name="item_number" value="{{$product->id}}">
  6. <input type="hidden" name="amount" value="{{$product->price}}">
  7. <input type="hidden" name="currency_code" value="USD">
  8. <input type="hidden" name="cancel_return" value="http://demo.expertphp.in/payment-cancel">
  9. <input type="hidden" name="return" value="http://demo.expertphp.in/payment-status">
  10. </form>
  11. <SCRIPT>document.frmTransaction.submit();</SCRIPT>

Phone: (+91) 8800417876
Noida, 201301