Class: Paymongo::TransactionGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/paymongo/transaction_gateway.rb

Instance Method Summary collapse

Constructor Details

#initialize(gateway) ⇒ TransactionGateway

Returns a new instance of TransactionGateway.



3
4
5
6
7
# File 'lib/paymongo/transaction_gateway.rb', line 3

def initialize(gateway)
  @gateway = gateway
  @config = gateway.config
  @config.assert_has_keys
end

Instance Method Details

#_do_create(path, params = nil) ⇒ Object



30
31
32
33
# File 'lib/paymongo/transaction_gateway.rb', line 30

def _do_create(path, params = nil)
  response = @config.https.post("#{@config.base_url}#{path}", params)
  _handle_transaction_response(response)
end

#_handle_transaction_response(response) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/paymongo/transaction_gateway.rb', line 35

def _handle_transaction_response(response)
  if response[:data]
    SuccessfulResult.new(
      transaction: Transaction._new(@gateway, response[:data])
    )
  elsif response[:errors]
    ErrorResult.new(@gateway, response[:errors])
  else
    raise UnexpectedError, 'expected :data'
  end
end

#create(attributes) ⇒ Object



26
27
28
# File 'lib/paymongo/transaction_gateway.rb', line 26

def create(attributes)
  _do_create '/payments', transaction: attributes
end

#sale(attributes) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/paymongo/transaction_gateway.rb', line 9

def sale(attributes)
  # Wrap the hash
  create(
    {
      data: {
        attributes: {
          amount: attributes[:amount],
          currency: attributes[:currency],
          description: attributes[:description],
          statement_descriptor: attributes[:statement_descriptor],
          source: { id: attributes[:token], type: 'token' }
        }
      }
    }
  )
end