Skip to content

Instantly share code, notes, and snippets.

@jkotchoff
Last active November 1, 2021 02:58
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jkotchoff/e60fdf048ec443272045 to your computer and use it in GitHub Desktop.
Save jkotchoff/e60fdf048ec443272045 to your computer and use it in GitHub Desktop.
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
@TeTiRoss
Copy link

TeTiRoss commented Sep 5, 2018

Hi! Thank you very much for this gist, especially for explenation how to get credentials.
But, in the Sample usage section you are showing that: subscription = GooglePlaySubscriptionVerification.new(package_name, product_id, purchase_token) will result in Google::Apis::AndroidpublisherV2::SubscriptionPurchase object, but actually you will get a GooglePlaySubscriptionVerification object. To get Google::Apis::AndroidpublisherV2::SubscriptionPurchase you will need to add .verify to GooglePlaySubscriptionVerification.new(package_name, product_id, purchase_token).

@sighmon
Copy link

sighmon commented Feb 21, 2020

To verify a product purchase:

def verify_product
  android_publisher.get_purchase_product @package_name, @product_id, @purchase_token
end

Response looks like this:

Success - #<Google::Apis::AndroidpublisherV3::ProductPurchase:0x00000000000
 @consumption_state=0,
 @developer_payload="",
 @kind="androidpublisher#productPurchase",
 @order_id="<order_id>",
 @purchase_state=0,
 @purchase_time_millis=1582251976970,
 @purchase_type=0>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment