beyond_canvas 0.19.2.pre → 0.23.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -45
  3. data/app/assets/images/icons/adblocker.svg +5 -0
  4. data/app/assets/images/icons/checkbox_checked.svg +1 -1
  5. data/app/assets/images/icons/checkbox_unchecked.svg +1 -1
  6. data/app/assets/images/icons/external_link.svg +1 -0
  7. data/app/assets/images/icons/toggle.svg +1 -0
  8. data/app/assets/images/icons/toggle_checked.svg +3 -0
  9. data/app/assets/images/icons/toggle_unchecked.svg +3 -0
  10. data/app/assets/javascripts/beyond_canvas/base.js +107 -44
  11. data/app/assets/stylesheets/beyond_canvas/base.scss +21 -18
  12. data/app/assets/stylesheets/beyond_canvas/components/_action_bar.scss +11 -6
  13. data/app/assets/stylesheets/beyond_canvas/components/_breadcrumbs.scss +8 -4
  14. data/app/assets/stylesheets/beyond_canvas/components/_buttons.scss +50 -56
  15. data/app/assets/stylesheets/beyond_canvas/components/_cards.scss +20 -12
  16. data/app/assets/stylesheets/beyond_canvas/components/_collapse.scss +33 -0
  17. data/app/assets/stylesheets/beyond_canvas/components/_flash.scss +13 -12
  18. data/app/assets/stylesheets/beyond_canvas/components/_forms.scss +2 -2
  19. data/app/assets/stylesheets/beyond_canvas/components/_inputs.scss +186 -38
  20. data/app/assets/stylesheets/beyond_canvas/components/_links.scss +80 -4
  21. data/app/assets/stylesheets/beyond_canvas/components/_main.scss +3 -3
  22. data/app/assets/stylesheets/beyond_canvas/components/_menu.scss +13 -0
  23. data/app/assets/stylesheets/beyond_canvas/components/_modals.scss +48 -18
  24. data/app/assets/stylesheets/beyond_canvas/components/_notices.scss +12 -13
  25. data/app/assets/stylesheets/beyond_canvas/components/_scrollbox.scss +5 -4
  26. data/app/assets/stylesheets/beyond_canvas/components/_select2.scss +70 -0
  27. data/app/assets/stylesheets/beyond_canvas/components/_statuses.scss +28 -0
  28. data/app/assets/stylesheets/beyond_canvas/components/_step_list.scss +67 -0
  29. data/app/assets/stylesheets/beyond_canvas/components/_tables.scss +10 -5
  30. data/app/assets/stylesheets/beyond_canvas/components/_titles.scss +1 -1
  31. data/app/assets/stylesheets/beyond_canvas/mailer.scss +2 -1
  32. data/app/assets/stylesheets/beyond_canvas/settings/_base_variables.scss +241 -0
  33. data/app/assets/stylesheets/beyond_canvas/settings/{_variables.scss → _constant_variables.scss} +60 -53
  34. data/app/assets/stylesheets/beyond_canvas/settings/_custom_variables.scss +233 -0
  35. data/app/assets/stylesheets/beyond_canvas/settings/_typography.scss +7 -3
  36. data/app/assets/stylesheets/beyond_canvas/utilities/_mixins.scss +39 -3
  37. data/app/controllers/beyond_canvas/authentications_controller.rb +18 -2
  38. data/app/controllers/beyond_canvas/system_controller.rb +3 -1
  39. data/app/controllers/beyond_canvas/webhooks_controller.rb +49 -0
  40. data/app/controllers/concerns/beyond_canvas/add_blocker_check.rb +17 -0
  41. data/app/controllers/concerns/beyond_canvas/custom_styles.rb +54 -0
  42. data/app/controllers/concerns/beyond_canvas/locale_management.rb +31 -8
  43. data/app/controllers/concerns/beyond_canvas/request_validation.rb +6 -0
  44. data/app/form_builders/beyond_canvas/form_builder.rb +62 -49
  45. data/app/helpers/beyond_canvas/application_helper.rb +48 -4
  46. data/app/helpers/beyond_canvas/form_tag_helper.rb +130 -0
  47. data/app/helpers/beyond_canvas/statuses_helper.rb +26 -0
  48. data/app/javascript/beyond_canvas/base.js +1 -1
  49. data/app/javascript/beyond_canvas/initializers/buttons.js +49 -29
  50. data/app/javascript/beyond_canvas/initializers/collapse.js +8 -0
  51. data/app/javascript/beyond_canvas/initializers/flash.js +33 -11
  52. data/app/javascript/beyond_canvas/initializers/inputs.js +9 -4
  53. data/app/javascript/beyond_canvas/initializers/modals.js +46 -10
  54. data/app/views/beyond_canvas/mailer/_header.html.erb +2 -2
  55. data/app/views/beyond_canvas/shared/_breadcrumbs.html.erb +5 -2
  56. data/app/views/beyond_canvas/shared/_flash.html.erb +14 -10
  57. data/app/views/beyond_canvas/shared/_head.html.erb +4 -0
  58. data/app/views/beyond_canvas/shared/_locales.html.erb +1 -1
  59. data/app/views/layouts/beyond_canvas/application.html.erb +1 -2
  60. data/app/views/layouts/beyond_canvas/public.html.erb +1 -1
  61. data/config/initializers/beyond_canvas/constants.rb +13 -0
  62. data/config/initializers/beyond_canvas/session_store.rb +8 -0
  63. data/lib/beyond_canvas/configuration.rb +9 -1
  64. data/lib/beyond_canvas/engine.rb +7 -0
  65. data/lib/beyond_canvas/rails/routes.rb +27 -7
  66. data/lib/beyond_canvas/version.rb +1 -1
  67. data/lib/beyond_canvas/webhook_event_registration.rb +19 -0
  68. data/lib/beyond_canvas.rb +6 -4
  69. data/lib/generators/beyond_canvas/custom_styles/templates/beyond_canvas_custom_styles.scss +33 -0
  70. data/lib/generators/beyond_canvas/install/install_generator.rb +6 -0
  71. data/lib/generators/beyond_canvas/install/templates/beyond_canvas.rb.erb +38 -1
  72. data/lib/generators/beyond_canvas/model/model_generator.rb +3 -0
  73. data/lib/generators/beyond_canvas/webhook/templates/webhooks_controller.rb +22 -0
  74. data/lib/generators/beyond_canvas/webhook/webhook_generator.rb +15 -0
  75. data/lib/models/concerns/authentication.rb +10 -3
  76. data/lib/models/concerns/utils.rb +6 -5
  77. data/lib/models/concerns/webhook.rb +123 -0
  78. data/lib/models/shop.rb +1 -0
  79. metadata +73 -40
  80. data/app/assets/stylesheets/beyond_canvas/components/_comments.scss +0 -6
  81. data/app/assets/stylesheets/beyond_canvas/components/_containers.scss +0 -37
  82. data/app/views/beyond_canvas/shared/_modal.html.erb +0 -6
  83. data/config/routes.rb +0 -12
