pay_me 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +111 -0
- data/Rakefile +33 -0
- data/app/controllers/pay_me/api/v1/invoices_controller.rb +30 -0
- data/app/controllers/pay_me/api/v1/stripe_plans_controller.rb +35 -0
- data/app/controllers/pay_me/application_controller.rb +10 -0
- data/app/helpers/pay_me/application_helper.rb +4 -0
- data/app/subscribers/pay_me/customer_subscriber.rb +42 -0
- data/app/subscribers/pay_me/plan_subscriber.rb +38 -0
- data/app/subscribers/pay_me/subscription_subscriber.rb +50 -0
- data/app/subscribers/pay_me/webhook_event_subscriber.rb +11 -0
- data/config/routes.rb +11 -0
- data/db/migrate/20160725204454_create_pay_me_subscriptions.rb +16 -0
- data/db/migrate/20161011170659_create_pay_me_plans.rb +8 -0
- data/db/migrate/20171030174731_add_quantity_to_pay_me_subscription.rb +6 -0
- data/db/migrate/20171130192525_create_pay_me_customers.rb +11 -0
- data/db/migrate/20180116193943_remove_customerable_from_subscriptions.rb +11 -0
- data/db/migrate/20180215235017_add_past_due_to_subscriptions.rb +5 -0
- data/lib/generators/pay_me/controllers/controllers_generator.rb +11 -0
- data/lib/generators/pay_me/controllers/templates/subscriptions_controller.rb +12 -0
- data/lib/generators/pay_me/install_generator.rb +39 -0
- data/lib/generators/pay_me/migrate_to_customer_model/USAGE +8 -0
- data/lib/generators/pay_me/migrate_to_customer_model/migrate_to_customer_model_generator.rb +9 -0
- data/lib/generators/pay_me/migrate_to_customer_model/templates/migration.rb.erb +64 -0
- data/lib/generators/pay_me/models/models_generator.rb +13 -0
- data/lib/generators/pay_me/models/templates/customer.rb +14 -0
- data/lib/generators/pay_me/models/templates/plan.rb +14 -0
- data/lib/generators/pay_me/models/templates/subscription.rb +14 -0
- data/lib/generators/pay_me/policies/policies_generator.rb +11 -0
- data/lib/generators/pay_me/policies/templates/subscription_policy.rb +34 -0
- data/lib/generators/pay_me/templates/pay_me_initializer.rb +17 -0
- data/lib/pay_me.rb +28 -0
- data/lib/pay_me/active_record.rb +6 -0
- data/lib/pay_me/concerns/controllers/customerable.rb +116 -0
- data/lib/pay_me/concerns/models/customerable.rb +54 -0
- data/lib/pay_me/concerns/models/stripe_customerable.rb +24 -0
- data/lib/pay_me/concerns/models/stripe_plannable.rb +31 -0
- data/lib/pay_me/concerns/models/stripe_subscribable.rb +43 -0
- data/lib/pay_me/configuration.rb +27 -0
- data/lib/pay_me/engine.rb +8 -0
- data/lib/pay_me/models.rb +4 -0
- data/lib/pay_me/railtie.rb +2 -0
- data/lib/pay_me/route_helpers.rb +21 -0
- data/lib/pay_me/services/customer.rb +141 -0
- data/lib/pay_me/version.rb +3 -0
- data/lib/pay_me/view_models/charge.rb +48 -0
- data/lib/tasks/pay_me_tasks.rake +4 -0
- data/lib/tasks/stripe.rake +22 -0
- data/test/controllers/pay_me/api/v1/invoices_controller_test.rb +82 -0
- data/test/controllers/pay_me/api/v1/plans_controller_test.rb +62 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/api/v1/users_controller.rb +10 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/pay_me/api/v1/subscriptions_controller.rb +12 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/pay_me/customer.rb +5 -0
- data/test/dummy/app/models/pay_me/plan.rb +5 -0
- data/test/dummy/app/models/pay_me/subscription.rb +5 -0
- data/test/dummy/app/models/user.rb +8 -0
- data/test/dummy/app/policies/pay_me/subscription_policy.rb +34 -0
- data/test/dummy/app/serializers/pay_me/subscription_serializer.rb +3 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +29 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +25 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +41 -0
- data/test/dummy/config/environments/production.rb +79 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/pay_me.rb +17 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +10 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/db/migrate/20160630181300_create_pay_me_users.rb +12 -0
- data/test/dummy/db/migrate/20180118001940_api_me_migrate_to_customer_model.rb +9 -0
- data/test/dummy/db/schema.rb +53 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/integration/customer_test.rb +395 -0
- data/test/lib/generators/pay_me/pay_me/migrate_to_customer_model_generator_test.rb +16 -0
- data/test/models/pay_me/customer_test.rb +9 -0
- data/test/models/pay_me/subscription_test.rb +9 -0
- data/test/models/pay_me/user_test.rb +6 -0
- data/test/pay_me_test.rb +7 -0
- data/test/support/concerns/api_test_helper.rb +15 -0
- data/test/support/concerns/stripe_helpers.rb +25 -0
- data/test/test_helper.rb +52 -0
- data/test/unit/concerns/customerable.rb +21 -0
- data/test/unit/models/customerable_test.rb +18 -0
- data/test/unit/services/customer_test.rb +75 -0
- data/test/unit/stubs/customerable.rb +28 -0
- data/test/unit/stubs/customerable_test.rb +16 -0
- data/test/webhooks/customer_webhook_test.rb +74 -0
- data/test/webhooks/plan_webhook_test.rb +74 -0
- metadata +288 -0
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module PayMe
|
4
|
+
module Api
|
5
|
+
module V1
|
6
|
+
class InvoicesControllerTest < ActionController::TestCase
|
7
|
+
setup do
|
8
|
+
StripeMock.start
|
9
|
+
@stripe_helper = StripeMock.create_test_helper
|
10
|
+
@customer = User.create!(email: 'person@wild.land')
|
11
|
+
@customer.add_default_payment_source(
|
12
|
+
@stripe_helper.generate_card_token(
|
13
|
+
{
|
14
|
+
number: '4242424242424242',
|
15
|
+
exp_month: 1,
|
16
|
+
exp_year: (DateTime.now + 1.year).year,
|
17
|
+
cvc: '123'
|
18
|
+
}
|
19
|
+
)
|
20
|
+
)
|
21
|
+
@plan = @stripe_helper.create_plan(
|
22
|
+
amount: 2000,
|
23
|
+
interval: 'month',
|
24
|
+
name: 'This is a automated tests monthly plan',
|
25
|
+
currency: 'usd',
|
26
|
+
id: 'AutoTest'
|
27
|
+
)
|
28
|
+
@subscription = Stripe::Subscription.create(
|
29
|
+
customer: @customer.stripe_customer_id,
|
30
|
+
plan: @plan.id
|
31
|
+
)
|
32
|
+
@invoice_item = Stripe::InvoiceItem.create(
|
33
|
+
customer: @customer.stripe_customer_id,
|
34
|
+
amount: 1000,
|
35
|
+
currency: 'usd',
|
36
|
+
description: 'One-time setup fee'
|
37
|
+
)
|
38
|
+
@invoice = Stripe::Invoice.create(customer: @customer.stripe_customer_id)
|
39
|
+
@routes = Engine.routes
|
40
|
+
end
|
41
|
+
|
42
|
+
teardown do
|
43
|
+
StripeMock.stop
|
44
|
+
end
|
45
|
+
|
46
|
+
test 'should get index' do
|
47
|
+
get :index
|
48
|
+
assert_response :success
|
49
|
+
|
50
|
+
json = JSON.parse(response.body)
|
51
|
+
refute_empty json['invoices']
|
52
|
+
assert_includes json['invoices'].map { |i| i['stripe_id'] }, @invoice.id
|
53
|
+
end
|
54
|
+
|
55
|
+
test 'should show invoice' do
|
56
|
+
get :show, id: @invoice.id
|
57
|
+
|
58
|
+
assert_response :success
|
59
|
+
|
60
|
+
json = JSON.parse(response.body)
|
61
|
+
|
62
|
+
@invoice.as_json.sort.each do |key, val|
|
63
|
+
next if val.nil?
|
64
|
+
skip if key == 'webhooks_delivered_at'
|
65
|
+
key = 'stripe_id' if key == 'id'
|
66
|
+
assert_equal val, json['invoice'][key]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
test 'should handle non invoice id' do
|
71
|
+
get :show, id: 'not_a_real_id'
|
72
|
+
|
73
|
+
assert_response :not_found
|
74
|
+
|
75
|
+
json = JSON.parse(response.body)
|
76
|
+
refute_nil json['errors']
|
77
|
+
assert_match(/no such invoice/i, json['reason'])
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module PayMe
|
4
|
+
module Api
|
5
|
+
module V1
|
6
|
+
class PlansControllerTest < ActionController::TestCase
|
7
|
+
setup do
|
8
|
+
StripeMock.start
|
9
|
+
@stripe_helper = StripeMock.create_test_helper
|
10
|
+
@plan = @stripe_helper.create_plan(
|
11
|
+
amount: 2000,
|
12
|
+
interval: 'month',
|
13
|
+
name: 'This is a automated tests monthly plan',
|
14
|
+
currency: 'usd',
|
15
|
+
id: 'AutoTest'
|
16
|
+
)
|
17
|
+
@routes = Engine.routes
|
18
|
+
end
|
19
|
+
|
20
|
+
teardown do
|
21
|
+
StripeMock.stop
|
22
|
+
# @plan.delete unless @plan.nil?
|
23
|
+
end
|
24
|
+
|
25
|
+
test 'should get index' do
|
26
|
+
skip("ApiMe integration")
|
27
|
+
get :index
|
28
|
+
assert_response :success
|
29
|
+
|
30
|
+
json = JSON.parse(response.body)
|
31
|
+
refute_empty json['plans']
|
32
|
+
assert_includes json['plans'].map { |i| i['stripe_id'] }, @plan.id
|
33
|
+
end
|
34
|
+
|
35
|
+
test 'should show plan' do
|
36
|
+
skip("ApiMe integration")
|
37
|
+
get :show, id: @plan.id
|
38
|
+
|
39
|
+
assert_response :success
|
40
|
+
|
41
|
+
json = JSON.parse(response.body)
|
42
|
+
@plan.as_json.sort.each do |key, val|
|
43
|
+
skip if key == 'webhooks_delivered_at'
|
44
|
+
key = 'stripe_id' if key == 'id'
|
45
|
+
assert_equal val, json['plan'][key]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
test 'should handle non plan id' do
|
50
|
+
skip("ApiMe integration")
|
51
|
+
get :show, id: 'not_a_real_id'
|
52
|
+
|
53
|
+
assert_response :not_found
|
54
|
+
|
55
|
+
json = JSON.parse(response.body)
|
56
|
+
refute_nil json['errors']
|
57
|
+
assert_match(/no such plan/i, json['reason'])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
@@ -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 any plugin's vendor/assets/javascripts directory 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/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
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 any plugin's vendor/assets/stylesheets directory 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 bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module PayMe
|
2
|
+
class SubscriptionPolicy < ApplicationPolicy
|
3
|
+
###
|
4
|
+
# Add the policy
|
5
|
+
###
|
6
|
+
# def create?
|
7
|
+
# record.owner_id == current_user.id || super
|
8
|
+
# end
|
9
|
+
|
10
|
+
# def show?
|
11
|
+
# true
|
12
|
+
# end
|
13
|
+
|
14
|
+
# def update?
|
15
|
+
# create?
|
16
|
+
# end
|
17
|
+
|
18
|
+
# def destroy?
|
19
|
+
# record.owner_id == current_user.id || super
|
20
|
+
# end
|
21
|
+
|
22
|
+
# def accept?
|
23
|
+
# current_user
|
24
|
+
# end
|
25
|
+
|
26
|
+
# def decline?
|
27
|
+
# current_user
|
28
|
+
# end
|
29
|
+
|
30
|
+
# def permitted_attributes
|
31
|
+
# [:email, :accepted, :meta]
|
32
|
+
# end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
6
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/test/dummy/bin/rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
6
|
+
|
7
|
+
Dir.chdir APP_ROOT do
|
8
|
+
# This script is a starting point to setup your application.
|
9
|
+
# Add necessary setup steps to this file:
|
10
|
+
|
11
|
+
puts '== Installing dependencies =='
|
12
|
+
system 'gem install bundler --conservative'
|
13
|
+
system 'bundle check || bundle install'
|
14
|
+
|
15
|
+
# puts "\n== Copying sample files =="
|
16
|
+
# unless File.exist?("config/database.yml")
|
17
|
+
# system "cp config/database.yml.sample config/database.yml"
|
18
|
+
# end
|
19
|
+
|
20
|
+
puts "\n== Preparing database =="
|
21
|
+
system 'bin/rake db:setup'
|
22
|
+
|
23
|
+
puts "\n== Removing old logs and tempfiles =="
|
24
|
+
system 'rm -f log/*'
|
25
|
+
system 'rm -rf tmp/cache'
|
26
|
+
|
27
|
+
puts "\n== Restarting application server =="
|
28
|
+
system 'touch tmp/restart.txt'
|
29
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require 'rails/all'
|
4
|
+
|
5
|
+
Bundler.require(*Rails.groups)
|
6
|
+
require 'pay_me'
|
7
|
+
|
8
|
+
module Dummy
|
9
|
+
class Application < Rails::Application
|
10
|
+
# Settings in config/environments/* take precedence over those specified here.
|
11
|
+
# Application configuration should go into files in config/initializers
|
12
|
+
# -- all .rb files in that directory are automatically loaded.
|
13
|
+
|
14
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
15
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
16
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
17
|
+
|
18
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
19
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
20
|
+
# config.i18n.default_locale = :de
|
21
|
+
|
22
|
+
# Do not swallow errors in after_commit/after_rollback callbacks.
|
23
|
+
config.active_record.raise_in_transactional_callbacks = true
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# SQLite version 3.x
|
2
|
+
# gem install sqlite3
|
3
|
+
#
|
4
|
+
# Ensure the SQLite 3 gem is defined in your Gemfile
|
5
|
+
# gem 'sqlite3'
|
6
|
+
#
|
7
|
+
default: &default
|
8
|
+
adapter: sqlite3
|
9
|
+
pool: 5
|
10
|
+
timeout: 5000
|
11
|
+
|
12
|
+
development:
|
13
|
+
<<: *default
|
14
|
+
database: db/development.sqlite3
|
15
|
+
|
16
|
+
# Warning: The database defined as "test" will be erased and
|
17
|
+
# re-generated from your development database when you run "rake".
|
18
|
+
# Do not set this db to the same as development or production.
|
19
|
+
test:
|
20
|
+
<<: *default
|
21
|
+
database: db/test.sqlite3
|
22
|
+
|
23
|
+
production:
|
24
|
+
<<: *default
|
25
|
+
database: db/production.sqlite3
|