首页 > 解决方案 > 如何使用 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 上。

标签: reactjsreact-nativereact-hooksin-app-purchasein-app-subscription

解决方案


推荐阅读