Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fadhlirahim/131beaf568c666271738f70895539a06 to your computer and use it in GitHub Desktop.
Save fadhlirahim/131beaf568c666271738f70895539a06 to your computer and use it in GitHub Desktop.
Verifying an Android subscription in a Ruby on Rails app using the Google Play API
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
# @subscription_id com.stocklight.stocklight.standardsubscription
# @purchase_token xxx
def self.verify_subscription(package_name, subscription_id, purchase_token)
client = self.google_api_client
# Verify whether the transaction_receipt is valid
subscriptions = client.discovered_api('androidpublisher', 'v1')
api_method = subscriptions.purchases.get
purchases = client.execute(api_method: api_method, parameters: {
"packageName" => package_name,
"subscriptionId" => subscription_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