@@ -3,26 +3,30 @@
3
3
  module BeyondCanvas
4
4
  class Configuration # :nodoc:
5
5
  attr_accessor :site_title, :site_logo, :favicon, :skip_webpacker, :encryption_key, :namespace, :cockpit_app,
6
- :open_app_url, :preinstalled, :debug_mode
6
+ :open_app_url, :preinstalled, :debug_mode, :webhook_site_url, :email_logo, :client_credentials
7
7
 
8
8
  include AssetRegistration
9
9
  include MenuItemRegistration
10
+ include WebhookEventRegistration
10
11
 
11
12
  def initialize
12
13
  @cockpit_app = false
13
14
  @debug_mode = false
15
+ @client_credentials = false
14
16
  @encryption_key = nil
15
17
  @favicon = nil
16
18
  @namespace = '/'
17
19
  @open_app_url = nil
18
20
  @preinstalled = false
19
21
  @site_logo = nil
22
+ @email_logo = nil
20
23
  @site_title = ::Rails.application.class.name.split('::').first.humanize
21
24
  @skip_webpacker = false
22
25
  end
23
26
 
24
27
  def setup!
25
28
  register_default_assets
29
+ register_default_webhook_events
26
30
  end
27
31
 
28
32
  private
@@ -31,5 +35,9 @@ module BeyondCanvas
31
35
  register_stylesheet 'beyond_canvas.css', media: 'screen'
