@@ -34,61 +34,51 @@ extension ApplePayAuthorizationDelegate: PKPaymentAuthorizationControllerDelegat
3434 _: PKPaymentAuthorizationController ,
3535 didSelectShippingContact contact: PKContact
3636 ) async -> PKPaymentRequestShippingContactUpdate {
37- logDebug ( " Method triggered " , method: " didSelectShippingContact " )
3837 pkEncoder. shippingContact = . success( contact)
3938
4039 // Clear selected shipping method to prevent stale identifier errors
4140 pkEncoder. selectedShippingMethod = nil
4241 pkDecoder. selectedShippingMethod = nil
43- logDebug ( " Cleared selected shipping method " , method: " didSelectShippingContact " )
4442
4543 do {
4644 let cartID = try pkEncoder. cartID. get ( )
47-
4845 let shippingAddress = try pkEncoder. shippingAddress. get ( )
49-
5046 // Store current cart state before attempting address update
5147 let previousCart = controller. cart
5248
5349 let cart = try await upsertShippingAddress ( to: shippingAddress)
54- logDebug ( " Shipping address upserted successfully " , method: " didSelectShippingContact " )
55-
5650 // If address update cleared delivery groups, revert to previous cart and show error
5751 if cart. deliveryGroups. nodes. isEmpty, previousCart? . deliveryGroups. nodes. isEmpty == false {
58- logError ( " Address update cleared delivery groups - reverting to previous cart " , method : " didSelectShippingContact " )
52+ logInfo ( " Address update cleared delivery groups - reverting to previous cart " )
5953 try setCart ( to: previousCart)
6054
6155 return pkDecoder. paymentRequestShippingContactUpdate ( errors: [ ValidationErrors . addressUnserviceableError] )
6256 }
63-
64- try setCart ( to: cart)
65- logDebug ( " Cart updated with new shipping address " , method: " didSelectShippingContact " )
57+ logDebug ( " Shipping address upsert complete " )
6658
6759 let result = try await controller. storefront. cartPrepareForCompletion ( id: cartID)
68- logDebug ( " Cart prepared for completion " , method: " didSelectShippingContact " )
69-
7060 try setCart ( to: result. cart)
7161
7262 return pkDecoder. paymentRequestShippingContactUpdate ( )
7363 } catch {
74- logError ( " Method failed : \( error) " , method : " didSelectShippingContact " )
64+ logError ( " Failed to set shipping contact : \( error) " )
7565 return await handleError ( error: error, cart: controller. cart) {
76- pkDecoder. paymentRequestShippingContactUpdate ( errors: $0)
66+ logInfo ( " Handling error with: \( $0) " )
67+ return pkDecoder. paymentRequestShippingContactUpdate ( errors: $0)
7768 }
7869 }
7970 }
8071
8172 /// Triggers on payment sheet presentation with default card, and if user changes payment method
8273 /// NOTE: If the user changes the card, the billingContact field will be nil when this fires again
8374 ///
84- /// This event is necessary in 'digital' (non-shippable) products flow .
75+ /// This event is necessary in 'digital' (non-shippable) product flows .
8576 /// Allows access to country so cart can determine taxes prior to `didAuthorizePayment`,
86- /// minimizing payment .amount discrepancies.
77+ /// minimizing `PKPayment .amount` discrepancies.
8778 func paymentAuthorizationController(
8879 _: PKPaymentAuthorizationController ,
8980 didSelectPaymentMethod paymentMethod: PKPaymentMethod
9081 ) async -> PKPaymentRequestPaymentMethodUpdate {
91- logDebug ( " Method triggered " , method: " didSelectPaymentMethod " )
9282 pkEncoder. selectedPaymentMethod = paymentMethod
9383
9484 do {
@@ -101,33 +91,34 @@ extension ApplePayAuthorizationDelegate: PKPaymentAuthorizationControllerDelegat
10191 let billingPostalAddress = try ? pkEncoder. billingPostalAddress. get ( ) ,
10292 let country = billingPostalAddress. country
10393 else {
104- logDebug ( " Skipping payment method processing - shipping required or no billing address " , method : " didSelectPaymentMethod " )
94+ logInfo ( " Skipping buyerIdentityUpdate: taxable address will be sourced from shipping contact " )
10595 return pkDecoder. paymentRequestPaymentMethodUpdate ( )
10696 }
97+
10798 let cartID = try pkEncoder. cartID. get ( )
108- logDebug ( " Updating buyer identity with country: \( country ) " , method : " didSelectPaymentMethod " )
99+
109100 try await controller. storefront. cartBuyerIdentityUpdate (
110101 id: cartID,
111102 input: . init(
112103 countryCode: country,
113104 customerAccessToken: configuration. common. customer? . customerAccessToken
114105 )
115106 )
116-
117- logDebug ( " Updating billing address " , method: " didSelectPaymentMethod " )
118- try await controller. storefront
119- . cartBillingAddressUpdate ( id: cartID, billingAddress: billingPostalAddress)
107+ logDebug ( " cartBuyerIdentityUpdate complete: \( country) " )
108+ try await controller. storefront. cartBillingAddressUpdate (
109+ id: cartID,
110+ billingAddress: billingPostalAddress
111+ )
120112
121113 let result = try await controller. storefront. cartPrepareForCompletion ( id: cartID)
122- logDebug ( " Cart prepared for completion after payment method selection " , method: " didSelectPaymentMethod " )
123114 try setCart ( to: result. cart)
124115
125116 return pkDecoder. paymentRequestPaymentMethodUpdate ( )
126117 } catch {
127- logError ( " Method failed: \( error) " , method: " didSelectShippingContact " )
128-
118+ logError ( " Failed to set billing address taxable country: \( error) " )
129119 return await handleError ( error: error, cart: controller. cart) {
130- pkDecoder. paymentRequestPaymentMethodUpdate ( errors: $0)
120+ logInfo ( " Handling error with: \( $0) " )
121+ return pkDecoder. paymentRequestPaymentMethodUpdate ( errors: $0)
131122 }
132123 }
133124 }
@@ -136,14 +127,13 @@ extension ApplePayAuthorizationDelegate: PKPaymentAuthorizationControllerDelegat
136127 _: PKPaymentAuthorizationController ,
137128 didSelectShippingMethod shippingMethod: PKShippingMethod
138129 ) async -> PKPaymentRequestShippingMethodUpdate {
139- logDebug ( " Method triggered " , method: " didSelectShippingMethod " )
140130 // Check if this shipping method identifier is still valid
141131 let availableShippingMethods = pkDecoder. shippingMethods
142132 let isValidMethod = availableShippingMethods. contains { $0. identifier == shippingMethod. identifier }
143133 let methodToUse : PKShippingMethod = isValidMethod ? shippingMethod : ( availableShippingMethods. first ?? shippingMethod)
144134
145135 if !isValidMethod {
146- logDebug ( " Selected shipping method invalid, using fallback method " , method : " didSelectShippingMethod " )
136+ logDebug ( " Selected shipping method invalid, using fallback method \( methodToUse ) " )
147137 }
148138
149139 pkEncoder. selectedShippingMethod = methodToUse
@@ -154,27 +144,24 @@ extension ApplePayAuthorizationDelegate: PKPaymentAuthorizationControllerDelegat
154144 let selectedDeliveryOptionHandle = try pkEncoder. selectedDeliveryOptionHandle. get ( )
155145 let deliveryGroupID = try pkEncoder. deliveryGroupID. get ( )
156146
157- logDebug ( " Updating selected delivery options " , method: " didSelectShippingMethod " )
158- let cart = try await controller. storefront
159- . cartSelectedDeliveryOptionsUpdate (
160- id: cartID,
161- deliveryGroupId: deliveryGroupID,
162- deliveryOptionHandle: selectedDeliveryOptionHandle. rawValue
163- )
147+ let cart = try await controller. storefront. cartSelectedDeliveryOptionsUpdate (
148+ id: cartID,
149+ deliveryGroupId: deliveryGroupID,
150+ deliveryOptionHandle: selectedDeliveryOptionHandle. rawValue
151+ )
164152 try setCart ( to: cart)
165- logDebug ( " Cart updated with selected delivery options " , method : " didSelectShippingMethod " )
153+ logDebug ( " cartSelectedDeliveryOptionsUpdate complete " )
166154
167155 let result = try await controller. storefront. cartPrepareForCompletion ( id: cartID)
168- logDebug ( " Cart prepared for completion after shipping method selection " , method: " didSelectShippingMethod " )
169-
170156 try setCart ( to: result. cart)
171157
172158 return pkDecoder. paymentRequestShippingMethodUpdate ( )
173159 } catch {
174- logError ( " Method failed: \( error) " , method : " didSelectShippingContact " )
160+ logError ( " Method failed: \( error) " )
175161
176- return await handleError ( error: error, cart: controller. cart) { _ in
177- pkDecoder. paymentRequestShippingMethodUpdate ( )
162+ return await handleError ( error: error, cart: controller. cart) {
163+ logInfo ( " Handling error with: \( $0) " )
164+ return pkDecoder. paymentRequestShippingMethodUpdate ( )
178165 }
179166 }
180167 }
@@ -183,7 +170,6 @@ extension ApplePayAuthorizationDelegate: PKPaymentAuthorizationControllerDelegat
183170 _: PKPaymentAuthorizationController ,
184171 didAuthorizePayment payment: PKPayment
185172 ) async -> PKPaymentAuthorizationResult {
186- logDebug ( " Method triggered - beginning payment authorization " , method: " didAuthorizePayment " )
187173 do {
188174 pkEncoder. payment = payment
189175 try ? await transition ( to: . paymentAuthorized( payment: payment) )
@@ -194,7 +180,7 @@ extension ApplePayAuthorizationDelegate: PKPaymentAuthorizationControllerDelegat
194180 || configuration. common. customer? . phoneNumber != nil
195181 || configuration. common. customer? . customerAccessToken != nil
196182 {
197- logDebug ( " Updating buyer identity with contact information " , method : " didAuthorizePayment " )
183+ logInfo ( " Updating buyer identity with contact information " )
198184 try await controller. storefront. cartBuyerIdentityUpdate (
199185 id: cartID,
200186 input: . init(
@@ -205,25 +191,25 @@ extension ApplePayAuthorizationDelegate: PKPaymentAuthorizationControllerDelegat
205191 )
206192 let result = try await controller. storefront. cartPrepareForCompletion ( id: cartID)
207193 try setCart ( to: result. cart)
208- logDebug ( " Buyer identity updated and cart prepared " , method : " didAuthorizePayment " )
194+ logDebug ( " cartBuyerIdentityUpdate complete " )
209195 }
210196
211197 if try pkDecoder. isShippingRequired ( ) {
212- logDebug ( " Processing shipping address for shippable products " , method : " didAuthorizePayment " )
198+ logDebug ( " Updating shipping address" )
213199 let shippingAddress = try pkEncoder. shippingAddress. get ( )
214200 _ = try await upsertShippingAddress ( to: shippingAddress, validate: true )
215201
216202 let result = try await controller. storefront. cartPrepareForCompletion ( id: cartID)
217203 try setCart ( to: result. cart)
218- logDebug ( " Shipping address processed and cart prepared " , method : " didAuthorizePayment " )
204+ logDebug ( " upsertShippingAddress complete " )
219205 } else {
220206 /// If the cart is entirely digital updating with a complete billingAddress
221207 /// allows us to resolve pending terms on taxes prior to cartPaymentUpdate
222- logDebug ( " Processing digital cart with billing address" , method : " didAuthorizePayment " )
208+ logDebug ( " Updating billing address" )
223209 guard
224210 let billingPostalAddress = try ? pkEncoder. billingPostalAddress. get ( )
225211 else {
226- logDebug ( " No billing address available for digital cart " , method : " didAuthorizePayment " )
212+ logDebug ( " No billing address available for digital cart " )
227213 return pkDecoder. paymentAuthorizationResult ( )
228214 }
229215
@@ -234,43 +220,43 @@ extension ApplePayAuthorizationDelegate: PKPaymentAuthorizationControllerDelegat
234220
235221 let result = try await controller. storefront. cartPrepareForCompletion ( id: cartID)
236222 try setCart ( to: result. cart)
237- logDebug ( " Digital cart billing address updated and cart prepared " , method : " didAuthorizePayment " )
223+ logDebug ( " cartBillingAddressUpdate complete " )
238224 }
239225
240- logDebug ( " Preparing payment data for submission " , method: " didAuthorizePayment " )
241226 let totalAmount = try pkEncoder. totalAmount. get ( )
242227 let applePayPayment = try pkEncoder. applePayPayment. get ( )
243228
244229 /// Taxes may become pending again fail to resolve despite updating within the didUpdatePaymentMethod
245230 /// So we retry one time to see if the error clears on retry
246- logDebug ( " Updating cart payment with retry logic " , method: " didAuthorizePayment " )
247231 _ = try await Task . retrying ( priority: nil , maxRetryCount: 1 ) {
232+ self . logDebug ( " attempting cartPaymentUpdate " )
248233 try await self . controller. storefront. cartPaymentUpdate (
249234 id: cartID,
250235 totalAmount: totalAmount,
251236 applePayPayment: applePayPayment
252237 )
253238 } . value
254- logDebug ( " Cart payment updated successfully " , method : " didAuthorizePayment " )
239+ logDebug ( " cartPaymentUpdate complete " )
255240
256- logDebug ( " Submitting cart for completion " , method: " didAuthorizePayment " )
257241 let response = try await controller. storefront. cartSubmitForCompletion ( id: cartID)
258- logDebug ( " Cart submitted for completion successfully " , method: " didAuthorizePayment " )
242+ logDebug ( " cartSubmitForCompletion complete " )
243+
259244 try ? await transition (
260245 to: . cartSubmittedForCompletion( redirectURL: response. redirectUrl. url)
261246 )
262247
263248 return pkDecoder. paymentAuthorizationResult ( )
264249 } catch {
265- logError ( " Method failed: \( error) " , method : " didSelectShippingContact " )
250+ logError ( " Method failed: \( error) " )
266251 return await handleError ( error: error, cart: controller. cart) {
267- pkDecoder. paymentAuthorizationResult ( errors: $0)
252+ logInfo ( " Handling error with: \( $0) " )
253+ return pkDecoder. paymentAuthorizationResult ( errors: $0)
268254 }
269255 }
270256 }
271257
272258 func paymentAuthorizationControllerDidFinish( _ controller: PKPaymentAuthorizationController ) {
273- logDebug ( " Payment authorization finished, current state: \( state) " , method : " paymentAuthorizationControllerDidFinish " )
259+ logDebug ( " current state: \( state) , transitioning to .completed " )
274260
275261 controller. dismiss {
276262 Task { try ? await self . transition ( to: . completed) }
@@ -282,20 +268,19 @@ extension ApplePayAuthorizationDelegate: PKPaymentAuthorizationControllerDelegat
282268 cart _: StorefrontAPI . Cart ? ,
283269 completion: ( _: [ Error ] ) -> T
284270 ) async -> T {
285- logDebug ( " Handling error with ErrorHandler " , method: " handleError " )
286271 guard let action = ErrorHandler . map ( error: error, cart: controller. cart) else {
287- logError ( " ErrorHandler could not map error - transitioning to unexpected error " , method : " handleError " )
272+ logInfo ( " ErrorHandler could not map error - transitioning to unexpected error " )
288273 try ? await transition ( to: . unexpectedError( error: abortError) )
289274 return completion ( [ abortError] )
290275 }
291276
292277 switch action {
293278 case let . showError( errors) :
294- logDebug ( " ErrorHandler mapped to showError - returning to Apple sheet " , method : " handleError " )
279+ logDebug ( " ErrorHandler mapped to showError - returning to Apple sheet " )
295280 try ? await transition ( to: . appleSheetPresented)
296281 return completion ( errors)
297282 case let . interrupt( reason, checkoutURL) :
298- logDebug ( " ErrorHandler mapped to interrupt with reason: \( reason) " , method : " handleError " )
283+ logDebug ( " ErrorHandler mapped to interrupt with reason: \( reason) " )
299284 try ? await transition ( to: . interrupt( reason: reason) )
300285 if let checkoutURL {
301286 self . checkoutURL = checkoutURL
0 commit comments