Skip to content

Commit

Permalink
Add support for Merchant Report (#54)
Browse files Browse the repository at this point in the history
* Add new API method, Bambora::V1::Reports::Merchants.get_all

* Add new API method, Bambora::V1::Reports::Merchants.get

* Add custom error classes

* Raise error for unauthenticated request

* Raise error for invalid request

* Raise an error for .get when passing the wrong argument
  • Loading branch information
harrylewis authored Mar 11, 2024
1 parent c62fa42 commit 114f49d
Show file tree
Hide file tree
Showing 4 changed files with 483 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/bambora/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

require 'bambora/client/version'
require 'bambora/credentials'
require 'bambora/errors'

# Adapters
require 'bambora/adapters/response'
Expand Down Expand Up @@ -45,6 +46,7 @@
require 'bambora/bank/payment_profile_resource'
require 'bambora/bank/batch_report_messages'
require 'bambora/bank/batch_report_resource'
require 'bambora/v1/reports/merchants'

module Bambora
##
Expand Down
36 changes: 36 additions & 0 deletions lib/bambora/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module Bambora
# All library-specific errors inherit from this error class. This helps make it easy for integrations to rescue
# specifically from errors raised by this library.
class Error < StandardError
attr_reader :payload

def initialize(message = nil, payload = {})
@message = message
@payload = payload

super(
<<~ERROR
#{@message}
#{JSON.pretty_generate(@payload).gsub("\n", "\n ")}
ERROR
)
end
end

# An error returned when the API returns an authentication failure.
class InvalidAuthenticationError < Error
def initialize(payload = {})
super(nil, payload)
end
end

# An error returned when the API returns an invalid request.
class InvalidRequestError < Error
def initialize(payload = {})
super(nil, payload)
end
end
end
39 changes: 39 additions & 0 deletions lib/bambora/v1/reports/merchants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module Bambora
module V1
module Reports
class Merchants
def self.get(merchant_id, options = {})
credentials = options[:credentials]

raise ArgumentError, "#{self}.get takes an integer argument" unless merchant_id.is_a?(Integer)

response =
Bambora::Rest::JSONClient
.new(base_url: 'https://api.na.bambora.com', merchant_id: credentials.merchant_id)
.get(path: "/v1/reports/merchants/#{merchant_id}", api_key: credentials.reporting_passcode)

raise InvalidAuthenticationError, response if response[:message] == 'Authentication failed'

raise InvalidRequestError, response if response == { Message: 'The request is invalid.' }

response
end

def self.get_all(options = {})
credentials = options[:credentials]

response =
Bambora::Rest::JSONClient
.new(base_url: 'https://api.na.bambora.com', merchant_id: credentials.merchant_id)
.get(path: '/v1/reports/merchants', api_key: credentials.reporting_passcode)

raise InvalidAuthenticationError, response if response[:message] == 'Authentication failed'

response
end
end
end
end
end
Loading

0 comments on commit 114f49d

Please sign in to comment.