Question

I have STRIPE integration in my ios application.

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.

How to do this , does SDK provide anything for this ?

OR passing 'Authorization_KEY' and 'STRIPE_TEST_PUBLIC_KEY' in header is the way ?

OR I need to implement whole 'OAuth 2.0' for this ?

Please help !

Thank you !

Was it helpful?

Solution

I don't think you can create a Stripe Customer with the Public Key. 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.

OTHER TIPS

Yes, nabeel is correct, the customer should be created by the server instead of the client app. 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.
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top