如何完成完成块 - Swift / Stripe付款

时间:2016-11-24 17:43:28

标签: swift stripe-payments completionhandler

这是我拥有的街区:

UIViewControllers

但这需要在其中,但我不知道如何正确地写它:

func createBackendChargeWithToken(_ token: STPToken, completion: @escaping STPTokenSubmissionHandler) {
    if backendChargeURLString != "" {
        if let url = URL(string: "https://wt-lightfalldev-gmail-com-0.run.webtask.io/stripe-payment/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: heads).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."]))
}

代码始终使用if (error) { completion(STPBackendChargeResultFailure, error); } else { completion(STPBackendChargeResultSuccess, nil); } 调用完成回调。所以它需要是一个if语句,我需要使用STPBackendChargeResult.failurefailure调用完成回调,具体取决于Alamofire请求将令牌发送到后端服务器的结果。

1 个答案:

答案 0 :(得分:1)

为什么不能把它放在开关中?

switch(response.result) {
case .success(_):
    if response.result.value != nil{
        print(response.result.value!)            
    }
    completion(STPBackendChargeResultSuccess, nil)
case .failure(_):
    print(response.result.error!)
    completion(STPBackendChargeResultFailure, error)
}
相关问题