Skip to content

Commit

Permalink
Implement SFSafariViewController as fallback when checkoutDidFail
Browse files Browse the repository at this point in the history
  • Loading branch information
markmur committed Mar 25, 2024
1 parent eb2df08 commit 092cb3e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
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 @@ extension CheckoutBridge {
}

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

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

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
37 changes: 32 additions & 5 deletions Sources/ShopifyCheckoutSheetKit/CheckoutWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SO

import UIKit
import WebKit
import SafariServices

class CheckoutWebViewController: UIViewController, UIAdaptivePresentationControllerDelegate {

Expand Down Expand Up @@ -176,11 +177,6 @@ extension CheckoutWebViewController: CheckoutWebViewDelegate {
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,35 @@ extension CheckoutWebViewController: CheckoutWebViewDelegate {
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, .checkoutUnavailable, .checkoutLiquidNotMigrated:
self.fallbackToSafariViewController()
default:
break
}
}

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

0 comments on commit 092cb3e

Please sign in to comment.