genesis_ruby 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -0
  3. data/Gemfile.lock +50 -41
  4. data/README.md +20 -0
  5. data/VERSION +1 -1
  6. data/lib/genesis_ruby/api/constants/transactions/parameters/non_financial/billing_api/order_by_fields.rb +33 -0
  7. data/lib/genesis_ruby/api/constants/transactions/parameters/non_financial/billing_api/response_fields.rb +67 -0
  8. data/lib/genesis_ruby/api/constants/transactions/parameters/non_financial/billing_api/transaction_types.rb +48 -0
  9. data/lib/genesis_ruby/api/constants/transactions/parameters/non_financial/sort_directions.rb +24 -0
  10. data/lib/genesis_ruby/api/constants/transactions/parameters/online_banking/payment_types.rb +3 -0
  11. data/lib/genesis_ruby/api/constants/transactions/parameters/refund/credit_reason_indicators.rb +30 -0
  12. data/lib/genesis_ruby/api/constants/transactions/parameters/refund/ticket_change_indicators.rb +24 -0
  13. data/lib/genesis_ruby/api/constants/transactions.rb +5 -0
  14. data/lib/genesis_ruby/api/mixins/requests/financial/bank_attributes.rb +26 -0
  15. data/lib/genesis_ruby/api/mixins/requests/financial/cards/installment_attributes.rb +18 -0
  16. data/lib/genesis_ruby/api/mixins/requests/non_financial/billing_api/sort_attributes.rb +18 -0
  17. data/lib/genesis_ruby/api/mixins/requests/non_financial/date_attributes.rb +64 -0
  18. data/lib/genesis_ruby/api/mixins/requests/non_financial/paging_attributes.rb +33 -0
  19. data/lib/genesis_ruby/api/mixins/requests/restricted_setter.rb +18 -0
  20. data/lib/genesis_ruby/api/request.rb +21 -1
  21. data/lib/genesis_ruby/api/requests/base/graphql.rb +60 -0
  22. data/lib/genesis_ruby/api/requests/base/versioned.rb +88 -0
  23. data/lib/genesis_ruby/api/requests/financial/capture.rb +6 -3
  24. data/lib/genesis_ruby/api/requests/financial/cards/authorize.rb +22 -19
  25. data/lib/genesis_ruby/api/requests/financial/cards/authorize3d.rb +26 -23
  26. data/lib/genesis_ruby/api/requests/financial/cards/sale.rb +22 -19
  27. data/lib/genesis_ruby/api/requests/financial/cards/sale3d.rb +26 -23
  28. data/lib/genesis_ruby/api/requests/financial/online_banking_payments/banco_do_brasil.rb +60 -0
  29. data/lib/genesis_ruby/api/requests/financial/online_banking_payments/eps.rb +47 -0
  30. data/lib/genesis_ruby/api/requests/financial/online_banking_payments/idebit/pay_in.rb +55 -0
  31. data/lib/genesis_ruby/api/requests/financial/online_banking_payments/idebit/pay_out.rb +28 -0
  32. data/lib/genesis_ruby/api/requests/financial/online_banking_payments/wechat.rb +53 -0
  33. data/lib/genesis_ruby/api/requests/financial/refund.rb +32 -12
  34. data/lib/genesis_ruby/api/requests/financial/sdd/recurring/init_recurring_sale.rb +23 -0
  35. data/lib/genesis_ruby/api/requests/financial/sdd/recurring/recurring_sale.rb +28 -0
  36. data/lib/genesis_ruby/api/requests/financial/sdd/refund.rb +26 -0
  37. data/lib/genesis_ruby/api/requests/financial/sdd/sale.rb +61 -0
  38. data/lib/genesis_ruby/api/requests/financial/wallets/alipay.rb +49 -0
  39. data/lib/genesis_ruby/api/requests/non_financial/billing_api/transactions.rb +203 -0
  40. data/lib/genesis_ruby/api/requests/non_financial/installments/fetch.rb +59 -0
  41. data/lib/genesis_ruby/api/requests/non_financial/installments/show.rb +58 -0
  42. data/lib/genesis_ruby/api/requests/non_financial/reconcile/date_range.rb +2 -50
  43. data/lib/genesis_ruby/builder.rb +7 -0
  44. data/lib/genesis_ruby/builders/graphql.rb +118 -0
  45. data/lib/genesis_ruby/builders/json.rb +30 -0
  46. data/lib/genesis_ruby/configuration.rb +11 -2
  47. data/lib/genesis_ruby/dependencies.rb +3 -0
  48. data/lib/genesis_ruby/network/adapter/net_http_adapter.rb +22 -6
  49. data/lib/genesis_ruby/network/base_network.rb +1 -14
  50. data/lib/genesis_ruby/parser.rb +3 -0
  51. data/lib/genesis_ruby/parsers/json.rb +18 -0
  52. data/lib/genesis_ruby/utils/formatters/response/formats/timestamp.rb +1 -1
  53. data/lib/genesis_ruby/utils/options/api_config.rb +28 -0
  54. data/lib/genesis_ruby/utils/options/network_adapter_config.rb +49 -14
  55. data/lib/genesis_ruby/utils/transactions/financial_types.rb +1 -1
  56. data/lib/genesis_ruby/utils/transactions/references/refundable_types.rb +1 -1
  57. data/lib/genesis_ruby/utils/transactions/wpf_types.rb +1 -1
  58. data/lib/genesis_ruby/version.rb +1 -1
  59. metadata +32 -3
  60. /data/lib/genesis_ruby/api/requests/financial/wallets/{pay_pay.rb → pay_pal.rb} +0 -0
