Skip to content

Instantly share code, notes, and snippets.

@jkotchoff
Last active November 1, 2021 02:58

Revisions

  1. jkotchoff revised this gist Apr 23, 2019. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions google_play_verification.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # This class shows uses version 0.9.x of the ruby google-api-client gem circa July 2016
    # This class shows uses version 0.28.7 of the ruby google-api-client gem circa April 2019
    # to query the Google Play subscription API.
    #
    # If using an older version of the google-api-client gem (ie. version 0.8.x), instead refer to:
    @@ -20,7 +20,7 @@
    #
    # https://developers.google.com/android-publisher/api-ref/purchases/subscriptions
    class GooglePlaySubscriptionVerification
    require 'google/apis/androidpublisher_v2'
    require 'google/apis/androidpublisher_v3'
    require 'googleauth'
    require 'googleauth/stores/file_token_store'

    @@ -50,7 +50,7 @@ class GooglePlaySubscriptionVerification
    ACCESS_TOKEN = 'abcd'
    REFRESH_TOKEN = 'abcd'

    Androidpublisher = Google::Apis::AndroidpublisherV2
    Androidpublisher = Google::Apis::AndroidpublisherV3

    # @param package_name - refers to the Google Play package name for the app
    # eg. 'com.stocklight.stocklightapp'
    @@ -76,6 +76,7 @@ def verify
    def android_publisher
    android_publisher = Androidpublisher::AndroidPublisherService.new.tap do |publisher|
    publisher.authorization = client
    publisher.authorization.fetch_access_token!
    end
    end

  2. jkotchoff revised this gist Jul 22, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions google_play_verification.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    # This class shows uses version 0.9.x of the ruby google-api-client gem circa July 2016
    # to query the Google Play subscription API.
    #
    # If using an older version of the google-api-client gem (ie. version 0.8.x), instead refer to:
    # https://gist.github.com/jkotchoff/e60fdf048ec443272045/e3e2c867633900d9d6f53de2de13aa0a0a16bb03
    #
    # Sample usage:
    #
    # package_name = 'com.stocklight.stocklightapp'
  3. jkotchoff revised this gist Jul 22, 2016. 1 changed file with 78 additions and 33 deletions.
    111 changes: 78 additions & 33 deletions google_play_verification.rb
    Original file line number Diff line number Diff line change
    @@ -1,46 +1,91 @@
    class GooglePlayVerification
    require 'google/api_client'
    # This class shows uses version 0.9.x of the ruby google-api-client gem circa July 2016
    # to query the Google Play subscription API.
    #
    # Sample usage:
    #
    # package_name = 'com.stocklight.stocklightapp'
    # product_id = 'com.stocklight.stocklight.standardsubscription'
    # purchase_token = 'kmigoi....4YuSQtU8U'
    # subscription = GooglePlaySubscriptionVerification.new(package_name, product_id, purchase_token)
    #
    # => #<Google::Apis::AndroidpublisherV2::SubscriptionPurchase:0x007fbcb3b89698 @auto_renewing=false,
    # @cancel_reason=0, @country_code="AU", @developer_payload="", @expiry_time_millis="1468673994725",
    # @kind="androidpublisher#subscriptionPurchase", @price_amount_micros="9490000", @price_currency_code="AUD",
    # @start_time_millis="1458133289294">
    #
    # subscription_cancelled = subscription.cancel_reason.present?
    #
    # https://developers.google.com/android-publisher/api-ref/purchases/subscriptions
    class GooglePlaySubscriptionVerification
    require 'google/apis/androidpublisher_v2'
    require 'googleauth'
    require 'googleauth/stores/file_token_store'

    # Refer: http://jonathanotto.com/blog/google_oauth2_api_quick_tutorial.html
    # These credentials come from creating an OAuth Web Application client ID
    # in the Google developer console
    #
    # refer: https://www.youtube.com/watch?v=hfWe1gPCnzc
    #
    # ie.
    # > visit http://console.developers.google.com
    # > API Manager
    # > Credentials
    # > Create Credentials (OAuth client ID)
    # > Application type: Web Application
    # > Authorised redirect URIs: https://developers.google.com/oauthplayground
    # * the resultant client ID / client secret goes in the following GOOGLE_KEY / GOOGLE_SECRET variables
    # > visit: https://developers.google.com/oauthplayground/
    # > Click the settings icon to show the OAuth 2.0 configuration
    # > Tick 'Use your own OAuth credentials'
    # > Enter the OAuth Client ID and OAuth Client secret that you have just created
    # > Check the entry for 'Google Play Developer API v2' in the scopes field and click 'Authorize APIs'
    # > Click 'Allow'
    # > Click 'Exchange authorization code for tokens'
    # * the resultant Refresh token and Access token go in the following REFRESH_TOKEN / ACCESS_TOKEN variables
    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)

    Androidpublisher = Google::Apis::AndroidpublisherV2

    # @param package_name - refers to the Google Play package name for the app
    # eg. 'com.stocklight.stocklightapp'
    #
    # @param product_id - refers to the id of the subscription type being checked
    # eg. 'com.stocklight.stocklight.standardsubscription'
    #
    # @param purchase_token - the purchase token receipt
    # eg. 'kmigoi....4YuSQtU8U'
    def initialize(package_name, product_id, purchase_token)
    @package_name = package_name
    @product_id = product_id
    @purchase_token = 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
    # Throws Google::Apis::ClientError if an invalid parameter is provided
    def verify
    android_publisher.get_purchase_subscription @package_name, @product_id, @purchase_token
    end

    private

    def android_publisher
    android_publisher = Androidpublisher::AndroidPublisherService.new.tap do |publisher|
    publisher.authorization = client
    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
    def client(scopes = [Androidpublisher::AUTH_ANDROIDPUBLISHER])
    Signet::OAuth2::Client.new(
    authorization_uri: 'https://accounts.google.com/o/oauth2/auth',
    token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
    client_id: GOOGLE_KEY,
    client_secret: GOOGLE_SECRET,
    access_token: ACCESS_TOKEN,
    refresh_token: REFRESH_TOKEN,
    scope: scopes,
    )
    end

    end
  4. cornflakesuperstar created this gist Sep 15, 2014.
    46 changes: 46 additions & 0 deletions google_play_verification.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    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