繁体   English   中英

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

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

我的 ios 应用程序中有 STRIPE 集成。

我能够使用用户输入的卡片详细信息生成令牌。

我将此令牌发送到服务器以进行付款过程。

没关系!

问题,我想使用它的 API 创建一个 STRIPE 客户。

如何做到这一点,SDK 是否为此提供了任何东西?

或者在标题中传递 'Authorization_KEY' 和 'STRIPE_TEST_PUBLIC_KEY' 是这样吗?

或者我需要为此实施整个“OAuth 2.0”?

请帮忙 !

谢谢 !

我认为您无法使用公钥创建 Stripe 客户。 我很确定这个请求需要密钥,所以它可能应该在服务器上而不是客户端应用程序上处理。

是的, 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.
        }
    }
}

暂无
暂无

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

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