因此,在我的应用程序中,当用户点击打开这些页面中的任何一个时,我想运行一个功能来检查产品是否已购买,如果没有,则购买产品。我发现了两个包,分别是react-native-iap和expo- in - app -purchase,但没有找到如何实现所需流程的明确说明。
const paidPage = async (productId , navTo) => {
// PASS THE PRODUCT ID AND A FUNCTION THAT NAVIGATE TO THE PAGE
// HERE I WANT TO CHECK IF THE PRODUCT IS PURCHASED, IF YES THEN NAVIGATE TO THE PAGE
// IF NO THEN PURCHASE THE PRODUCT
}发布于 2021-01-14 20:54:50
你必须检查你是否已经购买了该产品,并检查它是否仍然有效。使用react-native-iap包
RNIap.getPurchaseHistory().catch(() => {
console.log("error fetching purchase history")
}).then((res) => {
// here you will get all the purchase history
// filter with the product id for which you want to check
// if record exist that means you have to check its validity
})检查有效性不是直接的。你需要使用下面给出的代码。但是,下面receiptBody中的receipt可以是任何收据,因为response不显示其过期日期。响应有'latest_receipt_info‘,其中包含所有的购买和到期日期,您可以从其中筛选您需要的。
const receiptBody = {
"receipt-data" : receipt,
"password" : "" //password from the App-Specific Shared Secret in appstoreconnect in-app-purchase section for the app
}
await RNIap.validateReceiptIos(receiptBody, true).then((receipt) => {
try {
const renewalHistory = receipt.latest_receipt_info
// it has all purchase with expiry date
// filter for the one you are looking for
} catch (error) {}
})收据可以是上面receiptBody中的任何收据,所以你可以获得当前的收据,这样实现起来很快,如下所示
await RNIap.getReceiptIOS().then((res) => {
currentReceipt = res
})发布于 2021-03-06 01:19:58
您可能希望考虑使用其他第三方库来处理收据处理,这些库为如何处理应用内购买提供了更清晰的方向。
一个例子是NamiML,它有一个关于如何检查活跃购买的指南,这里有一个react原生示例:
https://docs.namiml.com/docs/react-to-changes-to-entitlements
要使用NamiML库进行检查的代码如下所示:
NativeModules.NamiEntitlementManagerBridge.isEntitlementActive("premium_access", (active) => {
if (active) {
// react to entitlement being active
}
});发布于 2021-05-24 22:10:04
对于react原生iap,如果用户购买了应用程序,但他们已经拥有它,你会得到一个购买错误代码"E_ALREADY_OWNED“。您可以使用async storage存储一个变量来检查页面是否已经被购买;如果没有,则转到页面并授予它。
useEffect(() => {
if (currentPurchaseError) {
if (
currentPurchaseError.code === "E_ALREADY_OWNED" &&
!isPagePurchased
) {
setAndStorePagePurchase(true)
}
}
}, [currentPurchaseError])有关如何将其实现为完整的简单应用程序的信息,请参阅this example。
https://stackoverflow.com/questions/63269404
复制相似问题