【问题标题】:Stripe Connect IOS Integration条纹连接 IOS 集成
【发布时间】:2018-08-22 12:27:01
【问题描述】:

Stripe 没有为 iOS 应用集成 Stripe Connect 提供任何文档。 Stripe Connect 实现不允许 URL Schemes 用于重定向 url,因此我不确定如何在应用程序内打开 url 并重定向到应用程序,同时获取带有授权码的重定向 url。

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

【问题讨论】:

标签: swift xcode stripe-payments stripe-connect


【解决方案1】:

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

import Stripe

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

@IBOutlet weak var payButton: UIButton!

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

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

4- 在 vi​​ewDidLoad 你可以初始化 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)
            }
        }

    }

【讨论】:

猜你喜欢
  • 2019-01-02
  • 2017-03-14
  • 2015-09-23
  • 2019-02-15
  • 2017-10-26
  • 2020-05-23
  • 2016-12-31
  • 2020-12-13
  • 2015-07-06
相关资源
最近更新 更多