如何配置Stripe的后端在Swift应用程序中实现?

时间:2016-07-21 06:24:48

标签: ios swift heroku stripe-payments alamofire

我在过去几个小时里一直在研究,并且一直在努力了解如何为Stripe实现后端。我不是很有经验,一些iOS Stripe文档令我困惑。很多资源建议使用Heroku / PHP并使用Alamofire或AFNetworking设置后端,但我对它不是很熟悉。我知道这是一个愚蠢的问题,但我正尽力学习!任何人都可以给我一个解释如何设置一个简单的后端/解释Alamofire或推荐资源如何正确实现Stripe?

1 个答案:

答案 0 :(得分:1)

我建议你学习如何做到这一点你应该在Javascript / Node.JS中使用Heroku来设置Express Server。

在iOS方面,我会使用Alamofire,它可以让您轻松地从Swift App进行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)
        }
    );
});