32
36
  register_javascript 'beyond_canvas.js'
33
37
  end
38
+
39
+ def register_default_webhook_events
40
+ register_webhook_event('app.uninstalled')
41
+ end
34
42
  end
35
43
  end
@@ -21,6 +21,7 @@ module BeyondCanvas
21
21
  ActiveSupport.on_load :action_controller do
22
22
  include ::BeyondCanvas::LocaleManagement
23
23
  include ::BeyondCanvas::RequestValidation
24
+ include ::BeyondCanvas::CustomStyles
24
25
  include ::BeyondCanvas::StatusCodes
25
26
  include ::BeyondCanvas::AuthenticationsHelper
26
27
  include ::BeyondCanvas::DebugHelper
@@ -29,5 +30,11 @@ module BeyondCanvas
29
30
  ::ActionController::Base.helper BeyondCanvas::Engine.helpers
30
31
  end
31
32
  end
33
+
34
+ config.after_initialize do
35
+ ActiveSupport.on_load :action_controller do
36
+ include ::BeyondCanvas::AddBlockerCheck
37
+ end
38
+ end
32
39
  end
33
40
  end
@@ -3,18 +3,38 @@
3
3
  module ActionDispatch
4
4
  module Routing
5
5
  class Mapper # :nodoc:
6
- def beyond_canvas_routes(options = nil)
7
- mount BeyondCanvas::Engine => BeyondCanvas.configuration.namespace
6
+ def beyond_canvas_routes(*resources)
7
+ scope BeyondCanvas.configuration.namespace do
8
+ put 'locale', to: 'beyond_canvas/system#update_locale', as: :update_locale
9
+ end
10
+
11
+ resources = resources.extract_options!
12
+
13
+ beyond_canvas_for_controllers(resources[:controllers] || {})
14
+ end
8
15
 
9
- BeyondCanvas.use_rails_app_controller = options.present? && options[:custom_controller].present?
16
+ protected
10
17
 
11
- set_routes if BeyondCanvas.use_rails_app_controller
18
+ def beyond_canvas_for_controllers(controllers)
19
+ [:authentications, :webhooks].each do |method|
20
+ controllers.key?(method) ? send("set_#{method}_routes", controllers[method]) : send("set_#{method}_routes")
21
+ end
22
+ end
23
+
24
+ def set_webhooks_routes(controller = 'beyond_canvas/webhooks')
25
+ scope BeyondCanvas.configuration.namespace do
26
+ resources :shops, only: [] do
27
+ member do
28
+ post :beyond_webhook, controller: controller, action: :webhook
29
+ end
30
+ end
31
+ end
12
32
  end
13
33
 
14
- def set_routes
34
+ def set_authentications_routes(controller = 'beyond_canvas/authentications')
15
35
  scope BeyondCanvas.configuration.namespace do
16
- get 'callback', controller: :authentications, action: :new
17
- post 'callback', controller: :authentications, action: :install
36
+ get 'callback', controller: controller, action: :new
37
+ post 'callback', controller: controller, action: :install
18
38
  end
19
39
  end
