Skip to content
Merged
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 @@ -21,7 +21,6 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import PassKit
import ShopifyAcceleratedCheckouts
import SwiftUI

Expand Down Expand Up @@ -78,7 +77,6 @@ private func createApplePayConfiguration(

return ShopifyAcceleratedCheckouts.ApplePayConfiguration(
merchantIdentifier: "merchant.com.shopify.example.ShopifyAcceleratedCheckoutsApp",
supportedNetworks: [.amex, .discover, .masterCard, .visa],
contactFields: fields
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extension StorefrontAPI {
typealias Money = StorefrontAPI.MoneyV2
typealias Address = StorefrontAPI.Address
typealias ApplePayPayment = StorefrontAPI.ApplePayPayment
typealias CardBrand = StorefrontAPI.CardBrand
}

/// Represents a cart in the Storefront API
Expand Down Expand Up @@ -353,10 +354,20 @@ extension StorefrontAPI {
/// Shop payment settings
struct ShopPaymentSettings: Codable {
let supportedDigitalWallets: [String]
let acceptedCardBrands: [String]
let acceptedCardBrands: [CardBrand]
let countryCode: String
}

/// Card brands supported by Shopify's payment system
enum CardBrand: String, Codable, CaseIterable {
case americanExpress = "AMERICAN_EXPRESS"
case dinersClub = "DINERS_CLUB"
case discover = "DISCOVER"
case jcb = "JCB"
case mastercard = "MASTERCARD"
case visa = "VISA"
}

// MARK: - Delivery Groups

/// Connection type for delivery groups
Expand Down Expand Up @@ -1010,7 +1021,8 @@ extension StorefrontAPI.Address {
shop.paymentSettings.countryCode

let paymentSettings = PaymentSettings(
countryCode: countryCode
countryCode: countryCode,
acceptedCardBrands: shop.paymentSettings.acceptedCardBrands
)

// Extract primary domain
Expand All @@ -1028,16 +1040,22 @@ extension StorefrontAPI.Address {
}

/// Payment settings for the shop
@available(iOS 17.0, *)
class PaymentSettings {
/// The shop's country code (e.g., "US", "CA")
let countryCode: String

init(countryCode: String) {
/// Card brands accepted by the merchant
let acceptedCardBrands: [StorefrontAPI.CardBrand]

init(countryCode: String, acceptedCardBrands: [StorefrontAPI.CardBrand] = []) {
self.countryCode = countryCode
self.acceptedCardBrands = acceptedCardBrands
}
}

/// Domain information for the shop
@available(iOS 17.0, *)
class Domain {
/// The host name of the domain (e.g., "example.myshopify.com")
let host: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ extension ShopifyAcceleratedCheckouts {
/// Configuration options for Apple Pay integration within Shopify Accelerated Checkouts.
///
/// This class encapsulates all necessary settings for enabling Apple Pay as a payment method,
/// including merchant identification, supported payment networks, and required contact information.
/// including merchant identification and required contact information. Supported payment networks
/// are automatically determined based on the merchant's Shopify configuration.
@Observable public class ApplePayConfiguration {
/// The merchant identifier for Apple Pay transactions.
///
Expand All @@ -44,12 +45,6 @@ extension ShopifyAcceleratedCheckouts {
/// - See: [Apple Developer Documentation - merchantIdentifier](https://developer.apple.com/documentation/passkit_apple_pay_and_wallet/pkpaymentrequest/1619305-merchantidentifier)
public let merchantIdentifier: String

/// Payment card networks supported for Apple Pay transactions.
///
/// Only card types included in this array will be displayed as available payment
/// options in the Apple Pay payment sheet.
public let supportedNetworks: [PKPaymentNetwork]

/// Contact information fields required during the Apple Pay payment flow.
///
/// Fields specified in this array will be marked as required in the payment sheet.
Expand All @@ -65,15 +60,14 @@ extension ShopifyAcceleratedCheckouts {
///
/// - Parameters:
/// - merchantIdentifier: The merchant identifier registered with Apple.
/// - supportedNetworks: Array of payment card networks to accept.
/// - contactFields: Contact information fields to require from the customer.
/// - Note: Supported payment networks are automatically determined based on the
/// merchant's accepted card brands configuration in Shopify.
public init(
merchantIdentifier: String,
supportedNetworks: [PKPaymentNetwork],
contactFields: [RequiredContactFields]
) {
self.merchantIdentifier = merchantIdentifier
self.supportedNetworks = supportedNetworks
self.contactFields = contactFields
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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 PassKit

@available(iOS 17.0, *)
enum CardBrandMapper {
/// Maps Shopify's CardBrand enum values to Apple Pay's PKPaymentNetwork values
/// - Parameter shopifyCardBrand: The card brand from Shopify's acceptedCardBrands
/// - Returns: The corresponding PKPaymentNetwork, or nil if the brand is not supported by Apple Pay
static func mapToPKPaymentNetwork(_ shopifyCardBrand: StorefrontAPI.CardBrand) -> PKPaymentNetwork? {
switch shopifyCardBrand {
case .americanExpress:
return .amex
case .discover:
return .discover
case .jcb:
return .JCB
case .mastercard:
return .masterCard
case .visa:
return .visa
case .dinersClub:
// Diners Club is not supported by Apple Pay
return nil
}
}

/// Maps an array of Shopify card brands to PKPaymentNetwork values
/// - Parameter shopifyCardBrands: Array of card brands from Shopify
/// - Returns: Array of PKPaymentNetwork values, filtering out any unsupported brands
static func mapToPKPaymentNetworks(_ shopifyCardBrands: [StorefrontAPI.CardBrand]) -> [PKPaymentNetwork] {
shopifyCardBrands.compactMap { mapToPKPaymentNetwork($0) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ class PKDecoder {
let currencyCode = cart.cost.totalAmount.currencyCode

paymentRequest.merchantIdentifier = configuration.applePay.merchantIdentifier
paymentRequest.supportedNetworks = configuration.applePay.supportedNetworks

// Map accepted card brands from Shopify to PKPaymentNetwork
let acceptedCardBrands = configuration.shopSettings.paymentSettings.acceptedCardBrands
paymentRequest.supportedNetworks = CardBrandMapper.mapToPKPaymentNetworks(acceptedCardBrands)

paymentRequest.countryCode = configuration.shopSettings.paymentSettings.countryCode
paymentRequest.currencyCode = currencyCode
initialCurrencyCode = currencyCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ShopSettingsTests: XCTestCase {
shipsToCountries: ["US", "CA"],
paymentSettings: StorefrontAPI.ShopPaymentSettings(
supportedDigitalWallets: ["APPLE_PAY", "SHOP_PAY"],
acceptedCardBrands: ["VISA", "MASTERCARD"],
acceptedCardBrands: [.visa, .mastercard],
countryCode: countryCode
),
moneyFormat: "${{amount}}"
Expand Down
12 changes: 5 additions & 7 deletions Tests/ShopifyAcceleratedCheckoutsTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import PassKit
@testable import ShopifyAcceleratedCheckouts

// MARK: - Configuration Helpers
Expand Down Expand Up @@ -70,7 +69,8 @@ extension ShopSettings {
url: "https://test-shop.myshopify.com"
),
paymentSettings: PaymentSettings(
countryCode: "US"
countryCode: "US",
acceptedCardBrands: [.visa, .mastercard, .americanExpress, .discover]
)
)
}
Expand All @@ -82,7 +82,8 @@ extension ShopSettings {
url: "https://test-shop.myshopify.com"
),
paymentSettings: PaymentSettings = PaymentSettings(
countryCode: "US"
countryCode: "US",
acceptedCardBrands: [.visa, .mastercard, .americanExpress, .discover]
)
) -> ShopSettings {
return ShopSettings(
Expand All @@ -98,18 +99,15 @@ extension ShopifyAcceleratedCheckouts.ApplePayConfiguration {
static var testConfiguration: ShopifyAcceleratedCheckouts.ApplePayConfiguration {
return ShopifyAcceleratedCheckouts.ApplePayConfiguration(
merchantIdentifier: "merchant.test.id",
supportedNetworks: [.visa],
contactFields: [.email, .phone]
)
}

static func testConfiguration(
merchantIdentifier: String = "merchant.test.id",
supportedNetworks: [PKPaymentNetwork] = [.visa]
merchantIdentifier: String = "merchant.test.id"
) -> ShopifyAcceleratedCheckouts.ApplePayConfiguration {
return ShopifyAcceleratedCheckouts.ApplePayConfiguration(
merchantIdentifier: merchantIdentifier,
supportedNetworks: supportedNetworks,
contactFields: [.email, .phone]
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import PassKit
@testable import ShopifyAcceleratedCheckouts
@testable import ShopifyCheckoutSheetKit
import XCTest
Expand Down Expand Up @@ -50,7 +49,6 @@ final class ApplePayCallbackTests: XCTestCase {

let applePayConfig = ShopifyAcceleratedCheckouts.ApplePayConfiguration(
merchantIdentifier: "test.merchant.id",
supportedNetworks: [.visa, .masterCard],
contactFields: []
)

Expand All @@ -60,7 +58,7 @@ final class ApplePayCallbackTests: XCTestCase {
host: "test-shop.myshopify.com",
url: "https://test-shop.myshopify.com"
),
paymentSettings: PaymentSettings(countryCode: "US")
paymentSettings: PaymentSettings(countryCode: "US", acceptedCardBrands: [.visa, .mastercard])
)

mockConfiguration = ApplePayConfigurationWrapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import PassKit
@testable import ShopifyAcceleratedCheckouts
import ShopifyCheckoutSheetKit
import SwiftUI
Expand All @@ -48,7 +47,6 @@

mockApplePayConfiguration = ShopifyAcceleratedCheckouts.ApplePayConfiguration(
merchantIdentifier: "test.merchant.id",
supportedNetworks: [.visa, .masterCard, .amex],
contactFields: []
)

Expand All @@ -58,7 +56,10 @@
host: "test-shop.myshopify.com",
url: "https://test-shop.myshopify.com"
),
paymentSettings: PaymentSettings(countryCode: "US")
paymentSettings: PaymentSettings(
countryCode: "US",
acceptedCardBrands: [.visa, .mastercard, .americanExpress]
)
)

mockConfiguration = ApplePayConfigurationWrapper(
Expand Down Expand Up @@ -238,7 +239,7 @@
configuration: mockConfiguration
)

var callbackSet = false

Check warning on line 242 in Tests/ShopifyAcceleratedCheckoutsTests/Wallets/ApplePay/ApplePayIntegrationTests.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

variable 'callbackSet' was written to, but never read

viewController.onCheckoutWebPixelEvent = { _ in
callbackSet = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import PassKit
@testable import ShopifyAcceleratedCheckouts
import ShopifyCheckoutSheetKit
import UIKit
Expand All @@ -36,7 +35,7 @@ class ApplePayViewControllerTests: XCTestCase {
super.setUp()

// Create mock shop settings
let paymentSettings = PaymentSettings(countryCode: "US")
let paymentSettings = PaymentSettings(countryCode: "US", acceptedCardBrands: [.visa, .mastercard])
let primaryDomain = Domain(host: "test-shop.myshopify.com", url: "https://test-shop.myshopify.com")
let shopSettings = ShopSettings(
name: "Test Shop",
Expand All @@ -53,7 +52,6 @@ class ApplePayViewControllerTests: XCTestCase {
// Create Apple Pay configuration
let applePayConfig = ShopifyAcceleratedCheckouts.ApplePayConfiguration(
merchantIdentifier: "test.merchant",
supportedNetworks: [.visa, .masterCard],
contactFields: []
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

mockApplePayConfiguration = ShopifyAcceleratedCheckouts.ApplePayConfiguration(
merchantIdentifier: "test.merchant.id",
supportedNetworks: [.visa, .masterCard],
contactFields: []
)

Expand All @@ -56,7 +55,7 @@
host: "test-shop.myshopify.com",
url: "https://test-shop.myshopify.com"
),
paymentSettings: PaymentSettings(countryCode: "US")
paymentSettings: PaymentSettings(countryCode: "US", acceptedCardBrands: [.visa, .mastercard])
)
}

Expand Down Expand Up @@ -98,7 +97,7 @@
secondCallbackInvoked = true
}

let view = AcceleratedCheckoutButtons(cartID: "gid://Shopify/Cart/test-cart-id")

Check warning on line 100 in Tests/ShopifyAcceleratedCheckoutsTests/Wallets/ApplePay/ApplePayViewModifierTests.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

initialization of immutable value 'view' was never used; consider replacing with assignment to '_' or removing it
.onComplete(firstAction)
.onComplete(secondAction) // Should replace the first
.environment(mockConfiguration)
Expand Down Expand Up @@ -135,14 +134,14 @@
var firstCallbackInvoked = false
var secondCallbackInvoked = false

let firstAction = { (_: CheckoutCompletedEvent) in

Check warning on line 137 in Tests/ShopifyAcceleratedCheckoutsTests/Wallets/ApplePay/ApplePayViewModifierTests.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

initialization of immutable value 'firstAction' was never used; consider replacing with assignment to '_' or removing it
firstCallbackInvoked = true
}
let secondAction = { (_: CheckoutCompletedEvent) in
secondCallbackInvoked = true
}

let view = AcceleratedCheckoutButtons(cartID: "gid://Shopify/Cart/test-cart-id")

Check warning on line 144 in Tests/ShopifyAcceleratedCheckoutsTests/Wallets/ApplePay/ApplePayViewModifierTests.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

initialization of immutable value 'view' was never used; consider replacing with assignment to '_' or removing it
.onCancel { firstCallbackInvoked = true }
.onCancel { secondCallbackInvoked = true } // Should replace the first
.environment(mockConfiguration)
Expand Down Expand Up @@ -208,8 +207,8 @@
// MARK: - Environment Propagation Tests

func testEnvironmentPropagation() {
var parentSuccessInvoked = false

Check warning on line 210 in Tests/ShopifyAcceleratedCheckoutsTests/Wallets/ApplePay/ApplePayViewModifierTests.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

initialization of variable 'parentSuccessInvoked' was never used; consider replacing with assignment to '_' or removing it

Check warning on line 210 in Tests/ShopifyAcceleratedCheckoutsTests/Wallets/ApplePay/ApplePayViewModifierTests.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

initialization of variable 'parentSuccessInvoked' was never used; consider replacing with assignment to '_' or removing it
var childSuccessInvoked = false

Check warning on line 211 in Tests/ShopifyAcceleratedCheckoutsTests/Wallets/ApplePay/ApplePayViewModifierTests.swift

View workflow job for this annotation

GitHub Actions / call-workflow-passing-data / test

variable 'childSuccessInvoked' was written to, but never read

// Create a custom container view
struct TestContainer: View {
Expand Down
Loading