【发布时间】:2021-04-24 11:09:09
【问题描述】:
由于缺乏显示所有功能如何链接在一起的示例,我在让应用购买正常工作方面有点挣扎
在我的应用中,我有一个应用内购买,基本上允许用户终生解锁一些受限功能。
所以一开始我想检查用户之前是否购买过该商品。 (通过获取历史记录)
如果他们有,我解锁该功能。
在我在android studio中制作的签名APK文件(android)上,我有以下问题:
-购买永远不会确认(尽管通过 react-native run-android 运行时会确认) - 如果您按两次购买按钮,则会出现“已连接到应用商店”错误 -我认为从签名文件运行时不会获取购买历史记录(尽管我可以在调试模式下在控制台中打印结果)
我不完全确定何时调用 await InAppPurchases.connectAsync();所以这可能是问题的一个潜在来源
所以这是我在“内部应用程序”中的代码。我的 App 组件只是包装在 redux 提供程序组件中的 InnerApp。内部应用包含所有导航堆栈,因此购买监听器应该是全局的。
例如
export default function App (){
...more code
return(
< Provider store={store} >
< InnerApp />
</ Provider >
}
内部应用代码
import * as InAppPurchases from 'expo-in-app-purchases';
export default function InnerApp (){
.....some more code
//gets purchase history
const getHistory = async ()=>{
await InAppPurchases.connectAsync();
let found=false
const { responseCode, results } = await InAppPurchases.getPurchaseHistoryAsync();
if (responseCode === InAppPurchases.IAPResponseCode.OK) {
results.forEach(result => {
if (result.acknowledged) {
found =true
// this is just saving to local storage (in case they are using the app offline)
savePurchaseHistory(true)
}else{
savePurchaseHistory(false)
}
});
}
if( found){
//updates a state in the redux store
dispatch(purchaseIAP() )
}else if(responseCode === IAPResponseCode.USER_CANCELED ){
dispatch(removeIAP() )
savePurchaseHistory(false)
}
await InAppPurchases.disconnectAsync();
}
//listens for purchases
const setUpIAP = async() => {
// Set purchase listener
await InAppPurchases.connectAsync();
await InAppPurchases.setPurchaseListener(({ responseCode, results, errorCode }) => {
// Purchase was successful
if (responseCode === InAppPurchases.IAPResponseCode.OK) {
results.forEach(purchase => {
if (!purchase.acknowledged) {
// Process transaction here and unlock content...
dispatch(purchaseIAP() )
// Then when you're done
InAppPurchases.finishTransactionAsync(purchase, false);
}
});
}
// Else find out what went wrong
if (responseCode === InAppPurchases.IAPResponseCode.USER_CANCELED) {
} else if (responseCode === InAppPurchases.IAPResponseCode.DEFERRED) {
console.log('User does not have permissions to buy but requested parental approval (iOS only)');
} else {
console.warn(`Something went wrong with the purchase. Received errorCode ${errorCode}`);
}
});
}
//The in app stuff is called when the component is mounted
useEffect(() => {
setUpIAP()
getHistory()
}, [ ] })
在我的应用程序中,我还有一个按钮,按下时会调用以下函数
const unlockModes = async () => {
try {
const items = Platform.select({
ios: [
'dev.products.all_modes'
],
android: ['all_modes'],
});
await connectAsync();
const products = await InAppPurchases.getProductsAsync(items);
if (products.results.length > 0) {
await InAppPurchases.purchaseItemAsync("all_modes");
}
} catch (err) {
alert("error occured while trying to purchase: " + err);
}
};
【问题讨论】:
-
似乎主要问题是在签名的 APK 中似乎没有解决 connectAsync() 但不知道为什么
标签: react-native expo