1

I'm having issues with grabbing solely the Apple Receipt from a promise.

This is the usual response: Promise {_40: 0, _65: 0, _55: null, _72: null} The _55: null contains the receipt whereas null is actually the receipt since the body is too big to be read.

I am also using Ignite-Andross boilerplate for my API calls. They're using apisauce.

I am using react-native-iap by dooboolab. I have tried these options.

  getLatestReceipt = async () => {
    var recentReceipt = await RNIap.requestReceiptIOS();
    this.setState({latestReceipt: recentReceipt});
    // Alert.alert(this.state.latestReceipt);
    console.log(this.state.latestReceipt);
  };

This prints out the current receipt in the console which is correct.

However, when I try to pass over the current receipt, the field is either read as undefined, null, or Promise {_40: 0, _65: 0, _55: null, _72: null}.

  var data = {
    apple_request: [
      {
        subscription_number: "A-S00000001",
        // Apple Receipt
       receipt_id: this.state.latestReceipt
      }
    ]
  };

Another solution that I tried was to JSON.parse(latestReceipt)["_55"] However, the result returned as 0-M, 1-I, 2-I, 3-m, 4-B... It was spitting out each individual receipt index, and I needed the whole value of _55 instead.

Lastly, the only option that worked was to hardcode the whole Apple Receipt in the receipt_id field. I'm looking for a way to avoid this.

Expected output

receipt_id: whatever apple receipt is read

Brian88
  • 11
  • 2
  • sounds like you are using `fetch` and need to `await response.json()` first – John Ruddell Jul 22 '19 at 20:02
  • Looks like I'm trying to mutate an immutable Promise. Is it possible to change an async Promise to sync? Promise {_40: 0, _65: 0, _55: null, _72: null}_40: 0_55: "MIImCAYJKoZIhvcNAQcCoIIl+TCCJfUCAQExCzAJBgUrDgMCGg"_65: 1_72: null} – Brian88 Jul 23 '19 at 18:26

1 Answers1

0

The solution to this issue was that the API call and and function that contained the JSON body needed to both be asynchronous calls. Wrapping:

async function getReceipt() {
    const latestReceipt = await RNIap.requestReceiptIOS();
    api.post(
      url,
      {
        apple_request: [
          {
            subscription: ${subscription_number},
            // Apple Receipt
            receipt_data: latestReceipt
          }
        ]
      },
      {
        headers: {
          "API-Token": ${api_token}
        }
      }
    );
  }

Api async/await call:

 const receiptCall = async () => await getReceipt(); 
Brian88
  • 11
  • 2