【发布时间】:2021-06-15 04:02:15
【问题描述】:
所以在过去的 2 天里,我一直为如何实现这个 Stripe API 感到困惑,到目前为止,它是最难解决的问题。所以我决定使用 Firebase 和 Cloud Functions 来集成 Stripe 功能,我发现它是无服务器的,这很棒。
我一直在尝试关注 this article on iOS Stripe API integration 和 this article showing how to create the cloud functions involving Stripe,到目前为止,我已经能够在创建新用户时创建一个 Stripe 客户。在那之后,我几乎不知道如何做我接下来想做的事情,即创建临时密钥。
我有这个功能是从另一个 SO 帖子中获得的:
exports.createEphemeralKeys = functions.https.onRequest((req, res) => {
var api_version = req.body.api_version;
var customerId = req.body.customerId;
if (!api_version) {
res.status(400).end();
return;
}
stripe.ephemeralKeys.create(
{ customer: customerId },
{ stripe_version: api_version },
function(err, key) {
return res.send(key);
});
});
这是MyAPIClient.swift文件中的方法:
func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
let url = self.baseURL.appendingPathComponent("ephemeral_keys")
var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)!
urlComponents.queryItems = [URLQueryItem(name: "api_version", value: apiVersion)]
var request = URLRequest(url: urlComponents.url!)
request.httpMethod = "POST"
let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
guard let response = response as? HTTPURLResponse,
response.statusCode == 200,
let data = data,
let json = ((try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]) as [String : Any]??) else {
completion(nil, error)
return
}
completion(json, nil)
})
task.resume()
}
现在这就是我感到困惑的地方,因为将 Stripe 与 Firebase 集成是无服务器的,我们应该在 baseURL 中输入什么?目前baseURL 变量为空,但我也收到.appendingPathComponent 不可用的错误。如果这是一个愚蠢的问题,请原谅我,但我宁愿看起来像一个完全的白痴,并最终弄清楚如何成功集成这个 API,而不是什么都不问。提前致谢。
【问题讨论】:
标签: swift firebase google-cloud-firestore google-cloud-functions stripe-payments