Class: SpookAndPay::Transaction
- Inherits:
-
Object
- Object
- SpookAndPay::Transaction
- 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
-
#id ⇒ Object
readonly
Basic attributes.
-
#provider ⇒ Object
readonly
Basic attributes.
-
#raw ⇒ Object
readonly
Basic attributes.
-
#status ⇒ Object
readonly
Basic attributes.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Implements value comparison i.e.
-
#authorized? ⇒ true, false
A simple predicate to check if the payment has been authorized.
-
#can_capture? ⇒ true, false
A predicate for checking if a transaction can be captured.
-
#can_refund? ⇒ true, false
A predicate for checking if a transaction can be refunded.
-
#can_void? ⇒ true, false
A predicate for checking if a transaction can be voided.
-
#capture! ⇒ Object
Captures an authorized transaction.
-
#initialize(provider, id, status, raw, opts = {}) ⇒ Transaction
constructor
As a bare minimum the transaction captures the transaction ID, it’s status and the raw response from the provider.
-
#partial_refund!(amount) ⇒ Object
Refunds the transaction.
-
#refund! ⇒ Object
Refunds the transaction.
-
#settled? ⇒ true, false
A simple predicate to see if the payment has been settled.
-
#settling? ⇒ true, false
A simple predicate which indicates if the payment is in the process of being settled.
-
#void! ⇒ Object
Voids a transaction.
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.
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
#id ⇒ Object (readonly)
Basic attributes
7 8 9 |
# File 'lib/spook_and_pay/transaction.rb', line 7 def id @id end |
#provider ⇒ Object (readonly)
Basic attributes
7 8 9 |
# File 'lib/spook_and_pay/transaction.rb', line 7 def provider @provider end |
#raw ⇒ Object (readonly)
Basic attributes
7 8 9 |
# File 'lib/spook_and_pay/transaction.rb', line 7 def raw @raw end |
#status ⇒ Object (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.
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.
90 91 92 |
# File 'lib/spook_and_pay/transaction.rb', line 90 def status == :authorized end |
#can_capture? ⇒ true, false
A predicate for checking if a transaction can be captured. Only true if the status is :authorized
106 107 108 |
# File 'lib/spook_and_pay/transaction.rb', line 106 def can_capture? 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
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
114 115 116 |
# File 'lib/spook_and_pay/transaction.rb', line 114 def can_void? ( 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.
149 150 151 152 |
# File 'lib/spook_and_pay/transaction.rb', line 149 def capture! raise InvalidActionError.new(id, :capture, status) unless 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.
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.
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.
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.
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.
160 161 162 163 |
# File 'lib/spook_and_pay/transaction.rb', line 160 def void! raise InvalidActionError.new(id, :void, status) unless or settling? provider.void_transaction(self) end |