From 74bd05c53ec5671e1a77c738308a75a603cb9a63 Mon Sep 17 00:00:00 2001 From: Cian Buckley Date: Wed, 4 Oct 2023 14:51:19 +0200 Subject: [PATCH] support checkout errors on bridge web events --- Sources/ShopifyCheckout/CheckoutBridge.swift | 9 ++++++--- Sources/ShopifyCheckout/CheckoutView.swift | 7 ++++++- Tests/ShopifyCheckoutTests/CheckoutBridgeTests.swift | 6 +++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Sources/ShopifyCheckout/CheckoutBridge.swift b/Sources/ShopifyCheckout/CheckoutBridge.swift index 36a9e1eb..89b4c7ed 100644 --- a/Sources/ShopifyCheckout/CheckoutBridge.swift +++ b/Sources/ShopifyCheckout/CheckoutBridge.swift @@ -33,13 +33,13 @@ enum CheckoutBridge { return "ShopifyCheckoutSDK/\(ShopifyCheckout.version) (\(schemaVersion);\(theme))" } - static func decode(_ message: WKScriptMessage) throws -> Event { + static func decode(_ message: WKScriptMessage) throws -> WebEvent { guard let body = message.body as? String, let data = body.data(using: .utf8) else { throw Error.invalidBridgeEvent() } do { - return try JSONDecoder().decode(Event.self, from: data) + return try JSONDecoder().decode(WebEvent.self, from: data) } catch { throw Error.invalidBridgeEvent(error) } @@ -53,9 +53,10 @@ extension CheckoutBridge { } extension CheckoutBridge { - enum Event: Decodable { + enum WebEvent: Decodable { case checkoutComplete case checkoutCanceled + case checkoutNotAvailable(String) case unsupported(String) enum CodingKeys: String, CodingKey { @@ -73,6 +74,8 @@ extension CheckoutBridge { self = .checkoutComplete case "close": self = .checkoutCanceled + case "checkout_not_available": + self = .checkoutNotAvailable(name) default: self = .unsupported(name) } diff --git a/Sources/ShopifyCheckout/CheckoutView.swift b/Sources/ShopifyCheckout/CheckoutView.swift index 4c9a6348..0257c849 100644 --- a/Sources/ShopifyCheckout/CheckoutView.swift +++ b/Sources/ShopifyCheckout/CheckoutView.swift @@ -97,9 +97,14 @@ class CheckoutView: WKWebView { extension CheckoutView: WKScriptMessageHandler { func userContentController(_ controller: WKUserContentController, didReceive message: WKScriptMessage) { do { - if case .checkoutComplete = try CheckoutBridge.decode(message) { + switch try CheckoutBridge.decode(message) { + case .checkoutComplete: CheckoutView.cache = nil viewDelegate?.checkoutViewDidCompleteCheckout() + case .checkoutNotAvailable(let message): + CheckoutView.cache = nil + viewDelegate?.checkoutViewDidFailWithError(error: .checkoutNotAvailable(message: message)) + default: break } } catch { viewDelegate?.checkoutViewDidFailWithError(error: .sdkError(underlying: error)) diff --git a/Tests/ShopifyCheckoutTests/CheckoutBridgeTests.swift b/Tests/ShopifyCheckoutTests/CheckoutBridgeTests.swift index 7c34bfde..368fce5b 100644 --- a/Tests/ShopifyCheckoutTests/CheckoutBridgeTests.swift +++ b/Tests/ShopifyCheckoutTests/CheckoutBridgeTests.swift @@ -65,7 +65,7 @@ class CheckoutBridgeTests: XCTestCase { let result = try CheckoutBridge.decode(mock) - guard case CheckoutBridge.Event.unsupported = result else { + guard case CheckoutBridge.WebEvent.unsupported = result else { return XCTFail("expected CheckoutScriptMessage.unsupportedEvent, got \(result)") } } @@ -79,7 +79,7 @@ class CheckoutBridgeTests: XCTestCase { let result = try CheckoutBridge.decode(mock) - guard case CheckoutBridge.Event.checkoutCanceled = result else { + guard case CheckoutBridge.WebEvent.checkoutCanceled = result else { return XCTFail("expected CheckoutScriptMessage.checkoutCanceled, got \(result)") } } @@ -93,7 +93,7 @@ class CheckoutBridgeTests: XCTestCase { let result = try CheckoutBridge.decode(mock) - guard case CheckoutBridge.Event.checkoutComplete = result else { + guard case CheckoutBridge.WebEvent.checkoutComplete = result else { return XCTFail("expected CheckoutScriptMessage.checkoutComplete, got \(result)") } }