Using google play voided purchase API in flutter


Google's voided purchase API can be used to find the purchases which are either refunded , cancelled, charged back  by the user and use the same to take action against the purchased item(in-app purchases) in your app.

you can get more information about the API here:

https://developers.google.com/android-publisher/voided-purchases


before using the API you need to set up API access clients:

1) create or use an existing API project in google play console:

=>play console=>menu=>settings=>developer account=>API access

https://play.google.com/console/api-access


2) create a service account and grant it necessary permissions ("view financial reports" permission)


3) create credentials for using the API:

=>google cloud console =><your project>=>API & services=>credentials=>create credentials=>service account

=>ensue to copy the service account credentials in the form:

{
  "private_key_id": ...,
  "private_key": ...,
  "client_email": ...,
  "client_id": ...,
  "type": "service_account"
}



Now, you are all set up to move on to the flutter integration part:

1) we will be using google client libraries for flutter:

https://pub.dev/packages/googleapis


2) defining scopes:

const _scopes = [AndroidPublisherApi.androidpublisherScope];

this scope will allow the service account to access the play console account,you can define more scopes as per your needs...


3) adding service account credentials:

final _credentials = ServiceAccountCredentials.fromJson(r'''
{
"private_key_id": "---",
"private_key": "---",
"client_email": "---",
"client_id": "---",
"type": "service_account"
}
''');

add you private key id,private key,client email and client id from the downloaded file(at the time of creating service account).


4) fetching voided purchases from API:

Future<void> fetchVoidedPurchases() async {

final httpClient = await clientViaServiceAccount(_credentials, _scopes);
try {
final api = AndroidPublisherApi(httpClient);

final voidedPurchases =
await api.purchases.voidedpurchases.list('<your app package name>');
final items = voidedPurchases.voidedPurchases;
print('Received ${items.length} voided purchases:(for last 30 days):');
for (var order in items) {
print(order.orderId);
}

} finally {
httpClient.close();
}


});



that's it, now you can revoke the in-app item from the users who have voided purchases and provide fair experience to all users...


Comments

Popular posts from this blog

Python and C++ program to implement multiplication of 2d array (Matrix multiplication)

How to find and replace a node in Linked List

What is AI (Artificial Intelligence )? and its characteristics