PhonePe Api Integration in Laravel 8 - deesoft service

PhonePe Api Integration in Laravel 8

Deepak Tailor Image
Deepak Tailor - Jul 23 2023
PhonePe Api Integration in Laravel 8
Introduction

In the fast-paced world of e-commerce and online transactions, providing a smooth and secure payment experience is crucial for businesses. PhonePe, one of India's leading digital payment platforms, offers a user-friendly and reliable solution for processing payments. If you're running a Laravel 8-based web application and want to enable PhonePe as a payment option, you're in the right place. In this article, we'll walk you through the step-by-step process of integrating PhonePe into your Laravel 8 project, allowing you to streamline payment processing and offer a seamless user experience.

Prerequisites

Before we dive into the integration process, ensure you have the following:

1. A working Laravel 8 project.

2. A PhonePe Business Account and API credentials (Client ID and Client Secret).

3. Basic knowledge of Laravel and PHP.

Step 1: Installation and Setup

Begin by creating a fresh Laravel 8 project if you haven't already. Install the required dependencies for integrating PhonePe by using Composer. In your terminal, run:

composer require phonepe/phonepe
Step 2: Configuration

Next, you need to configure PhonePe's API credentials in your Laravel project. Locate the `config/services.php` file and add the following code at the bottom:

'phonepe' => [
    'client_id' => env('PHONEPE_CLIENT_ID'),
    'client_secret' => env('PHONEPE_CLIENT_SECRET'),
    'redirect' => env('PHONEPE_REDIRECT_URL'),
],

Ensure that you set up the corresponding environment variables in the `.env` file:

PHONEPE_CLIENT_ID=your_phonepe_client_id
PHONEPE_CLIENT_SECRET=your_phonepe_client_secret
PHONEPE_REDIRECT_URL=https://your_website.com/payment/success
Step 3: Creating Routes and Controllers

Now, let's create the necessary routes and controllers to handle the payment process. In the `routes/web.php` file, add the following route:

Route::get('/payment', 'PhonePePaymentController@initiatePayment')->name('payment');
Route::get('/payment/success', 'PhonePePaymentController@paymentSuccess')->name('payment.success');

Create a new controller by running the command:

php artisan make:controller PhonePePaymentController

In the `PhonePePaymentController`, implement the `initiatePayment` and `paymentSuccess` methods:

use PhonePe\PhonePe;

class PhonePePaymentController extends Controller
{
    public function initiatePayment()
    {
        // Add your payment initiation logic here
    }

    public function paymentSuccess()
    {
        // Add your payment success handling logic here
    }
}
Step 4: Initiating the Payment

In the `initiatePayment` method, you'll need to use the PhonePe SDK to initiate the payment request. Here's a basic example:

use PhonePe\Requests\PaymentRequest;
use PhonePe\Enums\PaymentMode;

public function initiatePayment()
{
    $paymentRequest = new PaymentRequest();
    $paymentRequest->setOrderId('ORDER_ID');
    $paymentRequest->setAmount(10000); // Amount in paise (10000 paise = ?100)
    $paymentRequest->setCurrency('INR');
    $paymentRequest->setMode(PaymentMode::UPI);
    $paymentRequest->setCustomerName('John Doe');
    $paymentRequest->setCustomerEmail('john.doe@example.com');
    $paymentRequest->setCustomerPhone('1234567890');
    $paymentRequest->setRedirectUrl(config('services.phonepe.redirect'));

    $phonepe = new PhonePe();
    $response = $phonepe->createTransaction($paymentRequest);

    // Redirect user to PhonePe payment page using $response->getRedirectUrl()
}
Step 5: Handling Payment Success

In the `paymentSuccess` method, you'll handle the response when the payment is successful:

use PhonePe\Requests\StatusRequest;

public function paymentSuccess()
{
    $paymentId = request('txn_id'); // The transaction ID from the callback
    $phonepe = new PhonePe();

    $statusRequest = new StatusRequest();
    $statusRequest->setPaymentId($paymentId);

    $response = $phonepe->paymentStatus($statusRequest);

    // Process the payment success response
    if ($response->isSuccess()) {
        // Payment was successful. Add your business logic here.
    } else {
        // Payment was not successful. Handle accordingly.
    }
}
Conclusion

Congratulations! You have successfully integrated the PhonePe payment gateway into your Laravel 8 project. Users can now make secure transactions using PhonePe, providing a seamless experience on your platform. Remember to thoroughly test the payment flow before deploying it to production. Feel free to explore more features and functionalities offered by PhonePe to further enhance your payment processing system.

Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in