Dynamic Drag and Drop Form Builder with React: A Step-by-Step Guide
Read More
Hi there!
This post will provide you with a broad overview of how to integrate the PayPal Payment Gateway with Laravel 10.PayPal can be integrated into your Laravel application to give users a smooth payment experience. PayPal allows you to take payments from clients all over the world and even set up automatic payments for your goods or services.
In this article, we will walk you through the steps necessary to integrate PayPal into your Laravel application. We will be using the PayPal PHP SDK, which provides a simple and user-friendly interface for interacting with the PayPal API.
Here are the specifics for integrating PayPal in Laravel 10.
Activate your PayPal developer account. Login to PayPal
Open a business PayPal account.
Create a MyApps credentials account.
Install the Laravel application.
Install composer
Create a controller
Create route to Make payment channels
"Follow the instructions in the image to log in to your PayPal account. Once logged in, take the next critical step: create your own business PayPal account. Remember, this is a critical point that should not be missed!"
"After successfully creating your app, head over to the app's page and locate the client ID and secret key. These essential credentials are highlighted in the image below for your reference."
Create and set up a fresh Laravel application in accordance with your requirements. This action is required. (If you have previously made a new Laravel app, skip this step and go to Step 3).
To install the application and access the file to start integrating the PayPal Payment Gateway in Laravel, use the following code:
composer global require laravel/installer laravel new example-app
composer require paypal/rest-api-sdk-php
php artisan make:Controller PayPalController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PayPal\Api\Payer;
use PayPal\Api\Amount;
use PayPal\Api\Payment;
use PayPal\Api\Transaction;
use PayPal\Rest\ApiContext;
use PayPal\Api\RedirectUrls;
use PayPal\Api\PaymentExecution;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Exception\PayPalConnectionException;
class PayPalController extends Controller
{
public function payment()
{
$sum = 500;
$apiContext = new ApiContext(
new OAuthTokenCredential( 'ClientID', 'ClientSecret' ) );
// dd($apiContext);
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// dd($payer);
// Set redirect URLs
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(route('paypal.success'))
->setCancelUrl(route('paypal.cancel'));
// dd($redirectUrls);
// Set payment amount
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal($sum);
// Set transaction object
$transaction = new Transaction();
$transaction->setAmount($amount)
->setDescription(" Hello ");
// dd($transaction);
// Create the full payment object
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
// dd($payment);
// Create payment with valid API context
try {
$payment->create($apiContext);
// dd($payment);
// Get PayPal redirect URL and redirect the customer
// $approvalUrl =
return redirect($payment->getApprovalLink());
// dd($approvalUrl);
// Redirect the customer to $approvalUrl
} catch (PayPalConnectionException $ex) {
echo $ex->getCode();
echo $ex->getData();
die($ex);
} catch (Exception $ex) {
die($ex);
}
}
// this is success route
public function success(Request $request)
{
$apiContext = new ApiContext(
new OAuthTokenCredential('ClientID', 'ClientSecret') );
// Get payment object by passing paymentId
$paymentId = $_GET['paymentId'];
$payment = Payment::get($paymentId, $apiContext);
$payerId = $_GET['PayerID'];
// Execute payment with payer ID
$execution = new PaymentExecution();
$execution->setPayerId($payerId);
try {
dd('success');
} catch (PayPalConnectionException $ex) {
echo $ex->getCode();
echo $ex->getData();
die($ex);
} catch (Exception $ex) {
die($ex);
}
}
// this is cancel route
public function cancel()
{
dd('payment cancel');
}
}
Route::get('paypal-payment',[PayPalController::class,"payment"])->name('paypal.payment');
Route::get('paypal-success',[PayPalController::class,"success"])->name('paypal.success');
Route::get('paypal-cancel',[PayPalController::class,'cancel'])->name('paypal.cancel');
Now we are ready to run this application example with Laravel 8, so run the below command for a quick run:
php artisan serve
http://localhost:8000/paypal-payment
Finally, integrating the PayPal Payment Gateway into your Laravel application may assist you in expanding your online store or business. This connection allows your clients to make secure payments on your app or website.
Simply follow these simple instructions, and keep the code handy in case you need it. Following this process, you, you will quickly integrate the PayPal Payment Gateway into your Laravel application.
How to Integrate CoinGate with Laravel 10: Click here
Recent posts form our Blog
0 Comments
Like 0