Class: Workarea::Payment

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument
Defined in:
app/models/workarea/payment.rb,
app/models/workarea/payment/refund.rb,
app/models/workarea/payment/status.rb,
app/models/workarea/payment/tender.rb,
app/models/workarea/payment/address.rb,
app/models/workarea/payment/capture.rb,
app/models/workarea/payment/profile.rb,
app/models/workarea/payment/operation.rb,
app/models/workarea/payment/processing.rb,
app/models/workarea/payment/credit_card.rb,
app/models/workarea/payment/transaction.rb,
app/models/workarea/payment/saved_credit_card.rb,
app/models/workarea/payment/store_credit_card.rb,
app/models/workarea/payment/insufficient_funds.rb,
app/models/workarea/payment/refund/credit_card.rb,
app/models/workarea/payment/tender/credit_card.rb,
app/models/workarea/payment/capture/credit_card.rb,
app/models/workarea/payment/refund/store_credit.rb,
app/models/workarea/payment/tender/store_credit.rb,
app/models/workarea/payment/capture/store_credit.rb,
app/models/workarea/payment/purchase/credit_card.rb,
app/models/workarea/payment/authorize/credit_card.rb,
app/models/workarea/payment/credit_card_operation.rb,
app/models/workarea/payment/purchase/store_credit.rb,
app/models/workarea/payment/authorize/store_credit.rb,
app/models/workarea/payment/operation_implementation.rb

Defined Under Namespace

Modules: Authorize, CreditCard, CreditCardOperation, OperationImplementation, Processing, Purchase, Status Classes: Address, Capture, InsufficientFunds, MissingReference, Operation, Profile, Refund, SavedCreditCard, StoreCreditCard, Tender, Transaction

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.lookup(id, postal_code) ⇒ Payment?

Finds the payment order for the given ID and postal code. Both must match the payment order. Used to lookup orders for checking order status anonymously.

Parameters:

Returns:



35
36
37
38
# File 'app/models/workarea/payment.rb', line 35

def self.lookup(id, postal_code)
  payment = find_or_initialize_by(id: id)
  payment.try(:postal_code) == postal_code ? payment : nil
end

Instance Method Details

#adjust_tender_amounts(new_total) ⇒ Boolean

Adjust tender amounts to meet an order total. Used in checkout when updating payment.

Parameters:

Returns:

  • (Boolean)

    whether the payment is saved



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/workarea/payment.rb', line 104

def adjust_tender_amounts(new_total)
  set_store_credit if store_credit_balance > 0.to_m

  tenders.each do |tender|
    tender.amount = new_total

    # tender.amount could have been changed, e.g. if the store credit
    # balance is lower
    new_total -= tender.amount
  end

  save
end

#authorize!(options = {}) ⇒ Object



152
153
154
155
# File 'app/models/workarea/payment.rb', line 152

def authorize!(options = {})
  transactions = tenders.map { |t| t.build_transaction(action: 'authorize') }
  perform_operation(transactions, options)
end

#clear_credit_cardObject

Remove the current card for the payment. Used for clearing out a card when updating payment so another payment option could be selected



122
123
124
# File 'app/models/workarea/payment.rb', line 122

def clear_credit_card
  self.credit_card = nil
end

#credit_card?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'app/models/workarea/payment.rb', line 144

def credit_card?
  credit_card.present?
end

#nameString

For compatibility with admin features, models must respond to this method

Returns:



44
45
46
# File 'app/models/workarea/payment.rb', line 44

def name
  id
end

#purchasable?(total_amount) ⇒ Boolean

Whether or not the order is purchasable. This is defined as the tenders amount equals the amount passed in (the Order#total). Used in checkout to determine whether we should try to purchase the order.

If the order is not purchasable then all relavant validation errors will be added to the order.

Parameters:

Returns:

  • (Boolean)


136
137
138
# File 'app/models/workarea/payment.rb', line 136

def purchasable?(total_amount)
  tenders.all?(&:valid?) && tendered_amount == total_amount
end

#purchase!(options = {}) ⇒ Object



157
158
159
160
# File 'app/models/workarea/payment.rb', line 157

def purchase!(options = {})
  transactions = tenders.map { |t| t.build_transaction(action: 'purchase') }
  perform_operation(transactions, options)
end

#saved_credit_cardsObject



52
53
54
# File 'app/models/workarea/payment.rb', line 52

def saved_credit_cards
  profile.try(:credit_cards) || []
end

#set_address(attrs) ⇒ Object



73
74
75
76
77
# File 'app/models/workarea/payment.rb', line 73

def set_address(attrs)
  build_address unless address
  address.attributes = attrs
  save
end

#set_credit_card(attrs) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/workarea/payment.rb', line 79

def set_credit_card(attrs)
  build_credit_card unless credit_card
  credit_card.saved_card_id = nil
  credit_card.attributes = attrs.slice(
    :month,
    :year,
    :saved_card_id,
    :number,
    :cvv,
    :amount
  )
  save
end

#set_store_creditObject



93
94
95
96
# File 'app/models/workarea/payment.rb', line 93

def set_store_credit
  build_store_credit unless store_credit
  save
end

#statusObject



162
163
164
165
# File 'app/models/workarea/payment.rb', line 162

def status
  calculators = Workarea.config.payment_status_calculators.map(&:constantize)
  StatusCalculator.new(calculators, self).result
end

#store_credit?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'app/models/workarea/payment.rb', line 148

def store_credit?
  store_credit.present?
end

#store_credit_balanceObject



48
49
50
# File 'app/models/workarea/payment.rb', line 48

def store_credit_balance
  profile.try(:store_credit) || 0.to_m
end

#tendered_amountObject



140
141
142
# File 'app/models/workarea/payment.rb', line 140

def tendered_amount
  tenders.map(&:amount).sum.to_m
end

#tendersObject



67
68
69
70
71
# File 'app/models/workarea/payment.rb', line 67

def tenders
  Workarea.config.tender_types.flat_map do |type|
    send(type)
  end.compact
end

#totalObject



63
64
65
# File 'app/models/workarea/payment.rb', line 63

def total
  tenders.map(&:amount).sum || 0.to_m
end

#transactionsObject

TODO in v4 - change this to get transactions by foreign key. Currently, this doesn’t return all transactions for this payment if a tender failed validation.



59
60
61
# File 'app/models/workarea/payment.rb', line 59

def transactions
  tenders.map(&:transactions).flatten
end