@@ -0,0 +1,59 @@
1
+ module GenesisRuby
2
+ module Api
3
+ module Requests
4
+ module NonFinancial
5
+ module Installments
6
+ # Fetch the applicable installments for a given amount, currency and card brand.
7
+ class Fetch < Base::Versioned
8
+
9
+ attr_reader :amount, :card_number
10
+ attr_accessor :currency
11
+
12
+ # Installments Fetch initialization
13
+ def initialize(configuration, _builder_interface = nil)
14
+ super configuration
15
+
16
+ self.request_path = 'installments'
17
+ end
18
+
19
+ # Amount of transaction in minor currency unit
20
+ def amount=(value)
21
+ raise InvalidArgumentError, 'Invalid Amount given, only integer value allowed!' unless value.is_a? Integer
22
+
23
+ @amount = value
24
+ end
25
+
26
+ # Complete cc number of customer
27
+ def card_number=(value)
28
+ value = value.to_s
29
+
30
+ raise InvalidArgumentError, 'Invalid Credit Card number given!' unless value =~ /\A[0-9]{13,19}\Z/
31
+
32
+ @card_number = value
33
+ end
34
+
35
+ protected
36
+
37
+ # Fetch request field validations
38
+ def init_field_validations
39
+ super
40
+
41
+ required_fields.push *%i[amount currency card_number]
42
+ field_values.merge! currency: Constants::Currencies::Iso4217.all.map(&:upcase)
43
+ end
44
+
45
+ # Installments Fetch request structure
46
+ def request_structure
47
+ {
48
+ amount: amount,
49
+ currency: currency,
50
+ card_number: card_number
51
+ }
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,58 @@
1
+ module GenesisRuby
2
+ module Api
3
+ module Requests
4
+ module NonFinancial
5
+ module Installments
6
+ # Retrieves details about a specific installment based on its ID.
7
+ class Show < Base::Versioned
8
+
9
+ attr_writer :installment_id
10
+
11
+ # Installments Show initialization
12
+ def initialize(configuration, _builder_interface = nil)
13
+ super configuration
14
+
15
+ self.request_path = 'installments/:installment_id'
16
+ end
17
+
18
+ # Installment identifier
19
+ def installment_id
20
+ @installment_id.to_s
21
+ end
22
+
23
+ protected
24
+
25
+ # Override default Versioned Network configuration
26
+ def init_configuration
27
+ super
28
+
29
+ init_get_configuration
30
+ end
31
+
32
+ # Fetch request field validations
33
+ def init_field_validations
34
+ super
35
+
36
+ required_fields.push *%i[installment_id]
37
+ end
38
+
39
+ # Installments Show request structure
40
+ def request_structure
41
+ {}
42
+ end
43
+
44
+ # Override API endpoint with provided Installment ID
45
+ def process_request_parameters
46
+ super
47
+
48
+ request_path.sub!(':installment_id', installment_id)
49
+
50
+ init_api_gateway_configuration request_path: "#{version}/#{request_path}", include_token: false
51
+ end
52
+
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -8,43 +8,9 @@ module GenesisRuby
8
8
  # The response is paginated, each request will return 100 entries max.
