0

我正在尝试将 Razorpay 支付网关集成到我的基于 ecwid 的在线商店。根据 ecwid 文档中概述的流程,我需要解密我的商店提供的订单详细信息,将详细信息发送到我的支付网关进行处理,最后将客户重定向回商店。

基于 Razorpay 的文档,我需要将基于表单的脚本代码放置在按钮标记中。单击按钮时,将发送结帐表格,客户开始进行付款。

<button id="rzp-button1">Pay</button>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var options = {
  "key": "YOUR_KEY_ID",
  "amount": "2000", // 2000 paise = INR 20
  "name": "Merchant Name",
  "description": "Purchase Description",
  "image": "/your_logo.png",
  "handler": function (response){
      alert(response.razorpay_payment_id);
  },
  "prefill": {
      "name": "Harshil Mathur",
      "email": "harshil@razorpay.com"
  },
  "notes": {
    "address": "Hello World"
  },
  "theme": {
    "color": "#F37254"
  }
};
var rzp1 = new Razorpay(options);

document.getElementById('rzp-button1').onclick = function(e){
  rzp1.open();
  e.preventDefault();
}
</script>

现在这是我的问题。我已经成功地从 Ecwid 的商店中解密了有效负载,以获取包含金额的订单详细信息。如何将按钮中的金额与从商店获得的金额分配?

到目前为止,这是我的代码。它似乎很迂回,它仍然不起作用。任何帮助将不胜感激。

purchaseRouter.route('/')
.post((req, res, next) => {
  var algorithm = encryptionHelper.CIPHERS.AES_128_CBC;
  var originalBase64 = req.body.data.replace(/-/g, "+").replace(/_/g, "/");
  const key = process.env.ECWID_CLIENT_SECRET.slice(0, 16);
  var decText = encryptionHelper.decryptText(algorithm, key, originalBase64, 
   "base64");
  var jsonText = JSON.parse(decText);
  var amount = jsonText.cart.order.total;
  //var name = jsonText.billingPerson.name;
  var email = jsonText.cart.order.email;
  amount = amount*100;
  var options = {
   "key": process.env.RAZORPAY_KEY_ID,
   "amount": amount,
   "name": "Fluvium",
   "description": "Board & Combat Sportswear",
   "image": "/your_logo.png",
   "handler": function (response){
     console.log(response.razorpay_payment_id);
     if (typeof response.razorpay_payment_id == 'undefined' ||  
        response.razorpay_payment_id < 1) {
      console.log("Error" + response.razorpay_payment_id);;
     } else {
       document.getElementById('razorpay_payment_id').value =  
          response.razorpay_payment_id;
       document.getElementById('razorpay_signature').value = 
        response.razorpay_signature;
       document.razorpayform.submit();
     }
     location.href = redirect_url;
    },
    "prefill": {
      "name": "",
      "email": email
    },
    "notes": {
       "address": "Hello World"
    },
    "theme": {
       "color": "#F37254"
    }
   };
    //res.send(JSON.stringify(options));
  var html='';
  html += "<form name='razorpayform' action='/purchase/razorpay' 
      method='POST'>";
  html += "<input type='hidden name='razorpay_payment_id' 
      id='razorpay_payment_id'>"
  html += "<input type='hidden' name='razorpay_signature'  
      id='razorpay_signature' >"
  html += "</form>"
  html += "<script src='https://checkout.razorpay.com/v1/checkout.js'> 
      </script>";
  html += "<script>";
  html += "var options = " + JSON.stringify(options);
  html += "; var rzp1 = new Razorpay(options);";
  html += "rzp1.open();";
  html += "</script>";
 res.send(html);
});
4

1 回答 1

0

options.handler是一个函数,运行时会丢失JSON.stringify(options)

为了保存它,我们需要将函数逻辑转换为字符串。此字符串是一个IIFE,它返回您的原始字符串handler

所以,你的代码现在是

purchaseRouter.route('/')
.post((req, res, next) => {
  var algorithm = encryptionHelper.CIPHERS.AES_128_CBC;
  var originalBase64 = req.body.data.replace(/-/g, "+").replace(/_/g, "/");
  const key = process.env.ECWID_CLIENT_SECRET.slice(0, 16);
  var decText = encryptionHelper.decryptText(algorithm, key, originalBase64, 
   "base64");
  var jsonText = JSON.parse(decText);
  var amount = jsonText.cart.order.total;
  //var name = jsonText.billingPerson.name;
  var email = jsonText.cart.order.email;
  amount = amount*100;
  var options = {
   "key": process.env.RAZORPAY_KEY_ID,
   "amount": amount,
   "name": "Fluvium",
   "description": "Board & Combat Sportswear",
   "image": "/your_logo.png",
    "handler": "(function getHandler (response) { return function (response) { console.log(response.razorpay_payment_id); if (typeof response.razorpay_payment_id == 'undefined' || response.razorpay_payment_id < 1) { console.log('Error' + response.razorpay_payment_id); } else { document.getElementById('razorpay_payment_id').value = response.razorpay_payment_id; document.getElementById('razorpay_signature').value = response.razorpay_signature; document.razorpayform.submit(); } location.href = redirect_url; } })()",
    "prefill": {
      "name": "",
      "email": email
    },
    "notes": {
       "address": "Hello World"
    },
    "theme": {
       "color": "#F37254"
    }
   };
    //res.send(JSON.stringify(options));
  var html='';
  html += "<form name='razorpayform' action='/purchase/razorpay' 
      method='POST'>";
  html += "<input type='hidden name='razorpay_payment_id' 
      id='razorpay_payment_id'>"
  html += "<input type='hidden' name='razorpay_signature'  
      id='razorpay_signature' >"
  html += "</form>"
  html += "<script src='https://checkout.razorpay.com/v1/checkout.js'> 
      </script>";
  html += "<script>";
  html += "var options = " + JSON.stringify(options);
  html += "; options.handler = eval(options.handler);";
  html += "var rzp1 = new Razorpay(options);";
  html += "rzp1.open();";
  html += "</script>";
 res.send(html);
});

我添加html += "; options.handler = eval(options.handler);";并变形handler键成为一个字符串。

于 2018-10-03T06:34:55.833 回答