-
Notifications
You must be signed in to change notification settings - Fork 27
Migrate Accelerated Checkout to Storefront API 2025-07; unify cart completion errors & prepare-stage handling #538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,11 +62,10 @@ | |
|
|
||
| try await upsertShippingAddress(to: shippingAddress) | ||
|
|
||
| let result = try await controller.storefront.cartPrepareForCompletion(id: cartID) | ||
| try setCart(to: result.cart) | ||
| try await prepareCartForCompletion(id: cartID) | ||
|
|
||
| // If address update cleared delivery groups, revert to previous cart and show error | ||
| if result.cart?.deliveryGroups.nodes.isEmpty == true, previousCart?.deliveryGroups.nodes.isEmpty == false { | ||
| if controller.cart?.deliveryGroups.nodes.isEmpty == true, previousCart?.deliveryGroups.nodes.isEmpty == false { | ||
| try setCart(to: previousCart) | ||
|
|
||
| ShopifyAcceleratedCheckouts.logger.error("ApplePay: didSelectShippingContact deliveryGroups were unexpectedly empty") | ||
|
|
@@ -120,8 +119,7 @@ | |
| billingAddress: billingPostalAddress | ||
| ) | ||
|
|
||
| let result = try await controller.storefront.cartPrepareForCompletion(id: cartID) | ||
| try setCart(to: result.cart) | ||
| try await prepareCartForCompletion(id: cartID) | ||
|
|
||
| return pkDecoder.paymentRequestPaymentMethodUpdate() | ||
| } catch { | ||
|
|
@@ -162,9 +160,7 @@ | |
| deliveryOptionHandle: selectedDeliveryOptionHandle.rawValue | ||
| ) | ||
|
|
||
| let result = try await controller.storefront.cartPrepareForCompletion(id: cartID) | ||
|
|
||
| try setCart(to: result.cart) | ||
| try await prepareCartForCompletion(id: cartID) | ||
|
|
||
| return pkDecoder.paymentRequestShippingMethodUpdate() | ||
| } catch { | ||
|
|
@@ -198,8 +194,7 @@ | |
| customerAccessToken: configuration.common.customer?.customerAccessToken | ||
| ) | ||
| ) | ||
| let result = try await controller.storefront.cartPrepareForCompletion(id: cartID) | ||
| try setCart(to: result.cart) | ||
| try await prepareCartForCompletion(id: cartID) | ||
| } | ||
|
|
||
| if try pkDecoder.isShippingRequired() { | ||
|
|
@@ -214,8 +209,7 @@ | |
| try await reapplySelectedShippingMethod() | ||
| } | ||
|
|
||
| let result = try await controller.storefront.cartPrepareForCompletion(id: cartID) | ||
| try setCart(to: result.cart) | ||
| try await prepareCartForCompletion(id: cartID) | ||
| } else { | ||
| // If the cart is entirely digital updating with a complete billingAddress | ||
| // allows us to resolve pending terms on taxes prior to cartPaymentUpdate | ||
|
|
@@ -230,8 +224,7 @@ | |
| billingAddress: billingPostalAddress | ||
| ) | ||
|
|
||
| let result = try await controller.storefront.cartPrepareForCompletion(id: cartID) | ||
| try setCart(to: result.cart) | ||
| try await prepareCartForCompletion(id: cartID) | ||
| } | ||
|
|
||
| let totalAmount = try pkEncoder.totalAmount.get() | ||
|
|
@@ -240,7 +233,7 @@ | |
| // Taxes may become pending again fail to resolve despite updating within the didUpdatePaymentMethod | ||
| // So we retry one time to see if the error clears on retry | ||
| _ = try await Task.retrying(priority: nil, maxRetryCount: 1) { | ||
| try await self.controller.storefront.cartPaymentUpdate( | ||
|
Check warning on line 236 in Sources/ShopifyAcceleratedCheckouts/Wallets/ApplePay/ApplePayAuthorizationDelegate/ApplePayAuthorizationDelegate+Controller.swift
|
||
| id: cartID, | ||
| totalAmount: totalAmount, | ||
| applePayPayment: applePayPayment | ||
|
|
@@ -301,14 +294,42 @@ | |
| } | ||
|
|
||
| let cartID = try pkEncoder.cartID.get() | ||
| try await controller.storefront.cartPrepareForCompletion(id: cartID) | ||
| _ = try await prepareCartForCompletion(id: cartID) | ||
| try await controller.storefront.cartSelectedDeliveryOptionsUpdate( | ||
| id: cartID, | ||
| deliveryGroupId: deliveryGroupID, | ||
| deliveryOptionHandle: selectedDeliveryOptionHandle.rawValue | ||
| ) | ||
| } | ||
|
|
||
| // `prepareCartForCompletion` filters 'non-terminal' violations | ||
| // See: `ErrorHandler.filterApplePayResolvableViolations` | ||
| // | ||
| // Before `didAuthorizePayment`, Apple Pay redacts PII, these are expected to be resolved by submit | ||
| // After `didAuthorizePayment`, PII is available, but we continue filtering in `prepareForCompletion` so | ||
| // `cartSubmitForCompletion` can return all violations at once | ||
| // This gives the user the best chance to fix everything in a single pass. | ||
| // | ||
| @discardableResult | ||
| func prepareCartForCompletion(id: GraphQLScalars.ID) async throws -> StorefrontAPI.Cart? { | ||
| do { | ||
| let result = try await controller.storefront.cartPrepareForCompletion(id: id) | ||
| try setCart(to: result.cart) | ||
| return result.cart | ||
| } catch let error as StorefrontAPI.Errors { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to catch non-response errors? i.e network timeouts, decode failures etc
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any other error will follow the existing error handling which will be caught and handled in the callsite (those indicate an unexpected error and will abort to checkout kit) |
||
| if case let .response(_, _, .cartPrepareForCompletion(payload)) = error, | ||
| case let .notReady(notReady) = payload.result | ||
| { | ||
| let actionableErrors = ErrorHandler.filterApplePayResolvableViolations(errors: notReady.errors) | ||
| if actionableErrors.isEmpty { | ||
| try setCart(to: notReady.cart) | ||
| return notReady.cart | ||
| } | ||
| } | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| func handleError<T>( | ||
| error: Error, | ||
| cart _: StorefrontAPI.Cart?, | ||
|
|
@@ -329,6 +350,8 @@ | |
| self.checkoutURL = checkoutURL | ||
| } | ||
| return completion([abortError]) | ||
| case .continueFlow: | ||
| return completion([]) | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.