如何完成完成区块 - Swift / 条纹付款

How To Finish Completion Block - Swift / Stripe Payments

这是我的方块:

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

代码始终使用 STPBackendChargeResult.failure 调用完成回调。所以它需要是一个 if 语句,我需要根据 Alamofire 请求的结果使用 failuresuccess 调用完成回调以将令牌发送到后端服务器。

为什么不能直接放在开关里呢?

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)
}