Class: Spree::StoreCredit
- Inherits:
-
Base
- Object
- ApplicationRecord
- Base
- Spree::StoreCredit
show all
- Extended by:
- DisplayMoney
- Defined in:
- app/models/spree/store_credit.rb
Constant Summary
collapse
- VOID_ACTION =
'void'.freeze
- CANCEL_ACTION =
'cancel'.freeze
- CREDIT_ACTION =
'credit'.freeze
- CAPTURE_ACTION =
'capture'.freeze
- ELIGIBLE_ACTION =
'eligible'.freeze
- AUTHORIZE_ACTION =
'authorize'.freeze
- ALLOCATION_ACTION =
'allocation'.freeze
- DEFAULT_CREATED_BY_EMAIL =
'[email protected]'.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#actions ⇒ Object
-
#amount_remaining ⇒ Object
-
#authorize(amount, order_currency, options = {}) ⇒ Object
-
#can_capture?(payment) ⇒ Boolean
-
#can_credit?(payment) ⇒ Boolean
-
#can_void?(payment) ⇒ Boolean
-
#capture(amount, authorization_code, order_currency, options = {}) ⇒ Object
-
#credit(amount, authorization_code, order_currency, options = {}) ⇒ Object
-
#generate_authorization_code ⇒ Object
-
#validate_authorization(amount, order_currency) ⇒ Object
-
#void(authorization_code, options = {}) ⇒ Object
money_methods
Methods inherited from Base
belongs_to_required_by_default, page, spree_base_scopes
#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
36
37
38
|
# File 'app/models/spree/store_credit.rb', line 36
def action
@action
end
|
#action_amount ⇒ Object
Returns the value of attribute action_amount.
36
37
38
|
# File 'app/models/spree/store_credit.rb', line 36
def action_amount
@action_amount
end
|
#action_authorization_code ⇒ Object
Returns the value of attribute action_authorization_code.
36
37
38
|
# File 'app/models/spree/store_credit.rb', line 36
def action_authorization_code
@action_authorization_code
end
|
#action_originator ⇒ Object
Returns the value of attribute action_originator.
36
37
38
|
# File 'app/models/spree/store_credit.rb', line 36
def action_originator
@action_originator
end
|
Class Method Details
.default_created_by ⇒ Object
Instance Method Details
#amount_remaining ⇒ Object
41
42
43
|
# File 'app/models/spree/store_credit.rb', line 41
def amount_remaining
amount - amount_used - amount_authorized
end
|
#authorize(amount, order_currency, options = {}) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'app/models/spree/store_credit.rb', line 45
def authorize(amount, order_currency, options = {})
authorization_code = options[:action_authorization_code]
if authorization_code
if store_credit_events.find_by(action: AUTHORIZE_ACTION, authorization_code: authorization_code)
return true
end
else
authorization_code = generate_authorization_code
end
if validate_authorization(amount, order_currency)
update_attributes!(
action: AUTHORIZE_ACTION,
action_amount: amount,
action_originator: options[:action_originator],
action_authorization_code: authorization_code,
amount_authorized: amount_authorized + amount
)
authorization_code
else
errors.add(:base, Spree.t('store_credit_payment_method.insufficient_authorized_amount'))
false
end
end
|
#can_capture?(payment) ⇒ Boolean
146
147
148
|
# File 'app/models/spree/store_credit.rb', line 146
def can_capture?(payment)
payment.pending? || payment.checkout?
end
|
#can_credit?(payment) ⇒ Boolean
154
155
156
|
# File 'app/models/spree/store_credit.rb', line 154
def can_credit?(payment)
payment.completed? && payment.credit_allowed > 0
end
|
#can_void?(payment) ⇒ Boolean
150
151
152
|
# File 'app/models/spree/store_credit.rb', line 150
def can_void?(payment)
payment.pending? || (payment.checkout? && !payment.order.completed?)
end
|
#capture(amount, authorization_code, order_currency, options = {}) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'app/models/spree/store_credit.rb', line 80
def capture(amount, authorization_code, order_currency, options = {})
return false unless authorize(amount, order_currency, action_authorization_code: authorization_code)
if amount <= amount_authorized
if currency != order_currency
errors.add(:base, Spree.t('store_credit_payment_method.currency_mismatch'))
false
else
update_attributes!(
action: CAPTURE_ACTION,
action_amount: amount,
action_originator: options[:action_originator],
action_authorization_code: authorization_code,
amount_used: amount_used + amount,
amount_authorized: amount_authorized - amount
)
authorization_code
end
else
errors.add(:base, Spree.t('store_credit_payment_method.insufficient_authorized_amount'))
false
end
end
|
#credit(amount, authorization_code, order_currency, options = {}) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'app/models/spree/store_credit.rb', line 120
def credit(amount, authorization_code, order_currency, options = {})
capture_event = store_credit_events.find_by(action: CAPTURE_ACTION, authorization_code: authorization_code)
if currency != order_currency errors.add(:base, Spree.t('store_credit_payment_method.currency_mismatch'))
false
elsif capture_event && amount <= capture_event.amount
action_attributes = {
action: CREDIT_ACTION,
action_amount: amount,
action_originator: options[:action_originator],
action_authorization_code: authorization_code
}
create_credit_record(amount, action_attributes)
true
else
errors.add(:base, Spree.t('store_credit_payment_method.unable_to_credit', auth_code: authorization_code))
false
end
end
|
#generate_authorization_code ⇒ Object
158
159
160
|
# File 'app/models/spree/store_credit.rb', line 158
def generate_authorization_code
"#{id}-SC-#{Time.now.utc.strftime('%Y%m%d%H%M%S%6N')}"
end
|
#validate_authorization(amount, order_currency) ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'app/models/spree/store_credit.rb', line 71
def validate_authorization(amount, order_currency)
if amount_remaining.to_d < amount.to_d
errors.add(:base, Spree.t('store_credit_payment_method.insufficient_funds'))
elsif currency != order_currency
errors.add(:base, Spree.t('store_credit_payment_method.currency_mismatch'))
end
errors.blank?
end
|
#void(authorization_code, options = {}) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'app/models/spree/store_credit.rb', line 104
def void(authorization_code, options = {})
if auth_event = store_credit_events.find_by(action: AUTHORIZE_ACTION, authorization_code: authorization_code)
update_attributes!(
action: VOID_ACTION,
action_amount: auth_event.amount,
action_authorization_code: authorization_code,
action_originator: options[:action_originator],
amount_authorized: amount_authorized - auth_event.amount
)
true
else
errors.add(:base, Spree.t('store_credit_payment_method.unable_to_void', auth_code: authorization_code))
false
end
end
|