Blog

Home / Blog

Paypal payment gateway integration in laravel example

This example demonstrates how to seamlessly integrate Paypal payment gateway into a Laravel project, providing users with a secure and convenient way to make online transactions. The integration includes setting up routes, controllers, views, and handling payment responses to ensure a smooth and efficient payment process.

Paypal payment gateway integration in laravel github

This Laravel package seamlessly integrates the Paypal payment gateway into your application, allowing for secure and efficient transactions. With easy-to-follow setup instructions and comprehensive documentation, this Github repository makes integrating Paypal payments into your Laravel project a breeze.

If you are developing a web application using Laravel, integrating a payment gateway is essential for processing online transactions. One popular payment gateway that developers often rely on is PayPal. In this article, we will discuss how to integrate PayPal payment gateway in a Laravel application.

Why Use PayPal Payment Gateway?

PayPal is a trusted and widely used payment gateway that allows users to make secure online payments. By integrating PayPal into your Laravel application, you can offer your customers a convenient and reliable payment option. Additionally, PayPal supports both credit card and PayPal account payments, making it versatile for different types of users.

Setting Up PayPal in Laravel

To integrate PayPal into your Laravel application, you will need to create a PayPal Business account and obtain API credentials. These credentials, including the Client ID and Secret Key, will be used to authenticate your requests to the PayPal API.

Once you have obtained your API credentials, you can start integrating PayPal into your Laravel application. One of the most popular packages for integrating PayPal in Laravel is the paypal/rest-api-sdk-php package. To install this package, run the following command in your Laravel project directory:

```
composer require paypal/rest-api-sdk-php
```

Once the package is installed, you can start integrating PayPal into your Laravel application.

Creating a PayPal Payment Controller

To handle PayPal transactions in your Laravel application, you can create a new controller called PayPalPaymentController. In this controller, you will define methods for processing payments, creating payment forms, and handling callbacks from PayPal.

First, let's create a method for generating a PayPal payment form:

```php
use IlluminateHttpRequest;
use PayPalRestApiContext;
use PayPalAuthOAuthTokenCredential;
use PayPalApiAmount;
use PayPalApiPayer;
use PayPalApiPayment;
use PayPalApiRedirectUrls;
use PayPalApiTransaction;

class PayPalPaymentController extends Controller
{
private $apiContext;

public function __construct()
{
$this->apiContext = new ApiContext(
new OAuthTokenCredential('CLIENT_ID', 'SECRET_KEY')
);
}

public function createPayment(Request $request)
{
$payer = new Payer();
$payer->setPaymentMethod('paypal');

$amount = new Amount();
$amount->setTotal($request->amount);
$amount->setCurrency('USD');

$transaction = new Transaction();
$transaction->setAmount($amount);

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(url('payment/success'));
$redirectUrls->setCancelUrl(url('payment/cancel'));

$payment = new Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setTransactions([$transaction]);
$payment->setRedirectUrls($redirectUrls);

$payment->create($this->apiContext);

return redirect($payment->getApprovalLink());
}
}
```

In the above code, we create a new PayPal payment form by setting up a Payer, Amount, Transaction, and RedirectUrls objects. We then create a new Payment object and set the payment intent to 'sale'. Finally, we create the payment using the PayPal API context and redirect the user to the approval link.

Handling PayPal Callbacks

After the user completes the payment on the PayPal website, they will be redirected back to your Laravel application. To handle the callback from PayPal, you can create two new methods in your controller: one for success and one for cancel:

```php
class PayPalPaymentController extends Controller
{
...

public function paymentSuccess(Request $request)
{
$paymentId = $request->paymentId;
$payment = Payment::get($paymentId, $this->apiContext);

// Process successful payment
}

public function paymentCancel()
{
return redirect()->route('home')->with('error', 'Payment has been cancelled.');
}
}
```

In the paymentSuccess method, we retrieve the payment ID from the request and fetch the payment details using the PayPal API context. You can then process the payment and update your application's database accordingly.

Conclusion

Integrating PayPal payment gateway in Laravel is essential for processing online transactions securely and efficiently. By following the steps outlined in this article, you can create a seamless payment experience for your users and increase the usability and functionality of your Laravel application. PayPal offers a robust API that makes it easy to integrate payments into your Laravel application, ensuring a smooth and secure transaction process for your customers.

Payment gateway integration services

Streamline your online payment process with our expert payment gateway integration services. Securely accept payments and increase conversion rates with ease.