20
40
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BeyondCanvas
4
- VERSION = '0.19.2.pre'
4
+ VERSION = '0.23.0.pre'
5
5
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BeyondCanvas
4
+ module WebhookEventRegistration # :nodoc:
5
+ def register_webhook_event(event)
6
+ event.is_a?(Array) ? webhook_events.merge(event) : webhook_events.add(event)
7
+ end
8
+
9
+ def webhook_events
10
+ @webhook_events ||= Set.new
11
+ end
12
+
13
+ def clear_webhook_events!
14
+ webhook_events.clear
15
+ end
16
+
17
+ alias register_webhook_events register_webhook_event
18
+ end
19
+ end
data/lib/beyond_canvas.rb CHANGED
@@ -16,10 +16,11 @@ require 'beyond_api'
16
16
  require 'attr_encrypted'
17
17
 
18
18
  module BeyondCanvas # :nodoc:
19
- autoload :AssetRegistration, 'beyond_canvas/asset_registration'
20
- autoload :Configuration, 'beyond_canvas/configuration'
21
- autoload :MenuItemRegistration, 'beyond_canvas/menu_item_registration'
22
- autoload :ParameterSanitizer, 'beyond_canvas/parameter_sanitizer'
19
+ autoload :AssetRegistration, 'beyond_canvas/asset_registration'
20
+ autoload :Configuration, 'beyond_canvas/configuration'
21
+ autoload :MenuItemRegistration, 'beyond_canvas/menu_item_registration'
22
+ autoload :ParameterSanitizer, 'beyond_canvas/parameter_sanitizer'
23
+ autoload :WebhookEventRegistration, 'beyond_canvas/webhook_event_registration'
23
24
 
24
25
  module Models # :nodoc:
25
26
  autoload :Shop, 'models/shop'
@@ -27,6 +28,7 @@ module BeyondCanvas # :nodoc:
27
28
  module Concerns
28
29
  autoload :Authentication, 'models/concerns/authentication'
29
30
  autoload :Utils, 'models/concerns/utils'
31
+ autoload :Webhook, 'models/concerns/webhook'
30
32
  end
31
33
  end
32
34
 
@@ -47,6 +47,10 @@
47
47
  // $button-danger-background: $palette-danger;
48
48
  // $button-danger-color: $white;
49
49
 
50
+ // $button-disabled-background: rgb(208,208,208);
51
+ // $button-disabled-color: rgb(144,144,144);
52
+ // $button-disabled-text-shadow: 1px 1px 0 $white;
53
+
50
54
  // $button-border-radius: 3px;
51
55
  // $button-box-shadow: true;
52
56
 
@@ -77,6 +81,8 @@
77
81
  // $input-border-color: rgb(217, 216, 195);
78
82
  // $input-border-color-focus: $palette-primary;
79
83
  // $input-errors-color: $palette-danger;
84
+ // $input-disabled-background: rgb(245,244,239);
85
+ // $input-disabled-color: rgb(163,161,137);
80
86
 
81
87
  // ************************************************************
82
88
  // Checkboxes
@@ -148,6 +154,19 @@
148
154
  // $notice-border-radius: 4px;
149
155
  // $notice-color: rgb(153, 153, 153);
150
156
 
157
+ // ************************************************************
158
+ // Status
159
+ // ************************************************************
160
+
161
+ // $status-good-background: rgb(205,225,165);
162
+ // $status-good-color: rgb(85,110,41);
163
+ // $status-warning-background: rgb(249,220,166);
164
+ // $status-warning-color: rgb(178,120,18);
165
+ // $status-danger-background: rgb(247,174,163);
166
+ // $status-danger-color: rgb(164,35,34);
167
+ // $status-neutral-background: rgb(224,223,205);
168
+ // $status-neutral-color: rgb(155,151,100);
169
+
151
170
  // ************************************************************
152
171
  // Breadcrums
153
172
  // ************************************************************
@@ -197,3 +216,17 @@
197
216
 
198
217
  // $modal-close-icon-color: #8b8b8b;
199
218
  // $modal-background-color: rgba(0, 0, 0, 0.5);
219
+
220
+ // ************************************************************
221
+ // Select2
222
+ // ************************************************************
223
+
224
+ // $select2-border-color: $input-border-color;
225
+ // $select2-border-color-focus: $input-border-color-focus;
226
+ // $select2-close-icon-color: #79764b;
227
+ // $select2-close-icon-color-hover: darken($select2-close-icon-color, 15%);
228
+ // $select2-tag-background: rgb(223, 222, 204);
229
+ // $select2-tag-color: rgb(122, 118, 76);
230
+ // $select2-disabled-background: rgb(245, 244, 239);
231
+ // $select2-option-hover-background: rgb(229, 241, 240);
232
+ // $select2-option-hover-color: rgb(62, 62, 62);
@@ -38,6 +38,12 @@ module BeyondCanvas
38
38
  def copy_locale
39
39
  copy_file '../../../../../config/locales/en.yml', 'config/locales/beyond_canvas.en.yml'
40
40
  end
41
+
42
+ def copy_public
43
+ Dir[File.join(BeyondCanvas::Engine.root, 'public', '*')].each do |file|
44
+ copy_file file, File.join(Rails.root, 'public', File.basename(file))
45
+ end
46
+ end
41
47
  end
42
48
  end
43
49
  end
@@ -33,6 +33,18 @@ BeyondCanvas.setup do |config|
33
33
  #
34
34
  # config.debug_mode = false
35
35
 
36
+ # Set an optional image to be send as email. Accepts any
37
+ # string supported by image_tag's source parameter. Overrides
38
+ # :email_logo.
39
+ #
40
+ # config.email_logo = 'logo.png' # Can't be svg
41
+
42
+ # ==> Authorization configuration
43
+
44
+ # Set if you want to retrieve the shop token using `grant_type=client_credentials`
45
+ # on development environment. Default is `grant_type=authorization_code`.
46
+ # config.client_credentials = false
47
+
36
48
  # ==> Site configuration
37
49
 
38
50
  # Set the title that is displayed on the main layout.
@@ -43,7 +55,7 @@ BeyondCanvas.setup do |config|
43
55
  # string supported by image_tag's source parameter or a SVG file. Overrides
44
56
  # :site_title.
45
57
  #
46
- # config.site_logo = 'logo.png'
58
+ # config.site_logo = 'logo.svg'
47
59
 
48
60
  # Set the favicon of the site.
49
61
  #
@@ -86,4 +98,29 @@ BeyondCanvas.setup do |config|
86
98
  # ==> Database encryption
87
99
 
88
100
  config.encryption_key = '<%= SecureRandom.hex(32) %>'
101
+
102
+ # Webhooks event listeners
103
+ # You can inculde the beyond
104
+ # By default Beyond canvas is listening for app.unistalled event.
105
+
106
+ # URL for get webhooks whe you are using develpment
107
+
108
+ # ==> Webhooks
109
+
110
+ # By default Beyond Canvas subscribes to `app.uninstalled` webhook event and
111
+ # handles the uninstallation of an app.
112
+ # If you want to subscribe to any of the existing webhooks (`product.created`,
113
+ # `product.updated`, `order.created`, `order.updated`) apart from
114
+ # `app.uninstalled` on app installation process, add them like follows:
115
+ #
116
+ # To subscribe to a single webhook event:
117
+ # config.register_webhook_event('product.created')
118
+ #
119
+ # To subscribe to multiple webhook events:
120
+ # config.register_webhook_events(['product.created', 'product.updated'])
121
+
122
+ # Set a URL where webhooks will call on non-production environments. You can
123
+ # make use of https://webhook.site/.
124
+ # On production environments, the URL will be automatically generated.
125
+ config.webhook_site_url = nil
89
126
  end
@@ -42,6 +42,9 @@ module BeyondCanvas
42
42
  t.text :encrypted_beyond_refresh_token, null: true
43
43
  t.text :encrypted_beyond_refresh_token_iv, null: true
44
44
 
