Hello, today we will learn how to integrate the PayPal payment gateway into Laravel using an example that is straightforward and working code. This is one of the greatest courses on how to do this.
Integrating the payment gateway into many Laravel applications is a straightforward and easy challenge. You can use this information to incorporate the payment gateway into your PayPal and Laravel application.
In this tutorial, I'll walk you through integrating PayPal with Laravel step-by-step and provide an example using PayPal 6. PayPal payment gateway integration in Laravel 9 is simple. So that your user can quickly pay a paypal account with credit card information, create a Laravel 9 PayPal integration.
Using the srmklive laravel paypal package, we integrate the PayPal API. There are choices for Paypal api code in the srmklive/laravel-paypal package. The exit method will be used to terminate the Laravel 7/Laravel 6 system.
The most often used gateway for web development is, as we all know, the PayPal payment gateway. Most likely, the client or customers decide to transfer funds to the client's website through the PayPal payment gateway. PayPal is a considerate method of global transfer.
In this tutorial, we'll integrate PayPal into Laravel using the srmklive package. Just a few actions must be taken to finish the payment integration in PHP Laravel 9.
In this article, I'll walk you through a complete example of how to integrate the PayPal payment mechanism, including how to create a Laravel project, migration, model, route, blade file, and more. So, just adhere to the instructions as given in the roar.
Step 1: Install Laravel 9
Here, we'll use the following command to create a new Laravel project.
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install Composer Package
To use the approach, we must now install the srmklive/paypal package for PayPal connectivity. Therefore, run the command below after opening your terminal.
composer require srmklive/paypal
config/app.php
'providers' => [ Srmklive\PayPal\Providers\PayPalServiceProvider::class ]
The srmklive/Paypal package may also be customized, so if you wish to make changes, simply use the command below to obtain the config file for PayPal.php.
php artisan vendor:publish --provider "Srmklive\PayPal\Providers\PayPalServiceProvider"
config/paypal.php
<?php
/**
* PayPal Setting & API Credentials
* Created by Raza Mehdi <srmk@outlook.com>.</srmk@outlook.com>
*/
return [
'mode' => env('PAYPAL_MODE', 'sandbox')
'sandbox' => [
'username' => env('PAYPAL_SANDBOX_API_USERNAME', ''),
'password' => env('PAYPAL_SANDBOX_API_PASSWORD', ''),
'secret' => env('PAYPAL_SANDBOX_API_SECRET', ''),
'certificate' => env('PAYPAL_SANDBOX_API_CERTIFICATE', ''),
'app_id' => 'APP-80W284485P519543T',
],
'live' => [
'username' => env('PAYPAL_LIVE_API_USERNAME', ''),
'password' => env('PAYPAL_LIVE_API_PASSWORD', ''),
'secret' => env('PAYPAL_LIVE_API_SECRET', ''),
'certificate' => env('PAYPAL_LIVE_API_CERTIFICATE', ''),
'app_id' => '',
],
'payment_action' => 'Sale',
'currency' => env('PAYPAL_CURRENCY', 'USD'),
'billing_type' => 'MerchantInitiatedBilling',
'notify_url' => '',
'locale' => '',
'validate_ssl' => false,
];
Step 3: Add Routes
Here, the resource path for the PayPal payment gateway needs to be added. So, enter "routes/web.php" and add the following route.
routes/web.php
Route::get('payment', 'PayPalController@payment')->name('payment');
Route::get('cancel', 'PayPalController@cancel')->name('payment.cancel');
Route::get('payment/success', 'PayPalController@success')->name('payment.success');
Step 4: Create Controller
php artisan make:controller PayPalController
app/Http/Controllers/PayPalController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Srmklive\PayPal\Services\ExpressCheckout;
class PayPalController extends Controller
{
/**
* Responds with a welcome message with instructions
*
* @return \Illuminate\Http\Response
*/
public function payment()
{
$data = [];
$data['items'] = [
[
'name' => 'codesolutionstuff.com',
'price' => 100,
'desc' => 'Description for codesolutionstuff.com',
'qty' => 1
]
];
$data['invoice_id'] = 1;
$data['invoice_description'] = "Order #{$data['invoice_id']} Invoice";
$data['return_url'] = route('payment.success');
$data['cancel_url'] = route('payment.cancel');
$data['total'] = 100;
$provider = new ExpressCheckout;
$response = $provider->setExpressCheckout($data);
$response = $provider->setExpressCheckout($data, true);
return redirect($response['paypal_link']);
}
/**
* Responds with a welcome message with instructions
*
* @return \Illuminate\Http\Response
*/
public function cancel()
{
dd('Your payment is canceled. You can create cancel page here.');
}
/**
* Responds with a welcome message with instructions
*
* @return \Illuminate\Http\Response
*/
public function success(Request $request)
{
$response = $provider->getExpressCheckoutDetails($request->token);
if (in_array(strtoupper($response['ACK']), ['SUCCESS', 'SUCCESSWITHWARNING'])) {
dd('Your payment was successfully. You can create success page here.');
}
dd('Something is wrong.');
}
}
Step 5: Create View File
resources/views/products/welcome.blade.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel 6 PayPal Integration Tutorial - codesolutionstuff.com</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha256-YLGeXaapI0/5IgZopewRJcFXomhRMlYYjugPLSyNjTY=" crossorigin="anonymous" />
<!-- Styles -->
<style>
html,
body {
background-color: #fff;
color: #636b6f;
font-family: 'Nunito', sans-serif;
font-weight: 200;
height: 100vh;
margin: 0;
}
.content {
margin-top: 100px;
text-align: center;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<h1>Laravel 9 PayPal Integration Tutorial - codesolutionstuff.com</h1>
<table border="0" cellpadding="10" cellspacing="0" align="center">
<tr>
<td align="center"></td>
</tr>
<tr>
<td align="center">
<a href="https://www.paypal.com/in/webapps/mpp/paypal-popup" title="How PayPal Works"
onclick="javascript:window.open('https://www.paypal.com/in/webapps/mpp/paypal-popup','WIPaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=1060, height=700'); return false;"><img
src="https://www.paypalobjects.com/webstatic/mktg/Logo/pp-logo-200px.png" border="0"
alt="PayPal Logo"></a>
</td>
</tr>
</table>
<a href="{{ route('payment') }}" class="btn btn-success">Pay $100 from Paypal</a>
</div>
</div>
</body>
</html>
Step 6: Add Configuration
In this stage, we'll set configuration values in the .env file, including the PayPal login, secret, and certificate key.
.env
PAYPAL_MODE=sandbox
PAYPAL_SANDBOX_API_USERNAME=sb-e2n47..
PAYPAL_SANDBOX_API_PASSWORD=XKCGW...
PAYPAL_SANDBOX_API_SECRET=A0EXIz....
PAYPAL_CURRENCY=INR
PAYPAL_SANDBOX_API_CERTIFICATE=