Skip to content

Commit 08987d0

Browse files
committed
docs(ios): explanation on double events in faq
1 parent 823a754 commit 08987d0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/docs/guides/faq.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,37 @@ const handlePurchaseError = (error) => {
254254
};
255255
```
256256

257+
### I sometimes see both a success and an error for one subscription purchase
258+
259+
This can briefly happen due to StoreKit 2 event ordering and native background work. If you've already received a success and processed it, you can safely ignore a transient error that arrives shortly afterwards.
260+
261+
Tip (dedupe in app logic):
262+
263+
```tsx
264+
const lastSuccessAtRef = useRef(0);
265+
266+
const { finishTransaction } = useIAP({
267+
onPurchaseSuccess: async (purchase) => {
268+
lastSuccessAtRef.current = Date.now();
269+
await finishTransaction({ purchase, isConsumable: false });
270+
},
271+
onPurchaseError: (error) => {
272+
if (error.code === 'E_USER_CANCELLED') return;
273+
if (error.code === 'E_SERVICE_ERROR') {
274+
const dt = Date.now() - lastSuccessAtRef.current;
275+
if (dt >= 0 && dt < 1500) return; // Ignore spurious error
276+
}
277+
Alert.alert('Purchase Failed', error.message);
278+
},
279+
});
280+
```
281+
282+
Because of this timing model, all request* APIs (e.g., `requestPurchase`) are event-driven, not promise-based:
283+
284+
- `requestPurchase()` does not resolve with a result. It triggers the native flow and you must handle outcomes via `onPurchaseSuccess`/`onPurchaseError` (when using `useIAP`) or `purchaseUpdatedListener`/`purchaseErrorListener`.
285+
- Avoid relying on `await requestPurchase(...)` for the final outcome; multiple events and inter-session completions are possible.
286+
- This design ensures your app remains robust when the store delivers updates after app restarts or in edge timing cases.
287+
257288
### How do I handle network errors during purchases?
258289

259290
Network errors during purchases are tricky because the purchase might still go through. Always:

0 commit comments

Comments
 (0)