是的,nabeel 是正确的,客户应该由服务器而不是客户端应用程序创建。虽然,如果你想冒险,你可以这样做......
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.
}
}
}