rhino_project_subscriptions 0.20.0.alpha.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 97b95f54a8f041ad77905df6aa40be66592d3fad09323f86a176d35b2db6b8fd
4
+ data.tar.gz: e94969cce3e189625aa621c85f5c624e94c3aa9c4646e3fc65eab8f5a17b5a50
5
+ SHA512:
6
+ metadata.gz: 334fcac7368f6c1e9184dac5613295f798a3bed4186e0166b5034f6ae3037a43ae43cfc5a0861e6ba3527e156d4dc0b169450d1e24827407ef573c0c06b36819
7
+ data.tar.gz: 9c6b8330ba5b02d6b5b665a9f1a4c27fbd9b55418996d4596aa2d80112409df9b1b80846d9fec697c61f76ae4ce2e858cec802c0d5ff685389a66d12139b8e71
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020-present Codalio Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # RhinoSubscriptions
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rhino_subscriptions'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install rhino_subscriptions
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,35 @@
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
+ task :package
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'RhinoSubscriptions'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
20
+ load 'rails/tasks/engine.rake'
21
+
22
+ load 'rails/tasks/statistics.rake'
23
+
24
+ require 'bundler/gem_tasks'
25
+
26
+ require 'rake/testtask'
27
+
28
+ Rake::TestTask.new(:test) do |t|
29
+ t.libs << 'test'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = true
32
+ t.warning = false
33
+ end
34
+
35
+ task default: :test
File without changes
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stripe"
4
+
5
+ ::Stripe.api_key = ENV["STRIPE_SECRET_KEY"]
6
+
7
+ module Rhino
8
+ class StripeController < Rhino::BaseController
9
+ include Rhino::Authenticated
10
+
11
+ def prices
12
+ list = ::Stripe::Price.list({ limit: 5 })
13
+ prices = list["data"].map do |column|
14
+ { id: column["id"], name: column["nickname"], amount: column["unit_amount"] }
15
+ end
16
+ render json: {
17
+ publishableKey: ENV["STRIPE_PUBLISHABLE_KEY"],
18
+ prices:
19
+ }
20
+ end
21
+
22
+ def create_checkout_session
23
+ authorize nil
24
+ sc = get_stripe_customer(params["base_owner_id"])
25
+ session = checkout_session(sc.customer_id, params)
26
+ sc.current_stripe_session_id = session.id
27
+ sc.save
28
+
29
+ render json: {
30
+ sessionId: session.id
31
+ }
32
+ end
33
+
34
+ def customer
35
+ stripe_customer = Rhino::StripeCustomer.find_by(base_owner_id: params["base_owner_id"])
36
+ if stripe_customer
37
+ customer = ::Stripe::Customer.retrieve({
38
+ id: stripe_customer["customer_id"]
39
+ })
40
+ render json: {
41
+ customer: customer.data[0]
42
+ }
43
+ else
44
+ render json: {}
45
+ end
46
+ end
47
+
48
+ def subscriptions
49
+ stripe_customer = Rhino::StripeCustomer.find_by(base_owner_id: params["base_owner_id"])
50
+
51
+ if stripe_customer
52
+ customer = ::Stripe::Customer.retrieve({
53
+ id: stripe_customer["customer_id"],
54
+ expand: ["subscriptions"]
55
+ })
56
+
57
+ render json: {
58
+ subscriptions: customer.subscriptions.data
59
+ }
60
+
61
+ else
62
+ render json: {}
63
+ end
64
+ end
65
+
66
+ def check_session_id
67
+ stripe_customer = Rhino::StripeCustomer.find_by(base_owner_id: params["base_owner_id"])
68
+ render json: {
69
+ session_matched: ((stripe_customer && stripe_customer["current_stripe_session_id"]) == params["session_id"])
70
+ }
71
+ end
72
+
73
+ private
74
+ def checkout_session(customer_id, args)
75
+ ::Stripe::Checkout::Session.create(
76
+ success_url: ENV["FRONT_END_URL"] + args["success_url"],
77
+ cancel_url: ENV["FRONT_END_URL"] + args["cancel_url"],
78
+ payment_method_types: ["card"],
79
+ mode: "subscription",
80
+ line_items: [{
81
+ quantity: 1,
82
+ price: args["price"]
83
+ }],
84
+ customer: customer_id
85
+ )
86
+ end
87
+
88
+ def get_stripe_customer(_base_owner_id)
89
+ Rhino::StripeCustomer.find_or_create_by!(base_owner_id: params["base_owner_id"]) do |stripe_customer|
90
+ customer = ::Stripe::Customer.create(email: current_user.email)
91
+ stripe_customer.customer_id = customer.id
92
+ end
93
+ end
94
+
95
+ protected
96
+ def policy(record = nil)
97
+ StripePolicy.new(current_user, record)
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rhino
4
+ class StripeCustomer < ApplicationRecord
5
+ belongs_to :base_owner, class_name: "::#{Rhino.base_owner.name}"
6
+ rhino_owner_base
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class StripePolicy < Rhino::BasePolicy
4
+ def create_checkout_session?
5
+ authorize_action(admin?)
6
+ end
7
+
8
+ private
9
+ def admin?
10
+ Rhino.base_owner.roles_for_auth(auth_owner).any? { |k, _| k.to_s == "admin" }
11
+ end
12
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,12 @@
1
+ Rails.application.routes.draw do
2
+
3
+ scope Rhino.namespace do
4
+
5
+ get 'subscription/prices' , to: 'rhino/stripe#prices'
6
+ post 'subscription/customer' ,to: 'rhino/stripe#customer'
7
+ get 'subscription/subscriptions' ,to: 'rhino/stripe#subscriptions'
8
+ post 'subscription/create-checkout-session' ,to: 'rhino/stripe#create_checkout_session'
9
+ get 'subscription/check_session_id' ,to: 'rhino/stripe#check_session_id'
10
+
11
+ end
12
+ end
data/config/stripe.rb ADDED
@@ -0,0 +1,6 @@
1
+ Rails.configuration.stripe = {
2
+ :publishable_key => ENV['PUBLISHABLE_KEY'],
3
+ :secret_key => ENV['SECRET_KEY']
4
+ }
5
+
6
+ Stripe.api_key = Rails.configuration.stripe[:secret_key]
@@ -0,0 +1,12 @@
1
+ class RhinoCreateStripeCustomers < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :stripe_customers do |t|
4
+ t.references :user, null: false, foreign_key: true
5
+ t.string :customer_id
6
+ t.string :current_stripe_session_id
7
+
8
+ t.timestamps
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ class RhinoAlterStripeCustomers < ActiveRecord::Migration[6.0]
2
+ def change
3
+
4
+ remove_reference :stripe_customers, :user, index: true
5
+ add_reference :stripe_customers, :base_owner
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RhinoSubscriptions
4
+ module Generators
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ def install
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rhino_subscriptions/engine"
4
+ # require 'active_support'
5
+
6
+ module RhinoSubscriptions
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "rhino/engine"
5
+ require "rhino_subscriptions/version"
6
+
7
+ # https://guides.rubyonrails.org/engines.html#other-gem-dependencies
8
+ require "stripe"
9
+
10
+ module RhinoSubscriptions
11
+ class Engine < ::Rails::Engine
12
+ config.autoload_paths << File.expand_path("../../lib", __dir__)
13
+
14
+ initializer "rhino_subscriptions.register_module" do
15
+ config.after_initialize do
16
+ if ENV["STRIPE_SECRET_KEY"]
17
+ Rhino.registered_modules[:rhino_subscriptions] = {
18
+ version: RhinoSubscriptions::VERSION::STRING
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RhinoSubscriptions
4
+ # Returns the currently loaded version of Rhino core as a +Gem::Version+.
5
+ def self.gem_version
6
+ Gem::Version.new VERSION::STRING
7
+ end
8
+
9
+ module VERSION
10
+ MAJOR = 0
11
+ MINOR = 20
12
+ TINY = 0
13
+ PRE = "alpha.0"
14
+
15
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :rhino_subscriptions do
4
+ # Prevent migration installation task from showing up twice.
5
+ if Rake::Task.task_defined?("rhino_subscriptions_engine:install:migrations")
6
+ Rake::Task["rhino_subscriptions_engine:install:migrations"].clear_comments
7
+ end
8
+
9
+ desc "Install rhino_subscriptions"
10
+ task install: :environment do
11
+ Rake::Task["rhino_subscriptions_engine:install:migrations"].invoke if Rake::Task.task_defined?("rhino_subscriptions_engine:install:migrations")
12
+
13
+ Rails::Command.invoke :generate, ["rhino_subscriptions:install"]
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rhino_project_subscriptions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.20.0.alpha.0
5
+ platform: ruby
6
+ authors:
7
+ - JP Rosevear
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-08-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 7.1.0
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 7.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 7.1.0
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 7.1.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rhino_project_core
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '='
38
+ - !ruby/object:Gem::Version
39
+ version: 0.20.0.alpha.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '='
45
+ - !ruby/object:Gem::Version
46
+ version: 0.20.0.alpha.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: stripe
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 5.28.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '='
59
+ - !ruby/object:Gem::Version
60
+ version: 5.28.0
61
+ description: ''
62
+ email:
63
+ - jp@codalio.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - LICENSE
69
+ - README.md
70
+ - Rakefile
71
+ - app/assets/config/rhino_subscriptions_manifest.js
72
+ - app/controllers/rhino/stripe_controller.rb
73
+ - app/models/rhino/stripe_customer.rb
74
+ - app/policies/stripe_policy.rb
75
+ - config/routes.rb
76
+ - config/stripe.rb
77
+ - db/migrate/20210302208003_rhino_create_stripe_customers.rb
78
+ - db/migrate/20210302299092_rhino_alter_stripe_customers.rb
79
+ - lib/generators/rhino_subscriptions/install/install_generator.rb
80
+ - lib/rhino_project_subscriptions.rb
81
+ - lib/rhino_subscriptions/engine.rb
82
+ - lib/rhino_subscriptions/version.rb
83
+ - lib/tasks/rhino_subscriptions.rake
84
+ homepage: ''
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 3.5.11
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: ''
107
+ test_files: []