45
+ t.text :encrypted_beyond_shared_secret, null: true
46
+ t.text :encrypted_beyond_shared_secret_iv, null: true
47
+
45
48
  RUBY
46
49
  end
47
50
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ class WebhooksController < BeyondCanvas::WebhooksController
4
+
5
+ private
6
+
7
+ # def handle_app_uninstalled(_data)
8
+ # end
9
+
10
+ # def handle_order_created(data)
11
+ # end
12
+
13
+ # def handle_order_updated(data)
14
+ # end
15
+
16
+ # def handle_product_created(data)
17
+ # end
18
+
19
+ # def handle_product_updated(data)
20
+ # end
21
+
22
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BeyondCanvas
4
+ module Generators
5
+ class WebhookGenerator < Rails::Generators::Base # :nodoc:
6
+ desc 'Creates an inherited Webhook controller in the app/controllers folder'
7
+
8
+ source_root File.expand_path('templates', __dir__)
9
+
10
+ def create_controller
11
+ template 'webhooks_controller.rb', "app/controllers/webhooks_controller.rb"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -40,15 +40,22 @@ module BeyondCanvas
40
40
  # NOTE: This method is used during the shop creation, as it is the only point
41
41
  # we know about the authentication code
42
42
  #
43
- def authenticate(params_code)
43
+ def authenticate(code)
44
44
  session = BeyondApi::Session.new(api_url: beyond_api_url)
45
- session.token.create(params_code)
45
+
46
+ if Rails.env.development? && BeyondCanvas.configuration.client_credentials
47
+ session.token.client_credentials
48
+ else
49
+ session.token.authorization_code(code)
50
+ end
51
+
46
52
  update(beyond_access_token: session.access_token,
47
53
  beyond_refresh_token: session.refresh_token)
48
54
  end
49
55
 
50
56
  def authenticated?
51
- beyond_access_token.present? && beyond_refresh_token.present?
57
+ beyond_access_token.present? &&
58
+ (Rails.env.development? && BeyondCanvas.configuration.client_credentials || beyond_refresh_token.present?)
52
59
  end
53
60
  end
54
61
  end
@@ -30,7 +30,7 @@ module BeyondCanvas
30
30
  def refresh_token_if_needed
31
31
  token_timestamp = decoded_jwt['exp']
32
32
  current_timestamp = DateTime.now.to_i
33
- return unless token_timestamp - current_timestamp <= 3600
33
+ return unless token_timestamp - current_timestamp <= 0
34
34
 
35
35
  refresh_token
36
36
  end
@@ -40,15 +40,16 @@ module BeyondCanvas
40
40
  #
41
41
  def to_session
42
42
  BeyondApi::Session.new(api_url: beyond_api_url,
43
- access_token: beyond_access_token,
44
- refresh_token: beyond_refresh_token)
43
+ access_token: beyond_access_token,
44
+ refresh_token: beyond_refresh_token)
45
45
  end
46
46
 
47
47
  #
48
48
  # Returns the shop url
49
49
  #
50
- def url
51
- "https://#{URI.parse(beyond_api_url).host}"
50
+ def url(path = nil, params = nil)
51
+ path = path[1..-1] if path&.chr == '/'
52
+ "https://#{URI.parse(beyond_api_url).host}/#{path}#{'?' +params&.to_query if params.present?}"
52
53
  end
53
54
 
