简体   繁体   English

如何在 IOS 中使用 STRIPE API 创建 STRIPE 客户?

[英]How to create STRIPE customer using STRIPE API in IOS?

I have STRIPE integration in my ios application.我的 ios 应用程序中有 STRIPE 集成。

I am able to generate token using cards details entered by users.我能够使用用户输入的卡片详细信息生成令牌。

I send this TOKEN to server for payment process.我将此令牌发送到服务器以进行付款过程。

That's all fine !没关系!

Problem , is I want to create a STRIPE Customer using its API.问题,我想使用它的 API 创建一个 STRIPE 客户。

How to do this , does SDK provide anything for this ?如何做到这一点,SDK 是否为此提供了任何东西?

OR passing 'Authorization_KEY' and 'STRIPE_TEST_PUBLIC_KEY' in header is the way ?或者在标题中传递 'Authorization_KEY' 和 'STRIPE_TEST_PUBLIC_KEY' 是这样吗?

OR I need to implement whole 'OAuth 2.0' for this ?或者我需要为此实施整个“OAuth 2.0”?

Please help !请帮忙 !

Thank you !谢谢 !

I don't think you can create a Stripe Customer with the Public Key.我认为您无法使用公钥创建 Stripe 客户。 I'm quite sure Secret key is required for this request and so it should probably be handled on the server instead of the client app.我很确定这个请求需要密钥,所以它可能应该在服务器上而不是客户端应用程序上处理。

Yes, nabeel is correct, the customer should be created by the server instead of the client app.是的, nabeel是正确的,客户应该由服务器而不是客户端应用程序创建。 Although, if you want to risk it, you can do it like this...虽然,如果你想冒险,你可以这样做......

class StripeUtils {

    //Feed in the STPCardParams to create a Stripe token.
    func generateToken(with params: STPCardParams, completion: @escaping (Error?, STPToken?) -> ()) {

        STPAPIClient.shared().createToken(withCard: params) { (token, error) in
            completion(error, token)
        }
    }

    //Pass the token and user email address to get the STPCustomer
    func createUserWith(email: String, token: STPToken?, completion: @escaping (Error?, STPCustomer?) -> ()) {

        guard let token = token else {
            print("Token can not be nil")
            completion(*error*, nil)
            return
        }

        let headers = ["Authorization": "Bearer \(Constants.STRIPE_SECRET_KEY)"] //The secret key
        let body = ["email": email, "source": token.tokenId] as [String : Any]
        var paramString = String()

        body.forEach({ (key, value) in
            paramString = "\(paramString)\(key)=\(value)&"
        })
        let params = paramString.data(using: .utf8)

        //URLStrings.stripe_createUser is "https://api.stripe.com/v1/customers"
        //The APIManager is a class that takes urlString, params(HTTP body) and headers(HTTP headers) to get initialized.
        //(You can use Alamofire or whatever you use to handle APIs)
        //Instance of APIManager has a method called 'perform' with the URLSession's completion block  (Data?, URLResponse?, Error?) -> ()


        let manager = APIManager(urlString: URLStrings.stripe_createUser.description, params: params, headers: headers)

        manager.perform { (data, response, error) in

        //Use STPCustomerDeserializer intead of standard JSONSerialization to let Stripe hanlde the API response.

            let object = STPCustomerDeserializer.init(data: data, urlResponse: response, error: error)
            completion(object.error, object.customer)
        //That's it, you'll have a STPCustomer instance with stripeId if there were no errors.
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM