stripe_local 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +35 -0
  4. data/Rakefile +29 -0
  5. data/app/assets/javascripts/stripe_local/application.js +13 -0
  6. data/app/assets/javascripts/stripe_local/cards.js +2 -0
  7. data/app/assets/javascripts/stripe_local/charges.js +2 -0
  8. data/app/assets/javascripts/stripe_local/coupons.js +2 -0
  9. data/app/assets/javascripts/stripe_local/customers.js +2 -0
  10. data/app/assets/javascripts/stripe_local/discounts.js +2 -0
  11. data/app/assets/javascripts/stripe_local/invoices.js +2 -0
  12. data/app/assets/javascripts/stripe_local/line_items.js +2 -0
  13. data/app/assets/javascripts/stripe_local/plans.js +2 -0
  14. data/app/assets/javascripts/stripe_local/subscriptions.js +2 -0
  15. data/app/assets/stylesheets/stripe_local/application.css +13 -0
  16. data/app/assets/stylesheets/stripe_local/cards.css +4 -0
  17. data/app/assets/stylesheets/stripe_local/charges.css +4 -0
  18. data/app/assets/stylesheets/stripe_local/coupons.css +4 -0
  19. data/app/assets/stylesheets/stripe_local/customers.css +4 -0
  20. data/app/assets/stylesheets/stripe_local/discounts.css +4 -0
  21. data/app/assets/stylesheets/stripe_local/invoices.css +4 -0
  22. data/app/assets/stylesheets/stripe_local/line_items.css +4 -0
  23. data/app/assets/stylesheets/stripe_local/plans.css +4 -0
  24. data/app/assets/stylesheets/stripe_local/subscriptions.css +4 -0
  25. data/app/callbacks/stripe_local/plan_sync.rb +33 -0
  26. data/app/controllers/stripe_local/application_controller.rb +4 -0
  27. data/app/controllers/stripe_local/cards_controller.rb +6 -0
  28. data/app/controllers/stripe_local/charges_controller.rb +6 -0
  29. data/app/controllers/stripe_local/coupons_controller.rb +6 -0
  30. data/app/controllers/stripe_local/customers_controller.rb +6 -0
  31. data/app/controllers/stripe_local/discounts_controller.rb +6 -0
  32. data/app/controllers/stripe_local/invoices_controller.rb +6 -0
  33. data/app/controllers/stripe_local/line_items_controller.rb +6 -0
  34. data/app/controllers/stripe_local/plans_controller.rb +6 -0
  35. data/app/controllers/stripe_local/subscriptions_controller.rb +6 -0
  36. data/app/controllers/stripe_local/webhooks_controller.rb +13 -0
  37. data/app/helpers/stripe_local/application_helper.rb +4 -0
  38. data/app/helpers/stripe_local/cards_helper.rb +4 -0
  39. data/app/helpers/stripe_local/charges_helper.rb +4 -0
  40. data/app/helpers/stripe_local/coupons_helper.rb +4 -0
  41. data/app/helpers/stripe_local/customers_helper.rb +4 -0
  42. data/app/helpers/stripe_local/discounts_helper.rb +4 -0
  43. data/app/helpers/stripe_local/invoices_helper.rb +4 -0
  44. data/app/helpers/stripe_local/line_items_helper.rb +4 -0
  45. data/app/helpers/stripe_local/plans_helper.rb +4 -0
  46. data/app/helpers/stripe_local/subscriptions_helper.rb +4 -0
  47. data/app/mixins/stripe_local/object_adapter.rb +33 -0
  48. data/app/mixins/stripe_local/sync_plans.rb +39 -0
  49. data/app/models/stripe_local/balance.rb +48 -0
  50. data/app/models/stripe_local/card.rb +39 -0
  51. data/app/models/stripe_local/charge.rb +63 -0
  52. data/app/models/stripe_local/coupon.rb +21 -0
  53. data/app/models/stripe_local/customer.rb +98 -0
  54. data/app/models/stripe_local/discount.rb +36 -0
  55. data/app/models/stripe_local/invoice.rb +68 -0
  56. data/app/models/stripe_local/line_item.rb +56 -0
  57. data/app/models/stripe_local/plan.rb +41 -0
  58. data/app/models/stripe_local/subscription.rb +50 -0
  59. data/app/models/stripe_local/transaction.rb +45 -0
  60. data/app/models/stripe_local/transfer.rb +48 -0
  61. data/app/validators/email_pattern_validator.rb +7 -0
  62. data/app/validators/full_name_pattern_validator.rb +7 -0
  63. data/app/validators/legal_name_pattern_validator.rb +7 -0
  64. data/app/validators/phone_pattern_validator.rb +17 -0
  65. data/app/validators/plan_id_pattern_validator.rb +7 -0
  66. data/app/validators/slug_pattern_validator.rb +38 -0
  67. data/app/validators/state_abbreviation_validator.rb +13 -0
  68. data/app/validators/stripe_type_validator.rb +7 -0
  69. data/app/validators/zip_type_validator.rb +8 -0
  70. data/app/views/layouts/stripe_local/application.html.erb +14 -0
  71. data/config/routes.rb +22 -0
  72. data/db/migrate/20131122063517_load_stripe_tables.rb +200 -0
  73. data/lib/stripe_local/engine.rb +7 -0
  74. data/lib/stripe_local/version.rb +3 -0
  75. data/lib/stripe_local/webhook/subscriber.rb +34 -0
  76. data/lib/stripe_local/webhook/types.rb +45 -0
  77. data/lib/stripe_local/webhook.rb +55 -0
  78. data/lib/stripe_local/webhooks.rb +20 -0
  79. data/lib/stripe_local.rb +48 -0
  80. data/lib/tasks/stripe_local_tasks.rake +4 -0
  81. data/spec/dummy/README.rdoc +28 -0
  82. data/spec/dummy/Rakefile +6 -0
  83. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  84. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  85. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  86. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  87. data/spec/dummy/app/models/client.rb +4 -0
  88. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  89. data/spec/dummy/bin/bundle +3 -0
  90. data/spec/dummy/bin/rails +4 -0
  91. data/spec/dummy/bin/rake +4 -0
  92. data/spec/dummy/config/application.rb +21 -0
  93. data/spec/dummy/config/boot.rb +5 -0
  94. data/spec/dummy/config/database.yml +17 -0
  95. data/spec/dummy/config/environment.rb +5 -0
  96. data/spec/dummy/config/environments/development.rb +29 -0
  97. data/spec/dummy/config/environments/production.rb +80 -0
  98. data/spec/dummy/config/environments/test.rb +36 -0
  99. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  100. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  101. data/spec/dummy/config/initializers/inflections.rb +16 -0
  102. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  103. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  104. data/spec/dummy/config/initializers/session_store.rb +3 -0
  105. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  106. data/spec/dummy/config/locales/en.yml +23 -0
  107. data/spec/dummy/config/routes.rb +4 -0
  108. data/spec/dummy/config.ru +4 -0
  109. data/spec/dummy/db/migrate/20131102200937_create_clients.rb +11 -0
  110. data/spec/dummy/db/migrate/20131122104223_add_stripe_customer_id_to_clients.rb +5 -0
  111. data/spec/dummy/db/schema.rb +236 -0
  112. data/spec/dummy/log/development.log +1290 -0
  113. data/spec/dummy/log/test.log +7041 -0
  114. data/spec/dummy/public/404.html +58 -0
  115. data/spec/dummy/public/422.html +58 -0
  116. data/spec/dummy/public/500.html +57 -0
  117. data/spec/dummy/public/favicon.ico +0 -0
  118. data/spec/dummy/spec/spec_helper.rb +42 -0
  119. data/spec/fixtures/client_params.json +18 -0
  120. data/spec/fixtures/credit_card.json +18 -0
  121. data/spec/fixtures/stripe_local/balance_transactions.yml +25 -0
  122. data/spec/fixtures/stripe_local/cards.yml +21 -0
  123. data/spec/fixtures/stripe_local/charges.yml +37 -0
  124. data/spec/fixtures/stripe_local/coupons.yml +23 -0
  125. data/spec/fixtures/stripe_local/customers.yml +21 -0
  126. data/spec/fixtures/stripe_local/discounts.yml +13 -0
  127. data/spec/fixtures/stripe_local/invoices.yml +37 -0
  128. data/spec/fixtures/stripe_local/line_items.yml +19 -0
  129. data/spec/fixtures/stripe_local/plans.yml +17 -0
  130. data/spec/fixtures/stripe_local/subscriptions.yml +25 -0
  131. data/spec/fixtures/stripe_local/transfers.yml +17 -0
  132. data/spec/models/stripe_local/balance_spec.rb +47 -0
  133. data/spec/models/stripe_local/card_spec.rb +13 -0
  134. data/spec/models/stripe_local/charge_spec.rb +13 -0
  135. data/spec/models/stripe_local/customer_spec.rb +17 -0
  136. data/spec/models/stripe_local/invoice_spec.rb +17 -0
  137. data/spec/models/stripe_local/plan_spec.rb +33 -0
  138. data/spec/models/stripe_local/subscription_spec.rb +15 -0
  139. data/spec/models/stripe_local/transaction_spec.rb +14 -0
  140. data/spec/models/stripe_local/transfer_spec.rb +13 -0
  141. data/spec/spec_helper.rb +19 -0
  142. data/spec/webhook_fixtures/balance_transaction.json +23 -0
  143. data/spec/webhook_fixtures/charge.succeeded.json +45 -0
  144. data/spec/webhook_fixtures/customer.created.json +41 -0
  145. data/spec/webhook_fixtures/customer.subscription.created.json +27 -0
  146. data/spec/webhook_fixtures/customer_creation_response.json +66 -0
  147. data/spec/webhook_fixtures/invoice.payment_succeeded.json +55 -0
  148. data/spec/webhook_fixtures/plan.created.json +12 -0
  149. data/spec/webhook_fixtures/transfer.json +32 -0
  150. metadata +403 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae0a73dda734ad56a0fef09489b79ea06f339b19
