How to verify an In App Purchase With Flutter?

Viewed 1283

Using the in_app_purchase 0.6.0 plugin for IAP in a Flutter app, I can't find how to verify the purchase anywhere. In the package example at the part where you would verify the purchase it says:

// IMPORTANT!! Always verify a purchase before delivering the product.
// For the purpose of an example, we directly return true.
return Future<bool>.value(true);

I've found you can get verification data from the purchaseDetails & there are 3 parameters available

purchaseDetails.verificationData.serverVerificationData
purchaseDetails.verificationData.localVerificationData
purchaseDetails.verificationData.source

The server & local values are the same when I test, is comparing those to each-other considered the verification? The way the comments in the code are worded it makes it seem like you should pass one of those values and the source (ios or android) to some other verification method. If this is the case what method should be used to verify?

2 Answers

 NOTE: If you are making a subscription purchase/upgrade/downgrade, we recommend you to
 verify the latest status of you your subscription by using server side receipt validation
 and update the UI accordingly. The subscription purchase status shown

this was copied from an example of in_app_purchase 0.6.0 verification process should be on the server purchaseDetails.verificationData.serverVerificationData is a receipt, encoded with base64, you need to send it to the server and server on his side can use it to verify with App/Play store.

Bad news is the IAP package does not provide a direct method to validate purchases from respective appstores; this you must do by using the respective store APIs, it is recommended you store the purchase data in your own server, as you will need to store the purchaseTokens in-order to call the APIs and return correct data for the respective purchases, and if a user deletes the app/gets a new phone or clears data, there is no way to retrieve this info from a user experience point of view (or for handling expired/refunded subscriptions).

I recommend reading the Google flutter IAP tutorial: https://codelabs.developers.google.com/codelabs/flutter-in-app-purchases#9

Specifically the section on purchase handlers, it shows how you can hook into both the google play store and apples storeKit APIs to validate your purchases; the trick is the tutorial is written only for validation on purchase, however you can repurpose the functions to validate on demand.

You will need to be familiar with (or willing to learn) REST API calls and TS to follow along with the tutorial.

Related