【问题标题】:How do I configure a backend for Stripe to implement in a Swift App?如何为 Stripe 配置后端以在 Swift 应用程序中实现?
【发布时间】:2016-11-24 14:05:18
【问题描述】:

过去几个小时我一直在研究,一直在努力了解如何为 Stripe 实施后端。我不是很有经验,一些 iOS Stripe 文档让我感到困惑。很多资源建议使用 Heroku/PHP 和使用 Alamofire 或 AFNetworking 设置后端,但我对它不是很熟悉。我知道这是一个愚蠢的问题,但我正在努力学习!谁能给我解释一下如何设置一个简单的后端/解释 Alamofire 或推荐有关如何正确实施 Stripe 的资源?

【问题讨论】:

标签: ios swift heroku stripe-payments alamofire


【解决方案1】:

在 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)
        }
    );
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2014-10-02
    • 2021-11-19
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    相关资源
    最近更新 更多