Class: SpreeLoyalty::Redemption
- Inherits:
-
Object
- Object
- SpreeLoyalty::Redemption
- Defined in:
- app/services/spree_loyalty/redemption.rb
Overview
Converts points into a real Spree::StoreCredit grant, spendable immediately through the store's existing Store Credit payment method — deliberately no bespoke discount/coupon engine. Returns the created Spree::StoreCredit on success; raises SpreeLoyalty::Redemption::Error (with a message meant to be shown to the customer) on failure.
Defined Under Namespace
Classes: Error
Constant Summary collapse
- CATEGORY_NAME =
Lazily created once per store on first redemption, not seeded — a store that never runs loyalty never gets an unused category cluttering its Store Credit admin.
'Loyalty Rewards'.freeze
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.call ⇒ Object
15 |
# File 'app/services/spree_loyalty/redemption.rb', line 15 def self.call(...) = new.call(...) |
Instance Method Details
#call(account:, points:) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/services/spree_loyalty/redemption.rb', line 17 def call(account:, points:) points = points.to_i validate!(account, points) dollars = points_to_dollars(points) SpreeLoyalty::Account.transaction do store_credit = Spree::StoreCredit.create!( user: account.user, store: account.store, amount: dollars, currency: account.store.default_currency, category: loyalty_category, memo: "Redeemed #{points} loyalty points" ) account.transactions.create!( points: -points, kind: SpreeLoyalty::Transaction::REDEEMED, source: store_credit ) account.update!(points_balance: account.points_balance - points) store_credit end end |