Making payments for Google Play with verification on the server

I want to share the experience of connecting payments with verification on the server to the application from Google Play.

So. We have an application ready for publication ( link ). A payment project ( link ) has also been created and is associated with the application.

Further on the points.
1. You need to go to the Credential tab;
2. Create ClientID as a Web-Application and point redirect_uri to our server (for example, and Callback );
3. Create a key of type ServerKey (you can also with empty data).

On the server, we make the handler of the incoming variable code at .

She will come as a GET request.

Here is an example of processing in the languagePython with data storage in Radish . In this case, the code variable is the incoming data of the GET request to our server.

import requests, redis
Redis = redis.Redis()
data ='',{'code':code,'grant_type':'authorization_code','client_id':client_id,'client_secret':client_secret,'redirect_uri':''})
jdata = data.json()
if 'access_token' in jdata and 'token_type' in jdata and 'expires_in' in jdata:
    if "refresh_token" in jdata:

Next, you need to fill out the "Consent screen" page, as well as activate the "Google Play Android Developer API" API.

Now you need to authorize the service on our server.

Be sure to do this from the account from which the payment project was created.

Next, go to this account using the link , substituting ........ our ClientID.……………………………………

This link will send the code variable to our callback.

After activating the account, we can work with payments.

We transfer the payment data received by the client from Google to the server and verify it on our part.

import requests, redis
Redis = redis.Redis()
access_token = Redis.get('GooglePayAccess')
token_type  = Redis.get('GooglePayType')
if not access_token or not token_type:
			refresh_token = Redis.get('GooglePayRefresh')
			data ='',{'grant_type':'refresh_token','client_id':client_id,'client_secret':client_secret,'refresh_token':refresh_token})
			jdata = data.json()
			if 'access_token' in jdata and 'token_type' in jdata and 'expires_in' in jdata:
				access_token = jdata['access_token']
				token_type = jdata['token_type']
url = '' % (packageName,productId,purchaseToken,api_key)
response = requests.get(url,headers={"Authorization":"%s %s" % (token_type,access_token)})
jdata2 = response.json()

If the data received from the client matches the data from Google, then we can safely charge the user virtual currency.

Have a good sales!

Also popular now: