PHP code example of yogeshgupta / phonepe-laravel
1. Go to this page and download the library: Download yogeshgupta/phonepe-laravel 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/ */
yogeshgupta / phonepe-laravel example snippets
return [
'environment' => env('PHONEPE_ENV', 'uat'),
'uat' => [
'client_id' => env('PHONEPE_UAT_CLIENT_ID', ''),
'client_version' => env('PHONEPE_UAT_CLIENT_VERSION', ''),
'client_secret' => env('PHONEPE_UAT_CLIENT_SECRET', ''),
'base_url' => 'https://api-preprod.phonepe.com/apis/pg-sandbox',
'auth_url' => 'https://api-preprod.phonepe.com/apis/pg-sandbox',
'token_cache_path' => storage_path('app/phonepe/phonepe_token_uat.json'),
],
'prod' => [
// ...
],
];
use Yogeshgupta\PhonepeLaravel\Facades\PhonePe;
$result = PhonePe::initiatePayment($amount, $subscription_id, $payload);
if ($result['success']) {
return redirect($result['redirectUrl']);
} else {
\Log::error('Payment initiation failed: ' . $result['error']);
return back()->withErrors(['error' => $result['error']]);
}
use Yogeshgupta\PhonepeLaravel\Facades\PhonePe;
$result = PhonePe::verifyPhonePePayment('merchantOrderId123');
if ($result['success']) {
\Log::info('Payment status: ' . json_encode($result['data']));
return response()->json($result['data']);
} else {
\Log::error('Payment verification failed: ' . $result['error']);
return response()->json(['error' => $result['error']], 400);
}
namespace App\Http\Controllers;
use Yogeshgupta\PhonepeLaravel\Facades\PhonePe;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
class PaymentController extends Controller
{
/**
* Initiate a PhonePe payment.
*
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function initiate(Request $request)
{
// Validate request data
$validator = Validator::make($request->all(), [
'amount' => '_12345'; // Example order ID
// $amount = 10000; // Example amount in paisa (100 INR)
// Prepare payload
$payload = [
'merchantOrderId' => uniqid(),
'amount' => $amount,
'expireAfter' => 1200,
'metaInfo' => [
'udf1' => 'subscription_payment',
'udf2' => 'order_id_' . $orderId,
'udf3' => 'student_checkout',
'udf4' => '',
'udf5' => '',
],
];
try {
$result = PhonePe::initiatePayment($amount, $orderId, $payload);
if ($result['success']) {
if ($request->ajax()) {
return response()->json(data: $result);
}
return redirect($result['redirectUrl']);
}
Log::error('Payment initiation failed', [
'order_id' => $orderId,
'error' => $result['error'],
]);
return back()->withErrors(['error' => 'Payment initiation failed: ' . $result['error']]);
} catch (\Exception $e) {
Log::error('Unexpected error during payment initiation', [
'order_id' => $orderId,
'error' => $e->getMessage(),
]);
if ($request->ajax()) {
return response()->json([
'success' => false,
'message' => 'Unexpected error: ' . $e->getMessage(),
], 500);
}
return back()->withErrors(['error' => 'An unexpected error occurred. Please try again.']);
}
}
/**
* Verify a PhonePe payment status.
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function verify(Request $request)
{
// Validate request data
$validator = Validator::make($request->all(), [
'merchantOrderId' => '
bash
php artisan vendor:publish --tag=phonepe-config