Last active
November 1, 2015 01:27
-
-
Save oojikoo-gist/157a75197cd52c28d10e to your computer and use it in GitHub Desktop.
rails: google play verification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GooglePlayVerification | |
require 'google/api_client' | |
# Refer: http://jonathanotto.com/blog/google_oauth2_api_quick_tutorial.html | |
GOOGLE_KEY = 'xxxcc.apps.googleusercontent.com' | |
GOOGLE_SECRET = 'abcd' | |
ACCESS_TOKEN = 'abcd' | |
REFRESH_TOKEN = 'abcd' | |
APP_NAME = 'StockLight' | |
APP_VERSION = '1.0.1' | |
def self.process_receipt(transaction_receipt) | |
package_name = transaction_receipt.package_name | |
subscription_id = transaction_receipt.product_id | |
purchase_token = transaction_receipt.purchase_token | |
self.verify_subscription(package_name, subscription_id, purchase_token) | |
end | |
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/ | |
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 | |
# https://gist.github.com/jkotchoff/5632813 | |
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