Class: Monri::Transactions

Inherits:
Object
  • Object
show all
Defined in:
lib/monri/transactions.rb,
lib/monri/transactions/transaction.rb,
lib/monri/transactions/secure_message.rb,
lib/monri/transactions/transaction_response.rb

Defined Under Namespace

Classes: SecureMessage, Transaction, TransactionResponse

Constant Summary collapse

REQUIRED_FIELDS =
[
  :transaction_type,
  :amount,
  :ip,
  :order_info,
  :ch_address,
  :ch_city,
  :ch_country,
  :ch_email,
  :ch_full_name,
  :ch_phone,
  :ch_zip,
  :currency,
  :order_number,
  :language
].freeze
REQUIRED_TRX_MANAGEMENT_FIELDS =
[
  :amount,
  :currency,
  :order_number
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config=(value) ⇒ Monri::Config (writeonly)

Returns:



5
6
7
# File 'lib/monri/transactions.rb', line 5

def config=(value)
  @config = value
end

#http_client=(value) ⇒ Monri::HttpClient (writeonly)

Returns:



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

def http_client=(value)
  @http_client = value
end

Instance Method Details

#capture(params) ⇒ Object



77
78
79
80
81
82
# File 'lib/monri/transactions.rb', line 77

def capture(params)
  unless params.is_a?(Hash)
    raise Monri::Errors::InvalidArgumentsError.new('First parameter - params, should be a Hash')
  end
  trx_management(params.merge(transaction_type: 'capture'))
end

#refund(params) ⇒ Object



70
71
72
73
74
75
# File 'lib/monri/transactions.rb', line 70

def refund(params)
  unless params.is_a?(Hash)
    raise Monri::Errors::InvalidArgumentsError.new('First parameter - params, should be a Hash')
  end
  trx_management(params.merge(transaction_type: 'refund'))
end

#transaction(params) ⇒ Monri::Transactions::TransactionResponse

Parameters:

  • params (Hash)

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/monri/transactions.rb', line 35

def transaction(params)
  TransactionResponse.create do
    unless params.is_a?(Hash)
      raise Monri::Errors::InvalidArgumentsError.new('First parameter - params, should be a Hash')
    end

    missing_keys = REQUIRED_FIELDS.reject { |k| params.has_key?(k) }
    if missing_keys.length > 0
      raise Monri::Errors::InvalidArgumentsError.new("Missing required keys=#{missing_keys.join(', ')}")
    end

    params[:authenticity_token] = @config.authenticity_token
    digest_parts = [@config.merchant_key, params[:order_number], params[:amount], params[:currency]]
    params[:digest] = Digest::SHA512.hexdigest(digest_parts.join)

    req = { transaction: params }

    rv = @http_client.post('/v2/transaction', req)
    if rv.failed?
      raise rv.exception
    elsif rv.success?
      rv.body
    else
      raise "Unhandled state, exception=#{rv.exception}, failed=#{rv.failed?}, success=#{rv.success?}"
    end
  end
end

#void(params) ⇒ Object



63
64
65
66
67
68
# File 'lib/monri/transactions.rb', line 63

def void(params)
  unless params.is_a?(Hash)
    raise Monri::Errors::InvalidArgumentsError.new('First parameter - params, should be a Hash')
  end
  trx_management(params.merge(transaction_type: 'void'))
end