PHP code example of zarenta / phonepe

1. Go to this page and download the library: Download zarenta/phonepe library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zarenta / phonepe example snippets



->withMiddleware(function (Middleware $middleware) {

    $middleware->web(append: [

        \App\Http\Middleware\HandleInertiaRequests::class,

        \Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,

    ]);

    $middleware->validateCsrfTokens(except: [

        'phonepe/callback',  // Exclude PhonePe callback route from CSRF

    ]);

})



return [

    'merchantId' => env('PHONEPE_MERCHANT_ID'),

    'merchantUserId' => env('PHONEPE_MERCHANT_USER_ID'),

    'saltKey' => env('PHONEPE_SALT_KEY'),

    'saltIndex' => env('PHONEPE_SALT_INDEX'),

    'callBackUrl' => env('PHONEPE_CALLBACK_URL'),

    'env' => env('PHONEPE_ENV', 'sandbox'),

];



use PhonePe\PhonePeGateway;

public function initiatePayment(Request $request)

{

    $phonePe = new PhonePeGateway();

    try {

        $paymentUrl = $phonePe->makePayment(

            $amount = 1000, // Amount in rupees

            $redirectUrl = 'https://yourdomain.com/payment/success',

            $merchantTransactionId = 'your_unique_transaction_id',

            $phone = '9999999999',

            $email = '[email protected]',

            $shortName = 'Your Company',

            $message = 'Payment for Order #1234'

        );

        return redirect($paymentUrl);

    } catch (PhonePe\Exception\PhonePeException $e) {

        return response()->json(['error' => $e->getMessage()], 400);

    }

}



use PhonePe\PhonePeGateway;

public function checkTransactionStatus($transactionId)

{

    $phonePe = new PhonePeGateway();


    $status = $phonePe->getTransactionStatus(\request()->all());

    if ($status) {

        return response()->json(['status' => 'Transaction successful']);

    } else {

        return response()->json(['status' => 'Transaction failed or pending']);

    }

}



Route::post('/phonepe/callback', [YourPaymentController::class, 'handlePhonePeCallback']);

bash

php artisan vendor:publish --tag=phonepe-config