@@ -54,39 +54,39 @@ protocol PayController: AnyObject {
54
54
///
55
55
/// Example usage:
56
56
/// ```swift
57
- /// applePayViewController.onComplete = { [weak self] in
57
+ /// applePayViewController.onCheckoutComplete = { [weak self] event in
58
58
/// self?.presentSuccessScreen()
59
- /// self?.logAnalyticsEvent(.checkoutCompleted)
59
+ /// self?.logAnalyticsEvent(.checkoutCompleted, orderId: event.orderId )
60
60
/// }
61
61
/// ```
62
62
@MainActor
63
- public var onComplete : ( ( ) -> Void ) ?
63
+ public var onCheckoutComplete : ( ( CheckoutCompletedEvent ) -> Void ) ?
64
64
65
65
/// Callback invoked when an error occurs during the checkout process.
66
- /// This closure is called on the main thread when the payment fails or is cancelled .
66
+ /// This closure is called on the main thread when the payment fails.
67
67
///
68
68
/// Example usage:
69
69
/// ```swift
70
- /// applePayViewController.onFail = { [weak self] in
71
- /// self?.showErrorAlert()
72
- /// self?.logAnalyticsEvent(.checkoutFailed)
70
+ /// applePayViewController.onCheckoutFail = { [weak self] error in
71
+ /// self?.showErrorAlert(for: error )
72
+ /// self?.logAnalyticsEvent(.checkoutFailed, error: error )
73
73
/// }
74
74
/// ```
75
75
@MainActor
76
- public var onFail : ( ( ) -> Void ) ?
76
+ public var onCheckoutFail : ( ( CheckoutError ) -> Void ) ?
77
77
78
78
/// Callback invoked when the checkout process is cancelled by the user.
79
79
/// This closure is called on the main thread when the user dismisses the checkout.
80
80
///
81
81
/// Example usage:
82
82
/// ```swift
83
- /// applePayViewController.onCancel = { [weak self] in
83
+ /// applePayViewController.onCheckoutCancel = { [weak self] in
84
84
/// self?.resetCheckoutState()
85
85
/// self?.logAnalyticsEvent(.checkoutCancelled)
86
86
/// }
87
87
/// ```
88
88
@MainActor
89
- public var onCancel : ( ( ) -> Void ) ?
89
+ public var onCheckoutCancel : ( ( ) -> Void ) ?
90
90
91
91
/// Callback invoked to determine if checkout should recover from an error.
92
92
/// This closure is called on the main thread when an error occurs.
@@ -100,33 +100,33 @@ protocol PayController: AnyObject {
100
100
/// }
101
101
/// ```
102
102
@MainActor
103
- public var onShouldRecoverFromError : ( ( ShopifyCheckoutSheetKit . CheckoutError ) -> Bool ) ?
103
+ public var onShouldRecoverFromError : ( ( CheckoutError ) -> Bool ) ?
104
104
105
105
/// Callback invoked when the user clicks a link during checkout.
106
106
/// This closure is called on the main thread when a link is clicked.
107
107
///
108
108
/// Example usage:
109
109
/// ```swift
110
- /// applePayViewController.onClickLink = { [weak self] url in
110
+ /// applePayViewController.onCheckoutClickLink = { [weak self] url in
111
111
/// self?.handleExternalLink(url)
112
112
/// self?.logAnalyticsEvent(.linkClicked, url: url)
113
113
/// }
114
114
/// ```
115
115
@MainActor
116
- public var onClickLink : ( ( URL ) -> Void ) ?
116
+ public var onCheckoutClickLink : ( ( URL ) -> Void ) ?
117
117
118
118
/// Callback invoked when a web pixel event is emitted during checkout.
119
119
/// This closure is called on the main thread when pixel events occur.
120
120
///
121
121
/// Example usage:
122
122
/// ```swift
123
- /// applePayViewController.onWebPixelEvent = { [weak self] event in
123
+ /// applePayViewController.onCheckoutWebPixelEvent = { [weak self] event in
124
124
/// self?.trackPixelEvent(event)
125
125
/// self?.logAnalyticsEvent(.pixelFired, event: event)
126
126
/// }
127
127
/// ```
128
128
@MainActor
129
- public var onWebPixelEvent : ( ( ShopifyCheckoutSheetKit . PixelEvent ) -> Void ) ?
129
+ public var onCheckoutWebPixelEvent : ( ( PixelEvent ) -> Void ) ?
130
130
131
131
/// Initialization workaround for passing self to ApplePayAuthorizationDelegate
132
132
private var __authorizationDelegate : ApplePayAuthorizationDelegate !
@@ -186,7 +186,9 @@ protocol PayController: AnyObject {
186
186
return try await handleStorefrontError ( error)
187
187
} catch {
188
188
await authorizationDelegate. transition ( to: . terminalError( error: error) )
189
- await onFail ? ( )
189
+ if let checkoutError = error as? CheckoutError {
190
+ await onCheckoutFail ? ( checkoutError)
191
+ }
190
192
throw error
191
193
}
192
194
}
@@ -241,30 +243,41 @@ protocol PayController: AnyObject {
241
243
242
244
@available ( iOS 17 . 0 , * )
243
245
extension ApplePayViewController : CheckoutDelegate {
244
- @MainActor func checkoutDidComplete( event _: ShopifyCheckoutSheetKit . CheckoutCompletedEvent ) {
245
- onComplete ? ( )
246
+ func checkoutDidComplete( event: CheckoutCompletedEvent ) {
247
+ Task { @MainActor in
248
+ self . onCheckoutComplete ? ( event)
249
+ }
246
250
}
247
251
248
- @MainActor func checkoutDidFail( error _: ShopifyCheckoutSheetKit . CheckoutError ) {
249
- onFail ? ( )
252
+ func checkoutDidFail( error: CheckoutError ) {
253
+ Task { @MainActor in
254
+ self . onCheckoutFail ? ( error)
255
+ }
250
256
}
251
257
252
- @MainActor func checkoutDidCancel( ) {
253
- /// x right button on CSK doesn't dismiss automatically
254
- checkoutViewController? . dismiss ( animated: true )
255
-
256
- onCancel ? ( )
258
+ func checkoutDidCancel( ) {
259
+ Task { @MainActor in
260
+ /// x right button on CSK doesn't dismiss automatically
261
+ checkoutViewController? . dismiss ( animated: true )
262
+ self . onCheckoutCancel ? ( )
263
+ }
257
264
}
258
265
259
- @MainActor func shouldRecoverFromError( error: ShopifyCheckoutSheetKit . CheckoutError ) -> Bool {
260
- return onShouldRecoverFromError ? ( error) ?? false
266
+ func shouldRecoverFromError( error: CheckoutError ) -> Bool {
267
+ return MainActor . assumeIsolated {
268
+ self . onShouldRecoverFromError ? ( error) ?? false
269
+ }
261
270
}
262
271
263
- @MainActor func checkoutDidClickLink( url: URL ) {
264
- onClickLink ? ( url)
272
+ func checkoutDidClickLink( url: URL ) {
273
+ Task { @MainActor in
274
+ self . onCheckoutClickLink ? ( url)
275
+ }
265
276
}
266
277
267
- @MainActor func checkoutDidEmitWebPixelEvent( event: ShopifyCheckoutSheetKit . PixelEvent ) {
268
- onWebPixelEvent ? ( event)
278
+ func checkoutDidEmitWebPixelEvent( event: PixelEvent ) {
279
+ Task { @MainActor in
280
+ self . onCheckoutWebPixelEvent ? ( event)
281
+ }
269
282
}
270
283
}
0 commit comments