Skip to content

Instantly share code, notes, and snippets.

@nubbel
Forked from jkotchoff/google_play_verification.rb
Last active November 29, 2018 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nubbel/85241ef1afac2902804a to your computer and use it in GitHub Desktop.
Save nubbel/85241ef1afac2902804a to your computer and use it in GitHub Desktop.
The API for in-app purchases changed with version v1.1, see: https://developers.google.com/android-publisher/v1_1/inapppurchases/get
class GooglePlayVerification
require 'google/api_client'
# Refer:
# https://code.google.com/p/google-api-ruby-client/issues/detail?id=72
# and
# http://jonathanotto.com/blog/google_oauth2_api_quick_tutorial.html
# and
# http://milancermak.wordpress.com/2012/08/24/server-side-verification-of-google-play-subsc/
GOOGLE_KEY = 'xxx-xxx.apps.googleusercontent.com'
GOOGLE_SECRET = 'xxx'
ACCESS_TOKEN = 'xxx'
REFRESH_TOKEN = '1/4Qxxx'
APP_NAME = 'xxx'
APP_VERSION = '1.0.1'
def self.google_api_client
@@google_client ||= Google::APIClient.new(
auto_refresh_token: true,
application_name: APP_NAME,
application_version: APP_VERSION
).tap do |client|
client.authorization.client_id = GOOGLE_KEY
client.authorization.client_secret = GOOGLE_SECRET
client.authorization.access_token = ACCESS_TOKEN
client.authorization.refresh_token = REFRESH_TOKEN
end
end
# ie. https://developers.google.com/android-publisher/v1/
# eg.
# @package_name com.stocklight.stocklightapp
# @product_id com.stocklight.stocklight.standardsubscription
# @purchase_token xxx
def self.verify_subscription(package_name, product_id, purchase_token)
client = self.google_api_client
# Verify whether the transaction_receipt is valid
publisher = client.discovered_api('androidpublisher', 'v1.1')
api_method = publisher.inapppurchases.get
client.execute(api_method: api_method, parameters: {
"packageName" => package_name,
"productId" => product_id,
"token" => purchase_token
}).data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment