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 TableRun PHP artisan to create migration file for product table.
php artisan make:migration create_products_table
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- 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.
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- class Product extends Model
- {
- public $fillable = ['name','details','price'];
- }
Same as product, create a payment table using php artisan command.
php artisan make:migration create_payments_table
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- 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");
- }
- }
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- class Payment extends Model
- {
- public $fillable = ['item_number','transaction_id','currency_code','payment_status'];
- }
- 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'; });
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Product;
- use App\Payment;
- 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 '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 formIn this form we bind the selected product details with paypal form.
- <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="http://demo.expertphp.in/payment-cancel">
- <input type="hidden" name="return" value="http://demo.expertphp.in/payment-status">
- </form>
- <SCRIPT>document.frmTransaction.submit();</SCRIPT>