Use this page with AI

Copy this into your coding assistant and add your request.

Read https://openiap.dev/docs/events/purchase-error-listener and https://openiap.dev/llms.txt. Follow the reading instructions, detailed reference, and linked guides relevant to my task before making changes.
Inspect my existing project and reuse its framework and conventions. Ask me for missing product decisions. Implement the requested behavior and run the applicable checks.
Show the working result, the commands and actual test results, and any remaining limitations. Keep your explanation brief.

My request: [describe what customers should be able to do]
See an example request →

purchaseErrorListener

Fired when a purchase fails or is cancelled by the user.

Listener Setup

swift
// AsyncSequence approach
var purchaseErrors: AsyncStream<PurchaseError>

// Combine approach
var purchaseErrorPublisher: AnyPublisher<PurchaseError, Never>

Registers a listener for purchase error events.

swift
import OpenIap

let subscription = OpenIapModule.shared.purchaseErrorListener { error in
    Task {
        print("Purchase error: \(error.code) - \(error.message)")

        switch error.code {
        case .userCancelled:
            // User cancelled - no action needed
            break
        case .alreadyOwned:
            // Restore purchases instead
            try await OpenIapModule.shared.restorePurchases()
        case .networkError:
            showRetryDialog()
        default:
            showErrorMessage(error.message)
        }
    }
}

// Cleanup when done
OpenIapModule.shared.removeListener(subscription)

Error Payload

The error event delivers a PurchaseError object with error details. See Error Codes for complete reference.

Error Handling Strategy

Handle errors based on their error codes:

  • UserCancelled - No action required
  • ItemUnavailable - Check product availability
  • NetworkError - Retry with backoff
  • AlreadyOwned - Restore purchases
  • PurchaseVerificationFailed - Retry verification