Stripe Connect IOS集成

时间:2018-03-14 04:38:36

标签: swift xcode stripe-payments stripe-connect

Stripe 没有为IOS应用程序提供集成Stripe Connect的文档。 Stripe Connect实施不允许apply用于重定向网址,因此我不确定如何在获取带有授权码的重定向网址时打开应用程序内的网址并重定向到应用程序。

任何帮助将不胜感激。我该如何处理?

1 个答案:

答案 0 :(得分:0)

1-首先,您需要将条带导入项目:

import Stripe

2-制作付款按钮以执行付款操作:

@IBOutlet weak var payButton: UIButton!

3-创建条带文本字段的实例以处理卡数据输入:

class StripePaymentViewController: UIViewController {
    //stripe payment textfield
    let paymentTexField = STPPaymentCardTextField() 
}

4-在viewDidLoad中,您可以初始化textField:

func initializeStripeTextField() {
    // Initialize textField size
    paymentTexField.frame = CGRect(x: 15, y: 300, width: self.view.frame.width - 30, height: 60)
    // Changing textField background color
    paymentTexField.backgroundColor = .white
    // Setting the delegate (will be implemented later)
    paymentTexField.delegate = self
    // Adding the textField to the view
    view.addSubview(paymentTexField)
    // optional: hide the button until the payment data entered
    payButton.isHidden = true
}

5-实现条带textField委托以在填充卡数据后显示隐藏按钮

extension StripePaymentViewController: STPPaymentCardTextFieldDelegate {
    func paymentCardTextFieldDidChange(_ textField: STPPaymentCardTextField) {
        if textField.isValid {
            payButton.isHidden = false
        }
    }
}

6-使用'YOUR_STRIPE_KEY'添加付款按钮操作:

@IBAction func payAction(_ sender: Any) {
        // Get the card parameters
        let card = paymentTexField.cardParams

        // Aquire stripe token
        STPAPIClient.init(publishableKey: "YOUR_STRIPE_KEY").createToken(withCard: card) { (token, error) in
            if let error = error {
                print (error)
            } else if let token = token {
                // The aquired token
                print (token)
                // Send payment request (will be implemented later)
                self.payUsingToken(token: token)
            }
        }
    }

7-使用Alamofire付款请求

func payUsingToken(token: STPToken) {
        let baseUrl = "YOUR_BACKEND_PAYMENT_URL"
        // Request parameters
        var parameters = [String: String]()
        // Stripe aquired token
        parameters["pay_token"] = token.tokenId
        // The payment amount in cents (500 == 5$)
        parameters["money_amount"] = String(500 * 100)
        // Add other parameters if you want ...

        Alamofire.request(baseUrl, method: .post, parameters: parameters, encoding: URLEncoding.default) .responseJSON { response in
            switch response.result {
            case .success(let JSON):
                //Success
                print(JSON)

            case .failure(let error):
                //Failure
                print(error.localizedDescription)
            }
        }

    }