stripe_invoice 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +40 -0
  5. data/app/assets/javascripts/stripe_invoice/application.js +15 -0
  6. data/app/assets/javascripts/stripe_invoice/invoices.js +2 -0
  7. data/app/assets/stylesheets/stripe_invoice/application.css +13 -0
  8. data/app/assets/stylesheets/stripe_invoice/invoices.css +8 -0
  9. data/app/controllers/stripe_invoice/application_controller.rb +6 -0
  10. data/app/controllers/stripe_invoice/invoices_controller.rb +51 -0
  11. data/app/helpers/stripe_invoice/application_helper.rb +10 -0
  12. data/app/helpers/stripe_invoice/invoices_helper.rb +33 -0
  13. data/app/models/stripe_invoice/invoice.rb +43 -0
  14. data/app/views/layouts/stripe_invoice/application.html.erb +14 -0
  15. data/app/views/stripe_invoice/invoices/index.html.haml +12 -0
  16. data/app/views/stripe_invoice/invoices/show.html.haml +25 -0
  17. data/config/routes.rb +3 -0
  18. data/db/migrate/20140605194026_create_stripe_invoice_invoices.rb +14 -0
  19. data/lib/generators/stripe_invoice/install_generator.rb +32 -0
  20. data/lib/generators/stripe_invoice/views_generator.rb +28 -0
  21. data/lib/stripe_invoice.rb +41 -0
  22. data/lib/stripe_invoice/engine.rb +5 -0
  23. data/lib/stripe_invoice/version.rb +3 -0
  24. data/lib/tasks/stripe_invoice_tasks.rake +4 -0
  25. data/test/dummy/README.rdoc +261 -0
  26. data/test/dummy/Rakefile +7 -0
  27. data/test/dummy/app/assets/javascripts/application.js +15 -0
  28. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  29. data/test/dummy/app/controllers/application_controller.rb +3 -0
  30. data/test/dummy/app/helpers/application_helper.rb +2 -0
  31. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  32. data/test/dummy/config.ru +4 -0
  33. data/test/dummy/config/application.rb +59 -0
  34. data/test/dummy/config/boot.rb +10 -0
  35. data/test/dummy/config/database.yml +25 -0
  36. data/test/dummy/config/environment.rb +5 -0
  37. data/test/dummy/config/environments/development.rb +37 -0
  38. data/test/dummy/config/environments/production.rb +67 -0
  39. data/test/dummy/config/environments/test.rb +37 -0
  40. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  41. data/test/dummy/config/initializers/inflections.rb +15 -0
  42. data/test/dummy/config/initializers/mime_types.rb +5 -0
  43. data/test/dummy/config/initializers/secret_token.rb +7 -0
  44. data/test/dummy/config/initializers/session_store.rb +8 -0
  45. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  46. data/test/dummy/config/locales/en.yml +5 -0
  47. data/test/dummy/config/routes.rb +4 -0
  48. data/test/dummy/public/404.html +26 -0
  49. data/test/dummy/public/422.html +26 -0
  50. data/test/dummy/public/500.html +25 -0
  51. data/test/dummy/public/favicon.ico +0 -0
  52. data/test/dummy/script/rails +6 -0
  53. data/test/fixtures/stripe_invoice/invoices.yml +11 -0
  54. data/test/functional/stripe_invoice/invoices_controller_test.rb +9 -0
  55. data/test/integration/navigation_test.rb +10 -0
  56. data/test/stripe_invoice_test.rb +7 -0
  57. data/test/test_helper.rb +15 -0
  58. data/test/unit/helpers/stripe_invoice/invoices_helper_test.rb +6 -0
  59. data/test/unit/stripe_invoice/invoice_test.rb +9 -0
  60. metadata +222 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6cb60659317460b41ce49d43fa4b9c76a500d021
