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