Class: Workarea::Payment::GiftCard
- Inherits:
-
Object
- Object
- Workarea::Payment::GiftCard
- Includes:
- ApplicationDocument, Commentable
- Defined in:
- app/models/workarea/payment/gift_card.rb,
app/models/workarea/payment/gift_card/redemption.rb
Defined Under Namespace
Classes: Redemption
Class Method Summary collapse
-
.find_balance(token) ⇒ Money
Gift card balance for the given token.
- .find_by_token(token) ⇒ Object
- .find_by_token_and_email(token, email) ⇒ Object
-
.purchase(token, cents) ⇒ Payment::GiftCard
Performs purchase of the gift card that matches the given token.
-
.refund(token, cents) ⇒ Payment::GiftCard
Performs refund of the gift card that matches the given token.
-
.search_by_token_or_recipient(string) ⇒ Mongoid::Criteria
Find gift cards whose token or recipient matches the given string.
-
.sorts ⇒ Array<Workarea::Sort>
Sorts available for this class.
Instance Method Summary collapse
-
#expired? ⇒ Boolean
Whether the gift card has expired.
- #generate_token ⇒ Object
-
#last_redeemed_at ⇒ DateTime
The date of the last redemption.
-
#name ⇒ String
This is for compatibility with the admin, all models must implement this.
-
#purchase(cents) ⇒ Payment::GiftCard
Performs purchase for the amount in cents.
-
#refund(cents) ⇒ Payment::GiftCard
Performs refund for the amount in cents.
- #reset_token! ⇒ Object
- #to=(val) ⇒ Object
- #valid? ⇒ Boolean
Class Method Details
.find_balance(token) ⇒ Money
Gift card balance for the given token. If the Gift card does not exist, $0 is returned.
92 93 94 95 |
# File 'app/models/workarea/payment/gift_card.rb', line 92 def self.find_balance(token) card = not_expired.find_by_token(token) card ? card.balance : 0.to_m end |
.find_by_token(token) ⇒ Object
97 98 99 |
# File 'app/models/workarea/payment/gift_card.rb', line 97 def self.find_by_token(token) where(token: token.downcase).first end |
.find_by_token_and_email(token, email) ⇒ Object
101 102 103 |
# File 'app/models/workarea/payment/gift_card.rb', line 101 def self.find_by_token_and_email(token, email) where(token: token, to: email.downcase).not_expired.first end |
.purchase(token, cents) ⇒ Payment::GiftCard
Performs purchase of the gift card that matches the given token. Purchase is for the amount in cents.
71 72 73 |
# File 'app/models/workarea/payment/gift_card.rb', line 71 def self.purchase(token, cents) not_expired.find_by_token(token).purchase(cents) end |
.refund(token, cents) ⇒ Payment::GiftCard
Performs refund of the gift card that matches the given token. Refund is for the amount in cents.
82 83 84 |
# File 'app/models/workarea/payment/gift_card.rb', line 82 def self.refund(token, cents) find_by_token(token).refund(cents) end |
.search_by_token_or_recipient(string) ⇒ Mongoid::Criteria
Find gift cards whose token or recipient matches the given string. Used in admin filtering.
59 60 61 62 |
# File 'app/models/workarea/payment/gift_card.rb', line 59 def self.search_by_token_or_recipient(string) regex = /^#{::Regexp.quote(string)}/i any_of({ token: regex }, { to: regex }) end |
Instance Method Details
#expired? ⇒ Boolean
Whether the gift card has expired.
132 133 134 |
# File 'app/models/workarea/payment/gift_card.rb', line 132 def expired? !!(expires_at && Time.now >= expires_at) end |
#generate_token ⇒ Object
172 173 174 175 176 177 |
# File 'app/models/workarea/payment/gift_card.rb', line 172 def generate_token loop do tmp = SecureRandom.hex(Workarea.config.gift_card_token_length / 2).downcase break tmp unless self.class.where(token: tmp).exists? end end |
#last_redeemed_at ⇒ DateTime
The date of the last redemption.
140 141 142 |
# File 'app/models/workarea/payment/gift_card.rb', line 140 def last_redeemed_at redemptions.asc(:redeemed_at).last.try(:redeemed_at) end |
#name ⇒ String
This is for compatibility with the admin, all models must implement this
124 125 126 |
# File 'app/models/workarea/payment/gift_card.rb', line 124 def name I18n.t('workarea.payment_gift_card.name', token: token) end |
#purchase(cents) ⇒ Payment::GiftCard
Performs purchase for the amount in cents.
152 153 154 155 156 |
# File 'app/models/workarea/payment/gift_card.rb', line 152 def purchase(cents) raise(Workarea::Payment::InsufficientFunds) if balance.cents < cents inc('balance.cents' => 0 - cents, 'used.cents' => cents) self end |
#refund(cents) ⇒ Payment::GiftCard
Performs refund for the amount in cents.
163 164 165 166 |
# File 'app/models/workarea/payment/gift_card.rb', line 163 def refund(cents) inc('balance.cents' => cents, 'used.cents' => 0 - cents) self end |
#reset_token! ⇒ Object
168 169 170 |
# File 'app/models/workarea/payment/gift_card.rb', line 168 def reset_token! update_attribute(:token, generate_token) end |
#to=(val) ⇒ Object
105 106 107 108 109 110 111 |
# File 'app/models/workarea/payment/gift_card.rb', line 105 def to=(val) if val.nil? super(val) else super(val.to_s.downcase) end end |
#valid? ⇒ Boolean
113 114 115 116 117 118 |
# File 'app/models/workarea/payment/gift_card.rb', line 113 def valid?(*) super.tap do |result| self.balance = amount if result && !persisted? self.token = token.downcase if token.present? end end |