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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
6A257A152AFBB06300610DA5 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A257A142AFBB06300610DA5 /* Logger.swift */; };
6A2E77BE2CE606490067062D /* ProductGridView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A2E77BD2CE606400067062D /* ProductGridView.swift */; };
6A2E77C02CE618720067062D /* CartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A2E77BF2CE6186F0067062D /* CartView.swift */; };
CB88888882EE000000000001 /* CheckoutWithPayButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB88888882EE000000000002 /* CheckoutWithPayButton.swift */; };
6A3393632CEF742500E89FAA /* CheckoutController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A3393622CEF742200E89FAA /* CheckoutController.swift */; };
6A3393652CEF9E8100E89FAA /* Theme.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A3393642CEF9E7F00E89FAA /* Theme.swift */; };
6A3467332B600E64007314A8 /* LogsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A3467322B600E64007314A8 /* LogsView.swift */; };
Expand Down Expand Up @@ -61,6 +62,7 @@
6A257A142AFBB06300610DA5 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
6A2E77BD2CE606400067062D /* ProductGridView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductGridView.swift; sourceTree = "<group>"; };
6A2E77BF2CE6186F0067062D /* CartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CartView.swift; sourceTree = "<group>"; };
CB88888882EE000000000002 /* CheckoutWithPayButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckoutWithPayButton.swift; sourceTree = "<group>"; };
6A3393622CEF742200E89FAA /* CheckoutController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckoutController.swift; sourceTree = "<group>"; };
6A3393642CEF9E7F00E89FAA /* Theme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Theme.swift; sourceTree = "<group>"; };
6A3467322B600E64007314A8 /* LogsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogsView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -192,6 +194,7 @@
isa = PBXGroup;
children = (
6A2E77BF2CE6186F0067062D /* CartView.swift */,
CB88888882EE000000000002 /* CheckoutWithPayButton.swift */,
6A2E77BD2CE606400067062D /* ProductGridView.swift */,
6A3467322B600E64007314A8 /* LogsView.swift */,
4EBBA76E2A5F0CE200193E19 /* ProductView.swift */,
Expand Down Expand Up @@ -334,6 +337,7 @@
6A257A152AFBB06300610DA5 /* Logger.swift in Sources */,
4EBBA76D2A5F0CE200193E19 /* SceneDelegate.swift in Sources */,
6A2E77C02CE618720067062D /* CartView.swift in Sources */,
CB88888882EE000000000001 /* CheckoutWithPayButton.swift in Sources */,
6A3393652CEF9E8100E89FAA /* Theme.swift in Sources */,
CB3613492E98055700BBE31D /* UIView.swift in Sources */,
CB476E0F2E97CAC200878439 /* AddressSelectionViewController.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ struct CartView: View {
@ObservedObject var cartManager: CartManager = .shared
@ObservedObject var config: AppConfiguration = appConfiguration

@AppStorage(AppStorageKeys.showNativePayButton.rawValue)
var showNativePayButton: Bool = false

var body: some View {
if let lines = cartManager.cart?.lines.nodes {
ZStack(alignment: .bottom) {
Expand Down Expand Up @@ -122,102 +125,110 @@ struct CartView: View {
}
.sheet(isPresented: $showCheckoutSheet) {
if let url = cartManager.cart?.checkoutUrl {
ShopifyCheckout(checkout: url)
// .auth(token: "your-auth-token-here") // Uncomment to add authentication
.colorScheme(.automatic)
.onStart { event in
print("Checkout started with cart ID: \(event.cart.id)")
}
.onCancel {
showCheckoutSheet = false
}
.onComplete { event in
showCheckoutSheet = false
// Handle checkout completion
print("Checkout completed with order ID: \(event.orderConfirmation.order.id)")
}
.onFail { error in
showCheckoutSheet = false
// Handle checkout failure
print("Checkout failed: \(error)")
}
.onAddressChangeStart { event in
print(
"🎉 SwiftUI: Address change intent received for addressType: \(event.addressType)"
)

// Respond with updated cart after 2 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
let hardcodedAddress = CartDeliveryAddress(
firstName: "Jane",
lastName: "Smith",
address1: "456 SwiftUI Avenue",
address2: "Suite 200",
city: "Vancouver",
countryCode: "CA",
phone: "+1-604-555-0456",
provinceCode: "BC",
zip: "V6B 1A1"
)

let selectableAddress = CartSelectableAddress(
address: .deliveryAddress(hardcodedAddress),
selected: true
if showNativePayButton {
CheckoutWithPayButtonView(
checkoutURL: url,
isPresented: $showCheckoutSheet,
showPayButton: true
)
} else {
ShopifyCheckout(checkout: url)
// .auth(token: "your-auth-token-here") // Uncomment to add authentication
.colorScheme(.automatic)
.onStart { event in
print("Checkout started with cart ID: \(event.cart.id)")
}
.onCancel {
showCheckoutSheet = false
}
.onComplete { event in
showCheckoutSheet = false
// Handle checkout completion
print("Checkout completed with order ID: \(event.orderConfirmation.order.id)")
}
.onFail { error in
showCheckoutSheet = false
// Handle checkout failure
print("Checkout failed: \(error)")
}
.onAddressChangeStart { event in
print(
"Address change intent received for addressType: \(event.addressType)"
)
let delivery = CartDelivery(addresses: [selectableAddress])

let updatedCart = event.cart.copy(
delivery: .override(delivery)
)
// Respond with updated cart after 2 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
let hardcodedAddress = CartDeliveryAddress(
firstName: "Jane",
lastName: "Smith",
address1: "456 SwiftUI Avenue",
address2: "Suite 200",
city: "Vancouver",
countryCode: "CA",
phone: "+1-604-555-0456",
provinceCode: "BC",
zip: "V6B 1A1"
)

let response = CheckoutAddressChangeStartResponsePayload(cart: updatedCart)
let selectableAddress = CartSelectableAddress(
address: .deliveryAddress(hardcodedAddress),
selected: true
)
let delivery = CartDelivery(addresses: [selectableAddress])

print("🎉 SwiftUI: Responding with hardcoded Vancouver address")
do {
try event.respondWith(payload: response)
} catch {
print(
"Failed to respondwith: Responding with hardcoded Vancouver address"
let updatedCart = event.cart.copy(
delivery: .override(delivery)
)

let response = CheckoutAddressChangeStartResponsePayload(cart: updatedCart)

print("Responding with hardcoded Vancouver address")
do {
try event.respondWith(payload: response)
} catch {
print(
"Failed to respondwith: Responding with hardcoded Vancouver address"
)
}
}
}
}
.onPaymentMethodChangeStart { event in
print("🎉 SwiftUI: Payment method change start received")
.onPaymentMethodChangeStart { event in
print("Payment method change start received")

// Respond with updated cart after 2 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
let instrument = CreditCardPaymentInstrument(
externalReferenceId: "card-visa-1234"
)
// Respond with updated cart after 2 seconds
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
let instrument = CreditCardPaymentInstrument(
externalReferenceId: "card-visa-1234"
)

let paymentMethod = CartPaymentMethod(instruments: [instrument])
let payment = CartPayment(methods: [paymentMethod])

let updatedCart = Cart(
id: event.cart.id,
lines: event.cart.lines,
cost: event.cart.cost,
buyerIdentity: event.cart.buyerIdentity,
deliveryGroups: event.cart.deliveryGroups,
discountCodes: event.cart.discountCodes,
appliedGiftCards: event.cart.appliedGiftCards,
discountAllocations: event.cart.discountAllocations,
delivery: event.cart.delivery,
payment: payment
)
let paymentMethod = CartPaymentMethod(instruments: [instrument])
let payment = CartPayment(methods: [paymentMethod])

let updatedCart = Cart(
id: event.cart.id,
lines: event.cart.lines,
cost: event.cart.cost,
buyerIdentity: event.cart.buyerIdentity,
deliveryGroups: event.cart.deliveryGroups,
discountCodes: event.cart.discountCodes,
appliedGiftCards: event.cart.appliedGiftCards,
discountAllocations: event.cart.discountAllocations,
delivery: event.cart.delivery,
payment: payment
)

let response = CheckoutPaymentMethodChangeStartResponsePayload(cart: updatedCart)
let response = CheckoutPaymentMethodChangeStartResponsePayload(cart: updatedCart)

print("🎉 SwiftUI: Responding with hardcoded Visa ending in 1234")
do {
try event.respondWith(payload: response)
} catch {
print("Failed to respond with payment method change")
print("Responding with hardcoded Visa ending in 1234")
do {
try event.respondWith(payload: response)
} catch {
print("Failed to respond with payment method change")
}
}
}
}
.edgesIgnoringSafeArea(.all)
.edgesIgnoringSafeArea(.all)
}
}
}
} else {
Expand Down
Loading
Loading