Skip to content

Commit

Permalink
fix: set connected false in endConnection (#2807)
Browse files Browse the repository at this point in the history
### Summary

This PR addresses an issue with the `currentPurchase` status not updating correctly in the library after a successful in-app purchase. The problem occurs because the `purchaseListener` is only connected once when the app is first connected, and not after that. This requires users to repeat the purchase process by killing the app.

### Problem Description

After a successful in-app connection, the store purchase completes, but the `currentPurchase` status does not update. This is likely due to the `purchaseListener` being connected based on the `connected` value inside `useEffect`. The `connected` value changes from `false` to `true` only when the app first connects, and not subsequently, preventing proper subscription to the current order status.

### Solution

To resolve this, I added code inside the `endConnection` method to set `connected` to `false`. This ensures that every subscription updates correctly by changing `connected` to `true` when accessing the order page and to `false` when accessing `endConnection`. This approach has been tested and works as expected on both iOS and Android.

### Related Issue

This issue appears to be related to the following issue: [#2700](#2700).

### Conclusion

I hope this PR helps everyone experiencing this issue. Thank you for considering this improvement.
  • Loading branch information
nej1044 authored Aug 4, 2024
1 parent 5615869 commit 2e76f43
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/hooks/useIAP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const useIAP = (): IAP_STATUS => {
currentPurchase,
currentPurchaseError,
initConnectionError,
setConnected,
setProducts,
setSubscriptions,
setAvailablePurchases,
Expand Down Expand Up @@ -119,7 +120,10 @@ export const useIAP = (): IAP_STATUS => {
);

useEffect(() => {
setConnected(true);

return () => {
setConnected(false);
setCurrentPurchaseError(undefined);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/withIAPContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type IAPContextType = {
currentTransaction?: TransactionSk2;
currentPurchaseError?: PurchaseError;
initConnectionError?: Error;
setConnected: (connected: boolean) => void;
setProducts: (products: Product[]) => void;
setSubscriptions: (subscriptions: Subscription[]) => void;
setPurchaseHistory: (purchaseHistory: Purchase[]) => void;
Expand Down Expand Up @@ -86,6 +87,7 @@ export function withIAPContext<T>(Component: React.ComponentType<T>) {
currentTransaction,
currentPurchaseError,
initConnectionError,
setConnected,
setProducts,
setSubscriptions,
setPurchaseHistory,
Expand All @@ -104,6 +106,7 @@ export function withIAPContext<T>(Component: React.ComponentType<T>) {
currentTransaction,
currentPurchaseError,
initConnectionError,
setConnected,
setProducts,
setSubscriptions,
setPurchaseHistory,
Expand Down

0 comments on commit 2e76f43

Please sign in to comment.