Skip to content
Draft
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ xcuserdata
.vscode/settings.json
.claude/
CLAUDE.md

33 changes: 11 additions & 22 deletions Sources/ShopifyCheckoutSheetKit/CheckoutBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,19 @@ protocol CheckoutBridgeProtocol {
}

enum CheckoutBridge: CheckoutBridgeProtocol {
static let schemaVersion = "8.1"
static let messageHandler = "mobileCheckoutSdk"
internal static let userAgent = "ShopifyCheckoutSDK/\(ShopifyCheckoutSheetKit.version)"
static let schemaVersion = "2025-04"
static let messageHandler = "CheckoutSheetProtocolConsumer"

static var applicationName: String {
let theme = ShopifyCheckoutSheetKit.configuration.colorScheme.rawValue
let userAgentString = "\(userAgent) (\(schemaVersion);\(theme);standard)"

return userAgentWithOptionalSuffix(userAgentString)
let platform = ShopifyCheckoutSheetKit.configuration.platform?.rawValue ?? "iOS"
let userAgentString = "CheckoutKit/\(ShopifyCheckoutSheetKit.version) (\(platform)) CheckoutSheetProtocol/\(schemaVersion)"
return userAgentString
}

static var recoveryAgent: String {
let theme = ShopifyCheckoutSheetKit.configuration.colorScheme.rawValue
let userAgentString = "\(userAgent) (noconnect;\(theme);standard_recovery)"

return userAgentWithOptionalSuffix(userAgentString)
}

static func userAgentWithOptionalSuffix(_ userAgentString: String) -> String {
if let platform = ShopifyCheckoutSheetKit.configuration.platform?.rawValue {
return "\(userAgentString) \(platform)"
} else {
return userAgentString
}
let platform = ShopifyCheckoutSheetKit.configuration.platform?.rawValue ?? "iOS"
let userAgentString = "CheckoutKit/\(ShopifyCheckoutSheetKit.version) (\(platform)) CheckoutSheetProtocol/noconnect"
return userAgentString
}

static func instrument(_ webView: WKWebView, _ instrumentation: InstrumentationPayload) {
Expand Down Expand Up @@ -91,11 +80,11 @@ enum CheckoutBridge: CheckoutBridgeProtocol {

static internal func dispatchMessageTemplate(body: String) -> String {
return """
if (window.MobileCheckoutSdk && window.MobileCheckoutSdk.dispatchMessage) {
window.MobileCheckoutSdk.dispatchMessage(\(body));
if (window.Shopify?.CheckoutSheetProtocol?.postMessage) {
window.Shopify.CheckoutSheetProtocol.postMessage(\(body));
} else {
window.addEventListener('mobileCheckoutBridgeReady', function () {
window.MobileCheckoutSdk.dispatchMessage(\(body));
window.Shopify.CheckoutSheetProtocol.postMessage(\(body));
}, {passive: true, once: true});
}
"""
Expand Down
8 changes: 8 additions & 0 deletions Sources/ShopifyCheckoutSheetKit/CheckoutURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ public struct CheckoutURL {

return !["http", "https"].contains(scheme)
}

public func isWebLink() -> Bool {
guard let scheme = url.scheme else {
return false
}

return ["http", "https"].contains(scheme)
}
}
40 changes: 39 additions & 1 deletion Sources/ShopifyCheckoutSheetKit/CheckoutWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,12 @@ class CheckoutWebView: WKWebView {

if isPreload && isPreloadingAvailable {
isPreloadRequest = true
request.setValue("prefetch", forHTTPHeaderField: "Sec-Purpose")
}

// Add checkout kit headers
let headers = checkoutKitHeaders(isPreload: isPreload)
for (key, value) in headers {
request.setValue(value, forHTTPHeaderField: key)
}

load(request)
Expand Down Expand Up @@ -305,6 +310,39 @@ extension CheckoutWebView: WKNavigationDelegate {
return
}

// Check if we need to inject headers for main frame navigation
if action.targetFrame?.isMainFrame == true && CheckoutURL(from: url).isWebLink() {
let currentHeaders = action.request.allHTTPHeaderFields ?? [:]
let checkoutHeaders = checkoutKitHeaders()
var shouldOverride = false
var newHeaders = currentHeaders

let colorScheme = ShopifyCheckoutSheetKit.configuration.colorScheme
let shouldHaveColorSchemeHeader = !(colorScheme == .web || colorScheme == .automatic)

if shouldHaveColorSchemeHeader && !currentHeaders.hasColorSchemeHeader() {
let headersWithColorScheme = currentHeaders.withColorScheme()
newHeaders.merge(headersWithColorScheme) { _, new in new }
shouldOverride = true
}

if !currentHeaders.hasBrandingHeader() {
let headersWithBranding = currentHeaders.withBranding()
newHeaders.merge(headersWithBranding) { _, new in new }
shouldOverride = true
}

if shouldOverride {
var request = URLRequest(url: url)
for (key, value) in newHeaders {
request.setValue(value, forHTTPHeaderField: key)
}
webView.load(request)
decisionHandler(.cancel)
return
}
}

decisionHandler(.allow)
}

Expand Down
93 changes: 93 additions & 0 deletions Sources/ShopifyCheckoutSheetKit/Headers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
MIT License

Copyright 2023 - Present, Shopify Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import Foundation

internal struct Headers {
static let purpose = "Sec-Purpose"
static let purposePrefetch = "prefetch"

static let prefersColorScheme = "Sec-CH-Prefers-Color-Scheme"

static let branding = "X-Shopify-Checkout-Kit-Branding"
static let brandingCheckoutKit = "CHECKOUT_KIT"
static let brandingWeb = "WEB_DEFAULT"
}

internal func checkoutKitHeaders(isPreload: Bool = false) -> [String: String] {
var headers = [String: String]()

if isPreload {
headers[Headers.purpose] = Headers.purposePrefetch
}

return headers
.withColorScheme()
.withBranding()
}

internal extension Dictionary where Key == String, Value == String {
func withColorScheme() -> [String: String] {
var headers = self

let colorScheme = ShopifyCheckoutSheetKit.configuration.colorScheme
switch colorScheme {
case .light:
headers[Headers.prefersColorScheme] = "light"
case .dark:
headers[Headers.prefersColorScheme] = "dark"
case .automatic, .web:
break // Don't add header for automatic or web color schemes
}

return headers
}

func withBranding() -> [String: String] {
var headers = self

let colorScheme = ShopifyCheckoutSheetKit.configuration.colorScheme
switch colorScheme {
case .web:
headers[Headers.branding] = Headers.brandingWeb
default:
headers[Headers.branding] = Headers.brandingCheckoutKit
}

return headers
}

func hasColorSchemeHeader() -> Bool {
return hasHeader(Headers.prefersColorScheme)
}

func hasBrandingHeader() -> Bool {
return hasHeader(Headers.branding)
}

private func hasHeader(_ headerName: String) -> Bool {
return self.keys.contains { key in
key.caseInsensitiveCompare(headerName) == .orderedSame
}
}
}
20 changes: 10 additions & 10 deletions Tests/ShopifyCheckoutSheetKitTests/CheckoutBridgeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ class CheckoutBridgeTests: XCTestCase {
func testReturnsStandardUserAgent() {
let version = ShopifyCheckoutSheetKit.version
let schemaVersion = CheckoutBridge.schemaVersion
XCTAssertEqual(CheckoutBridge.applicationName, "ShopifyCheckoutSDK/\(version) (\(schemaVersion);automatic;standard)")
XCTAssertEqual(CheckoutBridge.applicationName, "CheckoutKit/\(version) (iOS) CheckoutSheetProtocol/\(schemaVersion)")
}

func testReturnsRecoveryUserAgent() {
let version = ShopifyCheckoutSheetKit.version
XCTAssertEqual(CheckoutBridge.recoveryAgent, "ShopifyCheckoutSDK/\(version) (noconnect;automatic;standard_recovery)")
XCTAssertEqual(CheckoutBridge.recoveryAgent, "CheckoutKit/\(version) (iOS) CheckoutSheetProtocol/noconnect")
}

func testReturnsUserAgentWithCustomPlatformSuffix() {
let version = ShopifyCheckoutSheetKit.version
let schemaVersion = CheckoutBridge.schemaVersion
ShopifyCheckoutSheetKit.configuration.platform = Platform.reactNative
XCTAssertEqual(CheckoutBridge.applicationName, "ShopifyCheckoutSDK/\(version) (\(schemaVersion);automatic;standard) ReactNative")
XCTAssertEqual(CheckoutBridge.recoveryAgent, "ShopifyCheckoutSDK/\(version) (noconnect;automatic;standard_recovery) ReactNative")
XCTAssertEqual(CheckoutBridge.applicationName, "CheckoutKit/\(version) (ReactNative) CheckoutSheetProtocol/\(schemaVersion)")
XCTAssertEqual(CheckoutBridge.recoveryAgent, "CheckoutKit/\(version) (ReactNative) CheckoutSheetProtocol/noconnect")
ShopifyCheckoutSheetKit.configuration.platform = nil
}

Expand Down Expand Up @@ -320,23 +320,23 @@ class CheckoutBridgeTests: XCTestCase {

private func expectedPresentedScript() -> String {
return """
if (window.MobileCheckoutSdk && window.MobileCheckoutSdk.dispatchMessage) {
window.MobileCheckoutSdk.dispatchMessage('presented');
if (window.Shopify?.CheckoutSheetProtocol?.postMessage) {
window.Shopify.CheckoutSheetProtocol.postMessage('presented');
} else {
window.addEventListener('mobileCheckoutBridgeReady', function () {
window.MobileCheckoutSdk.dispatchMessage('presented');
window.Shopify.CheckoutSheetProtocol.postMessage('presented');
}, {passive: true, once: true});
}
"""
}

private func expectedPayloadScript() -> String {
return """
if (window.MobileCheckoutSdk && window.MobileCheckoutSdk.dispatchMessage) {
window.MobileCheckoutSdk.dispatchMessage('payload', {"one": true});
if (window.Shopify?.CheckoutSheetProtocol?.postMessage) {
window.Shopify.CheckoutSheetProtocol.postMessage('payload', {"one": true});
} else {
window.addEventListener('mobileCheckoutBridgeReady', function () {
window.MobileCheckoutSdk.dispatchMessage('payload', {"one": true});
window.Shopify.CheckoutSheetProtocol.postMessage('payload', {"one": true});
}, {passive: true, once: true});
}
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CheckoutWebViewTests: XCTestCase {
XCTAssertTrue(recovery.isRecovery)
XCTAssertFalse(recovery.isBridgeAttached)
XCTAssertFalse(recovery.isPreloadingAvailable)
XCTAssertEqual(recovery.configuration.applicationNameForUserAgent, "ShopifyCheckoutSDK/\(ShopifyCheckoutSheetKit.version) (noconnect;automatic;standard_recovery)")
XCTAssertEqual(recovery.configuration.applicationNameForUserAgent, "CheckoutKit/\(ShopifyCheckoutSheetKit.version) (iOS) CheckoutSheetProtocol/noconnect")
XCTAssertTrue(recovery.configuration.allowsInlineMediaPlayback)
XCTAssertEqual(recovery.backgroundColor, backgroundColor)
XCTAssertFalse(recovery.isOpaque)
Expand Down
Loading