创建费用时出现node.js / Swift / Stripe / Webtask错误

时间:2016-11-24 04:30:56

标签: javascript node.js swift stripe-payments

我尝试使用node.js从Webtask服务器向Stripe创建费用。这是在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."]))
        }

这是后端通过Webtask使用node.js

的代码
    '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";
    }

1 个答案:

答案 0 :(得分:0)

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