How to verify purchase for android app in server side (google play in app billing v3)

前端 未结 6 486
再見小時候
再見小時候 2020-11-30 16:41

I have a simple app (needs user login with account). I provide some premium features for paid users, like more news content.

I need to record if the user has bought

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 17:00

    The documentation on this is confusing and weirdly verbose with the things that are almost inconsequential while leaving the actually important documentation almost unlinked and super hard to find. This should work great on most popular server platform that can run the google api client libraries, including Java, Python, .Net, and NodeJS, among others. Note: I've tested only the Python api client as shown below.

    Necessary steps:

    1. Make an API project, from the API Access link in your Google Play console

    2. Make a new service account, save the JSON private key that gets generated. You'll need to take this file to your server.

    3. Press Done in the Play console's service account section to refresh and then grant access to the service account

    4. Go get a google api client library for your server platform from https://developers.google.com/api-client-library

    5. Use your particular platform's client library to build a service interface and directly read the result of your purchase verification.

    You do not need to bother with authorization scopes, making custom requests calls, refreshing access tokens, etc. the api client library takes care of everything. Here's a python library usage example to verify a subscription:

    First, install the google api client in your pipenv like this:

    $ pipenv install google-api-python-client
    

    Then you can set up api client credentials using the private key json file for authenticating the service account.

    credentials = service_account.Credentials.from_service_account_file("service_account.json")
    

    Now you can verify subscription purchases or product purchases using the library, directly.

    #Build the "service" interface to the API you want
    service = googleapiclient.discovery.build("androidpublisher", "v3", credentials=credentials)
    
    #Use the token your API got from the app to verify the purchase
    result = service.purchases().subscriptions().get(packageName="your.app.package.id", subscriptionId="sku.name", token="token-from-app").execute()
    #result is a python object that looks like this ->
    # {'kind': 'androidpublisher#subscriptionPurchase', 'startTimeMillis': '1534326259450', 'expiryTimeMillis': '1534328356187', 'autoRenewing': False, 'priceCurrencyCode': 'INR', 'priceAmountMicros': '70000000', 'countryCode': 'IN', 'developerPayload': '', 'cancelReason': 1, 'orderId': 'GPA.1234-4567-1234-1234..5', 'purchaseType': 0}
    

    The documentation for the platform service interface for the play developer API is not linked in an easy to find way, for some it is downright hard to find. Here are the links for the popular platforms that I found:

    Python | Java | .NET | PHP | NodeJS (Github TS) | Go (Github JSON)

提交回复
热议问题