packages icon indicating copy to clipboard operation
packages copied to clipboard

[in_app_purchase_storekit] Fix consumable repurchase issue in StoreKit2

Open ANONYMOUSZED-beep opened this issue 1 month ago • 2 comments

Fixes https://github.com/flutter/flutter/issues/179111

Changes

  • Fixed inish() method to always call the completion handler, even when the transaction is not found
  • Added fallback to Transaction.unfinished when looking up transactions
  • Added proper error handling with PigeonError for transaction finish failures
  • Added tests for non-existent transaction finish and consumable repurchase scenarios

Problem

With StoreKit2, consumable products could only be purchased once because:

  1. The inish() method would never call its completion handler if the transaction wasn't found in Transaction.all
  2. This caused the Dart side to hang indefinitely
  3. Consumable transactions needed to be found in Transaction.unfinished as well

Solution

  • inish() now returns success even if the transaction is not found (it's effectively already complete)
  • etchTransaction() now checks both Transaction.all and Transaction.unfinished
  • Proper error handling added for unexpected failures

Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.

List which issues are fixed by this PR. You must list at least one issue.

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.

ANONYMOUSZED-beep avatar Nov 26 '25 09:11 ANONYMOUSZED-beep

image

Even using dependency_overrides to use your fixed code, I still can't make a second purchase. Am I missing something?

hellohejinyu avatar Dec 05 '25 02:12 hellohejinyu

Thanks for the feedback! After further investigation (including a report from @hellohejinyu that the error-returning approach didn't work), I've updated the implementation to return success instead of error when the transaction is not found.

Rationale:

If a transaction is not in Transaction.unfinished, it means the transaction has already been finished. This is actually a success case — the desired outcome (transaction completed) has been achieved.

Returning an error would break user flows because:

SK2PurchaseDetails.pendingCompletePurchase returns true for all purchased items Apps always call completePurchase() for purchases If the transaction was already auto-finished by StoreKit or finished in a previous session, returning an error would cause the purchase flow to fail The fix still uses Transaction.unfinished as you suggested, but treats "not found" as success — similar to how Apple's Transaction.finish() is designed to be idempotent (safe to call multiple times).

This should now properly allow consumable products to be repurchased after finishing.

ANONYMOUSZED-beep avatar Dec 05 '25 10:12 ANONYMOUSZED-beep