9
9
  class DateRange < Api::Request
10
10
 
11
- attr_reader :page
12
-
13
- # Start of the requested date range (time is optional)
14
- def start_date
15
- format = if start_date_time?
16
- Constants::DateTimeFormats::YYYY_MM_DD_H_I_S
17
- else
18
- Constants::DateTimeFormats::YYYY_MM_DD_ISO_8601
19
- end
20
-
21
- @start_date&.strftime format
22
- end
23
-
24
- # Start of the requested date range (time is optional)
25
- def start_date=(value)
26
- self.start_date_time = Utils::Common.date_has_time? value
27
-
28
- parse_date attribute: __method__, value: value, allow_empty: false
29
- end
30
-
31
- # Start of the requested date range (time is optional)
32
- def end_date
33
- format = if end_date_time?
34
- Constants::DateTimeFormats::YYYY_MM_DD_H_I_S
35
- else
36
- Constants::DateTimeFormats::YYYY_MM_DD_ISO_8601
37
- end
38
-
39
- @end_date&.strftime format
40
- end
11
+ include Api::Mixins::Requests::NonFinancial::DateAttributes
41
12
 
42
- # End of the requested date range (time is optional)
43
- def end_date=(value)
44
- self.end_date_time = Utils::Common.date_has_time? value
45
-
46
- parse_date attribute: __method__, value: value, allow_empty: true
47
- end
13
+ attr_reader :page
48
14
 
49
15
  # The page within the paginated result, defaults to 1
50
16
  def page=(value)
@@ -75,20 +41,6 @@ module GenesisRuby
75
41
  }
76
42
  end
77
43
 
78
- private
79
-
80
- attr_accessor :start_date_time, :end_date_time
81
-
82
- # Start Date has time within the given string
83
- def start_date_time?
84
- self.start_date_time ||= false
85
- end
86
-
87
- # End Date has time within the given string
88
- def end_date_time?
89
- self.end_date_time ||= false
90
- end
91
-
92
44
  end
93
45
  end
94
46
  end
@@ -1,5 +1,7 @@
1
1
  require 'genesis_ruby/builders/xml'
2
2
  require 'genesis_ruby/builders/form'
3
+ require 'genesis_ruby/builders/json'
4
+ require 'genesis_ruby/builders/graphql'
3
5
  require 'genesis_ruby/errors/builder_error'
4
6
 
5
7
  module GenesisRuby
@@ -15,11 +17,16 @@ module GenesisRuby
15
17
  # Builder FORM
16
18
  FORM = 'form'.freeze
17
19
 
20
+ # Builder GraphQL
21
+ GRAPHQL = 'graphql'.freeze
22
+
18
23
  # Initialize the Builder Interface based on the Request requirements
19
24
  def initialize(request_interface)
20
25
  case request_interface
21
26
  when XML then @builder_context = Builders::Xml.new
22
27
  when FORM then @builder_context = Builders::Form.new
28
+ when JSON then @builder_context = Builders::Json.new
29
+ when GRAPHQL then @builder_context = Builders::Graphql.new
23
30
  else
24
31
  raise GenesisRuby::BuilderError, 'Invalid Builder interface!'
25
32
  end
@@ -0,0 +1,118 @@
1
+ require 'genesis_ruby/builders/base'
2
+ require 'json'
3
+
4
+ module GenesisRuby
5
+ module Builders
6
+ # GraphQL builder
7
+ class Graphql < Base
8
+
9
+ # Builder GraphQL constructor
10
+ def initialize
11
+ @document = ''
12
+ super
13
+ end
14
+
15
+ # Outputs generated document
16
+ def output
17
+ @document
18
+ end
19
+
20
+ # Created GraphQL query from provided structure
21
+ def populate_nodes(structure)
22
+ validate_structure(structure)
23
+
24
+ @document = build_query(structure)
25
+ end
26
+
27
+ private
28
+
29
+ # Validates given structure
30
+ def validate_structure(structure)
31
+ raise BuilderError, 'Invalid Hash structure given in Graphql builder!' unless structure.is_a? Hash
32
+
33
+ root_key = structure.keys.first
34
+
35
+ raise BuilderError, 'Missing structure root key in Graphql builder' if root_key.nil?
36
+ raise BuilderError, 'Invalid GraphQL structure given in GraphQL builder' unless structure[root_key].is_a? Hash
37
+ end
38
+
39
+ # Build GraphQL Query
40
+ def build_query(structure)
41
+ ident = ' ' * 2
42
+ root_key = structure.keys.first
43
+ filters = build_filters structure[root_key][:filters]
44
+ response_fields = build_response_fields structure[root_key][:response_fields], ident * 2
45
+
46
+ format("%{root} {\n#{ident}%{action}(%{filters})\n#{ident}{\n%{response_fields}\n#{ident}}\n}",
47
+ root: root_key,
48
+ action: structure[root_key][:action].to_s,
49
+ filters: filters,
50
+ response_fields: response_fields)
51
+ end
52
+
53
+ # Build GraphQL filters
54
+ def build_filters(data)
55
+ out = ''
56
+
57
+ return out unless data.is_a? Hash
58
+
59
+ data.each do |key, value|
60
+ out += splat_hash Hash[key, value]
61
+ out += ', ' unless data.keys.last == key
62
+ end
63
+
64
+ out
65
+ end
66
+
67
+ # Builds given hash in the GraphQL syntax
68
+ # { key: { key1: 'value1', key2: 'value2' } } becomes key: { key1: value1, key2: value2 }
69
+ def splat_hash(data)
70
+ out = ''
71
+ root_key = data.keys.first
72
+
73
+ return out unless data[root_key].is_a? Hash
74
+
75
+ data[root_key].each do |key, value|
76
+ out += format '%{key}: %{value}', key: key.to_s, value: value.to_s
77
+ out += ', ' unless key == data[root_key].keys.last
78
+ end
79
+
80
+ format '%{key}: { %{out} }', key: root_key, out: out
81
+ end
82
+
83
+ # Build GraphQL query response fields
84
+ def build_response_fields(data, ident)
85
+ out = ''
86
+
87
+ return out unless data.is_a? Hash
88
+
89
+ data.each do |key, value|
90
+ out += formatted_array Hash[key, value], ident
91
+ out += "\n" unless key == data.keys.last
92
+ end
93
+
94
+ out
95
+ end
96
+
97
+ # Build given array into formatted GraphQL query items
98
+ # items: [item1 item2 item3] will output
99
+ # items
100
+ # {
101
+ # item1
102
+ # item2
103
+ # item3
104
+ # }
105
+ def formatted_array(data, ident)
106
+ out = ''
107
+ root_key = data.keys.first
108
+
109
+ return out unless data[root_key].is_a? Array
110
+
111
+ out += data[root_key].map { |key| "#{ident} #{key}" }.join("\n")
112
+
113
+ format "#{ident}%{key}\n#{ident}{\n%{out}\n#{ident}}", key: root_key, out: out
114
+ end
115
+
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,30 @@
1
+ require 'genesis_ruby/builders/base'
2
+ require 'json'
3
+
4
+ module GenesisRuby
5
+ module Builders
6
+ # JSON Document builder
7
+ class Json < Base
8
+
9
+ # JSON constructor
10
+ def initialize
11
+ @document = ''
12
+
13
+ super
14
+ end
15
+
16
+ # JSON output string
17
+ def output
18
+ @document
19
+ end
20
+
21
+ # Generate JSON document
22
+ def populate_nodes(structure)
23
+ @document = JSON.pretty_generate structure
24
+ rescue StandardError => e
25
+ raise BuilderError, "Given request structure can not be generated! #{e.message}"
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -20,8 +20,8 @@ module GenesisRuby
20
20
  # Default sanitize Response
