对如何在 APIClient.swift class 中创建 Stripe 临时密钥感到困惑
confused on how to create the Stripe ephemeral key in the APIClient.swift class
因此,在过去的 2 天里,我一直在为如何实现这个 Stripe API 而苦恼,这是迄今为止最难解决的问题。因此,我决定使用 Firebase 和 Cloud Functions 集成 Stripe 功能,我发现它是无服务器的,这很棒。
我一直在尝试关注 this article on iOS Stripe API integration and this article showing how to create the cloud functions involving Stripe,到目前为止,我已经能够在创建新用户时创建 Stripe 客户。在那之后,我几乎不知道如何做我接下来想做的事情,即创建临时密钥。
我有这个功能我从另一个 SO post:
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,而不是什么都不问。提前致谢。
baseURL
应设置为您的 Firebase 函数所在的 URL。查看 the Firebase documentation for invoking an HTTP function 了解详情。
URL 将是这样的:
https://<region>-<project-id>.cloudfunctions.net/
因此,在过去的 2 天里,我一直在为如何实现这个 Stripe API 而苦恼,这是迄今为止最难解决的问题。因此,我决定使用 Firebase 和 Cloud Functions 集成 Stripe 功能,我发现它是无服务器的,这很棒。
我一直在尝试关注 this article on iOS Stripe API integration and this article showing how to create the cloud functions involving Stripe,到目前为止,我已经能够在创建新用户时创建 Stripe 客户。在那之后,我几乎不知道如何做我接下来想做的事情,即创建临时密钥。
我有这个功能我从另一个 SO post:
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,而不是什么都不问。提前致谢。
baseURL
应设置为您的 Firebase 函数所在的 URL。查看 the Firebase documentation for invoking an HTTP function 了解详情。
URL 将是这样的:
https://<region>-<project-id>.cloudfunctions.net/