Skip to content

Map user-cancelled PlatformException to PurchaseError on purchase flow cancellation #619

@FeanorD

Description

@FeanorD

Description

When a user cancels the subscription purchase flow, _iap.requestPurchase(builder.build()) throws

PurchaseError(
    code: gentype.ErrorCode.ServiceError,
    message: 'Failed to request purchase: ${error.toString()}',
)

when it catches PlatformException(user-cancelled, User cancelled the purchase flow, wo_pledge_v1_monthly, null). I think, in this case, it would be great to map PlatformException to PurchaseError with the right error code, as it provides a code: 'user-cancelled' that could be mapped to ErrorCode.UserCancelled, and a String message.

Expected Behavior

When a user cancels the subscription purchase flow _iap.requestPurchase(...) should return PurchaseError(code: ErrorCode.UserCancelled) instead of a PurchaseError(code: ErrorCode.ServiceError, ...).

Screenshots

Image

Environment

  • flutter_inapp_purchase: 8.2.7
  • Flutter: 3.38.8
  • Dart: 3.10.7
  • Platforms: iOS 15.8 real device
  • iOS deployment target: 15.0
  • Xcode version: 26.2 (17C52)

Latest Version Check

  • Confirmed the issue reproduces on the latest flutter_inapp_purchase

To Reproduce

Steps to reproduce the behavior:

  1. Wrap _iap.requestPurchase(...) call in a try-catch
  2. Call _iap.requestPurchase(...)
  3. Cancel the subscription flow / close the native bottom sheet without purchasing the item
  4. Catch the PurchaseError(code: ErrorCode.ServiceError, ...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions