Class: Nihaopay::Transactions::Base

Inherits:
Object
  • Object
show all
Includes:
Api, Queryable
Defined in:
lib/nihaopay/transactions/base.rb

Direct Known Subclasses

Authorize, Capture

Constant Summary

Constants included from Queryable

Queryable::TIME_FORMAT

Constants included from Api

Api::LIVE_HOST, Api::TEST_HOST, Api::VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Queryable

included

Methods included from Api

included

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



10
11
12
# File 'lib/nihaopay/transactions/base.rb', line 10

def initialize(attributes = {})
  assign_attributes(attributes)
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



8
9
10
# File 'lib/nihaopay/transactions/base.rb', line 8

def amount
  @amount
end

#capturedObject

Returns the value of attribute captured.



8
9
10
# File 'lib/nihaopay/transactions/base.rb', line 8

def captured
  @captured
end

#currencyObject

Returns the value of attribute currency.



8
9
10
# File 'lib/nihaopay/transactions/base.rb', line 8

def currency
  @currency
end

#noteObject

Returns the value of attribute note.



8
9
10
# File 'lib/nihaopay/transactions/base.rb', line 8

def note
  @note
end

#referenceObject

Returns the value of attribute reference.



8
9
10
# File 'lib/nihaopay/transactions/base.rb', line 8

def reference
  @reference
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/nihaopay/transactions/base.rb', line 7

def status
  @status
end

#timeObject

Returns the value of attribute time.



8
9
10
# File 'lib/nihaopay/transactions/base.rb', line 8

def time
  @time
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/nihaopay/transactions/base.rb', line 7

def token
  @token
end

#transaction_idObject

Returns the value of attribute transaction_id.



7
8
9
# File 'lib/nihaopay/transactions/base.rb', line 7

def transaction_id
  @transaction_id
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/nihaopay/transactions/base.rb', line 7

def type
  @type
end

Class Method Details

.build(options = {}) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/nihaopay/transactions/base.rb', line 54

def build(options = {})
  options = Nihaopay::HashUtil.symbolize_keys(options)
  attributes = Nihaopay::HashUtil.slice(options, *valid_attributes)
  attributes[:token] ||= merchant_token
  attributes[:time] = attributes[:time] ? Time.parse(attributes[:time]) : Time.now
  response_keys_map.each { |k, v| attributes[v] = options[k] }
  new(attributes)
end

.build_from_response!(response) ⇒ Object



49
50
51
52
# File 'lib/nihaopay/transactions/base.rb', line 49

def build_from_response!(response)
  validate_resource!(response)
  build(response.parsed_response)
end

.request_body(params = {}) ⇒ Object



45
46
47
# File 'lib/nihaopay/transactions/base.rb', line 45

def request_body(params = {})
  params.map { |k, v| "#{k}=#{v}" }.join('&')
end

.request_headersObject



41
42
43
# File 'lib/nihaopay/transactions/base.rb', line 41

def request_headers
  authorization.merge('Content-Type' => 'application/x-www-form-urlencoded')
end

.response_keys_mapObject



67
68
69
# File 'lib/nihaopay/transactions/base.rb', line 67

def response_keys_map
  { id: :transaction_id }
end

.valid_attributesObject



63
64
65
# File 'lib/nihaopay/transactions/base.rb', line 63

def valid_attributes
  %i[token transaction_id type status captured currency reference amount note time].freeze
end

Instance Method Details

#cancelObject



26
27
28
# File 'lib/nihaopay/transactions/base.rb', line 26

def cancel
  Nihaopay::Transactions::Cancel.start(transaction_id, token: token)
end

#captureObject



14
15
16
# File 'lib/nihaopay/transactions/base.rb', line 14

def capture
  Nihaopay::Transactions::Capture.start(transaction_id, amount, currency, token: token)
end

#partial_capture(capture_amount) ⇒ Object



18
19
20
# File 'lib/nihaopay/transactions/base.rb', line 18

def partial_capture(capture_amount)
  Nihaopay::Transactions::Capture.start(transaction_id, capture_amount, currency, token: token)
end

#partial_refund(refund_amount, options = {}) ⇒ Object



35
36
37
38
# File 'lib/nihaopay/transactions/base.rb', line 35

def partial_refund(refund_amount, options = {})
  options[:token] = token
  Nihaopay::Transactions::Refund.start(transaction_id, refund_amount, currency, options)
end

#refund(options = {}) ⇒ Object



30
31
32
33
# File 'lib/nihaopay/transactions/base.rb', line 30

def refund(options = {})
  options[:token] = token
  Nihaopay::Transactions::Refund.start(transaction_id, amount, currency, options)
end

#releaseObject



22
23
24
# File 'lib/nihaopay/transactions/base.rb', line 22

def release
  Nihaopay::Transactions::Release.start(transaction_id, token: token)
end