0

我正在尝试为 razorpay 添加托管结帐,但我遇到了一个问题,即当我尝试通过 HTML 表单发送请求时它可以工作,但是当我通过 javascript 创建相同的表单时它不起作用。它返回错误代码 400 不支持当前的 HTTP 方法 Postman 请求也会发生同样的情况。我正在使用 laravel 5.8

HTML表单(工作):

          <form method="POST" action="https://api.razorpay.com/v1/checkout/embedded">  
            <input type="text" name="key_id" value="rzp_test_UtGYTNBzepZoVA">  
            <input type="text" name="order_id" value="razorpay_order_id">  
            <input type="text" name="name" value="Acme Corp">  
            <input type="text" name="description" value="A Wild Sheep Chase">  
            <input type="text" name="prefill[name]" value="Gaurav Kumar"> 
            <input type="text" name="prefill[contact]" value="9123456780">  
            <input type="text" name="prefill[email]" value="gaurav.kumar@example.com">  
            <input type="text" name="notes[shipping address]" value="L-16, The Business Centre, 61 Wellfield Road, New Delhi - 110001">  
            <input type="hidden" name="callback_url" value="https://example.com/payment-callback">
            <input type="hidden" name="cancel_url" value="https://example.com/payment-cancel">  <button>Submit</button>
        </form>

Javascript(不工作):

    var form = document.createElement("form");
    var element1 = document.createElement("input"); 
    var element2 = document.createElement("input");  
    var element3 = document.createElement("input");  
    var element4 = document.createElement("input"); 
    var element5 = document.createElement("input"); 
    var element6 = document.createElement("input"); 
    var element7 = document.createElement("input"); 
    var element8 = document.createElement("input"); 
    var element9 = document.createElement("input"); 
    var element10 = document.createElement("input"); 
    var element11= document.createElement("input"); 
    form.method = "POST";
    form.action = "https://api.razorpay.com/v1/checkout/embedded"; 
    element1.name="key_id";
    element1.setAttribute("type","text");
    element1.setAttribute("value",key_id);
    form.appendChild(element1);  

    element2.name="order_id";
    element2.setAttribute("type","text");
    element2.setAttribute("value",order_id);
    form.appendChild(element2);

    element3.name="name";
    element3.setAttribute("type","text");
    element3.setAttribute("value",name);
    form.appendChild(element3);

    element4.name="description";
    element4.setAttribute("type","text");
    element4.setAttribute("value",description);
    form.appendChild(element4);

    element5.name="prefill[name]";
    element5.setAttribute("type","text");
    element5.setAttribute("value",prefill["name"]);
    form.appendChild(element5);

    element6.name="prefill[contact]";
    element6.setAttribute("type","text");
    element6.setAttribute("value",prefill["contact"]);
    form.appendChild(element6);

    element7.name="prefill[email]";
    element7.setAttribute("type","text");
    element7.setAttribute("value",prefill["email"]);
    form.appendChild(element7);

    element8.name="notes[shipping address]";
    element8.setAttribute("type","text");
    element8.setAttribute("value",notes["shipping address"]);
    form.appendChild(element8);
    // form.appendChild(element8);

    element9.name="callback_url";
    element9.setAttribute("type","text");
    element9.setAttribute("value",callback_url);
    form.appendChild(element9);
    // form.appendChild(element9);

    element10.name="cancel_url";
    element10.setAttribute("type","text");
    element10.setAttribute("value",cancel_url);
    form.appendChild(element10);

    element11.value=document.querySelector('meta[name="csrf-token"]').getAttribute("content");
    element11.name="_method";
    element11.setAttribute("type","hidden");
    form.appendChild(element11);

    console.log(form);
    document.body.appendChild(form);
    console.log(form);
    form.submit();

返回错误

{
    "error": {
        "code": "BAD_REQUEST_ERROR",
        "description": "The current http method is not supported"
    }
} 
4

1 回答 1

-1

尝试使用下面的 JS。

您还可以在此处找到所有参数:https ://razorpay.com/docs/payment-gateway/web-integration/standard/#checkout-form

   "key":"<YOUR_KEY_ID>",
   // Enter the Key ID generated from the Dashboard 
    "amount":"29935",
   "name":"Acme Corp",
   "description":"A Wild Sheep Chase is the third novel by Japanese author Haruki Murakami",
 "image":"http://example.com/your_logo.png",
   "handler":function (response)   {  
      alert(response.razorpay_payment_id);
   },
   /** 
* You can track the modal lifecycle by * adding the below code in your options 
*/ 
    "modal":{  
      "ondismiss":function()      {  
         console.log(‘Checkout form closed’);
      }
   }
}; 
var rzp1 = new Razorpay(options);```


于 2019-09-10T11:31:03.270 回答