簡體   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