[in_app_purchase_storekit] Fix consumable repurchase issue in StoreKit2
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:
- The inish() method would never call its completion handler if the transaction wasn't found in Transaction.all
- This caused the Dart side to hang indefinitely
- 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
- [ ] I read the Contributor Guide and followed the process outlined there for submitting PRs.
- [ ] I read the Tree Hygiene page, which explains my responsibilities.
- [ ] I read and followed the relevant style guides and ran the auto-formatter.
- [ ] I signed the CLA.
- [ ] The title of the PR starts with the name of the package surrounded by square brackets, e.g.
[shared_preferences] - [ ] I linked to at least one issue that this PR fixes in the description above.
- [ ] I updated
pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under[^1]. - [ ] I updated
CHANGELOG.mdto add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under[^1]. - [ ] I updated/added any relevant documentation (doc comments with
///). - [ ] I added new tests to check the change I am making, or I have commented below to indicate which test exemption this PR falls under[^1].
- [ ] All existing and new tests are passing.
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.
Even using dependency_overrides to use your fixed code, I still can't make a second purchase. Am I missing something?
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.