Skip to content
Open
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 @@ -57,7 +57,7 @@ public struct ShopifyCheckout: UIViewControllerRepresentable, CheckoutConfigurab
var onFailAction: ((CheckoutError) -> Void)?

public init(checkout url: URL) {
checkoutURL = CheckoutProtocol.url(for: url, colorScheme: ShopifyCheckoutKit.configuration.colorScheme.rawValue)
Comment thread
toneymathews marked this conversation as resolved.
checkoutURL = CheckoutProtocol.url(for: url)

ShopifyCheckoutKit.invalidateOnConfigurationChange = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public func preload(checkout url: URL) {
return
}

let url = CheckoutProtocol.url(for: url, colorScheme: configuration.colorScheme.rawValue)
let url = CheckoutProtocol.url(for: url)
CheckoutWebView.preloadingActivatedByClient = true
CheckoutWebView.for(checkout: url).load(checkout: url, isPreload: true)
}
Expand All @@ -64,15 +64,15 @@ public func invalidate() {

@discardableResult
public func present(checkout url: URL, from: UIViewController, client: (any CheckoutCommunicationProtocol)? = nil) -> CheckoutViewController {
let decorated = CheckoutProtocol.url(for: url, colorScheme: configuration.colorScheme.rawValue)
let decorated = CheckoutProtocol.url(for: url)
let viewController = CheckoutViewController(checkout: decorated, client: client)
from.present(viewController, animated: true)
return viewController
}

@discardableResult
package func present(checkout url: URL, from: UIViewController, entryPoint: MetaData.EntryPoint, client: (any CheckoutCommunicationProtocol)? = nil) -> CheckoutViewController {
let decorated = CheckoutProtocol.url(for: url, colorScheme: configuration.colorScheme.rawValue)
let decorated = CheckoutProtocol.url(for: url)
let viewController = CheckoutViewController(checkout: decorated, client: client, entryPoint: entryPoint)
from.present(viewController, animated: true)
return viewController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ import Foundation

extension CheckoutProtocol {
/// Returns the given checkout URL with the query parameters required to
/// initiate the Embedded Checkout Protocol handshake (`ec_version`,
/// `ec_color_scheme`).
public static func url(for url: URL, colorScheme: String) -> URL {
/// initiate the Embedded Checkout Protocol handshake (`ec_version`).
public static func url(for url: URL) -> URL {
guard var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
return url
}
var queryItems = components.queryItems ?? []
queryItems.append(URLQueryItem(name: "ec_version", value: specVersion))
queryItems.append(URLQueryItem(name: "ec_color_scheme", value: colorScheme))
components.queryItems = queryItems
return components.url ?? url
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
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
@testable import ShopifyCheckoutProtocol
import Testing

@Suite("CheckoutProtocol URL Tests")
struct CheckoutProtocolURLTests {
private let baseURL = URL(string: "https://shop.com/cart/c/abc")!

private func queryItems(_ url: URL) -> [URLQueryItem] {
URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems ?? []
}

@Test func appendsEcVersion() {
let result = CheckoutProtocol.url(for: baseURL)
let items = queryItems(result)
#expect(items.contains(URLQueryItem(name: "ec_version", value: CheckoutProtocol.specVersion)))
}

@Test func preservesExistingQueryItems() throws {
let url = try #require(URL(string: "https://shop.com/cart/c/abc?key=cart_token&utm_source=email"))
let items = queryItems(CheckoutProtocol.url(for: url))
#expect(items.contains(URLQueryItem(name: "key", value: "cart_token")))
#expect(items.contains(URLQueryItem(name: "utm_source", value: "email")))
#expect(items.contains(URLQueryItem(name: "ec_version", value: CheckoutProtocol.specVersion)))
}
}
Loading