[英]node.js / Swift / Stripe / Webtask error in creating charge
I'm trying to create a charge to Stripe from the Webtask server using node.js. 我正在尝试使用node.js从Webtask服务器向Stripe收费。 Here is the function to send the token in Swift (iOS):
这是在Swift(iOS)中发送令牌的函数:
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."]))
}
And this is the code in the backend via Webtask using node.js 这是使用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>');
});
});
This is the error I get in Xcode: 这是我在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";
}
Use ctx.body.amount
instead of ctx.data.amount
: 使用
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.