receipts 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +174 -0
- data/Rakefile +2 -0
- data/examples/receipt.pdf +0 -0
- data/lib/receipts.rb +2 -0
- data/lib/receipts/receipt.rb +69 -0
- data/lib/receipts/version.rb +3 -0
- data/receipts.gemspec +26 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 93156bde839de1bb1cba243522aaa3128c415e66
|
4
|
+
data.tar.gz: a2bae268ad734907dc60553e8643a5da6a0fee3d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 929676439c1133ee96d7950e56b3e6b74404243c5f8e66d090c00e202df48d077ab55eea76a4e619f3f58ea0a22ba7aeecff0c6e5bd402369ef10d4b8a8ff83c
|
7
|
+
data.tar.gz: ae02d1b604d87f7cc9473444922451ad10dbd140a333e5fc11d8e538f91a81e49a3e732391c4ff359c588d88af3fc6b27785244e26ac9bb718da9a1c78f6b086
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Chris Oliver
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
# Receipts
|
2
|
+
|
3
|
+
Receipts for your Rails application that works with any payment provider.
|
4
|
+
|
5
|
+
Check out an [example PDF receipt](https://github.com/excid3/receipts/blob/master/examples/receipt.pdf?raw=true)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'receipts'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install receipts
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Adding receipts to your application is pretty simple. All you need is a
|
26
|
+
model that stores your transaction details. In this example our
|
27
|
+
application has a model named `Charge` that we will use.
|
28
|
+
|
29
|
+
We're going to add a method called `receipt` on our model called `Charge`
|
30
|
+
that will create a new receipt for the charge using attributes from the
|
31
|
+
model.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
# == Schema Information
|
35
|
+
#
|
36
|
+
# Table name: charges
|
37
|
+
#
|
38
|
+
# id :integer not null, primary key
|
39
|
+
# user_id :integer
|
40
|
+
# stripe_id :string(255)
|
41
|
+
# amount :integer
|
42
|
+
# card_last4 :string(255)
|
43
|
+
# card_type :string(255)
|
44
|
+
# card_exp_month :integer
|
45
|
+
# card_exp_year :integer
|
46
|
+
# uuid :string
|
47
|
+
# created_at :datetime
|
48
|
+
# updated_at :datetime
|
49
|
+
#
|
50
|
+
|
51
|
+
class Charge < ActiveRecord::Base
|
52
|
+
belongs_to :user
|
53
|
+
validates :stripe_id, uniqueness: true
|
54
|
+
|
55
|
+
def receipt
|
56
|
+
Receipts::Receipt.new(
|
57
|
+
id: id,
|
58
|
+
product: "GoRails",
|
59
|
+
company: {
|
60
|
+
name: "One Month, Inc.",
|
61
|
+
address: "37 Great Jones\nFloor 2\nNew York City, NY 10012",
|
62
|
+
email: "teachers@onemonth.com",
|
63
|
+
logo: Rails.root.join("app/assets/images/one-month-dark.png")
|
64
|
+
},
|
65
|
+
line_items: [
|
66
|
+
["Date", created_at.to_s],
|
67
|
+
["Account Billed", "#{user.name} (#{user.email})"],
|
68
|
+
["Product", "GoRails"],
|
69
|
+
["Amount", "$#{amount / 100}.00"],
|
70
|
+
["Charged to", "#{card_type} (**** **** **** #{card_last4})"],
|
71
|
+
["Transaction ID", uuid]
|
72
|
+
],
|
73
|
+
font: {
|
74
|
+
bold: Rails.root.join('app/assets/fonts/tradegothic/TradeGothic-Bold.ttf'),
|
75
|
+
normal: Rails.root.join('app/assets/fonts/tradegothic/TradeGothic.ttf'),
|
76
|
+
}
|
77
|
+
)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
Update the options for the receipt according to the data you want to
|
83
|
+
display.
|
84
|
+
|
85
|
+
## Customizing Your Receipts
|
86
|
+
|
87
|
+
* `id` - **Required**
|
88
|
+
|
89
|
+
This sets the ID of the charge on the receipt
|
90
|
+
|
91
|
+
* `product` or `message` - **Required**
|
92
|
+
|
93
|
+
You can set either the product or message options. If you set product, it will use the default message. If you want a custom message, you can set the message option to populate it with custom text.
|
94
|
+
|
95
|
+
* `company` - **Required**
|
96
|
+
|
97
|
+
Company consists of several required nested attributes.
|
98
|
+
|
99
|
+
* `name` - **Required**
|
100
|
+
* `address` - **Required**
|
101
|
+
* `email` - **Required**
|
102
|
+
* `logo` - **Required**
|
103
|
+
|
104
|
+
* `line_items` - **Required**
|
105
|
+
|
106
|
+
You can set as many line items on the receipts as you want. Just pass in an array with each item containing a name and a value to display on the receipt.
|
107
|
+
|
108
|
+
* `font` - *Optional*
|
109
|
+
|
110
|
+
If you'd like to use your own custom font, you can pass in the file paths to the `normal` and `bold` variations of your font. The bold font variation is required because it is used in the default message. If you wish to override that, you can pass in your own custom message instead.
|
111
|
+
|
112
|
+
|
113
|
+
## Rendering the Receipt PDF in your Controller
|
114
|
+
|
115
|
+
Here we have a charges controller that responds to the show action. When
|
116
|
+
you visit it with the PDF format, it calls the `receipt` method that we
|
117
|
+
just created on the `Charge` model.
|
118
|
+
|
119
|
+
We set the filename to be the date plus the product name. You can
|
120
|
+
customize the filename to your liking.
|
121
|
+
|
122
|
+
Next we set the response type which will be `application/pdf`
|
123
|
+
|
124
|
+
Optionally we can set the `disposition` to `:inline` which allows us to
|
125
|
+
render the PDF in the browser without forcing the download. If you
|
126
|
+
delete this option or change it to `:attachment` then the receipt will
|
127
|
+
be downloaded instead.
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
class ChargesController < ApplicationController
|
131
|
+
before_action :authenticate_user!
|
132
|
+
before_action :set_charge
|
133
|
+
|
134
|
+
def show
|
135
|
+
respond_to do |format|
|
136
|
+
format.pdf {
|
137
|
+
send_data @charge.receipt.render,
|
138
|
+
filename: "#{@charge.created_at.strftime("%Y-%m-%d")}-gorails-receipt.pdf",
|
139
|
+
type: "application/pdf",
|
140
|
+
disposition: :inline
|
141
|
+
}
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
private
|
146
|
+
|
147
|
+
def set_charge
|
148
|
+
@charge = current_user.charges.find(params[:id])
|
149
|
+
end
|
150
|
+
end
|
151
|
+
```
|
152
|
+
|
153
|
+
And that's it! Just create a `link_to` to your charge with the format of
|
154
|
+
`pdf` and you're good to go.
|
155
|
+
|
156
|
+
For example:
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
# config/routes.rb
|
160
|
+
resources :charges
|
161
|
+
```
|
162
|
+
|
163
|
+
```erb
|
164
|
+
<%= link_to "Download Receipt", charge_path(@charge, format: :pdf) %>
|
165
|
+
```
|
166
|
+
|
167
|
+
## Contributing
|
168
|
+
|
169
|
+
1. Fork it ( https://github.com/[my-github-username]/receipts/fork )
|
170
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
171
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
172
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
173
|
+
5. Create a new Pull Request
|
174
|
+
|
data/Rakefile
ADDED
Binary file
|
data/lib/receipts.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
module Receipts
|
2
|
+
class Receipt < Prawn::Document
|
3
|
+
attr_reader :attributes, :id, :company, :custom_font, :line_items, :logo, :message, :product
|
4
|
+
|
5
|
+
def initialize(attributes)
|
6
|
+
@attributes = attributes
|
7
|
+
@id = attributes.fetch(:id)
|
8
|
+
@company = attributes.fetch(:company)
|
9
|
+
@line_items = attributes.fetch(:line_items)
|
10
|
+
@custom_font = attributes.fetch(:font, {})
|
11
|
+
@message = attributes.fetch(:message) { default_message }
|
12
|
+
|
13
|
+
super(margin: 0)
|
14
|
+
|
15
|
+
setup_fonts if custom_font.any?
|
16
|
+
generate
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def default_message
|
22
|
+
"We've received your payment for #{attributes.fetch(:product)}. You can keep this receipt for your records. For questions, contact us anytime at <color rgb='326d92'><link href='mailto:#{company.fetch(:email)}.com?subject=\"Charge ##{id}\"'><b>#{company.fetch(:email)}</b></link></color>."
|
23
|
+
end
|
24
|
+
|
25
|
+
def setup_fonts
|
26
|
+
font_families.update "Primary" => custom_font
|
27
|
+
font "Primary"
|
28
|
+
end
|
29
|
+
|
30
|
+
def generate
|
31
|
+
bounding_box [0, 792], width: 612, height: 792 do
|
32
|
+
bounding_box [85, 792], width: 442, height: 792 do
|
33
|
+
header
|
34
|
+
charge_details
|
35
|
+
footer
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def header
|
41
|
+
move_down 60
|
42
|
+
image company.fetch(:logo), height: 32
|
43
|
+
|
44
|
+
move_down 8
|
45
|
+
text "<color rgb='a6a6a6'>RECEIPT FOR CHARGE ##{id}</color>", inline_format: true
|
46
|
+
|
47
|
+
move_down 30
|
48
|
+
text message, inline_format: true, size: 12.5, leading: 4
|
49
|
+
end
|
50
|
+
|
51
|
+
def charge_details
|
52
|
+
move_down 30
|
53
|
+
|
54
|
+
borders = line_items.length - 2
|
55
|
+
|
56
|
+
table(line_items, cell_style: { border_color: 'cccccc' }) do
|
57
|
+
cells.padding = 12
|
58
|
+
cells.borders = []
|
59
|
+
row(0..borders).borders = [:bottom]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def footer
|
64
|
+
move_down 45
|
65
|
+
text company.fetch(:name), inline_format: true
|
66
|
+
text "<color rgb='888888'>#{company.fetch(:address)}</color>", inline_format: true
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/receipts.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'receipts/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "receipts"
|
8
|
+
spec.version = Receipts::VERSION
|
9
|
+
spec.authors = ["Chris Oliver"]
|
10
|
+
spec.email = ["excid3@gmail.com"]
|
11
|
+
spec.summary = %q{Receipts for your Rails application that works with any payment provider.}
|
12
|
+
spec.description = %q{Receipts for your Rails application that works with any payment provider.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
|
24
|
+
spec.add_dependency 'prawn', '~> 1.3.0'
|
25
|
+
spec.add_dependency 'prawn-table', '~> 0.2.1'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: receipts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Oliver
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: prawn
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.3.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.3.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: prawn-table
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.2.1
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.2.1
|
69
|
+
description: Receipts for your Rails application that works with any payment provider.
|
70
|
+
email:
|
71
|
+
- excid3@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- examples/receipt.pdf
|
82
|
+
- lib/receipts.rb
|
83
|
+
- lib/receipts/receipt.rb
|
84
|
+
- lib/receipts/version.rb
|
85
|
+
- receipts.gemspec
|
86
|
+
homepage: ''
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.2.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Receipts for your Rails application that works with any payment provider.
|
110
|
+
test_files: []
|