Skip to content

Conversation

@markmur
Copy link
Contributor

@markmur markmur commented Aug 13, 2025

🚨 Breaking Changes

  1. Removed Deprecated Callback Methods

The following methods have been completely removed:

  • .onComplete { }
  • .onCancel { }
  • .onFail { }
  1. New CheckoutDelegate Pattern

Use .checkout(delegate:) to handle checkout flow events:

  class MyCheckoutDelegate: CheckoutDelegate {
      func checkoutDidComplete(event: CheckoutCompletedEvent) {
          // Handle successful checkout
      }

      func checkoutDidFail(error: CheckoutError) {
          // Handle checkout process failures
      }

      func checkoutDidCancel() {
          // Handle checkout cancellation
      }
  }
  1. New Validation Error Handling

.onError { } now handles validation errors only (not checkout failures):

  public enum AcceleratedCheckoutError: Error {
      /// Cart validation failed - API correctly rejected input data
      case validation(ValidationError)
  }

Migration Guide

✅ New API:

+  class MyDelegate: CheckoutDelegate {
+      func checkoutDidComplete(event: CheckoutCompletedEvent) {
+          CartManager.shared.resetCart()
+          print("Order created: \(event.orderDetails.id)")
+      }
+
+      func checkoutDidFail(error: CheckoutError) {
+          print("Checkout failed: \(error.localizedDescription)")
+          if !error.isRecoverable {
+              // Handle unrecoverable error
+          }
+      }
+
+      func checkoutDidCancel() {
+          print("Checkout cancelled")
+      }
+  }

  AcceleratedCheckoutButtons(cartID: cartId)
-     .onComplete { event in
-          // Reset cart on successful checkout
-          CartManager.shared.resetCart()
-      }
-      .onFail { error in
-          print("Checkout failed: \(error)")
-      }
-      .onCancel {
-          print("Checkout cancelled")
-      }
+      .checkout(delegate: MyDelegate())
+      .onError { error in
+          switch error {
+          case .validation(let validationError):
+              print("Validation failed with \(validationError.userErrors.count) errors:")
+              for userError in validationError.userErrors {
+                  print("• \(userError.message) at field: \(userError.field?.joined(separator: ".") ?? "unknown")")
+                  print("  Code: \(userError.code ?? "N/A")")
+              }
+          }
      }

New Features

Enhanced Validation Error Information

The new ValidationError provides detailed field-level validation feedback:

  .onError { error in
      if case .validation(let validationError) = error {
          // Access individual validation errors
          for userError in validationError.userErrors {
              print("Field: \(userError.field?.joined(separator: ".") ?? "unknown")")
              print("Message: \(userError.message)")
              print("Code: \(userError.code ?? "N/A")")
          }

          // Utility methods for easier error handling
          if error.hasValidationErrorCode("INVALID_EMAIL") {
              // Handle specific validation error
          }

          // Get all error messages at once
          let allMessages = error.validationMessages
      }
  }

Separation of Concerns

  • .checkout(delegate:): Handles checkout flow events (completion, failure, cancellation)
  • .onError { }: Handles validation errors that occur before checkout starts
  • CheckoutDelegate.checkoutDidFail(): Handles checkout process failures
  • Validation errors: Input data rejected by API (field validation, etc.)

Benefits

  1. Better Error Context: Validation errors now provide field-specific information
  2. Cleaner API: Single delegate pattern instead of multiple callback methods
  3. Type Safety: Clear distinction between validation and checkout errors
  4. Debugging: Enhanced error information for merchant debugging
  5. Consistency: Aligns with existing CheckoutSheetKit delegate patterns

Before you merge

Important

  • I've added tests to support my implementation
  • I have bumped the version number in the podspec.
  • I have added a Changelog entry.

@markmur markmur force-pushed the markmur/capture-user-errors branch 3 times, most recently from 251f9b1 to 2cc8c5b Compare August 14, 2025 09:41
@markmur markmur changed the title Remove checkoutURL argument from validateUserErrors function Refactor Error Handling for AcceleratedCheckouts Aug 14, 2025
@markmur markmur force-pushed the markmur/capture-user-errors branch 4 times, most recently from 1a55808 to df467ff Compare August 14, 2025 15:30
@markmur markmur marked this pull request as ready for review August 14, 2025 15:34
@markmur markmur requested a review from a team as a code owner August 14, 2025 15:34
@markmur markmur force-pushed the markmur/capture-user-errors branch 2 times, most recently from 8026017 to 93aa91f Compare August 15, 2025 09:55
@markmur markmur force-pushed the markmur/capture-user-errors branch from 1b406e0 to d5fac78 Compare August 18, 2025 09:53
let validationError = ValidationError(userErrors: [
ValidationError.UserError(message: "Test error", code: "TEST")
])
errorAction(AcceleratedCheckoutError.validation(validationError))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think AI is spoofing these tests to make it look like it's testing the behaviour

@markmur markmur force-pushed the markmur/capture-user-errors branch from 457d6e4 to c7efbad Compare August 18, 2025 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants