disco_app 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Rakefile +37 -0
- data/app/assets/images/disco_app/icon.svg +1 -0
- data/app/assets/javascripts/disco_app/disco_app.js +7 -0
- data/app/assets/stylesheets/disco_app/bootstrap/_custom.scss +54 -0
- data/app/assets/stylesheets/disco_app/bootstrap/_variables.scss +872 -0
- data/app/assets/stylesheets/disco_app/disco/_buttons.scss +31 -0
- data/app/assets/stylesheets/disco_app/disco/_cards.scss +43 -0
- data/app/assets/stylesheets/disco_app/disco/_forms.scss +23 -0
- data/app/assets/stylesheets/disco_app/disco/_sections.scss +61 -0
- data/app/assets/stylesheets/disco_app/disco/_type.scss +21 -0
- data/app/assets/stylesheets/disco_app/disco/mixins/_flexbox.scss +394 -0
- data/app/assets/stylesheets/disco_app/disco_app.scss +13 -0
- data/app/controllers/disco_app/app_proxy_controller.rb +41 -0
- data/app/controllers/disco_app/authenticated_controller.rb +44 -0
- data/app/controllers/disco_app/carrier_request_controller.rb +28 -0
- data/app/controllers/disco_app/charges_controller.rb +30 -0
- data/app/controllers/disco_app/install_controller.rb +26 -0
- data/app/controllers/disco_app/webhooks_controller.rb +42 -0
- data/app/helpers/disco_app/application_helper.rb +4 -0
- data/app/jobs/disco_app/app_installed_job.rb +41 -0
- data/app/jobs/disco_app/app_uninstalled_job.rb +3 -0
- data/app/jobs/disco_app/concerns/app_uninstalled_job.rb +19 -0
- data/app/jobs/disco_app/shop_job.rb +29 -0
- data/app/jobs/disco_app/shop_update_job.rb +16 -0
- data/app/models/disco_app/concerns/plan.rb +14 -0
- data/app/models/disco_app/concerns/shop.rb +62 -0
- data/app/models/disco_app/concerns/subscription.rb +14 -0
- data/app/models/disco_app/plan.rb +3 -0
- data/app/models/disco_app/session_storage.rb +18 -0
- data/app/models/disco_app/shop.rb +3 -0
- data/app/models/disco_app/subscription.rb +3 -0
- data/app/services/disco_app/charges_service.rb +73 -0
- data/app/services/disco_app/subscription_service.rb +25 -0
- data/app/services/disco_app/webhook_service.rb +30 -0
- data/app/views/disco_app/charges/activate.html.erb +1 -0
- data/app/views/disco_app/charges/create.html.erb +1 -0
- data/app/views/disco_app/charges/new.html.erb +45 -0
- data/app/views/disco_app/install/installing.html.erb +7 -0
- data/app/views/disco_app/install/uninstalling.html.erb +1 -0
- data/app/views/disco_app/proxy_errors/404.html.erb +1 -0
- data/app/views/disco_app/shared/_card.html.erb +16 -0
- data/app/views/disco_app/shared/_section.html.erb +17 -0
- data/app/views/layouts/application.html.erb +18 -0
- data/app/views/layouts/embedded_app.html.erb +41 -0
- data/app/views/sessions/new.html.erb +26 -0
- data/config/routes.rb +19 -0
- data/db/migrate/20150525000000_create_shops_if_not_existent.rb +15 -0
- data/db/migrate/20150525162112_add_status_to_shops.rb +5 -0
- data/db/migrate/20150525171422_add_meta_to_shops.rb +11 -0
- data/db/migrate/20150629210346_add_charge_status_to_shop.rb +5 -0
- data/db/migrate/20150814214025_add_more_meta_to_shops.rb +15 -0
- data/db/migrate/20151017231302_create_disco_app_plans.rb +13 -0
- data/db/migrate/20151017232027_create_disco_app_subscriptions.rb +15 -0
- data/db/migrate/20151017234409_move_shop_to_disco_app_engine.rb +5 -0
- data/lib/disco_app/engine.rb +11 -0
- data/lib/disco_app/version.rb +3 -0
- data/lib/disco_app.rb +4 -0
- data/lib/generators/disco_app/USAGE +5 -0
- data/lib/generators/disco_app/disco_app_generator.rb +159 -0
- data/lib/generators/disco_app/mailify/mailify_generator.rb +55 -0
- data/lib/generators/disco_app/reactify/reactify_generator.rb +31 -0
- data/lib/generators/disco_app/templates/assets/javascripts/application.js +17 -0
- data/lib/generators/disco_app/templates/assets/stylesheets/application.scss +5 -0
- data/lib/generators/disco_app/templates/config/puma.rb +15 -0
- data/lib/generators/disco_app/templates/controllers/home_controller.rb +7 -0
- data/lib/generators/disco_app/templates/initializers/disco_app.rb +1 -0
- data/lib/generators/disco_app/templates/initializers/shopify_app.rb +7 -0
- data/lib/generators/disco_app/templates/initializers/shopify_session_repository.rb +7 -0
- data/lib/generators/disco_app/templates/root/Procfile +2 -0
- data/lib/generators/disco_app/templates/views/home/index.html.erb +2 -0
- data/test/controllers/disco_app/install_controller_test.rb +50 -0
- data/test/controllers/disco_app/webhooks_controller_test.rb +58 -0
- data/test/controllers/home_controller_test.rb +61 -0
- data/test/disco_app_test.rb +7 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +17 -0
- data/test/dummy/app/assets/stylesheets/application.scss +5 -0
- data/test/dummy/app/controllers/application_controller.rb +6 -0
- data/test/dummy/app/controllers/home_controller.rb +7 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/jobs/disco_app/app_uninstalled_job.rb +11 -0
- data/test/dummy/app/models/disco_app/shop.rb +15 -0
- data/test/dummy/app/views/home/index.html.erb +2 -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/application.rb +37 -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 +85 -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/disco_app.rb +1 -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/omniauth.rb +9 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/shopify_app.rb +7 -0
- data/test/dummy/config/initializers/shopify_session_repository.rb +7 -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 +8 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/schema.rb +70 -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/fixtures/api/widget_store/shop.json +46 -0
- data/test/fixtures/disco_app/plans.yml +32 -0
- data/test/fixtures/disco_app/shops.yml +10 -0
- data/test/fixtures/disco_app/subscriptions.yml +26 -0
- data/test/fixtures/webhooks/app_uninstalled.json +46 -0
- data/test/integration/navigation_test.rb +10 -0
- data/test/jobs/disco_app/app_installed_job_test.rb +29 -0
- data/test/jobs/disco_app/app_uninstalled_job_test.rb +32 -0
- data/test/models/disco_app/plan_test.rb +5 -0
- data/test/models/disco_app/shop_test.rb +26 -0
- data/test/models/disco_app/subscription_test.rb +6 -0
- data/test/services/disco_app/subscription_service_test.rb +28 -0
- data/test/support/test_file_fixtures.rb +29 -0
- data/test/test_helper.rb +51 -0
- metadata +456 -0
@@ -0,0 +1,30 @@
|
|
1
|
+
module DiscoApp
|
2
|
+
class ChargesController < ApplicationController
|
3
|
+
include DiscoApp::AuthenticatedController
|
4
|
+
|
5
|
+
skip_before_action :verify_status, only: [:create, :activate]
|
6
|
+
|
7
|
+
# Display a "pre-charge" page, giving the opportunity to explain why a charge needs to be made.
|
8
|
+
def new
|
9
|
+
end
|
10
|
+
|
11
|
+
# Create a new charge for the currently logged in shop, then redirect to the charge's confirmation URL.
|
12
|
+
def create
|
13
|
+
if (shopify_charge = DiscoApp::ChargesService.create(@shop)).nil?
|
14
|
+
redirect_to action: :new and return
|
15
|
+
end
|
16
|
+
redirect_to shopify_charge.confirmation_url
|
17
|
+
end
|
18
|
+
|
19
|
+
# Attempt to activate a charge after a user has accepted or declined it. Redirect to the main application's root URL
|
20
|
+
# immediately afterwards - if the charge wasn't accepted, the flow will start again.
|
21
|
+
def activate
|
22
|
+
if (shopify_charge = DiscoApp::ChargesService.get_accepted_charge(@shop, params[:charge_id])).nil?
|
23
|
+
redirect_to action: :new and return
|
24
|
+
end
|
25
|
+
DiscoApp::ChargesService.activate(@shop, shopify_charge)
|
26
|
+
redirect_to main_app.root_url
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module DiscoApp
|
2
|
+
class InstallController < ApplicationController
|
3
|
+
include DiscoApp::AuthenticatedController
|
4
|
+
|
5
|
+
# Start the installation process for the current shop, then redirect to the installing screen.
|
6
|
+
def install
|
7
|
+
AppInstalledJob.perform_later(@shop.shopify_domain)
|
8
|
+
redirect_to action: :installing
|
9
|
+
end
|
10
|
+
|
11
|
+
# Display an "installing" page.
|
12
|
+
def installing
|
13
|
+
if @shop.installed?
|
14
|
+
redirect_to main_app.root_path
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Display an "uninstalling" page. Should be almost never used.
|
19
|
+
def uninstalling
|
20
|
+
if @shop.uninstalled?
|
21
|
+
redirect_to main_app.root_path
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module DiscoApp
|
2
|
+
class WebhooksController < ActionController::Base
|
3
|
+
|
4
|
+
before_action :verify_webhook
|
5
|
+
|
6
|
+
def process_webhook
|
7
|
+
# Get the topic and domain for this webhook.
|
8
|
+
topic = request.headers['HTTP_X_SHOPIFY_TOPIC']
|
9
|
+
domain = request.headers['HTTP_X_SHOPIFY_SHOP_DOMAIN']
|
10
|
+
|
11
|
+
# Ensure a domain was provided in the headers.
|
12
|
+
unless domain
|
13
|
+
head :bad_request
|
14
|
+
end
|
15
|
+
|
16
|
+
# Try to find a matching background job task for the given topic using class name.
|
17
|
+
job_class = DiscoApp::WebhookService.find_job_class(topic)
|
18
|
+
|
19
|
+
# Return bad request if we couldn't match a job class.
|
20
|
+
unless job_class.present?
|
21
|
+
head :bad_request
|
22
|
+
end
|
23
|
+
|
24
|
+
# Decode the body data and enqueue the appropriate job.
|
25
|
+
data = ActiveSupport::JSON::decode(request.body.read)
|
26
|
+
job_class.perform_later(domain, data)
|
27
|
+
|
28
|
+
render nothing: true
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
# Verify a webhook request.
|
34
|
+
def verify_webhook
|
35
|
+
unless DiscoApp::WebhookService.is_valid_hmac?(request.body.read.to_s, ShopifyApp.configuration.secret, request.headers['HTTP_X_SHOPIFY_HMAC_SHA256'])
|
36
|
+
head :unauthorized
|
37
|
+
end
|
38
|
+
request.body.rewind
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module DiscoApp
|
2
|
+
class AppInstalledJob < DiscoApp::ShopJob
|
3
|
+
|
4
|
+
before_enqueue { @shop.awaiting_install! }
|
5
|
+
before_perform { @shop.installing! }
|
6
|
+
after_perform { @shop.installed! }
|
7
|
+
|
8
|
+
def perform(domain)
|
9
|
+
|
10
|
+
# Install webhooks.
|
11
|
+
(base_webhook_topics + webhook_topics).each do |topic|
|
12
|
+
ShopifyAPI::Webhook.create(topic: topic, address: webhooks_url, format: 'json')
|
13
|
+
end
|
14
|
+
|
15
|
+
# Perform initial update of shop information.
|
16
|
+
DiscoApp::ShopUpdateJob.perform_now(domain)
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
# Return a list of additional webhook topics to listen for.
|
23
|
+
# This method should be overridden in the application.
|
24
|
+
def webhook_topics
|
25
|
+
[]
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Return a list of webhook topics that will always be set up for the application.
|
31
|
+
def base_webhook_topics
|
32
|
+
[:'app/uninstalled', :'shop/update']
|
33
|
+
end
|
34
|
+
|
35
|
+
# Return the absolute URL to the webhooks endpoint.
|
36
|
+
def webhooks_url
|
37
|
+
DiscoApp::Engine.routes.url_helpers.webhooks_url
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module DiscoApp::Concerns::AppUninstalledJob
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
|
6
|
+
before_enqueue { @shop.awaiting_uninstall! }
|
7
|
+
before_perform { @shop.uninstalling! }
|
8
|
+
after_perform { @shop.uninstalled! }
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform(domain, shop_data)
|
13
|
+
# Mark the shop's charge status as "cancelled" unless charges have been waived.
|
14
|
+
unless @shop.charge_waived?
|
15
|
+
@shop.charge_cancelled!
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# The base class for all jobs that should be performed in the context of a particular Shop's API session. The first
|
2
|
+
# argument to any job inheriting from this class must be the domain of the relevant store, so that the appropriate
|
3
|
+
# Shop model can be fetched and the temporary API session created.
|
4
|
+
|
5
|
+
module DiscoApp
|
6
|
+
class ShopJob < ActiveJob::Base
|
7
|
+
|
8
|
+
queue_as :default
|
9
|
+
|
10
|
+
before_perform { |job| find_shop(job) }
|
11
|
+
before_enqueue { |job| find_shop(job) }
|
12
|
+
|
13
|
+
around_enqueue { |job, block| shop_context(job, block) }
|
14
|
+
around_perform { |job, block| shop_context(job, block) }
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def find_shop(job)
|
19
|
+
@shop ||= Shop.find_by!(shopify_domain: job.arguments.first)
|
20
|
+
end
|
21
|
+
|
22
|
+
def shop_context(job, block)
|
23
|
+
@shop.temp {
|
24
|
+
block.call(job.arguments)
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module DiscoApp
|
2
|
+
class ShopUpdateJob < DiscoApp::ShopJob
|
3
|
+
|
4
|
+
def perform(domain, shop_data = nil)
|
5
|
+
# If we weren't provided with shop data (eg from a webhook), fetch it.
|
6
|
+
shop_data ||= ActiveSupport::JSON::decode(ShopifyAPI::Shop.current.to_json)
|
7
|
+
|
8
|
+
# Ensure we can access shop data through symbols.
|
9
|
+
shop_data = HashWithIndifferentAccess.new(shop_data)
|
10
|
+
|
11
|
+
# Update model attributes present in both our model and the data hash.
|
12
|
+
@shop.update_attributes(shop_data.except(:id, :created_at).slice(*DiscoApp::Shop.column_names))
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module DiscoApp::Concerns::Plan
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
|
6
|
+
has_many :subscriptions
|
7
|
+
has_many :shops, through: :subscriptions
|
8
|
+
|
9
|
+
enum status: [:available, :unavailable, :hidden]
|
10
|
+
|
11
|
+
scope :available, -> { where status: statuses[:available] }
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module DiscoApp::Concerns::Shop
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
include ShopifyApp::Shop
|
6
|
+
|
7
|
+
# Define relationships to plans and subscriptions.
|
8
|
+
has_many :subscriptions
|
9
|
+
has_many :plans, through: :subscriptions
|
10
|
+
|
11
|
+
# Define possible installation statuses as an enum.
|
12
|
+
enum status: [:never_installed, :awaiting_install, :installing, :installed, :awaiting_uninstall, :uninstalling, :uninstalled]
|
13
|
+
|
14
|
+
# Define possible charge statuses as an enum.
|
15
|
+
enum charge_status: [:charge_none, :charge_pending, :charge_accepted, :charge_declined, :charge_active, :charge_cancelled, :charge_waived]
|
16
|
+
|
17
|
+
# Define some useful scopes.
|
18
|
+
scope :status, -> (status) { where status: status }
|
19
|
+
scope :installed, -> { where status: statuses[:installed] }
|
20
|
+
scope :has_active_shopify_plan, -> { where.not(plan_name: [:cancelled, :frozen]) }
|
21
|
+
|
22
|
+
# Alias 'with_shopify_session' as 'temp', as per our existing conventions.
|
23
|
+
alias_method :temp, :with_shopify_session
|
24
|
+
|
25
|
+
# Return a hash of attributes that should be used to create a new charge for this shop.
|
26
|
+
# This method can be overridden by the inheriting Shop class in order to provide charges
|
27
|
+
# customised to a particular shop. Otherwise, the default settings configured in application.rb
|
28
|
+
# will be used.
|
29
|
+
def new_charge_attributes
|
30
|
+
{
|
31
|
+
type: Rails.configuration.x.shopify_charges_default_type,
|
32
|
+
name: Rails.configuration.x.shopify_app_name,
|
33
|
+
price: Rails.configuration.x.shopify_charges_default_price,
|
34
|
+
trial_days: Rails.configuration.x.shopify_charges_default_trial_days,
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
# Update this Shop's charge_status attribute based on the given Shopify charge object.
|
39
|
+
def update_charge_status(shopify_charge)
|
40
|
+
status_update_method_name = "charge_#{shopify_charge.status}!"
|
41
|
+
self.public_send(status_update_method_name) if self.respond_to? status_update_method_name
|
42
|
+
end
|
43
|
+
|
44
|
+
# Convenience method to get the currently active subscription for this Shop.
|
45
|
+
def current_subscription
|
46
|
+
subscriptions.active.first
|
47
|
+
end
|
48
|
+
|
49
|
+
# Return the absolute URL to the shop's storefront.
|
50
|
+
# @TODO: Account for HTTPS.
|
51
|
+
def url
|
52
|
+
"http://#{domain}"
|
53
|
+
end
|
54
|
+
|
55
|
+
# Return the absolute URL to the shop's admin.
|
56
|
+
def admin_url
|
57
|
+
"https://#{shopify_domain}/admin"
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module DiscoApp::Concerns::Subscription
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
|
6
|
+
belongs_to :shop
|
7
|
+
belongs_to :plan
|
8
|
+
|
9
|
+
enum status: [:active, :replaced, :cancelled]
|
10
|
+
|
11
|
+
scope :active, -> { where status: statuses[:active] }
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module DiscoApp
|
2
|
+
class SessionStorage
|
3
|
+
def self.store(session)
|
4
|
+
shop = Shop.find_or_initialize_by(shopify_domain: session.url)
|
5
|
+
shop.shopify_token = session.token
|
6
|
+
shop.save!
|
7
|
+
shop.id
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.retrieve(id)
|
11
|
+
return unless id
|
12
|
+
shop = Shop.find(id)
|
13
|
+
ShopifyAPI::Session.new(shop.shopify_domain, shop.shopify_token)
|
14
|
+
rescue ActiveRecord::RecordNotFound
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module DiscoApp
|
2
|
+
class ChargesService
|
3
|
+
|
4
|
+
# Create a new charge for the given Shop using the Shopify API.
|
5
|
+
#
|
6
|
+
# The attributes of the charge are fetched using the shop's `new_charge_attributes` method, which can be overriden
|
7
|
+
# to provide custom charge types for individual shops.
|
8
|
+
#
|
9
|
+
# Returns the new Shopify charge model on success, nil otherwise.
|
10
|
+
def self.create(shop)
|
11
|
+
shopify_charge = shop.temp {
|
12
|
+
self.charge_api_class(shop).create(self.new_charge_attributes(shop))
|
13
|
+
}
|
14
|
+
|
15
|
+
# If the charge was successfully created, update the charge status on the shop.
|
16
|
+
shop.update_charge_status(shopify_charge) if shopify_charge
|
17
|
+
|
18
|
+
# Return the charge.
|
19
|
+
shopify_charge
|
20
|
+
end
|
21
|
+
|
22
|
+
# Fetch the specified charge for the given Shop using the Shopify API and check that it has been actioned (either
|
23
|
+
# accepted or declined). Updates the shop object's charge status, then returns the charge if it was accepted or
|
24
|
+
# nil otherwise.
|
25
|
+
def self.get_accepted_charge(shop, charge_id)
|
26
|
+
begin
|
27
|
+
shopify_charge = shop.temp {
|
28
|
+
self.charge_api_class(shop).find(charge_id)
|
29
|
+
}
|
30
|
+
|
31
|
+
# If the charge was successfully fetched, update the status for the shop accordingly.
|
32
|
+
shop.update_charge_status(shopify_charge) if shopify_charge
|
33
|
+
|
34
|
+
shopify_charge
|
35
|
+
rescue
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Attempt to activate the given Shopify charge for the given Shop using the Shopify API.
|
41
|
+
# Returns true on successful activation, false otherwise.
|
42
|
+
def self.activate(shop, shopify_charge)
|
43
|
+
begin
|
44
|
+
shop.temp {
|
45
|
+
shopify_charge.activate
|
46
|
+
}
|
47
|
+
shop.charge_active!
|
48
|
+
true
|
49
|
+
rescue
|
50
|
+
false
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Merge the new_charge_attributes returned by the given shop model and merge them with some application-level
|
55
|
+
# charge attributes.
|
56
|
+
def self.new_charge_attributes(shop)
|
57
|
+
shop.new_charge_attributes.merge(
|
58
|
+
return_url: DiscoApp::Engine.routes.url_helpers.activate_charge_url,
|
59
|
+
test: !Rails.configuration.x.shopify_charges_real,
|
60
|
+
)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Get the appropriate Shopify API class for the given shop (either ApplicationCharge or RecurringApplicationCharge).
|
64
|
+
def self.charge_api_class(shop)
|
65
|
+
if shop.new_charge_attributes[:type] == :one_time
|
66
|
+
ShopifyAPI::ApplicationCharge
|
67
|
+
else
|
68
|
+
ShopifyAPI::RecurringApplicationCharge
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class DiscoApp::SubscriptionService
|
2
|
+
|
3
|
+
# Subscribe the given shop to the given plan.
|
4
|
+
def self.subscribe(shop, plan)
|
5
|
+
# Mark all existing active subscriptions as replaced.
|
6
|
+
shop.subscriptions.active.update_all(status: DiscoApp::Subscription.statuses[:replaced])
|
7
|
+
|
8
|
+
# Add the new subscription.
|
9
|
+
DiscoApp::Subscription.create!(
|
10
|
+
shop: shop,
|
11
|
+
plan: plan,
|
12
|
+
status: DiscoApp::Subscription.statuses[:active],
|
13
|
+
name: plan.name,
|
14
|
+
charge_type: plan.charge_type,
|
15
|
+
price: plan.default_price,
|
16
|
+
trial_days: plan.default_trial_days
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Cancel any active subscription for the given shop.
|
21
|
+
def self.cancel(shop)
|
22
|
+
shop.subscriptions.active.update_all(status: DiscoApp::Subscription.statuses[:cancelled])
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class DiscoApp::WebhookService
|
2
|
+
|
3
|
+
# Return true iff the provided hmac_to_verify matches that calculated from the
|
4
|
+
# give data and secret.
|
5
|
+
def self.is_valid_hmac?(body, secret, hmac_to_verify)
|
6
|
+
self.calculated_hmac(body, secret) == hmac_to_verify
|
7
|
+
end
|
8
|
+
|
9
|
+
# Calculate the HMAC for the given data and secret.
|
10
|
+
def self.calculated_hmac(body, secret)
|
11
|
+
digest = OpenSSL::Digest.new('sha256')
|
12
|
+
Base64.encode64(OpenSSL::HMAC.digest(digest, secret, body)).strip
|
13
|
+
end
|
14
|
+
|
15
|
+
# Try to find a job class for the given webhook topic.
|
16
|
+
def self.find_job_class(topic)
|
17
|
+
begin
|
18
|
+
# First try to find a top-level matching job class.
|
19
|
+
"#{topic}_job".gsub('/', '_').classify.constantize
|
20
|
+
rescue NameError
|
21
|
+
# If that fails, try to find a DiscoApp:: prefixed job class.
|
22
|
+
begin
|
23
|
+
%Q{DiscoApp::#{"#{topic}_job".gsub('/', '_').classify}}.constantize
|
24
|
+
rescue NameError
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
activate_charge
|
@@ -0,0 +1 @@
|
|
1
|
+
create_charge
|
@@ -0,0 +1,45 @@
|
|
1
|
+
<% provide(:title, 'Thankyou') %>
|
2
|
+
|
3
|
+
<div class="row">
|
4
|
+
<% if @shop.charge_declined? %>
|
5
|
+
<div class="alert alert-warning">
|
6
|
+
<p>
|
7
|
+
Oops! Looks like you declined the charge.
|
8
|
+
Unfortunately, you'll have to accept the charge on the next screen in order to continue installing the application.
|
9
|
+
</p>
|
10
|
+
</div>
|
11
|
+
<% elsif @shop.charge_cancelled? %>
|
12
|
+
<div class="alert alert-warning">
|
13
|
+
<p>
|
14
|
+
Your authorized charge for this application has expired.
|
15
|
+
This could have occurred if:
|
16
|
+
</p>
|
17
|
+
<ul>
|
18
|
+
<li>You uninstalled and reinstalled the application; or</li>
|
19
|
+
<li>Your plan level has changed.</li>
|
20
|
+
</ul>
|
21
|
+
<p>
|
22
|
+
In either case, it's no problem!
|
23
|
+
Simply click okay and you'll be asked to authorize a new charge.
|
24
|
+
Don't worry - you *wont'* be billed twice.
|
25
|
+
</p>
|
26
|
+
</div>
|
27
|
+
<% else %>
|
28
|
+
<div class="alert alert-success">
|
29
|
+
<p>
|
30
|
+
Thanks for installing <%= Rails.configuration.x.shopify_app_name %>!
|
31
|
+
</p>
|
32
|
+
<p>
|
33
|
+
Before we start setting things up, we need you to authorize a charge for the application.
|
34
|
+
</p>
|
35
|
+
</div>
|
36
|
+
<% end %>
|
37
|
+
</div>
|
38
|
+
|
39
|
+
<div class="row">
|
40
|
+
<%= form_tag disco_app.create_charge_path, method: 'POST', target: '_parent' do %>
|
41
|
+
<div class="form-group">
|
42
|
+
<%= submit_tag 'Okay', class: 'form-input' %>
|
43
|
+
</div>
|
44
|
+
<% end %>
|
45
|
+
</div>
|
@@ -0,0 +1 @@
|
|
1
|
+
uninstalling
|
@@ -0,0 +1 @@
|
|
1
|
+
404 Not Found
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<% disabled ||= false %>
|
2
|
+
<div class="next-card <% if disabled %>next-card--disabled<% end %>">
|
3
|
+
<% if content_for?(:card_header) %>
|
4
|
+
<header class="next-card__header">
|
5
|
+
<%= content_for :card_header %>
|
6
|
+
</header>
|
7
|
+
<% end %>
|
8
|
+
<section class="next-card__section">
|
9
|
+
<%= content_for :card_content %>
|
10
|
+
</section>
|
11
|
+
<% if content_for?(:card_footer) %>
|
12
|
+
<footer class="next-card__footer">
|
13
|
+
<%= content_for :card_footer %>
|
14
|
+
</footer>
|
15
|
+
<% end %>
|
16
|
+
</div>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<section class="section">
|
2
|
+
<div class="layout-content">
|
3
|
+
|
4
|
+
<aside class="layout-content__sidebar layout-content__first">
|
5
|
+
<% if content_for?(:section_summary) %>
|
6
|
+
<div class="section-summary">
|
7
|
+
<%= content_for :section_summary %>
|
8
|
+
</div>
|
9
|
+
<% end %>
|
10
|
+
</aside>
|
11
|
+
|
12
|
+
<section class="layout-content__main">
|
13
|
+
<%= content_for :section_content %>
|
14
|
+
</section>
|
15
|
+
|
16
|
+
</div>
|
17
|
+
</section>
|
@@ -0,0 +1,18 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title><%= yield(:title) %></title>
|
5
|
+
|
6
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
7
|
+
|
8
|
+
<%= csrf_meta_tags %>
|
9
|
+
|
10
|
+
<%= yield :extra_head %>
|
11
|
+
</head>
|
12
|
+
<body>
|
13
|
+
|
14
|
+
<%= yield %>
|
15
|
+
|
16
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
17
|
+
</body>
|
18
|
+
</html>
|
@@ -0,0 +1,41 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title><%= yield(:title) %></title>
|
5
|
+
|
6
|
+
<script src="//cdn.shopify.com/s/assets/external/app.js?<%= Time.now.strftime('%Y%m%d%H') %>"></script>
|
7
|
+
<script type="text/javascript">
|
8
|
+
// Initialise the Shopify App.
|
9
|
+
ShopifyApp.init({
|
10
|
+
"apiKey": "<%= ShopifyApp.configuration.api_key %>",
|
11
|
+
"shopOrigin": "<%= "https://#{ @shop_session.url }" if @shop_session %>",
|
12
|
+
"debug": <%= Rails.env.development? ? 'true' : 'false' %>
|
13
|
+
});
|
14
|
+
</script>
|
15
|
+
|
16
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
17
|
+
|
18
|
+
<%= csrf_meta_tags %>
|
19
|
+
|
20
|
+
<%= yield :extra_head %>
|
21
|
+
</head>
|
22
|
+
<body>
|
23
|
+
<script type="text/javascript">
|
24
|
+
ShopifyApp.Bar.initialize({
|
25
|
+
title: "<%= yield(:title) %>",
|
26
|
+
icon: "<%= image_url("disco_app/icon.svg") %>",
|
27
|
+
buttons: <%= content_for?(:buttons) ? content_for(:buttons) : '{}' %>
|
28
|
+
});
|
29
|
+
</script>
|
30
|
+
|
31
|
+
<%= yield %>
|
32
|
+
|
33
|
+
<% flash.each do |key, message| %>
|
34
|
+
<script type="text/javascript">
|
35
|
+
ShopifyApp.flash<%= (key == 'error') ? 'Error' : 'Notice' %>('<%= message %>');
|
36
|
+
</script>
|
37
|
+
<% end %>
|
38
|
+
|
39
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
40
|
+
</body>
|
41
|
+
</html>
|
@@ -0,0 +1,26 @@
|
|
1
|
+
<% provide(:title, 'Install') %>
|
2
|
+
|
3
|
+
<%= form_tag shopify_app.login_path do %>
|
4
|
+
<div class="modal-dialog">
|
5
|
+
<div class="modal-content">
|
6
|
+
<div class="modal-body">
|
7
|
+
|
8
|
+
<% flash.each do |message_type, message| %>
|
9
|
+
<div class="alert alert-<%= message_type %>"><%= message %></div>
|
10
|
+
<% end %>
|
11
|
+
|
12
|
+
<div class="form-group">
|
13
|
+
<div class="input-group">
|
14
|
+
<div class="input-group-addon">http://</div>
|
15
|
+
<input type="text" class="form-control" id="shop" name="shop" placeholder="your-store" autocomplete="off" autofocus="on" />
|
16
|
+
<div class="input-group-addon">.myshopify.com</div>
|
17
|
+
</div>
|
18
|
+
</div>
|
19
|
+
|
20
|
+
</div>
|
21
|
+
<div class="modal-footer">
|
22
|
+
<button type="submit" class="btn btn-primary">Install</button>
|
23
|
+
</div>
|
24
|
+
</div>
|
25
|
+
</div>
|
26
|
+
<% end %>
|