Class: Pin::Receipt

Inherits:
Base
  • Object
show all
Defined in:
lib/pin_up_receipts/receipt.rb

Overview

This class lets you generate and save receipts from a Pin::Charge.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(charge, your_details, logo_path = '', payment_options = {}, template_path = 'views/receipt.html.erb', save_path = 'tmp/receipt.html') ⇒ Receipt

Create a new PinUpReceipts::Receipt instance Args:

charge: Charge hash from Pin::Charge.find
your_details: (Array), an Array of details you would like displayed about you or your company
payment_options: (Hash), If your charge had extra components (such as late fees or discounts or taxes) pass them in here as a hash eg:

   payment_options = {}
   payment_options["fee"] = {"name" => "late fee", "amount" => "$10.00"}
   payment_options["tax"] = {"name" => "GST", "amount" => "$10.00"}
   payment_options["discount"] = {"name" => "Member Discount", "amount" => "$10.00"}

template_path: (String), path to your own template if you wish to design your own receipt (take a look at the included template for variable names)
save_path: (String), path that the HTML receipt will be saved


25
26
27
28
29
30
31
32
33
# File 'lib/pin_up_receipts/receipt.rb', line 25

def initialize(charge, your_details, logo_path='', payment_options={}, template_path = 'views/receipt.html.erb', save_path = 'tmp/receipt.html')
  @charge = charge
  @logo = logo_path
  @details = your_details
  @payment_options = payment_options
  @template_path = template_path if template_path
  @save_path = save_path if save_path
  @amount = number_to_currency(@charge["amount"], @charge["currency"])
end

Class Method Details

.rootObject

include Pin



8
9
10
# File 'lib/pin_up_receipts/receipt.rb', line 8

def self.root
  File.expand_path '../../..', __FILE__
end

Instance Method Details

#renderObject

Renders the HTML receipt but does not save it – useful for showing on screen



37
38
39
40
# File 'lib/pin_up_receipts/receipt.rb', line 37

def render
  template = ERB.new File.new(@template_path).read, nil, "%"
  template.result(binding)
end

#saveObject

Renders and saves the HTML receipt – useful for emailing receipt



44
45
46
47
48
# File 'lib/pin_up_receipts/receipt.rb', line 44

def save
  receipt = File.new(@save_path, 'w')
  receipt.write(render)
  receipt.close
end