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.2.3 app/models/solidus_bling/token.rb
solidus_bling-3.2.2 app/models/solidus_bling/token.rb
solidus_bling-3.2.1 app/models/solidus_bling/token.rb
solidus_bling-3.2 app/models/solidus_bling/token.rb
solidus_bling-3.1.1 app/models/solidus_bling/token.rb
solidus_bling-3.1.0 app/models/solidus_bling/token.rb
solidus_bling-3.0.3 app/models/solidus_bling/token.rb