如何在我的支付网关结账流程中结账时分配商店的订单价值(金额)?

How to assign store's order value(amount) while checking out to the amount in my payment gateway checkout process?

我正在尝试将 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);
});

options.handler 是一个函数,当您 运行 JSON.stringify(options).

时它会丢失

为了保存它,我们需要将函数逻辑转换成字符串。这个字符串是一个 IIFE,returns 你原来的 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 键变形为字符串。