【问题标题】:Completion handler not being called - Alamofire / stripe未调用完成处理程序 - Alamofire / stripe
【发布时间】:2019-12-07 16:05:32
【问题描述】:

我的完成处理程序永远不会被调用。

这个完成处理程序是用stripe编写的: (步骤 1 结束) https://stripe.com/docs/mobile/ios/standard#ephemeral-key

我尝试尽可能地简化函数(我之前让它变得更复杂),并在每个 .case 的末尾放置一个 completion() 。

当我点击 STPJSONResponseCompletionBlock 时,它会告诉我所需的参数是这样的:

json响应
JSON 响应,如果发生错误,则返回 nil。 错误
发生的错误(如果有)。

如果我调用 completion(nil, nil) 我可能会崩溃我的应用程序,它会给我这个错误: 2019-07-30 10:38:42.917984+0100 foodfactory[27595:2238502] *** -[STPEphemeralKeyManager _createKey] 中的断言失败,

2019-07-30 10:38:42.929757+0100 foodfactory[27595:2238502] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法按照协议 STPCustomerEphemeralKeyProvider 解析临时密钥响应。确保您的后端将临时密钥的未修改 JSON 发送到您的应用程序。欲了解更多信息,请参阅https://stripe.com/docs/mobile/ios/standard#prepare-your-api'

但是,如果我像下面的例子一样传入 json,它就永远不会调用..

此外,我已经尽可能接近地复制了将 json 发送到此完成处理程序的 NodeJS 代码(我在谷歌云功能上,所以必须进行一些更改,我检查了我的 JSON 即时接收和临时密钥在那里,如果我编写自己的方法来解码 json,我可以访问它)。

另外,因为我可以让它崩溃,我猜必须调用完成处理程序?

此代码使用完成处理程序调用函数:

    func changePaymentOptionsButtonTapped() {
        // Setup customer context
        let apiVersion = String(Stripe.version())
        MyAPIClient.sharedClient.createCustomerKey(withAPIVersion: apiVersion) { (ephemeralKeyJson, err) in
            print("ThisIsNeverCalled")
            if let ephemeralKey = ephemeralKeyJson?["id"] as? String {
            } else {
                print("Error parsing JSON from customer creation: See: MyAPIClient - func createCustomerKey")
            }
        }
    }

带有完成处理程序的函数:

    func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
        guard let customerId = KeychainWrapper.standard.string(forKey: Stripe_Customer_Id) else {
            print("Keychain wrapper not retrieving stripe customer Id at MyAPIClient")
//            completion(nil, nil)
            return
        }
        let url = "https://us-central1-foodfactory-813ab.cloudfunctions.net/request_ephemeral_key"
        Alamofire.request(url, method: .post, parameters: [
            "api_version": apiVersion,
            "customerId": customerId,
            ])
            .validate(statusCode: 200..<300)
            .responseJSON { responseJSON in
                switch responseJSON.result {
                case .success(let json):
                    completion(json as? [String: AnyObject], nil)
                case .failure(let error):
                    print("Error creating customer Key (retrieving ephemeral key) with Alamofire. See: MyAPIClient - func createCustomerKey")
                    completion(nil, error)
                }
        }
    }

有人有什么建议吗?我的猜测是这与我传递给完成处理程序的内容有关。

【问题讨论】:

  • 您最终成功地将其集成到您的应用程序中了吗?我的生活从未像现在这样陷入困境。
  • 老实说我不记得了,抱歉。我想我做到了,也许我用错了电话?我不想带你走错路。如果您不知道 Stripe 有一个 IRC 频道,您可以在其中向他们提问。如果不是为了这个问题,我去那里是为了一个非常相似的问题。我认为它最终变得(相当)简单?但话又说回来,我通常回去回答我自己的问题,所以也许我没有解决它。对不起,我能记住的就这些了。但如果它有帮助的话,我记得我对此有类似的感觉......还有很多很多次哈!
  • 别担心,我想出了第一步。提到 IRC 频道的效果很好,我肯定会经常检查。保重,感谢您的回复。
  • 没问题,很高兴我能帮上点忙!

标签: swift stripe-payments alamofire completionhandler


【解决方案1】:

这里肯定有错误:

json as? [String: AnyObject]

改成

json as? [String: Any]

原因是 JSON 字典可以包含不是 AnyObject 的东西 - 例如,StringstructIntBool 也不是对象

【讨论】:

  • 嗯,仍然没有被调用,还有其他建议吗?
  • 检查它是否被调用? - 我使用了 print 语句,它被调用了
猜你喜欢
  • 2015-12-17
  • 2021-03-18
  • 1970-01-01
  • 2014-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多