4
+ data.tar.gz: 3496a8652956ff5260ec4a9bace02eba69099f9d
5
+ SHA512:
6
+ metadata.gz: 2f8847d88e2b093e35a6af3d4d999469789313bccd488fbd05be48960ec099a0e28492b0bfc2b3c6536e997c3a5fed39e04cc2777f70a819a2d01f3e0b8989ff
7
+ data.tar.gz: 168dfd3d1524db54a61c580372c5d727b9fea4e2a4a58eeafdf214f7b9a4596380ae66721f85b1a118028483b07353ac854833289081fb2a633de1b0d9939682
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = StripeInvoice
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'StripeInvoice'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,8 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
5
+
6
+ table.invoice_summary td {
7
+ text-align: right;
8
+ }
@@ -0,0 +1,6 @@
1
+ module StripeInvoice
2
+ class ApplicationController < ::ApplicationController
3
+ layout 'application'
4
+ helper :application
5
+ end
6
+ end
@@ -0,0 +1,51 @@
1
+ require_dependency "stripe_invoice/application_controller"
2
+ require 'pdfkit' # no auto_require for gems in .gemspec
3
+
4
+ module StripeInvoice
5
+ class InvoicesController < ApplicationController
6
+
7
+ def index
8
+ @owner = send("current_#{::Koudoku.subscriptions_owned_by}")
9
+ redirect_to '/' unless @owner
10
+
11
+ @subscription = @owner.subscription
12
+
13
+ if @subscription
14
+ # I know this looks silly, but Koudoku actually stores the customer's stripe_id
15
+ # in the subscription model
16
+ @stripe_invoices = Stripe::Invoice.all(
17
+ :customer => @subscription.stripe_id,
18
+ :limit => 100)
19
+
20
+ @stripe_invoices.each do |sinvoice|
21
+ next if Invoice.find_by_stripe_id(sinvoice.id)
22
+ Invoice.create({
23
+ stripe_id: sinvoice.id,
24
+ owner_id: @owner.id,
25
+ date: sinvoice.date,
26
+ invoice_number: "ls-#{Date.today.year}-#{(Invoice.last ? (Invoice.last.id * 7) : 1).to_s.rjust(5, '0')}",
27
+ json: sinvoice
28
+ })
29
+ end
30
+ end # if subscription
31
+
32
+ @invoices = Invoice.where(owner_id: @owner.id).order('date DESC')
33
+ end
34
+
35
+ def show
36
+ @owner = send("current_#{::Koudoku.subscriptions_owned_by}")
37
+ redirect_to '/' unless @owner
38
+
39
+ @invoice = Invoice.find(params[:id])
40
+
41
+ respond_to do |format|
42
+ format.html {render layout: false}
43
+ format.pdf do
44
+ html = render_to_string(layout: false, formats: [:html])
45
+ pdf = ::PDFKit.new(html, page_size: 'Letter')
46
+ send_data(pdf.to_pdf, filename: "invoice-#{@invoice.number}.pdf")
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,10 @@
1
+ module StripeInvoice
2
+ module ApplicationHelper
3
+
4
+ def format_timestamp(timestamp)
5
+ l(Time.at(timestamp).to_datetime, :format => :short_date)
6
+ end
7
+
8
+ alias_method :ft, :format_timestamp
9
+ end
10
+ end
@@ -0,0 +1,33 @@
1
+ module StripeInvoice
2
+ module InvoicesHelper
3
+
4
+ # nicely formats the amount and currency
5
+ def format_currency(amount, currency)
6
+
7
+ currency = currency.upcase()
8
+ # assuming that all currencies are split into 100 parts is probably wrong
9
+ # on an i18n scale but it works for USD, EUR and LBP
10
+ # TODO fix this maybe?
11
+ amount = amount / 100
12
+ options = {
13
+ # use comma for euro
14
+ separator: currency == 'EUR' ? ',' : '.',
15
+ delimiter: currency == 'EUR' ? '.' : ',',
16
+ format: currency == 'EUR' ? '%n %u' : '%u%n',
17
+ }
18
+ case currency
19
+ when 'EUR'
20
+ options[:unit] = '€'
21
+ when 'LBP'
22
+ options[:unit] = '£'
23
+ when 'USD'
24
+ options[:unit] = '$'
25
+ else
26
+ options[:unit] = currency
27
+ end
28
+ return number_to_currency(amount, options)
29
+ end
30
+
31
+ alias_method :fc, :format_currency
32
+ end
33
+ end
@@ -0,0 +1,43 @@
1
+ module StripeInvoice
2
+ class Invoice < ActiveRecord::Base
3
+ attr_accessible :id, :invoice_number, :stripe_id, :json, :owner_id, :date
4
+
5
+ alias_attribute :number, :invoice_number
6
+
7
+ serialize :json, JSON
8
+
9
+
10
+ def indifferent_json
11
+ @json ||= json.with_indifferent_access
12
+ end
13
+
14
+
15
+ def subtotal
16
+ indifferent_json[:subtotal]
17
+ end
18
+
19
+ def discount
20
+ indifferent_json[:disocunt]
21
+ end
22
+
23
+ def total
24
+ indifferent_json[:total]
25
+ end
26
+
27
+ def period_start
28
+ indifferent_json[:period_start]
29
+ end
30
+
31
+ def period_end
32
+ indifferent_json[:period_end]
33
+ end
34
+
35
+ def currency
36
+ indifferent_json[:currency]
37
+ end
38
+
39
+ def lines
40
+ indifferent_json[:lines]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>StripeInvoice</title>
5
+ <%= stylesheet_link_tag "stripe_invoice/application", :media => "all" %>
6
+ <%= javascript_include_tag "stripe_invoice/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,12 @@
1
+ .row
2
+ .col-md-offset-1.col-md-10
3
+ %table.table.table-condensed.table-bordered.table-striped
4
+ %tr
5
+ %th id
6
+ %th usage period
7
+ %th total
8
+ - @invoices.each do |invoice|
9
+ %tr
10
+ %td= link_to invoice.id, invoice_path(invoice.id)
11
+ %td= "#{l(Time.at(invoice.period_start).to_datetime, :format => :short_date)} - #{l(Time.at(invoice.period_end).to_datetime, :format => :short_date)}"
12
+ %td= format_currency invoice.total, invoice.currency
@@ -0,0 +1,25 @@
1
+ %table(border=1)
2
+ %tr
3
+ %td name
4
+ %td value
5
+ %tr
6
+ %td id
7
+ %td= @invoice.id
8
+ %tr
9
+ %td period_start
10
+ %td= @invoice.period_start
11
+ %tr
12
+ %td period_end
13
+ %td= @invoice.period_end
14
+ %tr
15
+ %td subtotal
16
+ %td= format_currency(@invoice.subtotal, @invoice.currency)
17
+ - if @invoice.discount
18
+ %tr
19
+ %td discount
20
+ %td= format_currency(@invoice.discount, @invoice.currency)
21
+ %tr
22
+ %td total
23
+ %td= format_currency(@invoice.total, @invoice.currency)
24
+
25
+
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ StripeInvoice::Engine.routes.draw do
2
+ resources :invoices
3
+ end
@@ -0,0 +1,14 @@
1
+ class CreateStripeInvoiceInvoices < ActiveRecord::Migration
2
+ def change
3
+ create_table :stripe_invoice_invoices do |t|
4
+ t.integer :id
5
+ t.integer :owner_id
6
+ t.string :stripe_id
7
+ t.string :invoice_number
8
+ t.integer :date
9
+ t.text :json
10
+
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+
2
+ require 'rails/generators'
3
+
4
+ module StripeInvoice
5
+ class InstallGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+ source_root File.expand_path("../../../../app/views/stripe_invoice/invoices", __FILE__)
8
+ desc "installs stripe_invoice"
9
+
10
+ def install
11
+ unless defined?(StripeInvoice)
12
+ gem 'stripe_invoice'
13
+ end
14
+
15
+ api_key = SecureRandom.uuid
16
+
17
+ create_initializer api_key
18
+ say "Your webhooks API key is: #{api_key}"
19
+ end
20
+
21
+ def create_initializer(api_key)
22
+ create_file 'config/initializers/stripe_invoice.rb' do
23
+ <<-RUBY
24
+ StripeInvoice.setup do |config|
25
+ config.webhooks_api_key = "#{api_key}"
26
+ config.subscriptions_owned_by = :user
27
+ end
28
+ RUBY
29
+ end # create initializer file
30
+ end # create_initializer
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+
2
+ require 'rails/generators'
3
+
4
+ module StripeInvoice
5
+ class ViewsGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+ source_root "#{StripeInvoice::Engine.root}/app/views/stripe_invoice/invoices"
8
+ desc "installs stripe_invoice views into an application near you"
9
+
10
+ def install
11
+ say "copying StripeInvoice view files"
12
+ view_files.each do |file|
13
+ copy_file "#{file}", "app/views/stripe_invoice/invoices/#{file}"
14
+ end
15
+ end
16
+
17
+ # returns all files for the invoice views
18
+ def view_files
19
+ Dir.entries(engine_directory_path) - %w[. ..]
20
+ end
21
+
22
+ # shortcut for the directory path
23
+ def engine_directory_path
24
+ "#{StripeInvoice::Engine.root}/app/views/stripe_invoice/invoices"
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,41 @@
1
+ # shamelessly plugged from koudoku (http://github.com/andrewculver/koudoku)
2
+
3
+ require "stripe_invoice/engine"
4
+
5
+ require 'generators/stripe_invoice/install_generator'
6
+ require 'generators/stripe_invoice/views_generator'
7
+
8
+ module StripeInvoice
9
+ mattr_accessor :webhooks_api_key
10
+ @@webhooks_api_key = nil
11
+
12
+ mattr_accessor :subscriptions_owned_by
13
+ @@subscriptions_owned_by = nil
14
+
15
+ def self.setup
16
+ yield self
17
+
18
+ end
19
+
20
+ # e.g. :users
21
+ def self.owner_resource
22
+ subscriptions_owned_by.to_s.pluralize.to_sym
23
+ end
24
+
25
+ # e.g. :user_id
26
+ def self.owner_id_sym
27
+ # e.g. :user_id
28
+ (subscriptions_owned_by.to_s + '_id').to_sym
29
+ end
30
+
31
+ def self.owner_assignment_sym
32
+ # e.g. :user=
33
+ (subscriptions_owned_by.to_s + '=').to_sym
34
+ end
35
+
36
+ # e.g. Users
37
+ def self.owner_class
38
+ # e.g. User
39
+ subscriptions_owned_by.to_s.classify.constantize
40
+ end
41
+ end