Class: SpookAndPay::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/spook_and_pay/transaction.rb

Overview

A simple class representing an interaction with a provider. Each interaction has an ID, a type and may be successful or not. It is read-only.

Defined Under Namespace

Classes: InvalidActionError

Constant Summary collapse

FIELDS =

Extra set of fields which may or may not be present depending on the provider.

[:created_at, :updated_at, :amount, :credit_card, :type].freeze
TYPES =

The basic types for transactions.

[:purchase, :authorize, :capture, :credit, :void].freeze
STATUSES =

Acceptable set of statuses.

[:authorized, :settling, :settled, :voided, :refunded, :gateway_rejected].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, id, status, raw, opts = {}) ⇒ Transaction

As a bare minimum the transaction captures the transaction ID, it’s status and the raw response from the provider. Optionally, it can receive other fields via the opts hash.

Parameters:

  • SpookAndPay::Providers::Base

    provider

  • String

    id

  • status (String, nil)
  • Class

    raw

  • Hash

    opts

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • SpookAndPay::CreditCard (Object)

    :credit_card

  • Time (Object)

    :created_at

  • Time (Object)

    :updated_at

  • BigDecimal (Object)

    :amount

  • String (Object)

    :type



54
55
56
57
58
59
60
61
# File 'lib/spook_and_pay/transaction.rb', line 54

def initialize(provider, id, status, raw, opts = {})
  @provider   = provider
  @id         = id
  @status     = status.to_sym if status
  @raw        = raw

  FIELDS.each {|f| instance_variable_set(:"@#{f}", opts[f]) if opts.has_key?(f)}
end

Instance Attribute Details

#idObject (readonly)

Basic attributes



7
8
9
# File 'lib/spook_and_pay/transaction.rb', line 7

def id
  @id
end

#providerObject (readonly)

Basic attributes



7
8
9
# File 'lib/spook_and_pay/transaction.rb', line 7

def provider
  @provider
end

#rawObject (readonly)

Basic attributes



7
8
9
# File 'lib/spook_and_pay/transaction.rb', line 7

def raw
  @raw
end

#statusObject (readonly)

Basic attributes



7
8
9
# File 'lib/spook_and_pay/transaction.rb', line 7

def status
  @status
end

Instance Method Details

#==(other) ⇒ true, false

Implements value comparison i.e. if class and ID match, they are the same.

Parameters:

  • Class

    other

Returns:

  • (true, false)


68
69
70
# File 'lib/spook_and_pay/transaction.rb', line 68

def ==(other)
  other.is_a?(SpookAndPay::Transaction) and other.id == id
end

#authorized?true, false

A simple predicate to check if the payment has been authorized.

Returns:

  • (true, false)


90
91
92
# File 'lib/spook_and_pay/transaction.rb', line 90

def authorized?
  status == :authorized
end

#can_capture?true, false

A predicate for checking if a transaction can be captured. Only true if the status is :authorized

Returns:

  • (true, false)


106
107
108
# File 'lib/spook_and_pay/transaction.rb', line 106

def can_capture?
  authorized? and provider.supports_capture?
end

#can_refund?true, false

A predicate for checking if a transaction can be refunded. Only true if the status is :settled

Returns:

  • (true, false)


98
99
100
# File 'lib/spook_and_pay/transaction.rb', line 98

def can_refund?
  settled? and provider.supports_refund?
end

#can_void?true, false

A predicate for checking if a transaction can be voided. Only true if the status is :authorized or :submitted_for_settlement

Returns:

  • (true, false)


114
115
116
# File 'lib/spook_and_pay/transaction.rb', line 114

def can_void?
  (authorized? or settling?) and provider.supports_void?
end

#capture!Object

Captures an authorized transaction. Will only capture the amount authorized and will fail if the transaction is already captured.

Returns:

  • SpookAndPay::Result

Raises:



149
150
151
152
# File 'lib/spook_and_pay/transaction.rb', line 149

def capture!
  raise InvalidActionError.new(id, :capture, status) unless authorized?
  provider.capture_transaction(self)
end

#partial_refund!(amount) ⇒ Object

Refunds the transaction. The related credit card will be credited for the amount specified. It will only succeed for purchases or captured authorizations.

Parameters:

  • Numeric

    amount

Returns:

  • SpookAndPay::Result

Raises:



138
139
140
141
# File 'lib/spook_and_pay/transaction.rb', line 138

def partial_refund!(amount)
  raise InvalidActionError.new(id, :partial_refund, status) unless settled?
  provider.partially_refund_transaction(self, amount)
end

#refund!Object

Refunds the transaction. The related credit card will be credited for the amount captured. It will only succeed for purchases or captured authorizations.

Returns:

  • SpookAndPay::Result

Raises:



125
126
127
128
# File 'lib/spook_and_pay/transaction.rb', line 125

def refund!
  raise InvalidActionError.new(id, :refund, status) unless settled?
  provider.refund_transaction(self)
end

#settled?true, false

A simple predicate to see if the payment has been settled.

Returns:

  • (true, false)


75
76
77
# File 'lib/spook_and_pay/transaction.rb', line 75

def settled?
  status == :settled
end

#settling?true, false

A simple predicate which indicates if the payment is in the process of being settled.

Returns:

  • (true, false)


83
84
85
# File 'lib/spook_and_pay/transaction.rb', line 83

def settling?
  status == :settling
end

#void!Object

Voids a transaction. Can only be done when the transaction is in the authorized status. Otherwise it must be refunded.

Returns:

  • SpookAndPay::Result

Raises:



160
161
162
163
# File 'lib/spook_and_pay/transaction.rb', line 160

def void!
  raise InvalidActionError.new(id, :void, status) unless authorized? or settling?
  provider.void_transaction(self)
end