lib/payoneer/client.rb in payoneer-client-0.4 vs lib/payoneer/client.rb in payoneer-client-0.4.1

- old
+ new

@@ -64,21 +64,23 @@ } } } encoded_credentials = 'Basic ' + Base64.encode64("#{configuration.username}:#{configuration.api_password}").chomp - response = RestClient.post "#{configuration.json_base_uri}/payouts", params.to_json, content_type: 'application/json', accept: :json, Authorization: encoded_credentials - raise ResponseError.new(code: response.code, body: response.body) if response.code != 200 - hash = JSON.parse(response.body) - hash['PaymentID'] = hash['payout_id'] # Keep consistent with the normal payout response body + begin + response = RestClient.post "#{configuration.json_base_uri}/payouts", params.to_json, content_type: 'application/json', accept: :json, Authorization: encoded_credentials - if hash.key?('Code') - Response.new(hash['Code'], hash['Description']) - else - hash = block_given? ? yield(hash) : hash - Response.new(Response::OK_STATUS_CODE, hash) + hash = JSON.parse(response.body) + hash['PaymentID'] = hash['payout_id'] # Keep consistent with the normal payout response body + + create_response(hash) + rescue RestClient::Exception => e + if e.http_body + hash = JSON.parse(e.http_body) + create_response(hash, e.http_code) + end end end def payout_details(payee_id:, payment_id:) post('GetPaymentStatus', p4: payee_id, p5: payment_id) @@ -102,9 +104,18 @@ if hash.key?('Code') Response.new(hash['Code'], hash['Description']) else hash = block_given? ? yield(hash) : hash Response.new(Response::OK_STATUS_CODE, hash) + end + end + + def create_response(hash, http_code = Response::OK_STATUS_CODE) + if hash.key?('Code') + Response.new(hash['Code'], hash['Description']) + else + hash = block_given? ? yield(hash) : hash + Response.new(http_code, hash) end end end end