Skip to content

Instantly share code, notes, and snippets.

@jkotchoff
Last active July 6, 2023 17:57
Show Gist options
  • Save jkotchoff/5632813 to your computer and use it in GitHub Desktop.
Save jkotchoff/5632813 to your computer and use it in GitHub Desktop.

Revisions

  1. cornflakesuperstar revised this gist May 23, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions google_play_verification.rb
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,8 @@ class GooglePlayVerification
    # 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'
  2. cornflakesuperstar created this gist May 23, 2013.
    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:
    # https://code.google.com/p/google-api-ruby-client/issues/detail?id=72
    # and
    # http://jonathanotto.com/blog/google_oauth2_api_quick_tutorial.html
    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