gpt4 book ai didi

javascript - node.js/Swift/Stripe/Webtask 创建费用时出错

转载 作者:行者123 更新时间:2023-11-30 12:52:36 25 4
gpt4 key购买 nike

我正在尝试使用 node.js 从 Webtask 服务器创建对 Stripe 的收费。以下是在 Swift (iOS) 中发送 token 的函数:

    func createBackendChargeWithToken(_ token: STPToken, completion: @escaping STPTokenSubmissionHandler) {
if backendChargeURLString != "" {
if let url = URL(string: backendChargeURLString + "/payment") {
let chargeParams : [String: AnyObject] = ["stripeToken": token.tokenId as AnyObject, "amount": shirtPrice as AnyObject]

Alamofire.request(url, method: .post, parameters: chargeParams, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

switch(response.result) {
case .success(_):
if response.result.value != nil{
print(response.result.value!)
}
break

case .failure(_):
print(response.result.error!)
break

}
}
}
}
completion(STPBackendChargeResult.failure, NSError(domain: StripeDomain, code: 50, userInfo: [NSLocalizedDescriptionKey: "You created a token! Its value is \(token.tokenId). Now configure your backend to accept this token and complete a charge."]))
}

这是使用node.js通过Webtask在后端的代码

    'use latest';

import express from 'express';
import { fromExpress } from 'webtask-tools';
import bodyParser from 'body-parser';
import stripe from 'stripe';

var app = express();
app.use(bodyParser.json());

app.post('/payment', (req,res) => {
var ctx = req.webtaskContext;
var STRIPE_SECRET_KEY = ctx.secrets.STRIPE_SECRET_KEY;

stripe(STRIPE_SECRET_KEY).charges.create({
amount: ctx.data.amount,
currency: ctx.data.currency,
source: ctx.body.stripeToken,
description: ctx.data.description
}, (err, charge) => {
const status = err ? 400 : 200;
const message = err ? err.message : 'Payment done!';
res.writeHead(status, { 'Content-Type': 'text/html' });
return res.end('<h1>' + message + '</h1>');
});
});

这是我在 Xcode 中遇到的错误:

    You created a token! Its value is Optional("tok_19JEqjJGxqjWew1nNeyvAHto").
Now configure your backend to accept this token and complete a charge.
{
code = 400;
error = "Supplied code must return or export a function.";
message = "Invalid webtask code";
}

最佳答案

使用ctx.body.amount而不是ctx.data.amount:

// .. other code

stripe(STRIPE_SECRET_KEY).charges.create({
amount: ctx.data.amount, // <- you should be using ctx.body.amount
currency: ctx.data.currency,
source: ctx.body.stripeToken,
description: ctx.data.description
}, (err, charge) => {

// .. other code

关于javascript - node.js/Swift/Stripe/Webtask 创建费用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40778079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com