Node.js / swift / stripe / webtask创建充电时错误



我正在尝试使用node.js创建一个从WebTask服务器上的牵引力。这是在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."]))
        }

这是使用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

最新更新