21
21
  DEFAULT_SANITIZE_RESPONSE = true
22
22
 
23
- # Genesis basic configuration - credentials, endpoint, environment, token
24
- attr_writer :username, :password, :token, :interface, :format_response, :sanitize_response
23
+ # Genesis basic configuration - credentials, endpoint, environment, token, etc
24
+ attr_writer :username, :password, :token, :interface, :format_response, :sanitize_response, :billing_api_token
25
25
 
26
26
  # Configuration constructor
27
27
  def initialize
@@ -64,6 +64,11 @@ module GenesisRuby
64
64
  @force_smart_routing ||= false
65
65
  end
66
66
 
67
+ # Defines the Bearer token used for authorization in Billing API Service
68
+ def billing_api_token
69
+ @billing_api_token || ''
70
+ end
71
+
67
72
  # Genesis Request Timeout
68
73
  def timeout=(value)
69
74
  timeout = value.to_i
@@ -187,6 +192,10 @@ module GenesisRuby
187
192
  smart_router: {
188
193
  GenesisRuby::Api::Constants::Environments::PRODUCTION => 'prod.api.',
189
194
  GenesisRuby::Api::Constants::Environments::STAGING => 'staging.api.'
195
+ },
196
+ api_service: {
197
+ GenesisRuby::Api::Constants::Environments::PRODUCTION => 'prod.api.',
198
+ GenesisRuby::Api::Constants::Environments::STAGING => 'staging.api.'
190
199
  }
191
200
  }
192
201
  end
@@ -20,6 +20,9 @@ require 'genesis_ruby/api/requests/base/financial'
20
20
  require 'genesis_ruby/api/requests/base/financials/credit_card'
21
21
  require 'genesis_ruby/api/requests/base/financials/south_american_payments'
22
22
  require 'genesis_ruby/api/requests/base/reference'
23
+ require 'genesis_ruby/api/requests/base/versioned'
24
+ require 'genesis_ruby/api/requests/financial/sdd/sale'
25
+ require 'genesis_ruby/api/requests/base/graphql'
23
26
  require 'genesis_ruby/api/notification'
24
27
 
25
28
  # Load Financial and Non Financial API Requests
@@ -22,15 +22,15 @@ module GenesisRuby
22
22
  end
23
23
 
24
24
  # Send the request
25
- def execute
25
+ def execute # rubocop:disable Metrics/AbcSize
26
26
  raise NetworkError, 'Request is not initialized' unless @request
27
27
 
28
28
  safe_execute do
29
29
  case request_data.type
30
30
  when Api::Request::METHOD_POST then @response = @request.post path, request_data.body, headers
31
31
  when Api::Request::METHOD_PUT then @response = @request.put path, request_data.body, headers
32
- else
33
- raise 'Invalid Request Type!'
32
+ when Api::Request::METHOD_GET then @response = @request.get path, headers
33
+ else raise 'Invalid Request Type!'
34
34
  end
