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