Set up Paypal Payment Gateway Integration in Laravel PHP Example
In this post we will give you information about Set up Paypal Payment Gateway Integration in Laravel PHP Example. Hear we will give you detail about Set up Paypal Payment Gateway Integration in Laravel PHP ExampleAnd how to use it also give you demo for it if it is necessary.
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
- use IlluminateDatabaseSchemaBlueprint;
- use IlluminateDatabaseMigrationsMigration;
- class CreateProductsTable extends Migration
- {
- public functionup()
- {
- Schema::create('products',function(Blueprint $table){
- $table->increments('id');
- $table->string('name');
- $table->text('details');
- $table->float('price');
- $table->timestamps();
- });
- }
- public functiondown()
- {
- Schema::drop("products");
- }
- }
use IlluminateDatabaseSchemaBlueprint;use IlluminateDatabaseMigrationsMigration;class CreateProductsTable extends Migration{ public function up() { Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->text('details'); $table->float('price'); $table->timestamps(); }); } public function down() { Schema::drop("products"); }}Paste this code in your migration file for products and run php artisan migrate to create table in your database.
Product Model
- namespace App;
- use IlluminateDatabaseEloquentModel;
- class Product extends Model
- {
- public $fillable=['name','details','price'];
- }
namespace App;use IlluminateDatabaseEloquentModel;class Product extends Model{ public $fillable = ['name','details','price'];}Payment Table
Same as product, create a payment table using php artisan command.
php artisan make:migration create_payments_table
- use IlluminateDatabaseSchemaBlueprint;
- use IlluminateDatabaseMigrationsMigration;
- class CreatePaymentsTable extends Migration
- {
- public functionup()
- {
- Schema::create('payments',function(Blueprint $table){
- $table->increments('id');
- $table->string('item_number');
- $table->string('transaction_id');
- $table->string('currency_code');
- $table->string('payment_status');
- $table->timestamps();
- });
- }
- public functiondown()
- {
- Schema::drop("payments");
- }
- }
use IlluminateDatabaseSchemaBlueprint;use IlluminateDatabaseMigrationsMigration;class CreatePaymentsTable extends Migration{ public function up() { Schema::create('payments', function (Blueprint $table) { $table->increments('id'); $table->string('item_number'); $table->string('transaction_id'); $table->string('currency_code'); $table->string('payment_status'); $table->timestamps(); }); } public function down() { Schema::drop("payments"); }}Payment Model
- namespace App;
- use IlluminateDatabaseEloquentModel;
- class Payment extends Model
- {
- public $fillable=['item_number','transaction_id','currency_code','payment_status'];
- }
namespace App;use IlluminateDatabaseEloquentModel;class Payment extends Model{ public $fillable = ['item_number','transaction_id','currency_code','payment_status'];}routes.php
- Route::get('payment-status',array('as'=>'payment.status','uses'=>'PaymentController@paymentInfo'));
- Route::get('payment',array('as'=>'payment','uses'=>'PaymentController@payment'));
- Route::get('payment-cancel', function () { return 'Payment has been canceled'; });
Route::get('payment-status',array('as'=>'payment.status','uses'=>'PaymentController@paymentInfo'));Route::get('payment',array('as'=>'payment','uses'=>'PaymentController@payment'));Payment Controller
- <?php
- namespace AppHttpControllers;
- use IlluminateHttpRequest;
- use AppProduct;
- use AppPayment;
- class PaymentController extends Controller {
- public functionpayment(Request $request){
- $product=Product::find($request->id);
- returnview('payment',compact('product'));
- }
- public functionpaymentInfo(Request $request){
- if($request->tx){
- if($payment=Payment::where('transaction_id',$request->tx)->first()){
- $payment_id=$payment->id;
- }else{
- $payment=new Payment;
- $payment->item_number=$request->item_number;
- $payment->transaction_id=$request->tx;
- $payment->currency_code=$request->cc;
- $payment->payment_status=$request->st;
- $payment->save();
- $payment_id=$payment->id;
- }
- return'Pyament has been done and your payment id is : '.$payment_id;
- }else{
- return'Payment has failed';
- }
- }
- }
<?phpnamespace AppHttpControllers;use IlluminateHttpRequest;use AppProduct;use AppPayment;class PaymentController extends Controller { public function payment(Request $request){ $product=Product::find($request->id); return view('payment',compact('product')); } public function paymentInfo(Request $request){ if($request->tx){ if($payment=Payment::where('transaction_id',$request->tx)->first()){ $payment_id=$payment->id; }else{ $payment=new Payment; $payment->item_number=$request->item_number; $payment->transaction_id=$request->tx; $payment->currency_code=$request->cc; $payment->payment_status=$request->st; $payment->save(); $payment_id=$payment->id; } return redirect('home')->with('message','Pyament has been done and your payment id is : '.$payment_id); }else{ return 'Payment has failed'; } }}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.
- <formaction="https://www.sandbox.paypal.com/cgi-bin/webscr"method="post"name="frmTransaction"id="frmTransaction">
- <inputtype="hidden"name="business"value="{{$paypal_id}}">
- <inputtype="hidden"name="cmd"value="_xclick">
- <inputtype="hidden"name="item_name"value="{{$product->name}}">
- <inputtype="hidden"name="item_number"value="{{$product->id}}">
- <inputtype="hidden"name="amount"value="{{$product->price}}">
- <inputtype="hidden"name="currency_code"value="USD">
- <inputtype="hidden"name="cancel_return"value="//payment-cancel">
- <inputtype="hidden"name="return"value="//payment-status">
- </form>
- <SCRIPT>document.frmTransaction.submit();</SCRIPT>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" name="frmTransaction" id="frmTransaction"> <input type="hidden" name="business" value="{{$paypal_id}}"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="item_name" value="{{$product->name}}"> <input type="hidden" name="item_number" value="{{$product->id}}"> <input type="hidden" name="amount" value="{{$product->price}}"> <input type="hidden" name="currency_code" value="USD"> <input type="hidden" name="cancel_return" value="//payment-cancel"> <input type="hidden" name="return" value="//payment-status"></form><SCRIPT>document.frmTransaction.submit();</SCRIPT> Response text of paypal payment gateway which you will get after successfully payment :
- {"tx":"4V936557BM418705U","st":"Completed","amt":"10.00","cc":"USD","cm":"","item_number":"292"}
{"tx":"4V936557BM418705U","st":"Completed","amt":"10.00","cc":"USD","cm":"","item_number":"292"}How to Configure PayPal Sandbox Auto Return and Payment Data Transfer
Hope this code and post will helped you for implement Set up Paypal Payment Gateway Integration in Laravel PHP Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs