Laravel PhonePe Integration Example
In this post, we will cover the integration of PhonePe in a Laravel application, including generating a payment request and verifying the payment status.
Step 1: Set Up Your PhonePe Account
Before starting the integration, you must set up a PhonePe merchant account. Obtain your merchant ID, API key, and other necessary credentials.
Step 2: Install Required Libraries
Ensure your Laravel project is set up correctly. You might need to use
packages like guzzlehttp/guzzle
for making API requests:
composer require guzzlehttp/guzzle
Step 3: Configuring Your .env File
Add your PhonePe credentials to the .env
file:
PHONEPE_MERCHANT_ID=your_merchant_id
PHONEPE_API_KEY=your_api_key
PHONEPE_SALT_KEY=your_salt_key
PHONEPE_BASE_URL=https://api.phonepe.com
Step 4: Creating the Payment Request
Create a controller to handle the payment request:
<?php
namespace App\\Http\\Controllers;
use Illuminate\\Http\\Request;
use GuzzleHttp\\Client;
class PhonePeController extends Controller
{
public function initiatePayment(Request $request)
{
$amount = $request->amount * 100; // Convert to paise
$merchantId = env('PHONEPE_MERCHANT_ID');
$apiKey = env('PHONEPE_API_KEY');
$saltKey = env('PHONEPE_SALT_KEY');
$baseUrl = env('PHONEPE_BASE_URL');
$payload = [
'merchantId' => $merchantId,
'transactionId' => uniqid(), // Unique transaction ID
'amount' => $amount,
'currency' => 'INR',
'redirectUrl' => route('payment.callback'),
'validity' => time() + 600, // Payment validity in seconds
];
$data = json_encode($payload);
$hashedData = hash_hmac('sha256', $data, $saltKey);
$client = new Client();
$response = $client->post($baseUrl . '/v3/transaction/initiate', [
'headers' => [
'Content-Type' => 'application/json',
'X-VERIFY' => $hashedData . '###' . $apiKey,
],
'body' => $data,
]);
return response()->json(json_decode($response->getBody(), true));
}
}
Step 5: Payment Callback
Create a method to handle the callback from PhonePe and verify the payment status:
<?php
namespace App\\Http\\Controllers;
use Illuminate\\Http\\Request;
use GuzzleHttp\\Client;
class PhonePeController extends Controller
{
public function paymentCallback(Request $request)
{
$transactionId = $request->transactionId;
$merchantId = env('PHONEPE_MERCHANT_ID');
$apiKey = env('PHONEPE_API_KEY');
$saltKey = env('PHONEPE_SALT_KEY');
$baseUrl = env('PHONEPE_BASE_URL');
$url = $baseUrl . "/v3/transaction/$merchantId/$transactionId/status";
$hashedData = hash_hmac('sha256', $url, $saltKey);
$client = new Client();
$response = $client->get($url, [
'headers' => [
'X-VERIFY' => $hashedData . '###' . $apiKey,
],
]);
$statusResponse = json_decode($response->getBody(), true);
if ($statusResponse['success']) {
// Payment successful
// Update your order status or take necessary action
} else {
// Payment failed
}
return response()->json($statusResponse);
}
}
Step 6: Testing
Make sure to thoroughly test the integration using test credentials provided by PhonePe. Once everything is working as expected, you can switch to live mode.
With this setup, your Laravel application can handle PhonePe payments effectively!
0 Comments