【问题标题】:fetch customerId for myApiClient为 myApiClient 获取 customerId
【发布时间】:2021-02-15 16:47:01
【问题描述】:

我正在尝试使用 swift firebase 和 node.js 获取 myAPIClient 的 customerId。当我运行我的应用程序时,它崩溃并告诉我 customerId 为 nil。我需要这个 customerId 的原因是为了创建 ephemeralKey。

我在 node.js 中使用的代码是,

exports.createEphemeralKey = functions.https.onRequest((req, res) => {
const stripe_version = req.body.api_version;
const customerId = req.body.customerId

if (!stripe_version) {
console.log('I did not see any api version')
res.status(400).end()
return;
}

stripe.ephemeralKeys.create(
{customer: customerId},
{stripe_version: apiVersion}
).then((key) => {
 console.log("Ephemeral key: " + key)
 res.status(200).json(key)
}).catch((err) => {
console.log('stripe version is ' + stripe_version + " and customer id is " + customerId + " for key: " + stripe_key + " and err is " + err.message )
res.status(500).json(err)
});
});

我在 myApiClient 中使用的代码是。

    func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
  let url = self.baseURL.appendingPathComponent("ephemeral_keys")
    let defaults = UserDefaults.standard
    let customerId = defaults.string(forKey: "customerId")
    AF.request(url, method: .post, parameters: [
        "api_version": apiVersion, "customer_id": 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):
                completion(nil, error)
            }
    }
}

我一直坚持创建这个 ephemeralKey 有一段时间了,还问了其他问题,但没有得到真正的答案。有什么我想念的吗?我如何才能从我的 ios 项目内部的条带中实际访问 customersId。

【问题讨论】:

  • iOS 代码中的responseJSON 是什么样的?从 Stripe 返回的 ephemeralKey 可能不包含您期望的 customer_id。要创建 EphemeralKey,您需要 first 获取客户 ID 并作为请求参数发送到后端。您打算如何获取客户 ID?

标签: node.js swift google-cloud-functions stripe-payments


【解决方案1】:

您应该首先检查 let customerId = defaults.string(forKey: "customerId") 是否提供了有效的 customerId。您可以通过调试您的应用程序并检查customerId 的值来做到这一点。假设它是有效的,我发现有几件事可能会给您带来问题:

您使用的 Alamofire 库有 2 个具有不同属性和选项的参数编码器。你可以看到他们here。 我的假设是您也没有将customerId correclty 传递给请求的正文。我建议您先尝试JSONParameterEncoder。您的请求将是这样的:

AF.request(url, method: .post, parameters: [
  "api_version": apiVersion, "customer_id": customerId!
  ],
  encoder: JSONParameterEncoder.default)

您可能还需要根据您从 swift 应用程序传递信息的方式来调整从 Cloud Function 检索 customerID 的方式。这个docs 可能会提供一些关于如何解析 HTTP 请求信息的见解

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多