Class: Boutique::Purchase
- Inherits:
-
Object
- Object
- Boutique::Purchase
- Includes:
- DataMapper::Resource
- Defined in:
- lib/boutique.rb
Instance Method Summary collapse
- #boutique_id ⇒ Object
- #complete(txn_id, email, name) ⇒ Object
- #completed? ⇒ Boolean
-
#initialize(attr = {}) ⇒ Purchase
constructor
A new instance of Purchase.
- #link_downloads! ⇒ Object
- #maybe_refresh_downloads! ⇒ Object
- #paypal_form(notify_url) ⇒ Object
- #random_hash ⇒ Object
- #send_mail ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(attr = {}) ⇒ Purchase
Returns a new instance of Purchase.
185 186 187 188 189 |
# File 'lib/boutique.rb', line 185 def initialize(attr = {}) attr[:counter] ||= 0 attr[:secret] ||= random_hash super end |
Instance Method Details
#boutique_id ⇒ Object
224 225 226 227 228 |
# File 'lib/boutique.rb', line 224 def boutique_id (self.id.nil? || self.secret.nil?) ? raise('Cannot get boutique_id for unsaved purchase') : "#{self.id}-#{self.secret}" end |
#complete(txn_id, email, name) ⇒ Object
191 192 193 194 195 196 197 |
# File 'lib/boutique.rb', line 191 def complete(txn_id, email, name) self.transaction_id = txn_id self.email = email self.name = name self.completed_at = DateTime.now link_downloads! end |
#completed? ⇒ Boolean
199 200 201 |
# File 'lib/boutique.rb', line 199 def completed? !completed_at.nil? && !transaction_id.nil? end |
#link_downloads! ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/boutique.rb', line 212 def link_downloads! return if !completed? self.downloads = product.files.map do |file| linked_file = "/#{Date.today.strftime('%Y%m%d')}-#{random_hash}/#{File.basename(file)}" full_dir = File.dirname("#{Boutique.config.download_dir}#{linked_file}") `mkdir -p #{full_dir}` `ln -s #{file} #{Boutique.config.download_dir}#{linked_file}` "#{Boutique.config.download_path}#{linked_file}" end self.counter += 1 end |
#maybe_refresh_downloads! ⇒ Object
203 204 205 206 207 208 209 210 |
# File 'lib/boutique.rb', line 203 def maybe_refresh_downloads! if self.completed? && (self.downloads.nil? || self.downloads.any? {|d| !File.exist?(d) }) self.link_downloads! self.save end end |
#paypal_form(notify_url) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/boutique.rb', line 258 def paypal_form(notify_url) values = { :business => Boutique.config.pp_email, :cmd => '_xclick', :item_name => product.name, :item_number => product.code, :amount => product.price.to_f, :currency_code => 'USD', :notify_url => "#{notify_url}/#{boutique_id}", :return => "#{product.return_url}?b=#{boutique_id}", :cert_id => Boutique.config.pem_cert_id } {'action' => Boutique.config.pp_url, 'cmd' => '_s-xclick', 'encrypted' => encrypt(values)} end |
#random_hash ⇒ Object
230 231 232 |
# File 'lib/boutique.rb', line 230 def random_hash Digest::SHA1.hexdigest("#{DateTime.now}#{rand}")[0..9] end |
#send_mail ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/boutique.rb', line 234 def send_mail raise 'Cannot send link to incomplete purchase' if !completed? Pony.mail( :to => self.email, :from => self.product.support_email, :subject => "#{self.product.name} Receipt", :body => "Thanks for purchasing #{self.product.name}! " + "To download it, follow this link:\n\n" + " #{self.product.return_url}?b=#{boutique_id}\n\n" + "Please reply if you have any troubles.\n" ) end |
#to_json ⇒ Object
247 248 249 250 251 252 253 254 255 256 |
# File 'lib/boutique.rb', line 247 def to_json { :id => id, :counter => counter, :completed => completed?, :name => product.name, :code => product.code, :downloads => downloads }.to_json end |