-
-
Notifications
You must be signed in to change notification settings - Fork 641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I receive a popup saying, "You already own this item." #2852
Comments
I've already had this issue. My app crashed once after buying the product and before finishing the transaction. The fact that the transaction wasn't properly finished kept the item as an already owned non-consumable. Hope it helps |
@cuchillitos How did you solve this problem? |
@RouberR , it’s been a while since I worked on this, but I think the solution might be in handling the last transaction. Here’s what I did as far as I can remember: For example :
I hope it helps ! |
@cuchillitos Yes, thank you, I did it in a similar way and everything worked. |
Description
I am facing an issue with consumable product purchases. When a user purchases an item and kills the app before receiving a response, the payment is completed, but the data is not retrieved from the available purchases and purchase history.
Expected Behavior
get response from purchase history and finish the transection
Screenshots
See the attachment
Environment:
To Reproduce
Steps to reproduce the behavior:
This is my code:
import { Platform } from "react-native";
import Auth from "../Auth";
import {
clearTransactionIOS, finishTransaction,
flushFailedPurchasesCachedAsPendingAndroid,
getAvailablePurchases, getPurchaseHistory,
requestPurchase
} from "react-native-iap";
import sendEventToWeb from "../SendEventToWeb/SendEventToWeb";
const IAP_function = async (productId, info) => {
let data = JSON.parse(info)
const params = Platform.select({
ios: {
sku: productId,
andDangerouslyFinishTransactionAutomaticallyIOS: false
},
android: {
skus: [productId]
}
});
try {
const currentPurchase = await requestPurchase(params);
if (currentPurchase) {
var dictJSON1 = {
...currentPurchase,
...data,
action: "inapppurchase_response",
device_type: Platform.OS == 'ios' ? '2' : '1',
packageName: 'com.my.app',
gotResponseFrom: 'Normal Flow'
};
console.log("Normal Flow ==>", dictJSON1);
sendEventToWeb(dictJSON1);
}
};
const handlePurchaseError = async (error, productId) => {
let data = await Auth.getItem(productId)
data = data != null ? JSON.parse(data) : { isCancelled: true }
var dictJSON1 = {
...data,
action: "inapppurchase_response",
device_type: Platform.OS == 'ios' ? '2' : '1',
packageName: 'com.my.app',
...error,
};
sendEventToWeb(dictJSON1);
await Auth.removeItem(productId)
};
const ClearTransections = async (purchase) => {
try {
if (purchase?.transactionId || purchase?.purchaseToken || Platform.OS == 'ios') {
console.log("finishTransaction purchase -=>> ", purchase)
await finishTransaction({ purchase: purchase, isConsumable: true });
await Auth.removeItem(purchase.productId)
flushTransactions()
}
else if (purchase["0"]) {
console.log("finishTransaction purchase[0] -=>> ", purchase["0"])
await finishTransaction({ purchase: purchase["0"], isConsumable: true });
await Auth.removeItem(purchase["0"].productId)
flushTransactions()
}
else if (purchase == null) {
availablePurchases()
}
} catch (e) {
console.log("Clear Transections Failure ==>", e);
}
}
const flushTransactions = async () => {
if (Platform.OS === "android") {
await flushFailedPurchasesCachedAsPendingAndroid();
} else {
await clearTransactionIOS();
}
};
const fetchPurchaseHistory = async () => {
try {
const purchaseHistory = await getPurchaseHistory();
if (purchaseHistory.length > 0) {
purchaseHistory?.map(async item => {
let data = await Auth.getItem(item.productId)
data = data != null ? JSON.parse(data) : { isCancelled: true }
var dictJSON1 = {
...item,
...data,
action: "inapppurchase_response",
device_type: Platform.OS == 'ios' ? '2' : '1',
packageName: 'com.my.app',
gotResponseFrom: 'From Purchase History'
};
if (!data.isCancelled) {
console.log("fetchPurchaseHistory==>", dictJSON1);
sendEventToWeb(dictJSON1);
}
})
}
} catch (error) {
console.error("Error fetching purchase history: ", error);
}
};
const availablePurchases = async (call = 0) => {
try {
const purchaseHistory = await getAvailablePurchases();
if (purchaseHistory.length > 0) {
purchaseHistory?.map(async item => {
let data = await Auth.getItem(item.productId)
data = data != null ? JSON.parse(data) : { isCancelled: true }
var dictJSON1 = {
...item,
...data,
action: "inapppurchase_response",
device_type: Platform.OS == 'ios' ? '2' : '1',
packageName: 'com.my.app',
gotResponseFrom: 'From available Purchases'
};
if (!data.isCancelled) {
console.log("availablePurchases==>", dictJSON1);
sendEventToWeb(dictJSON1);
}
})
} else {
console.log("No pending transactions found.");
if (call == 1) {
fetchPurchaseHistory()
} else {
flushTransactions()
}
}
} catch (error) {
console.error("Error fetching purchase history: ", error);
}
};
The text was updated successfully, but these errors were encountered: