class SpookAndPay::Transaction

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.

Constants

FIELDS

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

STATUSES

Acceptable set of statuses.

TYPES

The basic types for transactions.

Attributes

id[R]

Basic attributes

provider[R]

Basic attributes

raw[R]

Basic attributes

status[R]

Basic attributes

Public Class Methods

new(provider, id, status, raw, opts = {}) click to toggle source

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.

@param SpookAndPay::Providers::Base provider @param String id @param [String, nil] status @param Class raw @param Hash opts @option opts SpookAndPay::CreditCard :credit_card @option opts Time :created_at @option opts Time :updated_at @option opts BigDecimal :amount @option opts String :type

# 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

Public Instance Methods

==(other) click to toggle source

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

@param Class other @return [true, false]

# File lib/spook_and_pay/transaction.rb, line 68
def ==(other)
  other.is_a?(SpookAndPay::Transaction) and other.id == id
end
authorized?() click to toggle source

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

@return [true, false]

# File lib/spook_and_pay/transaction.rb, line 90
def authorized?
  status == :authorized
end
can_capture?() click to toggle source

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

@return [true, false]

# File lib/spook_and_pay/transaction.rb, line 106
def can_capture?
  authorized? and provider.supports_capture?
end
can_refund?() click to toggle source

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

@return [true, false]

# File lib/spook_and_pay/transaction.rb, line 98
def can_refund?
  settled? and provider.supports_refund?
end
can_void?() click to toggle source

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

@return [true, false]

# File lib/spook_and_pay/transaction.rb, line 114
def can_void?
  (authorized? or settling?) and provider.supports_void?
end
capture!() click to toggle source

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

@return SpookAndPay::Result @raises SpookAndPay::Transaction::InvalidActionError @raises SpookAndPay::Providers::Base::NotSupportedError

# 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) click to toggle source

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

@param Numeric amount @return SpookAndPay::Result @raises SpookAndPay::Transaction::InvalidActionError @raises SpookAndPay::Providers::Base::NotSupportedError

# 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!() click to toggle source

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

@return SpookAndPay::Result @raises SpookAndPay::Transaction::InvalidActionError @raises SpookAndPay::Providers::Base::NotSupportedError

# 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?() click to toggle source

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

@return [true, false]

# File lib/spook_and_pay/transaction.rb, line 75
def settled?
  status == :settled
end
settling?() click to toggle source

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

@return [true, false]

# File lib/spook_and_pay/transaction.rb, line 83
def settling?
  status == :settling
end
void!() click to toggle source

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

@return SpookAndPay::Result @raises SpookAndPay::Transaction::InvalidActionError @raises SpookAndPay::Providers::Base::NotSupportedError

# 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