Phonepay integration in laravel

PhonePe Payment Gateway Integration in Laravel; In this post, we will phopepe integrate in our laravel application. Phopepe is India’s largets payment collection api platform that allows vendors to integration the Phonepe payment gateway to their e-commerce site to collect the amount from the subscription plan from the public users and collect transfer payment to a merchant bank account. Phonepe allows us to accept payment digitally.

https://developer.phonepe.com/v1/reference/pay-api-1 // get credientials
https://developer.phonepe.com/v1/docs/how-to-go-live-1
https://api.phonepe.com/apis/hermes (Production host url)

change till here:
https://api-preprod.phonepe.com/apis/ 
composer require ixudra/curl
php artisan make:controller PhonepayController
PhonepayController:

use Ixudra\Curl\Facades\Curl;

class Phonepay extends Controller
{

public function phonePe()
{
$data = array (
'merchantId' => 'PGTESTPAYUAT',
'merchantTransactionId' => uniqid(),
'merchantUserId' => 'MUID123',
'amount' => 10000,
'redirectUrl' => route('response'),
'redirectMode' => 'POST',
'callbackUrl' => route('response'),
'mobileNumber' => '8085289889',
'paymentInstrument' => 
array (
'type' => 'PAY_PAGE',
),
);

$encode = base64_encode(json_encode($data));

$saltKey = '099eb0cd-02cf-4e2a-8aca-3e6c6aff0399';
$saltIndex = 1;

$string = $encode.'/pg/v1/pay'.$saltKey;
$sha256 = hash('sha256',$string);

$finalXHeader = $sha256.'###'.$saltIndex;

$url = "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay"; <TESING URL>
$url = "https://api.phonepe.com/apis/hermes/pg/v1/pay"; <PRODUCTION URL>

$response = Curl::to($url)
->withHeader('Content-Type:application/json')
->withHeader('X-VERIFY:'.$finalXHeader)
->withData(json_encode(['request' => $encode]))
->post();

$rData = json_decode($response);

return redirect()->to($rData->data->instrumentResponse->redirectInfo->url);

}


public function response(Request $request)
{
$input = $request->all();

$saltKey = '099eb0cd-02cf-4e2a-8aca-3e6c6aff0399';
$saltIndex = 1;

$finalXHeader = hash('sha256','/pg/v1/status/'.$input['merchantId'].'/'.$input['transactionId'].$saltKey).'###'.$saltIndex;

$response = Curl::to('https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/status/'.$input['merchantId'].'/'.$input['transactionId'])
->withHeader('Content-Type:application/json')
->withHeader('accept:application/json')
->withHeader('X-VERIFY:'.$finalXHeader)
->withHeader('X-MERCHANT-ID:'.$input['transactionId'])
->get();

dd(json_decode($response));
} 

}
Route::get('phonepe',[PhonePeController::class,'phonePe']);
Route::any('phonepe-response',[PhonePeController::class,'response'])->name('response');
app/http/middleware/VerifyCsrfToken.php

protected $except = [
   '/phonepe-response'
];

 

Leave a Reply