hippo-fw 0.9.8 → 0.9.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/client/hippo/access/subscription-choice-layer.jsx +170 -0
- data/client/hippo/access/subscription-choice-layer/cancel-subscription.jsx +111 -0
- data/client/hippo/access/subscription-choice-layer/payment-form.jsx +154 -0
- data/client/hippo/access/subscription-choice-layer/subscription-choice.scss +29 -0
- data/client/hippo/boot.jsx +1 -1
- data/client/hippo/components/asset.jsx +1 -1
- data/client/hippo/components/asset.scss +1 -0
- data/client/hippo/components/data-table.jsx +36 -38
- data/client/hippo/components/date-time.jsx +25 -9
- data/client/hippo/components/form/api.js +1 -0
- data/client/hippo/components/form/fields.jsx +3 -2
- data/client/hippo/components/form/fields/checkbox-wrapper.jsx +1 -1
- data/client/hippo/components/form/fields/date-wrapper.jsx +1 -1
- data/client/hippo/components/form/fields/email-wrapper.jsx +31 -0
- data/client/hippo/components/form/fields/form-field.scss +8 -0
- data/client/hippo/components/form/fields/label.jsx +5 -7
- data/client/hippo/components/form/fields/select-wrapper.jsx +1 -1
- data/client/hippo/components/form/fields/tags-wrapper.jsx +1 -1
- data/client/hippo/components/form/fields/text-wrapper.jsx +1 -1
- data/client/hippo/components/form/fields/textarea-wrapper.jsx +1 -1
- data/client/hippo/components/form/wrapper.jsx +1 -1
- data/client/hippo/components/grid.js +1 -0
- data/client/hippo/components/master-detail.jsx +1 -1
- data/client/hippo/components/network-activity-overlay.jsx +6 -6
- data/client/hippo/components/payments/field.jsx +64 -0
- data/client/hippo/components/payments/field.scss +18 -0
- data/client/hippo/components/popout-window.jsx +1 -1
- data/client/hippo/components/query-builder.jsx +1 -1
- data/client/hippo/components/query-builder/boolean-picker.jsx +1 -1
- data/client/hippo/components/query-builder/clause-filter.jsx +2 -2
- data/client/hippo/components/query-builder/clause.jsx +16 -10
- data/client/hippo/components/query-builder/date-picker.jsx +1 -1
- data/client/hippo/components/query-builder/query-builder.scss +23 -2
- data/client/hippo/components/record-finder.jsx +5 -2
- data/client/hippo/components/record-finder/query-layer.jsx +1 -1
- data/client/hippo/components/save-button.jsx +1 -1
- data/client/hippo/components/screen.jsx +1 -1
- data/client/hippo/components/text-editor.jsx +82 -40
- data/client/hippo/components/text-editor/renderer.jsx +15 -35
- data/client/hippo/components/text-editor/renderer.scss +15 -0
- data/client/hippo/components/text-editor/text-editor.scss +2 -15
- data/client/hippo/components/text-editor/upload-adapter.js +66 -0
- data/client/hippo/components/time-zone-select.jsx +1 -1
- data/client/hippo/components/tool-tip.jsx +9 -14
- data/client/hippo/components/toolbar.jsx +16 -0
- data/client/hippo/components/warning-notification.jsx +1 -1
- data/client/hippo/extensions/base.js +3 -2
- data/client/hippo/extensions/index.js +1 -1
- data/client/hippo/lib/action_cable.js +8 -0
- data/client/hippo/lib/action_cable/cable.js +47 -0
- data/client/hippo/lib/action_cable/connection.js +192 -0
- data/client/hippo/lib/action_cable/connection_monitor.js +135 -0
- data/client/hippo/lib/action_cable/consumer.js +56 -0
- data/client/hippo/lib/action_cable/subscription.js +98 -0
- data/client/hippo/lib/action_cable/subscriptions.js +129 -0
- data/client/hippo/lib/date-range.js +22 -5
- data/client/hippo/lib/lazy-getter.js +31 -0
- data/client/hippo/lib/util.js +6 -1
- data/client/hippo/models/base.js +8 -3
- data/client/hippo/models/config.js +7 -2
- data/client/hippo/models/date-type.js +14 -0
- data/client/hippo/models/decorators.js +5 -5
- data/client/hippo/models/pub_sub.js +1 -1
- data/client/hippo/models/query/array-result.js +4 -1
- data/client/hippo/models/subscription.js +35 -0
- data/client/hippo/models/sync.js +1 -1
- data/client/hippo/models/tenant.js +14 -1
- data/client/hippo/react/component-not-found.jsx +14 -18
- data/client/hippo/screens/async-loading.jsx +46 -0
- data/client/hippo/screens/definition.js +2 -1
- data/client/hippo/screens/preferences.jsx +57 -0
- data/client/hippo/screens/system-settings.jsx +4 -6
- data/client/hippo/screens/system-settings/mailer-config.jsx +1 -1
- data/client/hippo/screens/system-settings/tenant.jsx +57 -4
- data/client/hippo/screens/user-management.jsx +2 -2
- data/client/hippo/screens/user-management/edit-form.jsx +1 -1
- data/client/hippo/styles/global/fancy-header.scss +2 -1
- data/client/hippo/styles/global/mixins.scss +14 -1
- data/client/hippo/testing/index.js +7 -0
- data/client/hippo/user.js +9 -2
- data/client/hippo/workspace/index.jsx +29 -8
- data/client/hippo/workspace/menu-group.jsx +1 -1
- data/client/hippo/workspace/menu-option.jsx +2 -0
- data/client/hippo/workspace/menu.jsx +30 -6
- data/client/hippo/workspace/screen.jsx +5 -1
- data/client/hippo/workspace/styles.scss +22 -3
- data/command-reference-files/initial/Gemfile +1 -1
- data/config/routes.rb +6 -0
- data/config/screens.rb +9 -17
- data/db/migrate/20171129024737_create_subscriptions.rb +12 -0
- data/fixtures/vcr_cassettes/Tenant_changes/sends_email_when_tenant_identifier_changes.yml +72 -0
- data/fixtures/vcr_cassettes/Tenant_isoloation/disallows_using_a_user_s_token_on_incorrect_domain.yml +141 -0
- data/fixtures/vcr_cassettes/Tenant_isoloation/isolates_bar_s_tenant_data_from_foo.yml +141 -0
- data/fixtures/vcr_cassettes/Tenant_isoloation/isolates_foo_s_tenant_data_from_bar.yml +141 -0
- data/hippo-fw.gemspec +4 -3
- data/lib/hippo.rb +1 -0
- data/lib/hippo/access/roles/basic_user.rb +1 -0
- data/lib/hippo/api/authentication_provider.rb +4 -5
- data/lib/hippo/api/handlers/asset.rb +9 -4
- data/lib/hippo/api/handlers/subscription.rb +39 -0
- data/lib/hippo/api/handlers/user_session.rb +0 -1
- data/lib/hippo/api/helper_methods.rb +8 -4
- data/lib/hippo/api/request_wrapper.rb +12 -1
- data/lib/hippo/command/console.rb +3 -3
- data/lib/hippo/concerns/asset_uploader.rb +8 -2
- data/lib/hippo/concerns/pub_sub.rb +8 -8
- data/lib/hippo/configuration.rb +6 -4
- data/lib/hippo/extension.rb +3 -2
- data/lib/hippo/model.rb +1 -0
- data/lib/hippo/models/subscription.rb +7 -0
- data/lib/hippo/payments.rb +129 -0
- data/lib/hippo/screen.rb +4 -2
- data/lib/hippo/screen/definition.rb +4 -0
- data/lib/hippo/spec_helper.rb +4 -4
- data/lib/hippo/templates/liquid/precision.rb +9 -0
- data/lib/hippo/tenant.rb +4 -5
- data/lib/hippo/user.rb +5 -5
- data/lib/hippo/version.rb +1 -1
- data/lib/hippo/webpack.rb +5 -1
- data/package-lock.json +437 -881
- data/package.json +19 -5
- data/spec/client/components/__snapshots__/query-builder.spec.jsx.snap +34 -1
- data/spec/client/components/__snapshots__/record-finder.spec.jsx.snap +1 -0
- data/spec/client/models/base.spec.js +7 -0
- data/spec/client/models/subscription.spec.js +8 -0
- data/spec/client/screens/__snapshots__/preferences.spec.jsx.snap +223 -0
- data/spec/client/screens/preferences.spec.jsx +10 -0
- data/spec/client/test-models.js +1 -1
- data/spec/client/workspace/__snapshots__/menu.spec.jsx.snap +12 -0
- data/spec/factories/subscription.rb +6 -0
- data/spec/factories/tenant.rb +1 -1
- data/spec/factories/user.rb +1 -1
- data/spec/server/api/tenant_change_spec.rb +11 -8
- data/spec/server/api/tenant_isolation_spec.rb +11 -8
- data/spec/server/api/user_sessions_spec.rb +10 -7
- data/spec/server/api/user_spec.rb +45 -0
- data/spec/server/models/subscription_spec.rb +10 -0
- data/spec/server/payment_helpers.rb +13 -0
- data/spec/server/print/form_spec.rb +1 -1
- data/spec/server/spec_helper.rb +3 -11
- data/templates/js/screen-definitions.js +4 -1
- data/templates/spec/factories/model.rb +1 -1
- data/views/hippo_root_view.erb +5 -5
- metadata +84 -25
- data/client/hippo/components/grid/config.json +0 -3
- data/client/hippo/components/grid/editors.scss +0 -78
- data/client/hippo/components/grid/index.js +0 -2
- data/client/hippo/components/grid/row-editor.scss +0 -74
- data/client/hippo/components/grid/styles.scss +0 -118
- data/client/hippo/components/text-editor/display-modes/Button.jsx +0 -20
- data/client/hippo/components/text-editor/display-modes/ToggleEdit.jsx +0 -23
- data/client/hippo/components/text-editor/display-modes/ToggleInsert.jsx +0 -22
- data/client/hippo/components/text-editor/display-modes/ToggleLayout.jsx +0 -22
- data/client/hippo/components/text-editor/display-modes/TogglePreview.jsx +0 -22
- data/client/hippo/components/text-editor/display-modes/ToggleResize.jsx +0 -22
- data/client/hippo/components/text-editor/display-modes/index.css +0 -0
- data/client/hippo/components/text-editor/display-modes/index.js +0 -30
- data/client/hippo/components/text-editor/image-plugin/Component/Display/index.js +0 -82
- data/client/hippo/components/text-editor/image-plugin/Component/Form/index.js +0 -42
- data/client/hippo/components/text-editor/image-plugin/Component/index.js +0 -16
- data/client/hippo/components/text-editor/image-plugin/Component/index.scss +0 -0
- data/client/hippo/components/text-editor/image-plugin/index.js +0 -32
- data/client/hippo/components/text-editor/image-plugin/index.scss +0 -25
- data/client/hippo/components/toolbar/changes-notification.scss +0 -63
- data/client/hippo/components/toolbar/index.js +0 -3
- data/client/hippo/components/toolbar/styles.scss +0 -74
data/hippo-fw.gemspec
CHANGED
@@ -36,7 +36,7 @@ Gem::Specification.new do |spec|
|
|
36
36
|
spec.add_dependency "liquid", "~> 4.0"
|
37
37
|
spec.add_dependency 'erb_latex', "~> 1.0"
|
38
38
|
spec.add_dependency 'shrine-memory', "~> 0.2"
|
39
|
-
spec.add_dependency '
|
39
|
+
spec.add_dependency 'factory_bot', "~> 4.8"
|
40
40
|
spec.add_dependency 'faker', "~> 1.7"
|
41
41
|
spec.add_dependency 'webmock', "~> 3.0"
|
42
42
|
spec.add_dependency "database_cleaner", "~> 1.3"
|
@@ -44,14 +44,15 @@ Gem::Specification.new do |spec|
|
|
44
44
|
spec.add_dependency "sinatra", "~> 2.0.0"
|
45
45
|
spec.add_dependency "rack-protection", "~> 2.0.0"
|
46
46
|
spec.add_dependency "rack", "~> 2.0"
|
47
|
-
|
47
|
+
spec.add_dependency "braintree", "~> 2.80"
|
48
48
|
spec.add_dependency "rack-cors", "~> 0.4"
|
49
49
|
spec.add_dependency "rack-test", "~> 0.6"
|
50
50
|
|
51
51
|
spec.add_dependency "bcrypt", "~> 3.1"
|
52
52
|
spec.add_dependency "shrine", "~> 2.4"
|
53
53
|
spec.add_dependency "image_processing", "~> 0.4"
|
54
|
-
|
54
|
+
spec.add_dependency 'image_optim', "~> 0.26"
|
55
|
+
spec.add_dependency 'image_optim_pack'
|
55
56
|
spec.add_dependency "execjs", "~> 2.6"
|
56
57
|
spec.add_dependency "fastimage", "~> 2.0"
|
57
58
|
spec.add_dependency "guard", "~> 2.13"
|
data/lib/hippo.rb
CHANGED
@@ -31,15 +31,14 @@ module Hippo
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
def allowed_access_to?(klass, options = {})
|
35
|
-
|
34
|
+
def allowed_access_to?(klass, handler, options = {})
|
36
35
|
return true if options[:public] == true and current_user.nil?
|
37
36
|
return false if current_user.nil?
|
38
37
|
case request.request_method
|
39
38
|
when 'GET'
|
40
39
|
klass.can_read_attributes?(request.params, current_user)
|
41
40
|
when 'POST', 'PATCH', 'PUT'
|
42
|
-
klass.can_write_attributes?(
|
41
|
+
klass.can_write_attributes?(handler.data, current_user)
|
43
42
|
when 'DELETE'
|
44
43
|
klass.can_delete_attributes?(request.params, current_user)
|
45
44
|
else
|
@@ -59,8 +58,8 @@ module Hippo
|
|
59
58
|
|
60
59
|
def wrap_model_access(model, req, options = {})
|
61
60
|
fail_request(req) and return unless Tenant.current
|
62
|
-
if allowed_access_to?(model, options)
|
63
|
-
::Hippo::User.scoped_to(current_user) do |
|
61
|
+
if allowed_access_to?(model, req, options)
|
62
|
+
::Hippo::User.scoped_to(current_user) do |user|
|
64
63
|
yield
|
65
64
|
end
|
66
65
|
else
|
@@ -48,10 +48,15 @@ module Hippo::API::Handlers
|
|
48
48
|
webpack = Hippo::API::Root.webpack
|
49
49
|
if Hippo.env.production?
|
50
50
|
lambda do
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
begin
|
52
|
+
file = webpack.file(params['asset'].to_sym, raise_on_not_found: false)
|
53
|
+
if file
|
54
|
+
response['Cache-Control'] = "public, max-age=0, must-revalidate"
|
55
|
+
redirect "/assets/#{file}"
|
56
|
+
else
|
57
|
+
halt 404
|
58
|
+
end
|
59
|
+
rescue Hippo::Webpack::NotFound
|
55
60
|
halt 404
|
56
61
|
end
|
57
62
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Hippo::API::Handlers
|
2
|
+
|
3
|
+
class Subscription < Hippo::API::ControllerBase
|
4
|
+
|
5
|
+
def show
|
6
|
+
plan = Hippo::Subscription
|
7
|
+
.find(params['id'])
|
8
|
+
.as_json.merge(
|
9
|
+
authorization: Hippo::Payments.payment_authorization
|
10
|
+
)
|
11
|
+
std_api_reply(:retrieve, plan, success: true)
|
12
|
+
end
|
13
|
+
|
14
|
+
def update
|
15
|
+
tenant = Hippo::Tenant.current
|
16
|
+
subscription = Hippo::Subscription.find(data['id'])
|
17
|
+
Hippo::Payments.set_tenant_payment_method(
|
18
|
+
tenant, subscription, data['nonce']
|
19
|
+
)
|
20
|
+
success = subscription.errors.none?
|
21
|
+
if success
|
22
|
+
tenant.subscription = subscription
|
23
|
+
tenant.save!
|
24
|
+
end
|
25
|
+
std_api_reply(:retrieve, subscription, success: success)
|
26
|
+
end
|
27
|
+
|
28
|
+
def destroy
|
29
|
+
tenant = Hippo::Tenant.current
|
30
|
+
if tenant.subscription_id != params['id'].to_i
|
31
|
+
return std_api_reply(:destroy, {}, success: false)
|
32
|
+
end
|
33
|
+
success = Hippo::Payments.cancel_subscription(tenant)
|
34
|
+
tenant.save! if success
|
35
|
+
std_api_reply(:retrieve, tenant.subscription, success: success)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -9,7 +9,6 @@ module Hippo::API::Handlers
|
|
9
9
|
lambda do
|
10
10
|
wrap_reply(with_transaction: false) do
|
11
11
|
user = Hippo::User.with_login(data['login']).first
|
12
|
-
|
13
12
|
if user && user.authenticate(data['password'])
|
14
13
|
{ success: true, message: "Login succeeded",
|
15
14
|
data: user.workspace_data }
|
@@ -32,10 +32,14 @@ module Hippo
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def data
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
if request.content_type == 'application/json'
|
36
|
+
@json_data ||= (
|
37
|
+
body = request.body.read
|
38
|
+
body.present? ? Oj.load(body) : {}
|
39
|
+
)
|
40
|
+
else
|
41
|
+
request.params
|
42
|
+
end
|
39
43
|
end
|
40
44
|
|
41
45
|
def request_origin
|
@@ -2,6 +2,10 @@ module Hippo
|
|
2
2
|
module API
|
3
3
|
|
4
4
|
module RequestWrapper
|
5
|
+
DEFAULT_OPTIONS = {
|
6
|
+
with_transaction: true, require_tenant: true
|
7
|
+
}
|
8
|
+
|
5
9
|
class << self
|
6
10
|
|
7
11
|
def get(*args)
|
@@ -68,12 +72,19 @@ module Hippo
|
|
68
72
|
end
|
69
73
|
end
|
70
74
|
|
75
|
+
def with_user(options = DEFAULT_OPTIONS)
|
76
|
+
authentication = Hippo::API::AuthenticationProvider.new(request)
|
77
|
+
wrap_reply(options) do
|
78
|
+
yield authentication.current_user
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
71
82
|
# Wraps a HTTP request in an optional DB transaction and converts yeilded data to JSON
|
72
83
|
#
|
73
84
|
# @param [options] options for additional checks
|
74
85
|
# @option opts [Boolean] :with_transaction rollback DB transaction if exceptions occur
|
75
86
|
# @option opts [Boolean] :require_tenant return error if tenant is not found
|
76
|
-
def wrap_reply(options =
|
87
|
+
def wrap_reply(options = DEFAULT_OPTIONS)
|
77
88
|
if options[:require_tenant] && Hippo::Tenant.current.nil?
|
78
89
|
return json_reply(
|
79
90
|
{ success: false, message: "invalid address",
|
@@ -12,15 +12,15 @@ module Hippo
|
|
12
12
|
require 'irb/completion'
|
13
13
|
require 'pp'
|
14
14
|
require 'faker'
|
15
|
-
require '
|
15
|
+
require 'factory_bot'
|
16
16
|
DB.establish_connection
|
17
17
|
ActiveRecord::Base.logger = Logger.new STDOUT
|
18
18
|
Configuration.apply
|
19
19
|
Hippo::Extensions.load_controlling_config
|
20
|
-
|
20
|
+
FactoryBot.definition_file_paths = Hippo::Extensions.map do |ext|
|
21
21
|
ext.root_path.join('spec/factories')
|
22
22
|
end
|
23
|
-
|
23
|
+
FactoryBot.find_definitions
|
24
24
|
Hippo::API::Cable.configure
|
25
25
|
MultiTenant.current_tenant = Hippo::Tenant.find_by_slug(
|
26
26
|
Hippo.env.development? ? 'dev' : 'system'
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'shrine'
|
2
2
|
require 'image_processing/mini_magick'
|
3
|
+
require 'image_optim'
|
3
4
|
|
4
5
|
module Hippo::Concerns
|
5
6
|
|
@@ -28,11 +29,16 @@ module Hippo::Concerns
|
|
28
29
|
|
29
30
|
|
30
31
|
process(:store) do |io, context|
|
32
|
+
|
31
33
|
if io.metadata['mime_type'] && io.metadata['mime_type'].starts_with?('image')
|
32
|
-
|
34
|
+
file = io.download
|
35
|
+
optimizer = ImageOptim.new
|
36
|
+
optimizer.optimize_image!(file)
|
37
|
+
|
38
|
+
size_1000 = resize_to_limit(file, 1000, 1000)
|
33
39
|
size_600 = resize_to_limit(size_1000, 600, 600)
|
34
40
|
size_300 = resize_to_limit(size_600, 300, 300)
|
35
|
-
{original:
|
41
|
+
{original: file, large: size_1000, medium: size_600, thumbnail: size_300}
|
36
42
|
else
|
37
43
|
{original: io}
|
38
44
|
end
|
@@ -50,10 +50,10 @@ module Hippo::Concerns
|
|
50
50
|
class_attribute :_event_listeners
|
51
51
|
base.valid_event_names = [ :save, :create, :update, :destroy ]
|
52
52
|
|
53
|
-
base.
|
54
|
-
base.
|
55
|
-
base.
|
56
|
-
base.
|
53
|
+
base.before_save :fire_save_pubsub_events
|
54
|
+
base.before_create :fire_create_pubsub_events
|
55
|
+
base.before_update :fire_update_pubsub_events
|
56
|
+
base.before_destroy :fire_destroy_pubsub_events
|
57
57
|
end
|
58
58
|
|
59
59
|
module ClassMethods
|
@@ -94,19 +94,19 @@ module Hippo::Concerns
|
|
94
94
|
|
95
95
|
protected
|
96
96
|
|
97
|
-
def
|
97
|
+
def fire_destroy_pubsub_events
|
98
98
|
fire_pubsub_event(:update, self)
|
99
99
|
end
|
100
100
|
|
101
|
-
def
|
101
|
+
def fire_update_pubsub_events
|
102
102
|
fire_pubsub_event(:update, self)
|
103
103
|
end
|
104
104
|
|
105
|
-
def
|
105
|
+
def fire_create_pubsub_events
|
106
106
|
fire_pubsub_event(:create, self)
|
107
107
|
end
|
108
108
|
|
109
|
-
def
|
109
|
+
def fire_save_pubsub_events
|
110
110
|
fire_pubsub_event(:save, self)
|
111
111
|
end
|
112
112
|
|
data/lib/hippo/configuration.rb
CHANGED
@@ -13,7 +13,7 @@ module Hippo
|
|
13
13
|
|
14
14
|
class Configuration
|
15
15
|
include Concerns::AttrAccessorWithDefault
|
16
|
-
|
16
|
+
attr_reader :secrets
|
17
17
|
|
18
18
|
# Since changing a config value inadvertently
|
19
19
|
# can have pretty drastic consequences that might not be
|
@@ -34,6 +34,7 @@ module Hippo
|
|
34
34
|
|
35
35
|
def initialize
|
36
36
|
@observers=Hash.new{ |hash,key| hash[key] = Array.new }
|
37
|
+
@secrets = Hashie::Mash.new
|
37
38
|
end
|
38
39
|
|
39
40
|
def on_change(config, &block)
|
@@ -54,6 +55,7 @@ module Hippo
|
|
54
55
|
|
55
56
|
controlling_ext = Hippo::Extensions.bootstrap
|
56
57
|
secrets = controlling_ext.root_path.join('config', 'secrets.yml')
|
58
|
+
|
57
59
|
@@secrets = Hashie::Mash.new(
|
58
60
|
secrets.exist? ? YAML.load(ERB.new(secrets.read).result) : {}
|
59
61
|
)
|
@@ -122,9 +124,9 @@ module Hippo
|
|
122
124
|
# types of assets to include into compiled package
|
123
125
|
config_option :static_asset_types, ['images','fonts']
|
124
126
|
|
125
|
-
config_option :website_domain, '
|
126
|
-
|
127
|
-
config_option :product_name, '
|
127
|
+
config_option :website_domain, ''
|
128
|
+
config_option :support_email, ''
|
129
|
+
config_option :product_name, ''
|
128
130
|
|
129
131
|
def api_path
|
130
132
|
mounted_at + 'api'
|
data/lib/hippo/extension.rb
CHANGED
@@ -99,7 +99,8 @@ module Hippo
|
|
99
99
|
def static_bootstrap_data
|
100
100
|
data = {}
|
101
101
|
%w{
|
102
|
-
api_path environment
|
102
|
+
api_path print_path_prefix environment assets_path_prefix
|
103
|
+
website_domain product_name support_email
|
103
104
|
}.each do |config|
|
104
105
|
data[config.to_sym] = Hippo.config.send(config)
|
105
106
|
end
|
@@ -110,7 +111,7 @@ module Hippo
|
|
110
111
|
ext_data = ext.static_bootstrap_data
|
111
112
|
data[ext.identifier] = ext_data unless ext_data.nil?
|
112
113
|
end
|
113
|
-
data[:
|
114
|
+
data[:subscription_plans] = Subscription.all.as_json
|
114
115
|
return data
|
115
116
|
end
|
116
117
|
|
data/lib/hippo/model.rb
CHANGED
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'braintree'
|
2
|
+
|
3
|
+
module Hippo
|
4
|
+
|
5
|
+
module Payments
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def gateway_config
|
9
|
+
Hippo.config.secrets.dig(:payments, :braintree) || {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def gateway
|
13
|
+
cc = gateway_config
|
14
|
+
return nil unless cc.present?
|
15
|
+
::Braintree::Gateway.new(
|
16
|
+
::Braintree::Configuration.new(
|
17
|
+
environment: cc['sandbox'] ? :sandbox : :production,
|
18
|
+
merchant_id: cc['merchant_id'],
|
19
|
+
public_key: cc['public_key'],
|
20
|
+
private_key: cc['private_key'],
|
21
|
+
logger: Hippo.logger
|
22
|
+
)
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
def payment_authorization
|
27
|
+
gw = ::Braintree::ClientTokenGateway.new(gateway)
|
28
|
+
gw.generate
|
29
|
+
end
|
30
|
+
|
31
|
+
def sync_plans
|
32
|
+
::Braintree::PlanGateway.new(gateway).all.each do |bt|
|
33
|
+
subscription = Hippo::Subscription.find_or_initialize_by(subscription_id: bt.id)
|
34
|
+
subscription.update_attributes(
|
35
|
+
name: bt.name, description: bt.description,
|
36
|
+
price: bt.price, trial_duration: bt.trial_duration
|
37
|
+
)
|
38
|
+
subscription.save!
|
39
|
+
end
|
40
|
+
true
|
41
|
+
end
|
42
|
+
|
43
|
+
def update_customer_for_tenant(tenant)
|
44
|
+
return unless gateway.present?
|
45
|
+
gateway.customer.update(
|
46
|
+
tenant.metadata['bt_customer_id'],
|
47
|
+
company: tenant.name,
|
48
|
+
email: tenant.email,
|
49
|
+
phone: tenant.phone_number
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
def create_customer_for_tenant(tenant)
|
54
|
+
return unless gateway.present?
|
55
|
+
result = gateway.customer.create(
|
56
|
+
company: tenant.name,
|
57
|
+
email: tenant.email,
|
58
|
+
phone: tenant.phone_number
|
59
|
+
)
|
60
|
+
if result.success?
|
61
|
+
tenant.metadata['bt_customer_id'] = result.customer.id
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def set_tenant_payment_method(tenant, subscription, nonce)
|
66
|
+
result = if tenant.metadata['payment_method_id']
|
67
|
+
gateway.payment_method.update(
|
68
|
+
tenant.metadata['payment_method_id'],
|
69
|
+
payment_method_nonce: nonce)
|
70
|
+
else
|
71
|
+
gateway.payment_method.create(
|
72
|
+
customer_id: tenant.metadata['bt_customer_id'],
|
73
|
+
payment_method_nonce: nonce,
|
74
|
+
options: { make_default: true })
|
75
|
+
end
|
76
|
+
|
77
|
+
if result.success?
|
78
|
+
tenant.metadata['payment_method_id'] = result.payment_method.token
|
79
|
+
else
|
80
|
+
subscription.errors.add(:base, result.errors.map(&:message).first)
|
81
|
+
return false
|
82
|
+
end
|
83
|
+
|
84
|
+
attrs = { plan_id: subscription.subscription_id,
|
85
|
+
payment_method_token: tenant.metadata['payment_method_id'] }
|
86
|
+
|
87
|
+
result = if tenant.metadata['bt_plan_id']
|
88
|
+
gateway.subscription.update(
|
89
|
+
tenant.metadata['bt_plan_id'],
|
90
|
+
attrs
|
91
|
+
)
|
92
|
+
else
|
93
|
+
gateway.subscription.create(attrs)
|
94
|
+
end
|
95
|
+
|
96
|
+
if result.success?
|
97
|
+
tenant.metadata['bt_plan_id'] = result.subscription.id
|
98
|
+
return true
|
99
|
+
else
|
100
|
+
subscription.errors.add(:base, result.errors.map(&:message).first)
|
101
|
+
return false
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def cancel_subscription(tenant)
|
106
|
+
result = gateway.subscription.cancel(
|
107
|
+
tenant.metadata['bt_plan_id']
|
108
|
+
)
|
109
|
+
if result.success?
|
110
|
+
tenant.metadata.delete('bt_plan_id')
|
111
|
+
tenant.subscription = nil
|
112
|
+
return true
|
113
|
+
else
|
114
|
+
tenant.subscription.errors.add(:base, result.errors.map(&:message).first)
|
115
|
+
return false
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
Tenant.observe(:create) do |tenant|
|
122
|
+
Payments.create_customer_for_tenant(tenant)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Tenant.observe(:update) do |tenant|
|
126
|
+
# Payments.update_customer_for_tenant(tenant)
|
127
|
+
# end
|
128
|
+
|
129
|
+
end
|