我正在使用webtask.io来创建基于Thomasz`示例here的条带费用,但是在他们从节点4升级到节点8后,这不再起作用了。知道如何再次使用这个工作吗?
var stripe = require('stripe');
module.exports = function (ctx, req, res) {
stripe(ctx.secrets.stripeSecretKey).charges.create({
amount: ctx.body.amount * 100,
currency: 'gbp',
source: ctx.body.stripeToken,
description: 'Test Payment',
}, function (error, charge) {
var status = error ? 400 : 200;
var message = error ? error.message : '<script>window.location.replace("' + ctx.body.redirectUrl + '");</script>';
res.writeHead(status, { 'Content-Type': 'text/html' });
return res.end('<h1>' + message + '</h1>');
});
};
错误:
{
"code": 500,
"error": "Script generated an unhandled synchronous exception.",
"details": "TypeError: Cannot read property 'amount' of undefined",
"name": "TypeError",
"message": "Cannot read property 'amount' of undefined",
"stack": "TypeError: Cannot read property 'amount' of undefined\n at module.exports (/data/io/ea441d92-6f8c-46b0-a9a9-6c8098f03aac/webtask.js:5:26)\n at Async.waterfall (/data/sandbox/lib/sandbox.js:519:33)\n at nextTask (/data/sandbox/node_modules/async/dist/async.js:5324:14)\n at next (/data/sandbox/node_modules/async/dist/async.js:5331:9)\n at /data/sandbox/node_modules/async/dist/async.js:969:16\n at Async.waterfall (/data/sandbox/lib/sandbox.js:408:24)\n at nextTask (/data/sandbox/node_modules/async/dist/async.js:5324:14)\n at next (/data/sandbox/node_modules/async/dist/async.js:5331:9)\n at /data/sandbox/node_modules/async/dist/async.js:969:16\n at _combinedTickCallback (internal/process/next_tick.js:138:11)"
}
答案 0 :(得分:0)
问题是Stripe Checkout发送到webtask的请求正文中没有amount
属性。这是设计使然 - data-amount
属性仅影响显示给用户的金额。原因是您不应使用前端的值来确定费用金额。打开我的浏览器devtools并更改金额对我来说是微不足道的,而现在不是为你的产品支付9.99美元,而是支付1美元:)
要解决此问题,您有两种选择:
amount
参数stripe.charges.create
直接设置为值。如果您尝试向客户收取特定金额,这是推荐的方法。隐藏输入:
<form action="your-server-side-code" method="POST">
<input type="hidden" name="amount" id="amount" value="999">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="API_KEY"
data-amount="999"
data-name="Test"
data-description="Widget"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto"
data-currency="usd">
</script>
</form>
答案 1 :(得分:0)
<强>解决!强>
如果您正在使用Full HTTP control
(context,req,res),除非将webtask令牌的pb
声明设置为1,否则请求的正文将无法使用。
为此,您需要使用以下命令创建脚本:
wt create my-script.js --secret stripeSecretKey={stripe_api_key} --parse-body --dependency stripe@3.3.4 --ignore-package-json