Class: Workarea::Payment::GiftCard

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Class Method Details

.find_balance(token) ⇒ Money

Gift card balance for the given token. If the Gift card does not exist, $0 is returned.

Parameters:

  • token (String)

    Representing Payment::GiftCard#token

Returns:

  • (Money)


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.

Parameters:

  • token (String)

    Representing Payment::GiftCard#token

  • cents (Integer)

    Amount of purchase

Returns:



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.

Parameters:

  • token (String)

    Representing Payment::GiftCard#token

  • cents (Integer)

    Amount to refund

Returns:



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.

Parameters:

  • (String)

Returns:

  • (Mongoid::Criteria)


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

.sortsArray<Workarea::Sort>

Sorts available for this class.

Returns:

  • (Array<Workarea::Sort>)


49
50
51
# File 'app/models/workarea/payment/gift_card.rb', line 49

def self.sorts
  [Workarea::Sort.newest, Workarea::Sort.modified]
end

Instance Method Details

#expired?Boolean

Whether the gift card has expired.

Returns:

  • (Boolean)


132
133
134
# File 'app/models/workarea/payment/gift_card.rb', line 132

def expired?
  !!(expires_at && Time.now >= expires_at)
end

#generate_tokenObject



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_atDateTime

The date of the last redemption.

Returns:

  • (DateTime)


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

#nameString

This is for compatibility with the admin, all models must implement this

Returns:

  • (String)


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.

Parameters:

  • cents (Integer)

    Amount of purchase

Returns:

Raises:

  • (Payment::InsufficientFunds)

    the balance of the gift card does not cover the 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.

Parameters:

  • cents (Integer)

    Amount of purchase

Returns:



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

Returns:

  • (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