Anonymous
Создание платежного шлюза Phonepe с использованием Phonepe checkout.js, но не удается инициировать платежи
Сообщение
Anonymous » 05 ноя 2024, 11:01
Я создаю интеграцию с телефоном, но не удалось инициировать платеж. Я получаю сообщения типа «Не удалось инициировать платеж».
Мой код:
Код: Выделить всё
document.getElementById("payWithPhonePe").addEventListener("click", function() {
alert("hi");
const amount = 15;
const orderId = "unique-order-id-" + new Date().getTime();
fetch("", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
amount: amount,
orderId: orderId
})
}).then(response => response.json()).then(data => {
if (data.success) {
PhonePe.init({
orderId: data.data.orderId,
merchantId: data.data.merchantId,
redirectUrl: data.data.redirectUrl,
success: (res) => {
alert("Payment was successful!");
console.log(res);
},
failure: (res) => {
alert("Payment failed.");
console.log(res);
},
close: () => {
alert("Payment window closed.");
}
});
} else {
alert("Payment initiation failed!");
}
}).catch(error => {
// console.error("Error:", error);
});
});
Бэкенд-код: Controller.php
метод:
Код: Выделить всё
function createPhonePePayment($amount, $orderId) {
$merchantId = "";
$merchantKey = "";
$url = "https://api.phonepe.com/apis/hermes/pg/v1/pay";
// Payment request payload
$payload = [
"merchantId" => $merchantId,
"transactionId" => $orderId,
"amount" => $amount * 100,
"currency" => "INR",
"redirectUrl" => site_url('beta/change_package_1')
];
$jsonPayload = json_encode($payload);
$checksum = hash_hmac('sha256', $jsonPayload, $merchantKey);
$headers = [
'Content-Type: application/json',
'X-VERIFY: ' . $checksum . "###" . $merchantId,
];
// Initialize cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$response = curl_exec($ch);
curl_close($ch);
// Decode the response
return json_decode($response, true);
}
public function create_phonePe(){
header('Content-Type: application/json');
$input = json_decode(file_get_contents("php://input"), true);
if (!isset($input['amount']) || !isset($input['orderId'])) {
echo json_encode(["success" => false, "error" => "Invalid input."]);
return;
}
$amount = $input['amount'];
$orderId = $input['orderId'];
$responseData = $this->createPhonePePayment($amount, $orderId);
if ($responseData && isset($responseData['data']['instrumentResponse']['redirectUrl'])) {
echo json_encode([
"success" => true,
"data" => [
"orderId" => $orderId,
"merchantId" => $merchantId,
"redirectUrl" => $responseData['data']['instrumentResponse']['redirectUrl']
]
]);
} else {
echo json_encode([
"success" => false,
"error" => isset($responseData['error']) ? $responseData['error'] : "Failed to initiate payment."
]);
}
}
Пожалуйста, помогите мне решить проблему. Я использую правильные учетные данные, такие как идентификатор продавца и ключ API. Я хочу решить проблему сбоя инициации платежей при интеграции Phonepe.
Подробнее здесь:
https://stackoverflow.com/questions/791 ... ailed-to-i
1730793695
Anonymous
Я создаю интеграцию с телефоном, но не удалось инициировать платеж. Я получаю сообщения типа «Не удалось инициировать платеж». Мой код: [code]document.getElementById("payWithPhonePe").addEventListener("click", function() { alert("hi"); const amount = 15; const orderId = "unique-order-id-" + new Date().getTime(); fetch("", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ amount: amount, orderId: orderId }) }).then(response => response.json()).then(data => { if (data.success) { PhonePe.init({ orderId: data.data.orderId, merchantId: data.data.merchantId, redirectUrl: data.data.redirectUrl, success: (res) => { alert("Payment was successful!"); console.log(res); }, failure: (res) => { alert("Payment failed."); console.log(res); }, close: () => { alert("Payment window closed."); } }); } else { alert("Payment initiation failed!"); } }).catch(error => { // console.error("Error:", error); }); }); [/code] [code] Pay with PhonePe [/code] Бэкенд-код: Controller.php метод: [code]function createPhonePePayment($amount, $orderId) { $merchantId = ""; $merchantKey = ""; $url = "https://api.phonepe.com/apis/hermes/pg/v1/pay"; // Payment request payload $payload = [ "merchantId" => $merchantId, "transactionId" => $orderId, "amount" => $amount * 100, "currency" => "INR", "redirectUrl" => site_url('beta/change_package_1') ]; $jsonPayload = json_encode($payload); $checksum = hash_hmac('sha256', $jsonPayload, $merchantKey); $headers = [ 'Content-Type: application/json', 'X-VERIFY: ' . $checksum . "###" . $merchantId, ]; // Initialize cURL $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute the request $response = curl_exec($ch); curl_close($ch); // Decode the response return json_decode($response, true); } public function create_phonePe(){ header('Content-Type: application/json'); $input = json_decode(file_get_contents("php://input"), true); if (!isset($input['amount']) || !isset($input['orderId'])) { echo json_encode(["success" => false, "error" => "Invalid input."]); return; } $amount = $input['amount']; $orderId = $input['orderId']; $responseData = $this->createPhonePePayment($amount, $orderId); if ($responseData && isset($responseData['data']['instrumentResponse']['redirectUrl'])) { echo json_encode([ "success" => true, "data" => [ "orderId" => $orderId, "merchantId" => $merchantId, "redirectUrl" => $responseData['data']['instrumentResponse']['redirectUrl'] ] ]); } else { echo json_encode([ "success" => false, "error" => isset($responseData['error']) ? $responseData['error'] : "Failed to initiate payment." ]); } } [/code] Пожалуйста, помогите мне решить проблему. Я использую правильные учетные данные, такие как идентификатор продавца и ключ API. Я хочу решить проблему сбоя инициации платежей при интеграции Phonepe. Подробнее здесь: [url]https://stackoverflow.com/questions/79158035/creating-phonepe-payment-gateway-using-phonepe-checkout-js-but-it-is-failed-to-i[/url]