【问题标题】:Stripe iOS didCreatePaymentResult never gets calledStripe iOS didCreatePaymentResult 永远不会被调用
【发布时间】:2020-10-13 13:36:42
【问题描述】:

问题看起来很简单,didCreatePaymentResult 永远不会被调用。

但是,在我的旧示例项目中,取自您的 iOS 支付意图示例,每次创建或选择卡时都会调用 didCreatePaymentResult,这是工作项目的存储库:https://github.com/glennposadas/stripe-example-ios-nodejs

但是,我主要关心的是我目前的项目。

我在这两个项目中都使用v19.2.0,我什至尝试过v19.3.0

我真的很想使用 Stripe Charge,但我相信 Stripe 不支持 Apple 付费。所以我别无选择,只能使用 Stripe Payment Intent


CoreService.swift(符合 STPCustomerEphemeralKeyProvider)

extension CoreService: STPCustomerEphemeralKeyProvider {
    func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
        orderServiceProvider.request(.requestEphemeralKey(stripeAPIVersion: apiVersion)) { (result) in
            switch result {
            case let .success(response):
                guard let json = ((try? JSONSerialization.jsonObject(with: response.data, options: []) as? [String : Any]) as [String : Any]??) else {
                    completion(nil, NSError(domain: "Error parsing stripe data", code: 300, userInfo: nil))
                    return
                }
                
                completion(json, nil)
                
            default:
                UIViewController.current()?.alert(title: "Error stripe", okayButtonTitle: "OK", withBlock: nil)
            }
        }
    }
}

PaymentController.swift

class PaymentViewController: BaseViewController {
    
    // MARK: - Properties

    private var paymentContext: STPPaymentContext!
    private let paymentConstantValue: Int = 3000
    
    // MARK: - Functions
    // MARK: Overrides
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setupStripe()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.hideNavBar(animated: true)
    }
    
    @IBAction func creditCardButtonTapped(_ sender: Any) {
        self.paymentContext.presentPaymentOptionsViewController()
    }
    
    private func setupStripe() {
        let config = STPPaymentConfiguration.shared()
        config.appleMerchantIdentifier = "merchant.com.gsample.app"
        config.companyName = "Scoutd LLC"
        config.requiredBillingAddressFields = .none
        config.requiredShippingAddressFields = .none
        config.additionalPaymentOptions = .applePay
        
        let customerContext = STPCustomerContext(keyProvider: CoreService())
        
        let paymentContext = STPPaymentContext(
            customerContext: customerContext,
            configuration: config,
            theme: STPTheme.default()
        )
        
        let userInformation = STPUserInformation()
        paymentContext.prefilledInformation = userInformation
        paymentContext.paymentAmount = self.paymentConstantValue
        paymentContext.paymentCurrency = "usd"
        
        self.paymentContext = paymentContext
        
        self.paymentContext.delegate = self
        self.paymentContext.hostViewController = self
    }
}

// MARK: - STPPaymentContextDelegate

extension PaymentViewController: STPPaymentContextDelegate {
    func paymentContextDidChange(_ paymentContext: STPPaymentContext) {
        print("paymentContextDidChange")
    }
    
    func paymentContext(_ paymentContext: STPPaymentContext, didFailToLoadWithError error: Error) {
        // error alert....
    }
    
    func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: @escaping STPPaymentStatusBlock) {
        print("didCreatePaymentResult ✅")
    }
    
    func paymentContext(_ paymentContext: STPPaymentContext, didFinishWith status: STPPaymentStatus, error: Error?) {
        switch status {
        case .success:
            // success
        case .error:
            // error alert....
        default:
            break
        }
    }
}

【问题讨论】:

    标签: ios stripe-payments


    【解决方案1】:

    已解决!这应该可以帮助工程师在未来努力实现 Stripe。

    所以就我而言,我有两个按钮:

    1. 苹果支付
    2. 信用卡。

    对我来说绝对的解决方案是处理paymentContextselectedPaymentOption

    场景:

    1. 如果点击了 Apple Pay 按钮,则显示 Apple Pay sheet,而不显示 Stripe 的添加/选择卡 UI。
    2. 如果点击信用卡按钮,请不要出示 Apple Pay Sheet,而是出示 Select Card。
    3. 与#2 相关,如果有选择的选项,请致电requestPayment()

    瞧! didCreatePaymentResult 现在被调用了!

    // MARK: IBActions
    
    @IBAction func applePayButtonTapped(_ sender: Any) {
        if self.paymentContext.selectedPaymentOption is STPApplePayPaymentOption {
            self.paymentContext.requestPayment()
        }
    }
    
    @IBAction func creditCardButtonTapped(_ sender: Any) {
        if let selectedPaymentOption = self.paymentContext.selectedPaymentOption,
            !(selectedPaymentOption is STPApplePayPaymentOption) {
            
            self.paymentContext.requestPayment()
            return
        }
        
        self.paymentContext.presentPaymentOptionsViewController()
    }
    

    【讨论】:

    • 感谢您跟进您的解决方案!
    猜你喜欢
    • 1970-01-01
    • 2012-10-27
    • 2013-10-12
    • 2012-03-27
    • 2013-08-29
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    相关资源
    最近更新 更多