question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android] purchaseUpdatedListener firing only once, not on every subscription transaction

See original GitHub issue

Version of react-native-iap

"react-native-iap": "4.5.4",

Version of react-native

"react-native": "0.61.4",

Platforms you faced the error (IOS or Android or both?)

Android, not tested on iOS

Expected behavior

purchaseUpdatedListener firing every time purchase is received

Actual behavior

purchaseUpdatedListener firing only once for first recipt, then nothing happens

Tested environment (Emulator? Real Device?)

Real device, run in dev env with test google account (subscription recurring every 5 minutes - email is incoming but receipt is not).

Simplified code I am using:

import React from 'react';
import { Text, View, TouchableOpacity, Alert } from 'react-native';
import RNIap, {
  purchaseUpdatedListener,
  purchaseErrorListener,
} from 'react-native-iap';

import { SpinnerOverlay } from '../components/SpinnerOverlay';

import { itemSkus } from '../config/in-app';
import { validateRecipt } from '../utils/validateRecipt';
import { logger } from '../utils/logger';

export class SubscriptionScreenInner extends React.Component {
  purchaseUpdateSubscription = null;
  purchaseErrorSubscription = null;

  constructor(props) {
    super(props);
    this.state = {
      products: [],
      productsLoaded: false,
      isProcessing: false
    };
  }

  componentDidMount = async () => {
    try {
      if (itemSkus) {
        await RNIap.initConnection();
        await RNIap.flushFailedPurchasesCachedAsPendingAndroid();
        const products = await RNIap.getSubscriptions(itemSkus);
        this.setState({ products, productsLoaded: true });

        this.purchaseUpdateSubscription = purchaseUpdatedListener(
          async (purchase) => {
            try {
              const receipt = purchase.transactionReceipt;
              if (receipt) {
                const validate = await validateRecipt(purchase);

                if (validate) {
                  await RNIap.finishTransaction(purchase, false);
                  this.props.fetchUserProfile();
                  this.resetScreenState();
                  Alert.alert('receipt validated', 'cool');
                }
              }
            } catch (error) {
              this.resetScreenState();
              logger('Validate recipt error', error);
            }
          }
        );

        this.purchaseErrorSubscription = purchaseErrorListener((error) => {
          this.resetScreenState();
          logger('Purchase error during loading', error);
        });
      }
    } catch (error) {
      logger('Store prroducts error', error);
    }
  };

  componentWillUnmount() {
    if (this.purchaseUpdateSubscription) {
      this.purchaseUpdateSubscription.remove();
    }

    if (this.purchaseErrorSubscription) {
      this.purchaseErrorSubscription.remove();
    }

    RNIap.endConnection();
  }

  requestSubscription = async (sku) => {
    this.setState({ isProcessing: true }, async () => {
      await RNIap.requestSubscription(sku);
    });
  };

  resetScreenState = () => {
    this.setState({ isProcessing: false });
  };

  render() {
    const { products, productsLoaded, isProcessing } = this.state;

    if (!productsLoaded || isProcessing) {
      return (
        <View>
          <SpinnerOverlay />
        </View>
      );
    }

    return (
      <View>
        <View>
          <View>
            <View>
              <Text>Buy monthly subscription</Text>
            </View>

            <View>
              <TouchableOpacity onPress={() => this.requestSubscription(products[0].productId)}>
                <Text>Buy</Text>
              </TouchableOpacity>
            </View>
          </View>
        </View>
      </View>
    );
  }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:37 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
hyochancommented, Oct 26, 2020

Oh, I see. You are having a problem with the next subscription event. Let me test this out and come back.

2reactions
hyochancommented, Oct 31, 2020

Thanks for the reminder! I’ll test this out tomorrow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native In-App Purchases (Android) - YouTube
In this video I am going to explain in detail how to implement in-app purchases for your Android app using the react-native-iap module....
Read more >
react-native-iap - npm
This react-native module will help you access the In-app purchases capabilities of your phone on the Android , iOS platforms and the Amazon ......
Read more >
dooboolab/react-native-iap (Raised $40.00) - Issuehunt
clearTransaction () throwing "Cannot finish a purchasing transaction" ... [Android] purchaseUpdatedListener firing only once, not on every subscription ...
Read more >
Integrate the Android IAP API | In-App Purchasing
If the subscription is continuous and has never been canceled at any point, the app will only receive one receipt for that subscription/customer....
Read more >
React Native: Subscriptions with In-App Purchases - Ross Bulat
From here, iOS will display some prompts for the user to confirm the transaction. If confirmed, the purchaseUpdatedListener event will fire, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found