lib/active_merchant/billing/gateways/alelo.rb in activemerchant-1.133.0 vs lib/active_merchant/billing/gateways/alelo.rb in activemerchant-1.137.0

- old
+ new

@@ -108,12 +108,22 @@ headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded' } - parsed = parse(ssl_post(url('captura-oauth-provider/oauth/token'), post_data(params), headers)) - Response.new(true, parsed[:access_token], parsed) + begin + raw_response = ssl_post(url('captura-oauth-provider/oauth/token'), post_data(params), headers) + rescue ResponseError => e + raise OAuthResponseError.new(e) + else + response = parse(raw_response) + if (access_token = response[:access_token]) + Response.new(true, access_token, response) + else + raise OAuthResponseError.new(response) + end + end end def remote_encryption_key(access_token) response = parse(ssl_get(url('capture/key'), request_headers(access_token))) Response.new(true, response[:publicKey], response) @@ -142,13 +152,15 @@ key: key, uuid: uuid, access_token: access_token, multiresp: multiresp.responses.present? ? multiresp : nil } - rescue ResponseError => error + rescue ActiveMerchant::OAuthResponseError => e + raise e + rescue ResponseError => e # retry to generate a new access_token when the provided one is expired - raise error unless try_again && %w(401 404).include?(error.response.code) && @options[:access_token].present? + raise e unless retry?(try_again, e, :access_token) @options.delete(:access_token) @options.delete(:encryption_key) ensure_credentials false end @@ -204,18 +216,24 @@ 'encryption_uuid' => credentials[:uuid] }) multiresp.process { resp } multiresp + rescue ActiveMerchant::OAuthResponseError => e + raise OAuthResponseError.new(e) rescue ActiveMerchant::ResponseError => e # Retry on a possible expired encryption key - if try_again && %w(401 404).include?(e.response.code) && @options[:encryption_key].present? + if retry?(try_again, e, :encryption_key) @options.delete(:encryption_key) commit(action, body, options, false) else res = parse(e.response.body) Response.new(false, res[:messageUser] || res[:error], res, test: test?) end + end + + def retry?(try_again, error, key) + try_again && %w(401 404).include?(error.response.code) && @options[key].present? end def success_from(action, response) case action when 'capture/transaction/refund'