4
+ data.tar.gz: 58b6831c0ef5cbbb834e68e4972f6958c45b60ae
5
+ SHA512:
6
+ metadata.gz: 6ec4268e51a6f3db8fd413e8cce695269abf324041f26b2a64cfbb8fe9fd4ebbef1bb94c130642d9df95a73df4ae04a688159ae7ce8e38a52dc845c2e469f91e
7
+ data.tar.gz: 7c46dd84d7a868e8001238461594e630ec5ae3d1c078649e897b206982cd8d4876367da076d2cedbf0f695545045b99474c461dad2c7a0841845851d9e17ec82
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Andy Cohen
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.md ADDED
@@ -0,0 +1,35 @@
1
+ # ♠ _Stripe Local_
2
+
3
+ A rails engine that maintains a local store of all your Stripe resources, keeping everything up to date with Stripe via their API and Webhook service.
4
+ Manages the complexities of keeping two data sources in sync while providing the speed and simplicity of dealing with purely local data in a purely synchronous fashion.
5
+
6
+ ## <b style="color:red;">[</b>UNDER CONSTRUCTION<b style="color:red;">]</b> Not in running condition!
7
+
8
+ StripeLocal is an attempt at extracting a framework for Stripe Integration that I have found myself building into several large scale CRM projects. It is my first Rails Engine and I am still learning the ends and outs of Engine building. If you find something I am doing terribly wrong, please bring it to my attention by opening an issue. I appreciate your help.
9
+
10
+ #### Feel free to fork and hack. I'm also happy to accept pull requests...
11
+
12
+
13
+
14
+ ## License
15
+
16
+ (The MIT License) *[Andy Cohen](mailto:outlawandy@gmail.com?)&nbsp;&copy;&nbsp;2012-2014*
17
+
18
+ Permission is hereby granted, free of charge, to any person obtaining
19
+ a copy of this software and associated documentation files (the
20
+ 'Software'), to deal in the Software without restriction, including
21
+ without limitation the rights to use, copy, modify, merge, publish,
22
+ distribute, sublicense, and/or sell copies of the Software, and to
23
+ permit persons to whom the Software is furnished to do so, subject to
24
+ the following conditions:
25
+
26
+ The above copyright notice and this permission notice shall be
27
+ included in all copies or substantial portions of the Software.
28
+
29
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
30
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'StripeLocal'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rspec/core/rake_task'
25
+
26
+
27
+ task test: :spec
28
+
29
+ task default: :test
@@ -0,0 +1,13 @@
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
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= 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,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,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,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,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,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,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,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,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,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,33 @@
1
+ module StripeLocal
2
+ class PlanSync
3
+ require 'stripe'
4
+
5
+ class<<self
6
+ def then_sync %i{from_hash}
7
+ Stripe::Plan.create from_hash
8
+ end
9
+
10
+ def then_destroy id
11
+ Stripe::Plan.retrieve( id ).delete
12
+ end
13
+ end
14
+
15
+ def after_create plan
16
+ attributes = {
17
+ id: plan.id,
18
+ name: plan.name,
19
+ amount: plan.amount,
20
+ currency: 'usd',
21
+ interval: plan.interval,
22
+ interval_count: plan.interval_count,
23
+ trial_period_days: 0
24
+ }
25
+ PlanSync.delay.then_sync attributes
26
+ end
27
+
28
+ def after_destroy plan
29
+ PlanSync.delay.then_destroy plan.id
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+ module StripeLocal
2
+ class ApplicationController < ::ApplicationController
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ require_dependency "stripe_local/application_controller"
2
+
3
+ module StripeLocal
4
+ class CardsController < ApplicationController
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require_dependency "stripe_local/application_controller"
2
+
3
+ module StripeLocal
4
+ class ChargesController < ApplicationController
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require_dependency "stripe_local/application_controller"
2
+
3
+ module StripeLocal
4
+ class CouponsController < ApplicationController
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require_dependency "stripe_local/application_controller"
2
+
3
+ module StripeLocal
4
+ class CustomersController < ApplicationController
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require_dependency "stripe_local/application_controller"
2
+
3
+ module StripeLocal
4
+ class DiscountsController < ApplicationController
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require_dependency "stripe_local/application_controller"
2
+
3
+ module StripeLocal
4
+ class InvoicesController < ApplicationController
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require_dependency "stripe_local/application_controller"
2
+
3
+ module StripeLocal
4
+ class LineItemsController < ApplicationController
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require_dependency "stripe_local/application_controller"
2
+
3
+ module StripeLocal
4
+ class PlansController < ApplicationController
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require_dependency "stripe_local/application_controller"
2
+
3
+ module StripeLocal
4
+ class SubscriptionsController < ApplicationController
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ module StripeLocal
2
+ module WebhooksController < ApplicationController
3
+ def events
4
+ begin
5
+ _event_ = Stripe::Event.retrieve params[:id]
6
+ Webhook.publish _event_
7
+ head :ok
8
+ rescue Stripe::StripeError
9
+ head :unauthorized
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ module StripeLocal
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module StripeLocal
2
+ module CardsHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module StripeLocal
2
+ module ChargesHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module StripeLocal
2
+ module CouponsHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module StripeLocal
2
+ module CustomersHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module StripeLocal
2
+ module DiscountsHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module StripeLocal
2
+ module InvoicesHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module StripeLocal
2
+ module LineItemsHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module StripeLocal
2
+ module PlansHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module StripeLocal
2
+ module SubscriptionsHelper
3
+ end
4
+ end
@@ -0,0 +1,33 @@
1
+ module StripeLocal
2
+ module ObjectAdapter
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def find_remote id
7
+ object = stripe_object.retrieve id
8
+ new object
9
+ end
10
+
11
+ # read access to stripe object
12
+ def stripe_object
13
+ @stripe_object ||= Object.qualified_const_get "Stripe::#{self.to_s.classify}"
14
+ end
15
+
16
+ #=!=#
17
+ # attr_accessor like <tt>macro</tt> for DateTime columns of a localized Stripe Object class
18
+ # to use:
19
+ # * Pass in one or more symbolized column names.
20
+ # Generates setter methods for converting JSON style `epoch` timestamps to the proper DateTime values your database expects.
21
+ # +Note:+ Regular <tt>DateTime</tt> objects are perfectly acceptable as arguments because the value is always coerced into an <tt>Integer</tt> before conversion.
22
+ #=¡=#
23
+ def time_writer *array_of_syms
24
+ array_of_syms.each do |sym|
25
+ define_method ":#{sym}=" do |epoch|
26
+ write_attribute sym, Time.at( epoch.to_i ) unless epoch.nil?
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ module StripeLocal
2
+ module SyncPlans
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ after_create :synchronize_create
7
+
8
+ private :synchronize_create, :synchronize_destroy
9
+ end
10
+
11
+ module ClassMethods
12
+
13
+ def delete id, destroy_remote = true
14
+ if super( id )
15
+ synchronize_destroy( id ) if destroy_remote == true
16
+ end
17
+ end
18
+ end
19
+
20
+ def synchronize_create
21
+ hash = self.attributes
22
+ unless !!hash.delete( :synced )
23
+ Stripe::Plan.create hash #TODO: handle asynchronously
24
+ end
25
+ end
26
+
27
+ def synchronize_destroy id
28
+ begin
29
+ Stripe::Plan.delete id
30
+ rescue Stripe::Error
31
+ end
32
+ end
33
+
34
+ def api_destroy plan
35
+ Plan.destroy
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,48 @@
1
+ module StripeLocal
2
+ class Balance < ActiveRecord::Base
3
+ include ObjectAdapter
4
+
5
+ @@cache ||= {}
6
+ after_create do |record|
7
+ @@cache = { current: record.available, pending: record.pending }
8
+ end
9
+
10
+ class<<self
11
+ def available
12
+ @@cache[:current] ||= all.pluck( :available ).last || 0
13
+ end
14
+ alias :current :available
15
+
16
+ def pending
17
+ @@cache[:pending] ||= all.pluck( :pending ).last || 0
18
+ end
19
+
20
+ def previous_available
21
+ @@cache[:previous] ||= all.pluck( :available )[-2] || 0
22
+ end
23
+ alias :previous :previous_available
24
+
25
+ def previous_pending
26
+ @@cache[:previous_pending] ||= all.pluck( :pending )[-2] || 0
27
+ end
28
+
29
+ def changed
30
+ @@cache[:changed] ||= ( self.current - self.previous )
31
+ end
32
+
33
+ def event update
34
+ if ( self.pending == update[:pending] ) && ( self.available == update[:available] )
35
+ Balance.last.touch
36
+ else
37
+ Balance.create( update )
38
+ end
39
+ end
40
+ end
41
+
42
+ def to_s
43
+ Balance.available.to_money
44
+ end
45
+ alias :inspect :to_s
46
+ end
47
+
48
+ end
@@ -0,0 +1,39 @@
1
+ module StripeLocal
2
+ class Card < ActiveRecord::Base
3
+ primary_key = :id
4
+
5
+ has_many :charges, inverse_of: :card
6
+ belongs_to :customer, inverse_of: :cards
7
+
8
+ class<<self
9
+ def create attrs_hash
10
+ super normalize( attrs_hash )
11
+ end
12
+
13
+ def normalize hash
14
+ hash.each_with_object({}) do |(k,v),h|
15
+ key = case k.to_sym
16
+ when :customer then :customer_id
17
+ when :type then :brand
18
+ when ->(x){attribute_method? x} then k.to_sym
19
+ else next
20
+ end
21
+ h[key] = v
22
+ end
23
+ end
24
+ end
25
+
26
+ # =!=#>>>
27
+ # string :id
28
+ # string :customer_id
29
+ # string :name
30
+ # integer :exp_month
31
+ # integer :exp_year
32
+ # string :brand
33
+ # string :last4
34
+ # string :cvc_check
35
+ # string :fingerprint
36
+ # =¡=#>>>
37
+ end
38
+
39
+ end
@@ -0,0 +1,63 @@
1
+ module StripeLocal
2
+ class Charge < ActiveRecord::Base
3
+ include ObjectAdapter
4
+
5
+ belongs_to :card, inverse_of: :charges
6
+ belongs_to :customer, inverse_of: :charges
7
+ has_one :transaction, as: :source
8
+ has_one :invoice, inverse_of: :charge
9
+
10
+ self.primary_key = :id
11
+
12
+ time_writer :created
13
+
14
+ def metadata= hash
15
+ MultiJson.dump hash
16
+ end
17
+
18
+ def metadata
19
+ MultiJson.load read_attribute( :metadata ), symbolize_keys: true
20
+ end
21
+
22
+ class<<self
23
+ def create object
24
+ super normalize( object )
25
+ end
26
+
27
+ def normalize attrs
28
+ attrs.each_with_object({}) do |(k,v),h|
29
+ key = case k.to_sym
30
+ when :customer then :customer_id
31
+ when :invoice then :invoice_id
32
+ when :balance_transaction then :transaction_id
33
+ when :card then h[:card_id] = v.id and next
34
+ when :created then h[:created] = Time.at(v) and next
35
+ when ->(x){attribute_method? x} then k.to_sym
36
+ else next
37
+ end
38
+ h[key] = v
39
+ end
40
+ end
41
+ end
42
+
43
+
44
+ #=!=#>>>
45
+ # string :id
46
+ # string :card_id
47
+ # string :customer_id
48
+ # string :invoice_id
49
+ # string :transaction_id
50
+ # integer :amount
51
+ # boolean :captured
52
+ # boolean :refunded
53
+ # boolean :paid
54
+ # datetime :created
55
+ # string :currency
56
+ # integer :amount_refunded
57
+ # string :description
58
+ # string :failure_code
59
+ # string :failure_message
60
+ # text :metadata
61
+ #=¡=#>>>
62
+ end
63
+ end
@@ -0,0 +1,21 @@
1
+ module StripeLocal
2
+ class Coupon < ActiveRecord::Base
3
+ include ObjectAdapter
4
+
5
+ time_writer :redeem_by
6
+
7
+
8
+ #=!=#>>>
9
+ # string :id
10
+ # integer :percent_off
11
+ # integer :amount_off
12
+ # string :currency
13
+ # string :duration
14
+ # datetime :redeem_by
15
+ # integer :max_redemptions
16
+ # integer :times_redeemed
17
+ # integer :duration_in_months
18
+ # boolean :synced
19
+ #=¡=#>>>
20
+ end
21
+ end