Introduction:
Paypal is a global payment option that is compatible with most nations that allow online money transfers. Paypal offers a more rapid and secure method of money transfers. The majority of e-commerce companies use Paypal to receive payments from clients because of its widespread use.
We will incorporate the Paypal payment gateway into a Laravel application in this post. This e-commerce website makes use of the Laravel framework for backend development. We’ll go over everything step-by-step right from the start.
Follow the steps below:
Step 1: Create New laravel Project
Create a new project with the command as below.
composer create-project laravel/laravel paypal --prefer-dist
After the new project has been created, go to your project directory.
cd paypal
Step 2: Install Packages for Paypal Payment Gateway Using Composer
Run the following command.
composer require srmklive/paypal:~3.0
Step 3: Create PayPal credentials
After installing the PayPal package, we must enter PayPal developer mode and create a new sandbox account in order to obtain the client_id and secret_key needed for PayPal integration. You must obtain the client_id and secret_key as indicated below after logging into PayPal. We must first create the application in order to obtain the client_id and secret_key. Now, create an app and take a look at the screenshot below. Open the Developer Dashboard and log in.
Click Create Apps.
Fill in the name of the application that was created.
Then you will get the client key and secret key that will be used in the application.
Step 4: Configure the package
You enter your project and add the key and secret key that you obtained from the.env file when the package installation is finished.
PAYPAL_MODE=sandbox
#Paypal sandbox credential
PAYPAL_SANDBOX_CLIENT_ID=AXELAz06GFLR.............................QNu7zyjuYpFLu1g
PAYPAL_SANDBOX_CLIENT_SECRET=EA9dinW1.............................PUzgVQCz7fK4tqe1-jLZCyHzZ0tDTRAx-6qJdIY933Q
Use the vendor:publish command below to change the package’s default configuration settings.
php artisan vendor:publish --provider "Srmklive\PayPal\Providers\PayPalServiceProvider"
This will create a configuration fileĀ config/paypal.phpĀ with the details below, which you can modify.
Step 5: Create Routes
In order to test the application test transaction, we must now establish an application route. Add the new route below by opening the route/web.php application route file.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PayPalController;
Route::get('create-transaction', [PayPalController::class, 'createTransaction'])->name('createTransaction');
Route::get('process-transaction', [PayPalController::class, 'processTransaction'])->name('processTransaction');
Route::get('success-transaction', [PayPalController::class, 'successTransaction'])->name('successTransaction');
Route::get('cancel-transaction', [PayPalController::class, 'cancelTransaction'])->name('cancelTransaction');
Step 6: Create Controller
After we create a route, then next we create a controller using php artisan.
php artisan make:controller PayPalController
We already have a controller in the directoryĀ app/Http/Controllers/PayPalController.php
. Open it and add the code below.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Srmklive\PayPal\Services\PayPal as PayPalClient;
class PayPalController extends Controller
{
/**
* create transaction.
*
* @return \Illuminate\Http\Response
*/
public function createTransaction()
{
return view('transaction');
}
/**
* process transaction.
*
* @return \Illuminate\Http\Response
*/
public function processTransaction(Request $request)
{
$provider = new PayPalClient;
$provider->setApiCredentials(config('paypal'));
$paypalToken = $provider->getAccessToken();
$response = $provider->createOrder([
"intent" => "CAPTURE",
"application_context" => [
"return_url" => route('successTransaction'),
"cancel_url" => route('cancelTransaction'),
],
"purchase_units" => [
0 => [
"amount" => [
"currency_code" => "USD",
"value" => "1.00"
]
]
]
]);
if (isset($response['id']) && $response['id'] != null) {
// redirect to approve href
foreach ($response['links'] as $links) {
if ($links['rel'] == 'approve') {
return redirect()->away($links['href']);
}
}
return redirect()
->route('createTransaction')
->with('error', 'Something went wrong.');
} else {
return redirect()
->route('createTransaction')
->with('error', $response['message'] ?? 'Something went wrong.');
}
}
/**
* success transaction.
*
* @return \Illuminate\Http\Response
*/
public function successTransaction(Request $request)
{
$provider = new PayPalClient;
$provider->setApiCredentials(config('paypal'));
$provider->getAccessToken();
$response = $provider->capturePaymentOrder($request['token']);
if (isset($response['status']) && $response['status'] == 'COMPLETED') {
return redirect()
->route('createTransaction')
->with('success', 'Transaction complete.');
} else {
return redirect()
->route('createTransaction')
->with('error', $response['message'] ?? 'Something went wrong.');
}
}
/**
* cancel transaction.
*
* @return \Illuminate\Http\Response
*/
public function cancelTransaction(Request $request)
{
return redirect()
->route('createTransaction')
->with('error', $response['message'] ?? 'You have canceled the transaction.');
}
}
Step 7: Create blade file to create payment button
Our plan is to develop a view that will guide the transaction processing. Add the following code to the blade view resources/views/transaction.blade.php file that has been created.
<!DOCTYPE html>
<html>
<head>
<title>PayPal Gateway test by dharmu</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<div class="container" width="50px">
<div class="panel panel-primary" style="margin-top:110px;">
<div class="panel-heading">
<h3 class="text-center">PayPal Gateway test by dharmu</h3>
</div>
<div class="panel-body">
<form action="{{ route('processTransaction') }}" method="get" enctype="multipart/form-data">
@if($message = Session::get('message'))
<p>{!! $message !!}</p>
<?php Session::forget('success'); ?>
@endif
<div class="row">
<div class="col-md-12">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name" required>
</div>
<div class="col-md-12">
<strong>Mobile No.:</strong>
<input type="text" name="mobile" class="form-control" maxlength="10" placeholder="Mobile No." required>
</div>
<div class="col-md-12">
<strong>Email:</strong>
<input type="email" name="email" class="form-control" placeholder="Email" required>
</div>
<div class="col-md-12">
<br>
<div class="btn btn-info">
Claim charge : $1
</div>
</div>
<div class="col-md-12">
<br>
<button type="submit" class="btn btn-success">PROCEED</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Step 8 : Run the app
Paypal integration is finished. We must now complete a transaction. Use the following Artisan command to launch the Laravel server.
php artisan serve
http://localhost:8000/create-transaction
click Pay, it will display the payment form.
Thanks for Reading.