Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] Implement SFSafariViewController as fallback when checkoutDidFail #146

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ extension CartViewController: CheckoutDelegate {
dismiss(animated: true)
}

func checkoutDidFail(errors: [ShopifyCheckoutSheetKit.CheckoutError]) {
print(#function, errors)
}
// func checkoutDidFail(errors: [ShopifyCheckoutSheetKit.CheckoutError]) {
// print(#function, errors)
// }

func checkoutDidClickContactLink(url: URL) {
if UIApplication.shared.canOpenURL(url) {
Expand All @@ -173,16 +173,16 @@ extension CartViewController: CheckoutDelegate {
}

func checkoutDidFail(error: ShopifyCheckoutSheetKit.CheckoutError) {
switch error {
case .sdkError(let underlying):
print(#function, underlying)
forceCloseCheckout("Checkout Unavailable")
case .checkoutExpired(let message): forceCloseCheckout(message)
case .checkoutUnavailable(let message): forceCloseCheckout(message)
case .checkoutLiquidNotMigrated(let message):
print(#function, message)
forceCloseCheckout("Checkout Unavailable")
}
// switch error {
// case .sdkError(let underlying):
// print(#function, underlying)
// forceCloseCheckout("Checkout Unavailable")
// case .checkoutExpired(let message): forceCloseCheckout(message)
// case .checkoutUnavailable(let message): forceCloseCheckout(message)
// case .checkoutLiquidNotMigrated(let message):
// print(#function, message)
// forceCloseCheckout("Checkout Unavailable")
// }
}

func checkoutDidEmitWebPixelEvent(event: ShopifyCheckoutSheetKit.PixelEvent) {
Expand Down
5 changes: 4 additions & 1 deletion Sources/ShopifyCheckoutSheetKit/CheckoutBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,13 @@
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

throw NSError(domain: "com.shopify", code: 1, userInfo: [NSLocalizedDescriptionKey: "Fake Error"])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentionally throwing an error here anytime we receive a bridge event


let container = try decoder.container(keyedBy: CodingKeys.self)
let name = try container.decode(String.self, forKey: .name)


Check failure on line 104 in Sources/ShopifyCheckoutSheetKit/CheckoutBridge.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Vertical Whitespace Violation: Limit vertical whitespace to a single empty line; currently 2 (vertical_whitespace)
switch name {
case "completed":
let checkoutCompletedEventDecoder = CheckoutCompletedEventDecoder()
Expand Down
4 changes: 1 addition & 3 deletions Sources/ShopifyCheckoutSheetKit/CheckoutDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ extension CheckoutDelegate {
handleUrl(url)
}

public func checkoutDidFail(error: CheckoutError) throws {
throw error
}
public func checkoutDidFail(error: CheckoutError) throws {}

private func handleUrl(_ url: URL) {
if UIApplication.shared.canOpenURL(url) {
Expand Down
39 changes: 34 additions & 5 deletions Sources/ShopifyCheckoutSheetKit/CheckoutWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import UIKit
import WebKit
import SafariServices

class CheckoutWebViewController: UIViewController, UIAdaptivePresentationControllerDelegate {

Expand Down Expand Up @@ -176,11 +177,6 @@
delegate?.checkoutDidComplete(event: event)
}

func checkoutViewDidFailWithError(error: CheckoutError) {
CheckoutWebView.invalidate()
delegate?.checkoutDidFail(error: error)
}

func checkoutViewDidClickLink(url: URL) {
delegate?.checkoutDidClickLink(url: url)
}
Expand All @@ -193,4 +189,37 @@
func checkoutViewDidEmitWebPixelEvent(event: PixelEvent) {
delegate?.checkoutDidEmitWebPixelEvent(event: event)
}

func checkoutViewDidFailWithError(error: CheckoutError) {
CheckoutWebView.invalidate()
delegate?.checkoutDidFail(error: error)

guard ShopifyCheckoutSheetKit.configuration.handleFallbackOnError else {
return
}

switch error {
case .sdkError: fallthrough

Check failure on line 202 in Sources/ShopifyCheckoutSheetKit/CheckoutWebViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

No Fallthrough only Violation: Fallthroughs can only be used if the `case` contains at least one other statement (no_fallthrough_only)
case .checkoutUnavailable: fallthrough

Check failure on line 203 in Sources/ShopifyCheckoutSheetKit/CheckoutWebViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

No Fallthrough only Violation: Fallthroughs can only be used if the `case` contains at least one other statement (no_fallthrough_only)
case .checkoutLiquidNotMigrated:
self.fallbackToSafariViewController()
default:
break

Check failure on line 207 in Sources/ShopifyCheckoutSheetKit/CheckoutWebViewController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Indentation Width Violation: Code should be indented using one tab or 4 spaces (indentation_width)
}
}

/// Private

private func getRootViewController() -> UIViewController? {
return UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.rootViewController
}

private func fallbackToSafariViewController() {
dismiss(animated: true) {
let safariViewController = SFSafariViewController(url: self.checkoutURL)
if let rootViewController = self.getRootViewController() {
rootViewController.present(safariViewController, animated: true)
}
}
}
}
4 changes: 4 additions & 0 deletions Sources/ShopifyCheckoutSheetKit/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public struct Configuration {
public var logger: Logger = NoOpLogger()

public var title: String = NSLocalizedString("shopify_checkout_sheet_title", value: "Checkout", comment: "The title of the checkout sheet.")

/// Trigger a SafariViewController in the event of a checkout error.
/// Set to "false" to disable this feature.
public var handleFallbackOnError = true
}

extension Configuration {
Expand Down
Loading