Home > Enterprise >  (Edited) I am having an issue with my application not giving in-game currency
(Edited) I am having an issue with my application not giving in-game currency

Time:12-29

[Edited - Last time I forgot to add the code]

So i'm having an issue with my code not adding in-game currency to the user after they buy a product. All the internal testing and money work is well- the purchase goes through and the money is received. There is just an issue with adding the currency, which are in this case emblems, to the user. Unfortunately my friend wrote this code who no longer works on the project and I'm not very familiar with any of this, as I normally do UI stuff. Please let me know if I should upload any other images or provide any further information.

I'm not sure how far along it gets in the code before it breaks, but what I do know is using a button for testing and adding the same asyncstorage.getitem and setitem works when an onpress for a button. Again, I am very unfamiliar with all of this stuff so let me know what I need to provide to help.

This code works:

AsyncStorage.getItem('emblems').then(value =>{AsyncStorage.setItem('emblems', '10')})

This code doesn't work

<BuyBox Price='$0.99' EN='1 Emblem' onPress={async () => {
                    if (this.state.offerings != null) {
                        const pkg = this.state.offerings.emblems.availablePackages.find(offering => offering.identifier === 'ispy_1_emblems');
                        if (pkg) {
                            const {customerInfo, productIdentifier} = await Purchases.purchasePackage(pkg);
                            if (typeof customerInfo.entitlements.active.my_entitlement_identifier !== "undefined") {
                                console.log("purchase successful")
                                AsyncStorage.getItem('emblems').then(value => {
                                       if (value == null){
                                        AsyncStorage.setItem('emblems', '1');
                                    } else {
                                        AsyncStorage.setItem('emblems', (Number(value)   1).toString());
                                    }
                                })
                            }
                        }
                    }
                }}/>

CodePudding user response:

You forget to mention which library you using. I'm guessing you're using Revenuecat react-native-purchases, you can print pkg in console to see many details including currency

const {customerInfo, productIdentifier} = await Purchases.purchasePackage(pkg);

CodePudding user response:

use try to test it, I think it will giving error for the first time when there's no value on emblems

try {
  const value = await AsyncStorage.getItem("emblems");
  if (value !== null) {
    AsyncStorage.setItem("emblems", (Number(value)   1).toString());
  } else {
    //first time in
    await AsyncStorage.setItem("emblems", "1"); //Set  1
  }
} catch (e) {
  // error reading value
  await AsyncStorage.setItem("emblems", "1");
}
  • Related