Shopify / checkout-sheet-kit-swift

Shopify’s Checkout Sheet Kit makes it simple to perform a checkout inside your Swift native app.
MIT License
38 stars 16 forks source link

Crash when tapping AmazonPay in ShopifyCheckoutKit presented from SwiftUI #71

Closed natumn closed 11 months ago

natumn commented 11 months ago

Hi, I'm encountering a crash in the app, and it's causing me some trouble!

Description

When tapping AmazonPay within ShopifyCheckoutKit displayed from SwiftUI, it crashes.

Steps to Reproduce

  1. Use SwiftUI to call the following ShopifyCheckout ViewController.
  2. Display ShopifyCheckoutKit.
  3. Tap the AmazonPay button to proceed with the payment.

Expected Behavior

The AmazonPay payment process should proceed without any issues, similar to credit card payments, ShopPay, and ApplePay.

Actual Behavior

The app crashes when attempting to proceed with the AmazonPay payment. Other payment methods, such as credit card payments, ShopPay, and ApplePay, work correctly.

Code

struct ShopifyCheckout: UIViewControllerRepresentable {
    let url: URL
    let onCompleted: () -> Void

    func makeUIViewController(context: Context) -> UIViewController {
        let viewController = CheckoutViewController(url: url, onCompleted: onCompleted)
        return viewController
    }

    func updateUIViewController(_ viewController: UIViewController, context: Context) {}
}

private final class CheckoutViewController: UIViewController, CheckoutDelegate {
    let url: URL
    let onCompleted: () -> Void

    init(url: URL, onCompleted: @escaping () -> Void) {
        self.url = url
        self.onCompleted = onCompleted
        super.init(nibName: nil, bundle: nil)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidAppear(_ animated: Bool) {
        ShopifyCheckoutKit.present(checkout: url, from: self, delegate: self)
        super.viewDidAppear(animated)
    }

    func checkoutDidComplete() {
        dismiss(animated: true)
        super.dismiss(animated: true)
        onCompleted()
    }

    func checkoutDidCancel() {
        dismiss(animated: true)
        super.dismiss(animated: true)
        onCompleted()
    }

    func checkoutDidFail(error: ShopifyCheckoutKit.CheckoutError) {
        fatalError(error.localizedDescription)
    }

    func checkoutDidClickContactLink(url: URL) {
        if UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url)
        }
    }
}

Environment

iOS Version: iOS 17.1.2 Xcode Version: Xcode 15.0.1 ShopifyCheckoutKit Version: 0.6.0

edward-shopify commented 11 months ago

Hey @natumn,

This was an oversight in our implementation. Amazon Pay is not yet supported in ShopifyCheckoutKit dev preview, we have plans to integrate this (but no firm ETA) yet.

We are shipping a fix our end that will disable Amazon Pay as a payment option within ShopifyCheckoutKit until it is correctly supported.