35
35
  end
36
36
  end
@@ -90,11 +90,27 @@ module GenesisRuby
90
90
 
91
91
  # Define Headers
92
92
  def headers
93
+ data = common_headers
94
+
95
+ data.merge! payload_headers unless request_data.type == Api::Request::METHOD_GET
96
+ data.merge! request_data.headers if request_data.headers.is_a?(Hash) && !request_data.headers.empty?
97
+
98
+ data
99
+ end
100
+
101
+ # Provides default request headers
102
+ def common_headers
103
+ {
104
+ 'Authorization' => request_data.user_login.to_s,
105
+ 'User-Agent' => request_data.user_agent.to_s
106
+ }
107
+ end
108
+
109
+ # Payload Headers
110
+ def payload_headers
93
111
  {
94
112
  'Content-Type' => request_data.format,
95
- 'Content-Length' => request_data.body&.length.to_s,
96
- 'Authorization' => "Basic #{request_data.user_login}",
97
- 'User-Agent' => request_data.user_agent
113
+ 'Content-Length' => request_data.body&.length.to_s
98
114
  }
99
115
  end
100
116
 
@@ -1,6 +1,4 @@
1
- require 'genesis_ruby/errors/invalid_argument_error'
2
1
  require 'genesis_ruby/utils/options/network_adapter_config'
3
- require 'base64'
4
2
 
5
3
  module GenesisRuby
6
4
  module Network
@@ -75,18 +73,7 @@ module GenesisRuby
75
73
 
76
74
  # Map the Request to the Network Adapter object
77
75
  def adapter_data_mapper(request)
78
- network_adapter_config.map_from_request(request).merge(
79
- {
80
- user_login: build_user_login,
81
- user_agent: network_user_agent,
82
- timeout: configuration.timeout
83
- }
84
- )
85
- end
86
-
87
- # Build the User login string used for the Authorization Header
88
- def build_user_login
89
- Base64.urlsafe_encode64("#{configuration.username}:#{configuration.password}")
76
+ network_adapter_config.map_from_request(request, configuration).merge({ user_agent: network_user_agent })
90
77
  end
91
78
 
92
79
  # The default user agent for every Network
@@ -1,4 +1,5 @@
1
1
  require 'genesis_ruby/parsers/xml'
2
+ require 'genesis_ruby/parsers/json'
2
3
  require 'genesis_ruby/errors/parser_error'
3
4
 
4
5
  module GenesisRuby
@@ -16,6 +17,8 @@ module GenesisRuby
16
17
  case response_interface
17
18
  when XML
18
19
  @parser_context = GenesisRuby::Parsers::Xml.new
20
+ when JSON
21
+ @parser_context = GenesisRuby::Parsers::Json.new
19
22
  else
20
23
  raise GenesisRuby::ParserError, 'Invalid Parser interface!'
21
24
  end
@@ -0,0 +1,18 @@
1
+ require 'genesis_ruby/parsers/base'
2
+ require 'json'
3
+
4
+ module GenesisRuby
5
+ module Parsers
6
+ # JSON Document parser
7
+ class Json < Base
8
+
9
+ # Parse received document
10
+ def parse_document(document)
11
+ @structure = JSON.parse(document, symbolize_names: true)
12
+ rescue StandardError => e
13
+ raise ParserError, "Given JSON string can not be parsed! #{e.message}"
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -11,7 +11,7 @@ module GenesisRuby
11
11
 
12
12
  # The Response keys that the formatter will be applied to
13
13
  def formatting_keys
14
- %w(timestamp)
14
+ %w(timestamp transactionDate valueDate)
15
15
  end
16
16
 
17
17
  # Format the given timestamp to DateTime object
@@ -13,6 +13,8 @@ module GenesisRuby
13
13
  self.type = GenesisRuby::Api::Request::METHOD_POST
