voltrue2 / in-app-purchase

A Node.js module for in-App-Purchase for iOS, Android, Amazon and Windows.
http://iap.gracenode.org
Other
1.04k stars 287 forks source link

Apple: getting receipt data if IAP list is empty #299

Closed triplef closed 4 years ago

triplef commented 4 years ago

We have a use case where we want to process Apple receipts without IAPs and access metadata like "original_application_version".

Unfortunately the following code in apple.js currently causes the library to throw an exception for this case: https://github.com/voltrue2/in-app-purchase/blob/65b058f0c764fcbe7f5c86082bea3dddb2474f90/lib/apple.js#L377-L390

Is there a way to call iap.validate() with such a receipt and still access the data.receipt field? It is passed as the second parameter to the callback in line 389 above, but I haven’t found a way to access it, as the "catch" block will only allow you to reference the first (error) parameter.

Thanks!

triplef commented 4 years ago

I was able to achieve the above by passing my own callback function to validate() like this:

const validatedData = await new Promise((resolve, reject) => {
  iap.validate(
    receipt,
    (error, response) => {
      if (error) {
        error.validatedData = response; // attach validation data to error
        reject(error);
      } else {
        resolve(response);
      }
    }
  );
});