我正在尝试在Ionic React中实现来自https://github.com/j3k0/cordova-plugin-purchase的InAppPurchase 2。
似乎ready事件从未被调用过。
下面是我的代码:
....
import { IAPProduct, InAppPurchase2 as iap } from "@ionic-native/in-app-purchase-2";
const App: React.FC = () => {
useEffect(() => {
const init = async () => {
await initInAppPurchase();
setInitialized(true);
}
init();
}, [])
....
}
export const initInAppPurchase = () => {
if (isPlatform('android') || isPlatform('ios')) {
iap.verbosity = iap.DEBUG;
iap.register({
id: "com.mysoftwares.posapp.test",
alias: "Test",
type: iap.NON_CONSUMABLE
});
iap.when("com.mysoftwares.posapp.test").updated((product: IAPProduct) => {
if (product.owned)
console.log('Product owned')
else
console.log('Product not owned')
});
iap.when("com.mysoftwares.posapp.test").approved(function (product: any) {
product.finish();
});
iap.ready(() => {
alert("Product ready!")
let product = iap.get('Test');
alert(product);
if (product.canPurchase) {
iap.order('Test');
}
})
iap.refresh();
}
}以下是debug verbose的日志:
[store.js] DEBUG: state: com.mysoftwares.posapp.test -> registered
[store.js] DEBUG: store.trigger -> triggering action refreshed
InAppBilling[js]: setup ok
InAppBilling[js]: load ["com.mysoftwares.posapp.test"]
InAppBilling[js]: listener: {"type":"ready","data":{}}
[store.js] DEBUG: store.trigger -> triggering action refresh-finished
InAppBilling[js]: setup ok
InAppBilling[js]: load ["com.mysoftwares.posapp.test","com.mysoftwares.posapp.test"]
InAppBilling[js]: listener: {"type":"ready","data":{}}
InAppBilling[js]: setup ok
InAppBilling[js]: load ["com.mysoftwares.posapp.test","com.mysoftwares.posapp.test","com.mysoftwares.posapp.test"]
InAppBilling[js]: listener: {"type":"ready","data":{}}
InAppBilling[js]: setup ok我正在使用React Hooks。我已经在Google Play Console中发布了我的应用程序,并在Google Play Console中的In-app Products部分添加了测试产品。目前我的应用程序在open testing phase上。
我之前在没有Ionic的情况下用纯Apache Cordova编写了一个应用程序,它工作得很好,但React中的这个就不行了。
我的代码出了什么问题?请帮帮我。
发布于 2021-10-28 03:49:27
我正在模拟器上测试它,这是愚蠢的。在移动设备上,它工作得很好。
https://stackoverflow.com/questions/69733252
复制相似问题