14
14
  self.format = Builder::XML
15
15
  self.parser_skip_root_node = true
16
+ self.authorization = GenesisRuby::Api::Request::AUTH_TYPE_BASIC
17
+ self.bearer_token = nil
16
18
  end
17
19
 
18
20
  # Load pre-defined JSON configuration
@@ -22,6 +24,8 @@ module GenesisRuby
22
24
  self.type = GenesisRuby::Api::Request::METHOD_POST
23
25
  self.format = Builder::JSON
24
26
  self.parser_skip_root_node = false
27
+ self.authorization = GenesisRuby::Api::Request::AUTH_TYPE_BASIC
28
+ self.bearer_token = nil
25
29
  end
26
30
 
27
31
  # Load pre-defined FORM configuration
@@ -31,6 +35,30 @@ module GenesisRuby
31
35
  self.type = GenesisRuby::Api::Request::METHOD_POST
32
36
  self.format = Builder::FORM
33
37
  self.parser_skip_root_node = true
38
+ self.authorization = GenesisRuby::Api::Request::AUTH_TYPE_BASIC
39
+ self.bearer_token = nil
40
+ end
41
+
42
+ # Load pre-defined GET configuration
43
+ def load_get_config
44
+ self.protocol = GenesisRuby::Api::Request::PROTOCOL_HTTPS
45
+ self.port = GenesisRuby::Api::Request::PORT_HTTPS
46
+ self.type = GenesisRuby::Api::Request::METHOD_GET
47
+ self.format = Builder::XML
48
+ self.parser_skip_root_node = false
49
+ self.authorization = GenesisRuby::Api::Request::AUTH_TYPE_BASIC
50
+ self.bearer_token = nil
51
+ end
52
+
53
+ # Load pre-defined GraphQL configuration
54
+ def load_graphql_config
55
+ self.protocol = GenesisRuby::Api::Request::PROTOCOL_HTTPS
56
+ self.port = GenesisRuby::Api::Request::PORT_HTTPS
57
+ self.type = GenesisRuby::Api::Request::METHOD_POST
58
+ self.format = Builder::GRAPHQL
59
+ self.parser_skip_root_node = false
60
+ self.authorization = GenesisRuby::Api::Request::AUTH_TYPE_TOKEN
61
+ self.bearer_token = nil
34
62
  end
35
63
 
36
64
  end
@@ -1,4 +1,6 @@
1
1
  require 'genesis_ruby/utils/options/base'
2
+ require 'genesis_ruby/errors/invalid_argument_error'
3
+ require 'base64'
2
4
 
3
5
  module GenesisRuby
4
6
  module Utils
@@ -7,13 +9,17 @@ module GenesisRuby
7
9
  class NetworkAdapterConfig < Base
8
10
 
9
11
  # Map the Adapter configuration from the Request object
10
- def map_from_request(request) # rubocop:disable Metrics/AbcSize
11
- self.body = request.build_document
12
- self.url = request.api_config.url
13
- self.type = request.api_config.type
14
- self.port = request.api_config.port
15
- self.protocol = request.api_config.protocol
16
- self.format = fetch_content_type(request.api_config.format)
12
+ def map_from_request(request, configuration) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
13
+ self.body = request.build_document
14
+ self.url = request.api_config.url
15
+ self.type = request.api_config.type
16
+ self.port = request.api_config.port
17
+ self.protocol = request.api_config.protocol
18
+ self.format = fetch_content_type request.api_config.format
19
+ self.authorization = request.api_config.authorization
20
+ self.user_login = build_user_login request, configuration
21
+ self.setimeout = configuration.timeout
22
+ self.headers = build_additional_headers request
17
23
 
18
24
  self
19
25
  end
@@ -23,17 +29,46 @@ module GenesisRuby
23
29
  # Retrieve the Request data format that must be used as Content-Type header
24
30
  def fetch_content_type(data_format)
25
31
  case data_format
