<?php ob_start();
if (isset($_POST['pay_by_phone_pe'])) {
//production
$phone_pay_merchant_id = 'id';
$phone_pay_secret_key = 'eedd02bf-d18a-462d-a3bd-d93d5b3116aEYE';
//testing
//$phone_pay_merchant_id = 'PGTESTPAYUAT';
//$phone_pay_secret_key = '099eb0cd-02cf-4e2a-8aca-3e6c6aff0399';
$amount = $_POST['amount'];
$currency = 'INR';
$transaction_id = uniqid();
$merchantKey = 'eedd02bf-d18a-462d-a3bd-d93d5b3116aEYE';
$data = array(
"merchantId" => "ID",
"merchantTransactionId" => $transaction_id,
"merchantUserId" => "1",
"amount" => $amount*100,
"redirectUrl" => $_POST['return'],
"redirectMode" => "POST",
"callbackUrl" => $_POST['return'],
"mobileNumber" => "9825454588",
"paymentInstrument" => array(
"type" => "PAY_PAGE"
)
);
// Convert the Payload to JSON and encode as Base64
$payloadMain = base64_encode(json_encode($data));
$payload = $payloadMain."/pg/v1/pay".$merchantKey;
$Checksum = hash('sha256', $payload);
$Checksum = $Checksum.'###1';
// X-VERIFY - SHA256(base64 encoded payload + "/pg/v1/pay" + salt key) + ### + salt index
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.phonepe.com/apis/hermes/pg/v1/pay",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'request' => $payloadMain
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"X-VERIFY: ".$Checksum,
"accept: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
// echo "cURL Error #:" . $err;
header('Location: paymentfailed.php?cURLError='.$err);
} else {
echo $response;
$responseData = json_decode($response, true);
$url = $responseData['data']['instrumentResponse']['redirectInfo']['url'];
header('Location: '.$url);
}
}
?>
No comments:
Post a Comment