reactjs - 如何使用 React Native IAP 在 React Native 中确认应用程序订阅
问题描述
早上好,开发人员,在使用 React Native IAP 库在我的应用程序中接受订阅后,我一直在尝试确认 android 的付款,但它似乎不起作用。
我在我的应用程序中使用上下文 API,下面是我的代码:
import React, { createContext, useState,useEffect, useContext, } from 'react';
import RNIap, {
InAppPurchase,
PurchaseError,
SubscriptionPurchase,
acknowledgePurchaseAndroid,
consumePurchaseAndroid,
finishTransaction,
finishTransactionIOS,
purchaseErrorListener,
purchaseUpdatedListener,
} from 'react-native-iap';
import { Alert, Platform, } from 'react-native'
import { Container, Header, Content, Toast, Button, Text } from 'native-base';
export const IApContext = createContext();
const itemSkus = Platform.select({
ios: [
'kriss.once.removeads',
'kriss.sub.removeads'
],
android: [
'com.sublegal.submonthly',
]
});
export const IApController = ({ children }) => {
const [products, setProducts] = useState([])
const [showads, setShowads] = useState(true);
const [wascalled, setWascalled] = useState(false);
const initIAp = async () => {
try {
await RNIap.initConnection();
purchaseUpdateSubscription = purchaseUpdatedListener(
async (purchase= SubscriptionPurchase) => {
console.log('purchase', purchase);
const receipt = purchase.transactionReceipt
? purchase.transactionReceipt
: purchase.originalJson;
console.log(receipt)
if (receipt) {
try {
acknowledgePurchaseAndroid(purchase.purchaseToken);
const ackResult = acknowledgePurchaseAndroid(purchase.purchaseToken);
console.info('ackResult', ackResult);
RNIap.finishTransaction(purchase, true);
} catch (ackErr) {
console.warn('ackErr', ackErr);
}
}
},
);
} catch (err) {
console.warn(err.code, err.message);
}
try {
const products = await RNIap.getProducts(itemSkus);
if (Platform.OS === 'android') {
const subscription = await RNIap.getSubscriptions(itemSkus);
products.push(subscription[0])
}
console.log(products)
setProducts({products});
if (products) {
setWascalled(true)
} else {
setWascalled(false)
}
console.log(wascalled)
} catch (err) {
console.warn(err); // standardized err.code and err.message available
}
}
makeSubscription = async (sku) => {
try {
RNIap.requestSubscription(sku);
toggleAds(false)
purchaseUpdateSubscription = purchaseUpdatedListener(
async (purchase= SubscriptionPurchase) => {
console.log('purchase', purchase);
const receipt = purchase.transactionReceipt
? purchase.transactionReceipt
: purchase.originalJson;
console.log(receipt)
if (receipt) {
try {
acknowledgePurchaseAndroid(purchase.purchaseToken);
const ackResult = acknowledgePurchaseAndroid(purchase.purchaseToken);
console.info('ackResult', ackResult);
RNIap.finishTransaction(purchase, true);
} catch (ackErr) {
console.warn('ackErr', ackErr);
}
}
},
);
} catch (err) {
console.warn(err.code, err.message);
}
}
checkValidPurchase = async () => {
try {
const purchases = await RNIap.getAvailablePurchases();
purchases.forEach(async (purchase) => {
switch (purchase.productId) {
case 'com.sublegal.submonthly':
// await AsyncStorage.setItem('removeadsmonthly', JSON.stringify(res));
toggleAds(false)
console.warn('No 1 purchase is available');
break
// case 'com.myproduct.blegal':
// await AsyncStorage.setItem('removeadsmonthly', JSON.stringify(res));
// toggleAds(false)
// break
case 'com.kriss.remove_ads_monthly':
// await AsyncStorage.setItem('removeadsmonthly', JSON.stringify(res));
toggleAds(false)
break
case 'com.kriss.remove_ad_forever':
// await AsyncStorage.setItem('removeadsmonthly', JSON.stringify(res));
toggleAds(false)
break
default: console.warn('you do not have any purchase');
}
})
} catch (err) {
console.warn(err);
}
}
const toggleAds = value => {
if (value === true) {
setShowads(true);
} else {
setShowads(false);
}
};
return (
<IApContext.Provider value={{
initIAp,
wascalled,
showads,
checkValidPurchase,
products,
makeSubscription,
}}>
{children}
</IApContext.Provider>
);
}
使用上面的代码,我可以进行订阅,但我的用户会在三天内获得退款,因为付款未被确认。我真的需要如何解决这个问题的帮助。该应用程序已经在 google playstore 上。
解决方案
推荐阅读
- html - 如何在我的 img 标签中包含点击图像的索引?
- arrays - JOLT - 将数组拆分为 Nifi 数据库记录的元素
- sql - 从日期列中获取最后 N 个周期,而不考虑周期(年、月、日、小时等)
- rest - 使用 vSphere REST API 将磁盘附加到虚拟机
- c# - 从提交按钮创建 CSV 文件并在创建文件失败时显示错误消息
- c - 关于堆内存中的块对齐
- asp.net - 在每次调用实体时在 EF Core 中填充计算列
- python - 下载的 CSV 文件 - UTF-8-BOM 格式的问题
- r - 附表 R 脚本
- python - Python用空而不是单引号替换包含数值的字段的NaN值,单引号稍后将被视为字符串