【发布时间】: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