Class: Spree::StoreCredit
- Inherits:
-
Object
- Object
- Spree::StoreCredit
- Extended by:
- DisplayMoney
- Includes:
- Metadata, Metafields, SingleStoreResource
- 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
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#action_amount ⇒ Object
Returns the value of attribute action_amount.
-
#action_authorization_code ⇒ Object
Returns the value of attribute action_authorization_code.
-
#action_originator ⇒ Object
Returns the value of attribute action_originator.
Class Method Summary collapse
Instance Method Summary collapse
- #actions ⇒ Object
- #amount=(amount) ⇒ Object
- #amount_remaining ⇒ Object
- #authorize(amount, order_currency, options = {}) ⇒ Object
- #can_be_deleted? ⇒ Boolean
- #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
- #editable? ⇒ Boolean
- #generate_authorization_code ⇒ Object
- #validate_authorization(amount, order_currency) ⇒ Object
- #void(authorization_code, options = {}) ⇒ Object
Methods included from DisplayMoney
Methods included from Metadata
#metadata, #metadata=, #public_metadata=
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
52 53 54 |
# File 'app/models/spree/store_credit.rb', line 52 def action @action end |
#action_amount ⇒ Object
Returns the value of attribute action_amount.
52 53 54 |
# File 'app/models/spree/store_credit.rb', line 52 def action_amount @action_amount end |
#action_authorization_code ⇒ Object
Returns the value of attribute action_authorization_code.
52 53 54 |
# File 'app/models/spree/store_credit.rb', line 52 def @action_authorization_code end |
#action_originator ⇒ Object
Returns the value of attribute action_originator.
52 53 54 |
# File 'app/models/spree/store_credit.rb', line 52 def action_originator @action_originator end |
Class Method Details
.default_created_by ⇒ Object
193 194 195 196 197 |
# File 'app/models/spree/store_credit.rb', line 193 def default_created_by Spree::Deprecation.warn('StoreCredit#default_created_by is deprecated and will be removed in Spree 5.5. Please use store.users.first instead.') Spree::Store.current.users.first end |
Instance Method Details
#actions ⇒ Object
164 165 166 |
# File 'app/models/spree/store_credit.rb', line 164 def actions [CAPTURE_ACTION, VOID_ACTION, CREDIT_ACTION] end |
#amount=(amount) ⇒ Object
57 58 59 |
# File 'app/models/spree/store_credit.rb', line 57 def amount=(amount) self[:amount] = Spree::LocalizedNumber.parse(amount) end |
#amount_remaining ⇒ Object
64 65 66 |
# File 'app/models/spree/store_credit.rb', line 64 def amount_remaining amount - amount_used - end |
#authorize(amount, order_currency, options = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/models/spree/store_credit.rb', line 68 def (amount, order_currency, = {}) = [:action_authorization_code] if if store_credit_events.find_by(action: AUTHORIZE_ACTION, authorization_code: ) # Don't authorize again on capture return true end else = end if (amount, order_currency) update!( action: AUTHORIZE_ACTION, action_amount: amount, action_originator: [:action_originator], action_authorization_code: , amount_authorized: + amount ) else errors.add(:base, Spree.t('store_credit_payment_method.insufficient_authorized_amount')) false end end |
#can_be_deleted? ⇒ Boolean
184 185 186 |
# File 'app/models/spree/store_credit.rb', line 184 def can_be_deleted? amount_used.zero? && .zero? end |
#can_capture?(payment) ⇒ Boolean
168 169 170 |
# File 'app/models/spree/store_credit.rb', line 168 def can_capture?(payment) payment.pending? || payment.checkout? end |
#can_credit?(payment) ⇒ Boolean
176 177 178 |
# File 'app/models/spree/store_credit.rb', line 176 def can_credit?(payment) payment.completed? && payment.credit_allowed > 0 end |
#can_void?(payment) ⇒ Boolean
172 173 174 |
# File 'app/models/spree/store_credit.rb', line 172 def can_void?(payment) payment.pending? || (payment.checkout? && !payment.order.completed?) end |
#capture(amount, authorization_code, order_currency, options = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'app/models/spree/store_credit.rb', line 102 def capture(amount, , order_currency, = {}) return false unless (amount, order_currency, action_authorization_code: ) if amount <= if currency != order_currency errors.add(:base, Spree.t('store_credit_payment_method.currency_mismatch')) false else update!( action: CAPTURE_ACTION, action_amount: amount, action_originator: [:action_originator], action_authorization_code: , amount_used: amount_used + amount, amount_authorized: - amount ) 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
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'app/models/spree/store_credit.rb', line 142 def credit(amount, , order_currency, = {}) # Find the amount related to this authorization_code in order to add the store credit back capture_event = store_credit_events.find_by(action: CAPTURE_ACTION, authorization_code: ) if currency != order_currency # sanity check to make sure the order currency hasn't changed since the auth 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: [:action_originator], action_authorization_code: } create_credit_record(amount, action_attributes) true else errors.add(:base, Spree.t('store_credit_payment_method.unable_to_credit', auth_code: )) false end end |
#editable? ⇒ Boolean
180 181 182 |
# File 'app/models/spree/store_credit.rb', line 180 def editable? amount_used.zero? && .zero? end |
#generate_authorization_code ⇒ Object
188 189 190 |
# File 'app/models/spree/store_credit.rb', line 188 def "#{id}-SC-#{Time.now.utc.strftime('%Y%m%d%H%M%S%6N')}" end |
#validate_authorization(amount, order_currency) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'app/models/spree/store_credit.rb', line 93 def (amount, order_currency) if BigDecimal(amount_remaining, 3) < BigDecimal(amount, 3) 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/models/spree/store_credit.rb', line 126 def void(, = {}) 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: , action_originator: [:action_originator], amount_authorized: - auth_event.amount ) true else errors.add(:base, Spree.t('store_credit_payment_method.unable_to_void', auth_code: )) false end end |