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

最佳答案

我建议学习如何执行此操作,您应该在Javascript / Node.JS中执行此操作,并使用Heroku之类的工具来设置Express Server。

在iOS方面,我将使用Alamofire,它可以让您轻松地从Swift应用程序进行API调用。其实现如下所示(用于创建新客户):

let apiURL = "https://YourDomain.com/add-customer"
let params = ["email": "[email protected]"]
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)
        }
    );
});

关于ios - 如何为Stripe配置后端以在Swift App中实现?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38496453/

10-13 08:39