26
- when Builder::XML
27
- 'text/xml'
28
- when Builder::JSON
29
- 'application/json'
30
- when Builder::FORM
31
- 'application/x-www-form-urlencoded'
32
+ when Builder::XML then 'text/xml'
33
+ when Builder::JSON then 'application/json'
34
+ when Builder::FORM then 'application/x-www-form-urlencoded'
35
+ when Builder::GRAPHQL then 'application/graphql'
36
+ else raise InvalidArgumentError, 'Invalid request format type. Allowed are XML, JSON and FORM'
37
+ end
38
+ end
39
+
40
+ # Build the User login string used for the Authorization Header
41
+ def build_user_login(request, configuration)
42
+ auth = request.api_config.authorization
43
+ kind = fetch_authorization_kind auth
44
+
45
+ return "#{kind} #{request.api_config.bearer_token}" if auth == Api::Request::AUTH_TYPE_TOKEN
46
+
47
+ "#{kind} #{Base64.urlsafe_encode64("#{configuration.username}:#{configuration.password}")}"
48
+ end
49
+
50
+ # Fetches the Request authorization kind
51
+ # TODO: Add AUTH_TYPE_NONE, should be used for METHOD CONTINUE request
52
+ def fetch_authorization_kind(auth)
53
+ case auth
54
+ when Api::Request::AUTH_TYPE_BASIC
55
+ 'Basic'
56
+ when Api::Request::AUTH_TYPE_TOKEN
57
+ 'Bearer'
32
58
  else
33
- raise InvalidArgumentError, 'Invalid request format type. Allowed are XML, JSON and FORM'
59
+ raise InvalidArgumentError, 'Invalid Authorization given in the Request. Allowed: BASIC, TOKEN'
34
60
  end
35
61
  end
36
62
 
63
+ # Build additional headers to be added based on the specific request format
64
+ def build_additional_headers(request)
65
+ headers = {}
66
+
67
+ headers.merge! 'Accept' => 'application/json' if request.api_config.format == Builder::GRAPHQL
68
+
69
+ headers
70
+ end
71
+
37
72
  end
38
73
  end
39
74
  end
@@ -11,7 +11,7 @@ module GenesisRuby
11
11
  # Return array containing all available Web Payment Form transaction types
12
12
  def all # rubocop:disable Metrics/MethodLength
13
13
  [
14
- AFRICAN_MOBILE_PAYOUT, AFRICAN_MOBILE_SALE, APPLE_PAY, ARGENCARD, AURA, AUTHORIZE, AUTHORIZE_3D,
14
+ AFRICAN_MOBILE_PAYOUT, AFRICAN_MOBILE_SALE, ALIPAY, APPLE_PAY, ARGENCARD, AURA, AUTHORIZE, AUTHORIZE_3D,
15
15
  BALOTO, BANCOMER, BANCONTACT, BANCO_DE_OCCIDENTE, BANCO_DO_BRASIL, BITPAY_PAYOUT, BITPAY_REFUND,
16
16
  BITPAY_SALE, BOLETO, BRADESCO,
17
17
  CABAL, CAPTURE, CASHU, CENCOSUD, CREDIT,
@@ -12,7 +12,7 @@ module GenesisRuby
12
12
  # Array containing all available Refundable Types
13
13
  def all # rubocop:disable Metrics/MethodLength
14
14
  [
15
- AFRICAN_MOBILE_PAYOUT, AFRICAN_MOBILE_SALE, APPLE_PAY,
15
+ AFRICAN_MOBILE_PAYOUT, AFRICAN_MOBILE_SALE, ALIPAY, APPLE_PAY,
16
16
  BALOTO, BANCOMER, BANCONTACT, BANCO_DE_OCCIDENTE, BANCO_DO_BRASIL, BITPAY_SALE, BOLETO, BRADESCO,
17
17
  CAPTURE, CASHU,
18
18
  DAVIVIENDA,