'React Native Expo In App Purchase send custom variable to setPurchaseListener
I am implementing In App Purchase function in Expo bare workflow using Expo-In-App-Purchase library. In their documentation it says you need to implement setPurchaseListener on a global scope.
That's currently how I have it set up in my App.tsx file:
// Set purchase listener
setPurchaseListener(async ({ responseCode, results, errorCode }) => {
if (responseCode === IAPResponseCode.OK) {
for (const purchase of results!) {
if (!purchase.acknowledged) {
const {
orderId,
purchaseToken,
acknowledged,
transactionReceipt,
productId,
}: InAppPurchase = purchase;
// in android, consumeItem should be set to false to acknowledge the purchase
// in iOS this isn't needed because it's already specified in app store connect
const consumeItem = Platform.OS === "ios";
finishTransactionAsync(purchase, consumeItem).then(
(response) => {
//send call to back end
}
);
}
}
} else if (responseCode === IAPResponseCode.USER_CANCELED) {
//Do nothing. User cancelled
} else {
console.warn(
`Something went wrong with the purchase. Received response code ${responseCode} and errorCode ${errorCode}`
);
//catch all for all errors
if (
errorCode === IAPErrorCode.SERVICE_TIMEOUT ||
errorCode === IAPErrorCode.SERVICE_DISCONNECTED ||
errorCode === IAPErrorCode.SERVICE_UNAVAILABLE
) {
showMessage(`Service unavailable (${errorCode})`, true);
} else {
showMessage(
`Something went wrong with the purchase. Error code (${errorCode}). Please contact us with the error code if this persists`,
true
);
}
}
});
My question is if I have this defined in my App.tsx file, how is it possible to send a custom variable to my back end after a user purchases something? For example, someone can be in multiple leagues, they buy something for a particular league that they are in, how can I send the success back to my back end stating that the purchase was made for a particular league?
Would I just need to add multiple setPurchaseListeners? If that's so, is there a way to stop listening after a user navigates away from a page?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
