Class: Spree::StoreCredit
Constant Summary
collapse
- VOID_ACTION =
'void'
- CREDIT_ACTION =
'credit'
- CAPTURE_ACTION =
'capture'
- ELIGIBLE_ACTION =
'eligible'
- AUTHORIZE_ACTION =
'authorize'
- ALLOCATION_ACTION =
'allocation'
- ADJUSTMENT_ACTION =
'adjustment'
- INVALIDATE_ACTION =
'invalidate'
Instance Attribute Summary collapse
#imported
Instance Method Summary
collapse
-
#amount=(number) ⇒ Object
Sets this store credit’s amount to a new value, parsing it as a localized number if the new value is a string.
-
#amount_remaining ⇒ Object
-
#authorize(amount, order_currency, options = {}) ⇒ Object
-
#can_void?(payment) ⇒ Boolean
-
#capture(amount, authorization_code, order_currency, options = {}) ⇒ Object
-
#credit(amount, authorization_code, order_currency, options = {}) ⇒ Object
-
#editable? ⇒ Boolean
-
#generate_authorization_code ⇒ Object
-
#invalidate(reason, user_performing_invalidation) ⇒ Object
-
#invalidateable? ⇒ Boolean
-
#invalidated? ⇒ Boolean
-
#update_amount(amount, reason, user_performing_update) ⇒ Object
-
#validate_authorization(amount, order_currency) ⇒ Object
-
#void(authorization_code, options = {}) ⇒ Object
money_methods
#actions, #can_capture?, #can_credit?, #reusable?
Methods inherited from Base
display_includes
#generate_permalink, #save_permalink
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
38
39
40
|
# File 'app/models/spree/store_credit.rb', line 38
def action
@action
end
|
#action_amount ⇒ Object
Returns the value of attribute action_amount.
38
39
40
|
# File 'app/models/spree/store_credit.rb', line 38
def action_amount
@action_amount
end
|
#action_authorization_code ⇒ Object
Returns the value of attribute action_authorization_code.
38
39
40
|
# File 'app/models/spree/store_credit.rb', line 38
def action_authorization_code
@action_authorization_code
end
|
#action_originator ⇒ Object
Returns the value of attribute action_originator.
38
39
40
|
# File 'app/models/spree/store_credit.rb', line 38
def action_originator
@action_originator
end
|
#store_credit_reason ⇒ Object
Returns the value of attribute store_credit_reason.
38
39
40
|
# File 'app/models/spree/store_credit.rb', line 38
def store_credit_reason
@store_credit_reason
end
|
Instance Method Details
#amount=(number) ⇒ Object
Sets this store credit’s amount to a new value, parsing it as a localized number if the new value is a string.
49
50
51
|
# File 'app/models/spree/store_credit.rb', line 49
def amount=(number)
self[:amount] = Spree::LocalizedNumber.parse(number)
end
|
#amount_remaining ⇒ Object
53
54
55
56
|
# File 'app/models/spree/store_credit.rb', line 53
def amount_remaining
return 0.0.to_d if invalidated?
amount - amount_used - amount_authorized
end
|
#authorize(amount, order_currency, options = {}) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'app/models/spree/store_credit.rb', line 58
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:)
return true
end
else
authorization_code = generate_authorization_code
end
if validate_authorization(amount, order_currency)
update!({
action: AUTHORIZE_ACTION,
action_amount: amount,
action_originator: options[:action_originator],
action_authorization_code: authorization_code,
amount_authorized: amount_authorized + amount
})
authorization_code
else
false
end
end
|
#can_void?(payment) ⇒ Boolean
158
159
160
|
# File 'app/models/spree/store_credit.rb', line 158
def can_void?(payment)
payment.pending?
end
|
#capture(amount, authorization_code, order_currency, options = {}) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'app/models/spree/store_credit.rb', line 93
def capture(amount, authorization_code, order_currency, options = {})
return false unless authorize(amount, order_currency, action_authorization_code: authorization_code)
auth_event = store_credit_events.find_by!(action: AUTHORIZE_ACTION, authorization_code:)
if amount <= auth_event.amount
if currency != order_currency
errors.add(:base, I18n.t('spree.store_credit.currency_mismatch'))
false
else
update!({
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 - auth_event.amount
})
authorization_code
end
else
errors.add(:base, I18n.t('spree.store_credit.insufficient_authorized_amount'))
false
end
end
|
#credit(amount, authorization_code, order_currency, options = {}) ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'app/models/spree/store_credit.rb', line 136
def credit(amount, authorization_code, order_currency, options = {})
capture_event = store_credit_events.find_by(action: CAPTURE_ACTION, authorization_code:)
if currency != order_currency errors.add(:base, I18n.t('spree.store_credit.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, I18n.t('spree.store_credit.unable_to_credit', auth_code: authorization_code))
false
end
end
|
#editable? ⇒ Boolean
171
172
173
|
# File 'app/models/spree/store_credit.rb', line 171
def editable?
!amount_remaining.zero?
end
|
#generate_authorization_code ⇒ Object
162
163
164
165
166
167
168
169
|
# File 'app/models/spree/store_credit.rb', line 162
def generate_authorization_code
[
id,
'SC',
Time.current.utc.strftime('%Y%m%d%H%M%S%6N'),
SecureRandom.uuid
].join('-')
end
|
#invalidate(reason, user_performing_invalidation) ⇒ Object
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'app/models/spree/store_credit.rb', line 193
def invalidate(reason, user_performing_invalidation)
if invalidateable?
self.action = INVALIDATE_ACTION
self.store_credit_reason = reason
self.action_originator = user_performing_invalidation
self.invalidated_at = Time.current
save
else
errors.add(:invalidated_at, I18n.t('spree.store_credit.errors.cannot_invalidate_uncaptured_authorization'))
false
end
end
|
#invalidateable? ⇒ Boolean
175
176
177
|
# File 'app/models/spree/store_credit.rb', line 175
def invalidateable?
!invalidated? && amount_authorized.zero?
end
|
#invalidated? ⇒ Boolean
179
180
181
|
# File 'app/models/spree/store_credit.rb', line 179
def invalidated?
!!invalidated_at
end
|
#update_amount(amount, reason, user_performing_update) ⇒ Object
183
184
185
186
187
188
189
190
191
|
# File 'app/models/spree/store_credit.rb', line 183
def update_amount(amount, reason, user_performing_update)
previous_amount = self.amount
self.amount = amount
self.action_amount = self.amount - previous_amount
self.action = ADJUSTMENT_ACTION
self.store_credit_reason = reason
self.action_originator = user_performing_update
save
end
|
#validate_authorization(amount, order_currency) ⇒ Object
84
85
86
87
88
89
90
91
|
# File 'app/models/spree/store_credit.rb', line 84
def validate_authorization(amount, order_currency)
if amount_remaining.to_d < amount.to_d
errors.add(:base, I18n.t('spree.store_credit.insufficient_funds'))
elsif currency != order_currency
errors.add(:base, I18n.t('spree.store_credit.currency_mismatch'))
end
errors.blank?
end
|
#void(authorization_code, options = {}) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'app/models/spree/store_credit.rb', line 119
def void(authorization_code, options = {})
if auth_event = store_credit_events.find_by(action: AUTHORIZE_ACTION, authorization_code:)
update!({
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, I18n.t('spree.store_credit.unable_to_void', auth_code: authorization_code))
false
end
end
|