Sha256: 5e4b24aef2b1ad871eba8a085805301aace339fab01ac60ad76fc237bdb2250c

Contents?: true

Size: 1.26 KB

Versions: 27

Compression:

Stored size: 1.26 KB

Contents

module SolidusBling
  class Token < ::Ac::Base
    BASE_URL = "https://www.bling.com.br/Api/v3"

    def authorize bling_account, code
      basic_encoded = Base64.strict_encode64("#{bling_account.client_id}:#{bling_account.client_secret}")
      headers = {
        "Content-Type": "application/x-www-form-urlencoded",
        Authorization: "Basic #{basic_encoded}"
      }
      body = {
        code: code,
        grant_type: "authorization_code"
      }
      response_json = post("/oauth/token", headers:, body:).json
      bling_account.update(
        access_token: response_json["access_token"],
        token_expires_in: DateTime.now.utc + response_json["expires_in"].seconds,
        refresh_token: response_json["refresh_token"]
      )
    rescue
      false
    end

    def refresh bling_account
      response_json = BlingApi::Client.new.refresh_token(
        client_id: bling_account.client_id,
        client_secret: bling_account.client_secret,
        refresh_token: bling_account.refresh_token
      )
      bling_account.update(
        access_token: response_json["access_token"],
        token_expires_in: DateTime.now.utc + response_json["expires_in"].seconds,
        refresh_token: response_json["refresh_token"]
      )
    rescue
      false
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
solidus_bling-3.4.7 app/models/solidus_bling/token.rb
solidus_bling-3.4.6 app/models/solidus_bling/token.rb
solidus_bling-3.4.5 app/models/solidus_bling/token.rb
solidus_bling-3.4.4 app/models/solidus_bling/token.rb
solidus_bling-3.4.3 app/models/solidus_bling/token.rb
solidus_bling-3.4.2 app/models/solidus_bling/token.rb
solidus_bling-3.4.1 app/models/solidus_bling/token.rb
solidus_bling-3.4.0 app/models/solidus_bling/token.rb
solidus_bling-3.3.6 app/models/solidus_bling/token.rb
solidus_bling-3.3.5 app/models/solidus_bling/token.rb
solidus_bling-3.3.3 app/models/solidus_bling/token.rb
solidus_bling-3.3.2 app/models/solidus_bling/token.rb
solidus_bling-3.3.1 app/models/solidus_bling/token.rb
solidus_bling-3.3.0 app/models/solidus_bling/token.rb
solidus_bling-3.2.9 app/models/solidus_bling/token.rb
solidus_bling-3.2.8 app/models/solidus_bling/token.rb
solidus_bling-3.2.7 app/models/solidus_bling/token.rb
solidus_bling-3.2.6 app/models/solidus_bling/token.rb
solidus_bling-3.2.5 app/models/solidus_bling/token.rb
solidus_bling-3.2.4 app/models/solidus_bling/token.rb