2016-11-24 12 views
0

node.jsを使用してWebtaskサーバーからStripeへの請求を作成しようとしています。node.js/Swift/Stripe/Webtaskの作成にエラーが発生しました

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の

'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で取得エラーですを使用してWebtaskを経由してバックエンドのコードです:ここでスウィフト(iOS版)にトークンを送信する機能があります:

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"; 
    } 

答えて

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 
関連する問題