使用phonepe checkout.js创建phonepe支付网关但发起支付失败

问题描述 投票:0回答:1

我正在创建phonepe集成,但未能启动付款,我收到消息。

我的前台代码是:

document.getElementById("payWithPhonePe").addEventListener("click", function() {
  alert("hi");
  const amount = 15;
  const orderId = "unique-order-id-" + new Date().getTime();
  fetch("<?php echo site_url("beta/change_package_1/create_phonePe"); ?>", {
      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);      
  });
});
<script src="https://mercury.phonepe.com/web/bundle/checkout.js"></script>

<button id="payWithPhonePe">Pay with PhonePe</button>

后端代码为: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."
            ]);
        }
    }

请帮我解决问题。我正在使用正确的凭据,例如商家 ID 和 API 密钥。我想解决phonepe集成支付发起失败的问题。

javascript php codeigniter phonepe
1个回答
0
投票

能否给出一些错误提示,如果没有错误提示可能需要在php.ini中开启错误,然后在PHP代码中使用try-catch等一些代码块来捕获具体的错误

© www.soinside.com 2019 - 2024. All rights reserved.