早上好,开发人员,我一直在尝试使用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>
);
}
有了上面的代码,我可以订阅,但我的用户在三天内得到退款,因为付款是不承认的。我真的需要帮助来解决这个问题。这款应用已经在谷歌的playstore上上架了。
1条答案
按热度按时间bkkx9g8r1#
你的代码有几处不对。您当前的代码是为确认消耗品购买而设置的,对于订阅,参数略有不同。
acknowledgePurchaseAndroid();
需要一个对象,而不是字符串。正确的格式为acknowledgePurchaseAndroid({token: purchase.purchaseToken});
RNIap.finishTransaction(purchase, true);
第二个值用于IAP是否为耗材。订阅的正确格式应该是RNIap.finishTransaction(purchase, false);
,或者如果您想让代码易于阅读,以便将来引用,如果您计划不以变量形式发送值,则应该是RNIap.finishTransaction({purchase:purchase, isConsumable: false}