useEffect(() => {
const init = async () => {
try {
if (Platform.OS === "android") {
await Purchases.configure({ apiKey: APIKeys.android });
} else {
await Purchases.configure({ apiKey: APIKeys.ios });
}
Purchases.setLogLevel(LOG_LEVEL.DEBUG);
Purchases.addCustomerInfoUpdateListener((customerInfo) => {
console.log(
"CustomerInfo update received:",
JSON.stringify(customerInfo, null, 2)
);
updateCustomerInfo(customerInfo);
});
await loadOfferings();
} catch (error) {
console.error("Error initializing Purchases:", error);
} finally {
setLoading(false);
}
};
init();
// Cleanup function
return () => {
// Remove the listener when component unmounts
Purchases.removeCustomerInfoUpdateListener();
};
}, ]);
// load offerings a user can purchase
const loadOfferings = async () => {
try {
const offerings = await Purchases.getOfferings();
console.log("offerings", offerings);
const currentOffering = offerings.current;
if (currentOffering) {
setPackages(currentOffering.availablePackages);
console.log("Packages", currentOffering.availablePackages 0].product);
setPricePerMonthString(
currentOffering.availablePackagesu0].product.pricePerMonthString
);
} else {
console.log("No current offering available");
}
} catch (error) {
console.error("Error loading offerings:", error);
console.error("Error details:", JSON.stringify(error, null, 2));
}
};
// purchase a package
const purchasePackage = async (pack) => {
try {
const purchaseMade = await Purchases.purchasePackage(pack);
console.log("purchaseMade", purchaseMade);
Toast.show({
type: "success",
text1: "Purchase successful",
text2: "Thank you for your purchase",
});
} catch (error) {
console.error("Error purchasing package:", error);
console.error("Error details:", JSON.stringify(error, null, 2));
Toast.show({
type: "error",
text1: "Error purchasing package",
text2: error.message,
});
}
};
const updateCustomerInfo = async (customerInfo) => {
console.log("Checking entitlements:", customerInfo?.entitlements?.active);
if (customerInfo?.entitlements?.active<"Pro Features"] !== undefined) {
console.log(
"User has Pro Features entitlement",
customerInfo?.entitlements?.activem"Pro Features"],
typeof customerInfo?.entitlements?.activev"Pro Features"]
);
Toast.show({
type: "success",
text1: "user updated info",
text2: "Thank you for your purchase",
});
await updateUserSubscription();
} else {
console.log("User does not have Pro Features entitlement");
}
};
// restore purchases
const restorePurchases = async () => {
try {
const restoredInfo = await Purchases.restorePurchases();
console.log("restoredInfo", restoredInfo);
Toast.show({
type: "success",
text1: "Purchases restored",
text2: "Your purchases have been restored",
});
} catch (error) {
console.error("Error restoring purchases:", error);
console.error("Error details:", JSON.stringify(error, null, 2));
Toast.show({
type: "error",
text1: "Error restoring purchases",
text2: error.message,
});
}
};
const updateUserSubscription = async () => {
// const currentToken = await AsyncStorage.getItem("token");
if (!token) {
console.error("No token available for subscription update", token);
return;
}
try {
const response = await axios.post(
`${API_URL}subscription/subscribe`,
{},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
console.log("Backend subscription update response:", response.data);
if (response.data.subscriptionActive) {
// const res = await checkSubscription();
// console.warn("calling from update", res);
Toast.show({
type: "success",
text1: "Subscription activated",
text2: "Your account has been updated",
});
}
} catch (error) {
console.error(
"Subscription update failed:",
error.response?.data || error.message
);
Toast.show({
type: "error",
text1: "Update failed",
text2: "Please try again or contact support",
});
}
};
i need help guys..
trying to update my user state in the backend once purchase is successful and also use purchase event listener to listen for new purchase ….
but the event listener is calling the update customer info which updates my backend unnecessarily … im stuck …
my first app btw pls go easy