提问者:小点点

条形iOS didCreatePaymentResult从不调用


问题似乎很简单,didCreatePaymentResult从未被调用。

但是,在我的旧样例项目中(取自用于支付意图的iOS示例),每次创建或选择卡时都会调用DidCreatePaymentResult,下面是工作项目的repo:https://github.com/glennposadas/stripe-example-ios-nodejs

但再次强调,我主要关心的是我目前的项目。

我在这两个项目中都使用了V19.2.0,我甚至尝试了V19.3.0。

我真的想用Stripe收费,但我相信Stripe不支持Apple pay。 所以我别无选择,只能使用条纹支付意图。

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
        }
    }
}

共1个答案

匿名用户

解决了! 这应该可以帮助工程师们在未来挣扎于Stripe实现。

所以在我的例子中,我有两个按钮:

  1. Apple Pay
  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()
}