Stripe 文档告诉我在后端公开三个 API,但没有告诉我如何去做
Stripe Documentation tells me to expose three APIs on my back end but not how to do so
在查看将 Stripe 合并到我的 Swift iOS 应用程序中的文档时,我读到它告诉我:"you should expose three APIs on your backend for your iOS app to communicate with."
它指的三个API是检索客户API、创建卡API和客户更新API。然后文档列出了代码片段,说明您应该在后端执行哪些操作以调用这些 APIs,使用我不熟悉的 Spark 框架完成。
我的问题(Stripe 的文档似乎没有解释)是如何将我的后端暴露给这三个 API 以便我可以调用这些函数?是通过进口声明吗?或者这是 handled/unnecessary 因为我将使用不需要导入的 Spark 框架吗?
Link 到相关的 Stripe 文档:https://stripe.com/docs/mobile/ios
Spark 框架是为 Java 示例选择的框架;代码区域正上方是一些 link 到其他语言片段的选项卡(Ruby/Sinatra、Python/django、PHP、Node.js/Express),所以也许你的language/framework 是否存在 - 或者您使用的是不同的 Java 框架?
关于您的问题,您需要在服务器端应用程序上实现这三个 API 端点 - 然后每个端点都联系 Stripe API - 为了让您的 iOS 应用程序使用它们。
您可能还想查看 Stripe 的 Java 库:https://github.com/stripe/stripe-java/
我建议学习如何执行此操作,您应该在 Javascript / Node.JS 中执行此操作,并使用 Heroku 之类的工具来设置 Express Server。
在 iOS 方面,我会使用 Alamofire,这将使您可以轻松地从 Swift 应用程序进行 API 调用。其实现看起来像这样(用于创建新客户):
let apiURL = "https://YourDomain.com/add-customer"
let params = ["email": "hello@test.com"]
let heads = ["Accept": "application/json"]
Alamofire.request(.POST, apiURL, parameters: params, headers: heads)
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
在服务器端,假设你使用的是 Express 有这样的东西:
app.post('/add-customer', function (req, res) {
stripe.customers.create(
{ email: req.body.email },
function(err, customer) {
err; // null if no error occured
customer; // the created customer object
res.json(customer) // Send newly created customer back to client (Swift App)
}
);
});
希望对您有所帮助。
在查看将 Stripe 合并到我的 Swift iOS 应用程序中的文档时,我读到它告诉我:"you should expose three APIs on your backend for your iOS app to communicate with."
它指的三个API是检索客户API、创建卡API和客户更新API。然后文档列出了代码片段,说明您应该在后端执行哪些操作以调用这些 APIs,使用我不熟悉的 Spark 框架完成。
我的问题(Stripe 的文档似乎没有解释)是如何将我的后端暴露给这三个 API 以便我可以调用这些函数?是通过进口声明吗?或者这是 handled/unnecessary 因为我将使用不需要导入的 Spark 框架吗?
Link 到相关的 Stripe 文档:https://stripe.com/docs/mobile/ios
Spark 框架是为 Java 示例选择的框架;代码区域正上方是一些 link 到其他语言片段的选项卡(Ruby/Sinatra、Python/django、PHP、Node.js/Express),所以也许你的language/framework 是否存在 - 或者您使用的是不同的 Java 框架?
关于您的问题,您需要在服务器端应用程序上实现这三个 API 端点 - 然后每个端点都联系 Stripe API - 为了让您的 iOS 应用程序使用它们。
您可能还想查看 Stripe 的 Java 库:https://github.com/stripe/stripe-java/
我建议学习如何执行此操作,您应该在 Javascript / Node.JS 中执行此操作,并使用 Heroku 之类的工具来设置 Express Server。
在 iOS 方面,我会使用 Alamofire,这将使您可以轻松地从 Swift 应用程序进行 API 调用。其实现看起来像这样(用于创建新客户):
let apiURL = "https://YourDomain.com/add-customer"
let params = ["email": "hello@test.com"]
let heads = ["Accept": "application/json"]
Alamofire.request(.POST, apiURL, parameters: params, headers: heads)
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
在服务器端,假设你使用的是 Express 有这样的东西:
app.post('/add-customer', function (req, res) {
stripe.customers.create(
{ email: req.body.email },
function(err, customer) {
err; // null if no error occured
customer; // the created customer object
res.json(customer) // Send newly created customer back to client (Swift App)
}
);
});
希望对您有所帮助。