54
55
  def has_scope?(scope)
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BeyondCanvas
4
+ module Models
5
+ module Concerns
6
+ module Webhook # :nodoc:
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ # Attribute accessor to generate webhook url
11
+ attr_accessor :http_host
12
+
13
+ ##############################################################################
14
+ # Encrypted attribute configuration
15
+ ##############################################################################
16
+
17
+ attr_encrypted :beyond_shared_secret, key: [BeyondCanvas.configuration.encryption_key].pack('H*')
18
+
19
+ ##############################################################################
20
+ # Validations
21
+ ##############################################################################
22
+
23
+ # Database fields
24
+
25
+ validates :beyond_shared_secret,
26
+ presence: {
27
+ unless: -> { encrypted_beyond_shared_secret_was.blank? }
28
+ }
29
+
30
+ ##############################################################################
31
+ # Instance methods
32
+ ##############################################################################
33
+
34
+ #
35
+ # Subscribes to Beyond webhooks:
36
+ # * Callback URI: {APP_URL}/shops/:id/beyond_webhook
37
+ #
38
+ def subscribe_to_beyond_webhooks
39
+ return if BeyondCanvas.configuration.webhook_events.to_a.empty?
40
+
41
+ # Unsubscribe from all existing Beyond webhooks
42
+ self.unsubscribe_from_all_beyond_webhooks
43
+ # Create and save a signer
44
+ self.create_signer
45
+ # Subscribe to webhooks
46
+ self.to_session.webhook_subscriptions.create(
47
+ callback_uri: beyond_webhook_url(self.id),
48
+ event_types: BeyondCanvas.configuration.webhook_events.to_a
49
+ )
50
+ end
51
+
52
+ #
53
+ # Unsubscribe from Beyond webhooks
54
+ #
55
+ def unsubscribe_from_all_beyond_webhooks
56
+ self.refresh_token_if_needed
57
+
58
+ session = self.to_session
59
+
60
+ session.webhook_subscriptions.all.dig(:embedded, :subscriptions)&.each do |webhook|
61
+ session.webhook_subscriptions.delete(webhook.dig(:id))
62
+ end
63
+ end
64
+
65
+ #
66
+ # Update suscription from Beyond webhooks
67
+ #
68
+ def update_suscription_of_beyond_webhooks(callback_uri = beyond_webhook_url(self.id))
69
+ self.refresh_token_if_needed
70
+
71
+ session = self.to_session
72
+
73
+ session.webhook_subscriptions.all.dig(:embedded, :subscriptions)&.each do |webhook|
74
+ session.webhook_subscriptions.update(webhook.dig(:id), callback_uri: callback_uri,
75
+ event_types: BeyondCanvas.configuration.webhook_events.to_a)
76
+ end
77
+ end
78
+
79
+
80
+ private
81
+
82
+ #
83
+ # Deletes all existing signers and creates and stores a new one
84
+ #
85
+ def create_signer
86
+ # Get all existing signer ids
87
+ signer_ids = self.to_session.signers.all.dig(:embedded, :signers)&.map(&:id)
88
+ # Remove the first signer if max number of signers reached to allow to create a new one
89
+ self.to_session.signers.delete(signer_ids.slice!(0)) if signer_ids.count == 5
90
+
91
+ # Create and save the new signer
92
+ signer = self.to_session.signers.create
93
+ self.update(beyond_shared_secret: signer.shared_secret)
94
+ # Delete old signers
95
+ signer_ids&.each { |signer_id| self.to_session.signers.delete(signer_id) }
96
+ end
97
+
98
+ #
99
+ # Generates the Beyond webhook url for a given shop_id. If Rails.env is development, it uses the WEBHOOK_SITE_URL
100
+ # environment variable. In any other case, it uses the request.env['HTTP_HOST'], that should be
101
+ # {APP_URL}/shops/:id/beyond_webhook
102
+ #
103
+ def beyond_webhook_url(shop_id)
104
+ if Rails.env.development?
105
+ obtain_webhook_site_url
106
+ else
107
+ Rails.application.routes.url_helpers.beyond_webhook_shop_url(shop_id, host: http_host || ENV['HTTP_HOST'],
108
+ protocol: 'https')
109
+ end
110
+ end
111
+
112
+ def obtain_webhook_site_url
113
+ if BeyondCanvas.configuration.webhook_site_url.blank?
114
+ raise 'Set BeyondCanvas.config.webhook_site_url environment variable for develpment'
115
+ end
116
+
117
+ BeyondCanvas.configuration.webhook_site_url
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end