我正在使用webtask.io创建一个基于Thomasz`示例here的条带电荷,但是在他们从Node4升级到Node8之后,这不再起作用。你知道怎么让它再次工作吗?
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)"
}发布于 2018-06-05 21:26:10
问题是,在请求正文中没有amount属性,该属性将发送给The任务。这是设计出来的- data-amount属性只影响显示给用户的数量。这样做的原因是,您不应该使用来自前端的值来决定费用的数量。打开我的浏览器devtools并更改金额对我来说是微不足道的,现在我不再为您的产品支付9.99美元,而是支付1美元:)
要解决此问题,您有两个选择:
stripe.charges.create的amount参数直接设置为一个值。如果您尝试向客户收取特定金额,则建议使用此方法。隐藏输入:
<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>发布于 2018-06-06 01:29:01
已解决!
如果您使用的是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-jsonhttps://stackoverflow.com/questions/50700321
复制相似问题