shopify_app 17.0.5 → 17.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE/bug-report.md +63 -0
- data/.github/ISSUE_TEMPLATE/config.yml +1 -0
- data/.github/ISSUE_TEMPLATE/feature-request.md +33 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +17 -1
- data/CHANGELOG.md +5 -0
- data/CONTRIBUTING.md +76 -0
- data/Gemfile.lock +61 -61
- data/README.md +72 -593
- data/app/controllers/concerns/shopify_app/shop_access_scopes_verification.rb +32 -0
- data/app/controllers/shopify_app/callback_controller.rb +18 -2
- data/docs/Quickstart.md +15 -77
- data/docs/Upgrading.md +110 -0
- data/docs/shopify_app/authentication.md +124 -0
- data/docs/shopify_app/engine.md +82 -0
- data/docs/shopify_app/generators.md +127 -0
- data/docs/shopify_app/handling-access-scopes-changes.md +8 -0
- data/docs/shopify_app/script-tags.md +28 -0
- data/docs/shopify_app/session-repository.md +88 -0
- data/docs/shopify_app/testing.md +38 -0
- data/docs/shopify_app/webhooks.md +72 -0
- data/lib/generators/shopify_app/home_controller/templates/home_controller.rb +2 -0
- data/lib/generators/shopify_app/home_controller/templates/unauthenticated_home_controller.rb +1 -0
- data/lib/generators/shopify_app/install/install_generator.rb +30 -1
- data/lib/generators/shopify_app/install/templates/omniauth.rb +1 -0
- data/lib/generators/shopify_app/install/templates/shopify_app.rb.tt +5 -2
- data/lib/generators/shopify_app/install/templates/shopify_provider.rb.tt +8 -0
- data/lib/generators/shopify_app/shop_model/shop_model_generator.rb +27 -0
- data/lib/generators/shopify_app/shop_model/templates/db/migrate/add_shop_access_scopes_column.erb +5 -0
- data/lib/generators/shopify_app/shop_model/templates/shop.rb +1 -1
- data/lib/generators/shopify_app/shopify_app_generator.rb +1 -1
- data/lib/generators/shopify_app/user_model/templates/db/migrate/add_user_access_scopes_column.erb +5 -0
- data/lib/generators/shopify_app/user_model/templates/user.rb +1 -1
- data/lib/generators/shopify_app/user_model/user_model_generator.rb +27 -0
- data/lib/shopify_app.rb +10 -0
- data/lib/shopify_app/access_scopes/noop_strategy.rb +13 -0
- data/lib/shopify_app/access_scopes/shop_strategy.rb +24 -0
- data/lib/shopify_app/access_scopes/user_strategy.rb +41 -0
- data/lib/shopify_app/configuration.rb +22 -0
- data/lib/shopify_app/omniauth/omniauth_configuration.rb +64 -0
- data/lib/shopify_app/session/in_memory_shop_session_store.rb +9 -7
- data/lib/shopify_app/session/in_memory_user_session_store.rb +9 -7
- data/lib/shopify_app/session/shop_session_storage_with_scopes.rb +58 -0
- data/lib/shopify_app/session/user_session_storage_with_scopes.rb +58 -0
- data/lib/shopify_app/utils.rb +12 -0
- data/lib/shopify_app/version.rb +1 -1
- data/package.json +1 -1
- data/shopify_app.gemspec +1 -1
- metadata +27 -8
- data/.github/ISSUE_TEMPLATE.md +0 -19
- data/docs/install-on-dev-shop.png +0 -0
- data/docs/test-your-app.png +0 -0
- data/lib/generators/shopify_app/install/templates/shopify_provider.rb +0 -20
data/README.md
CHANGED
@@ -1,651 +1,130 @@
|
|
1
|
-
Shopify App
|
2
|
-
|
3
|
-
[![Version][gem]][gem_url] [![Build Status](https://github.com/Shopify/shopify_app/workflows/CI/badge.svg)](https://github.com/Shopify/shopify_app/actions?query=workflow%3ACI)
|
1
|
+
# Shopify App
|
2
|
+
|
3
|
+
[![Version][gem]][gem_url] [![Build Status](https://github.com/Shopify/shopify_app/workflows/CI/badge.svg)](https://github.com/Shopify/shopify_app/actions?query=workflow%3ACI) ![Supported Rails version][supported_rails_version]
|
4
4
|
|
5
5
|
[gem]: https://img.shields.io/gem/v/shopify_app.svg
|
6
6
|
[gem_url]: https://rubygems.org/gems/shopify_app
|
7
|
+
[supported_rails_version]: https://img.shields.io/badge/rails-%3C6.1.0-orange
|
7
8
|
|
9
|
+
This gem builds Rails applications that can be embedded in the Shopify Admin.
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
[Introduction](#introduction) |
|
12
|
+
[Requirements](#requirements) |
|
13
|
+
[Usage](#usage) |
|
14
|
+
[Documentation](#documentation) |
|
15
|
+
[Contributing](/CONTRIBUTING.md) |
|
16
|
+
[License](/LICENSE)
|
12
17
|
|
13
|
-
|
18
|
+
## Introduction
|
14
19
|
|
15
|
-
|
16
|
-
-----------------
|
17
|
-
- [Introduction](#introduction)
|
18
|
-
- [Become a Shopify App Developer](#become-a-shopify-app-developer)
|
19
|
-
- [Installation](#installation)
|
20
|
-
- [Generators](#generators)
|
21
|
-
- [Mounting the Engine](#mounting-the-engine)
|
22
|
-
- [Authentication](#authentication)
|
23
|
-
- [WebhooksManager](#webhooksmanager)
|
24
|
-
- [ScripttagsManager](#scripttagsmanager)
|
25
|
-
- [RotateShopifyTokenJob](#rotateshopifytokenjob)
|
26
|
-
- [App Tunneling](#app-tunneling)
|
27
|
-
- [AppProxyVerification](#appproxyverification)
|
28
|
-
- [Troubleshooting](#troubleshooting)
|
29
|
-
- [Testing an embedded app outside the Shopify admin](#testing-an-embedded-app-outside-the-shopify-admin)
|
30
|
-
- [Migration to 13.0.0](#migrating-to-1300)
|
31
|
-
- [Questions or problems?](#questions-or-problems)
|
32
|
-
- [Rails 6 Compatibility](#rails-6-compatibility)
|
33
|
-
- [Upgrading from 8.6 to 9.0.0](#upgrading-from-86-to-900)
|
20
|
+
This gem includes a Rails engine, generators, modules, and mixins that help create Rails applications that work with Shopify APIs. The [Shopify App Rails engine](/docs/shopify_app/engine.md) provides all the code required to implement OAuth with Shopify. The [default Shopify App generator](/docs/shopify_app/generators.md#-environment-rails-generate-shopify_app) builds an app that can be embedded in the Shopify Admin and secures it with [session tokens](https://shopify.dev/concepts/apps/building-embedded-apps-using-session-tokens).
|
34
21
|
|
35
|
-
|
36
|
-
|
37
|
-
Get started with the [Shopify Admin API](https://help.shopify.com/en/api/getting-started) faster; This gem includes a Rails Engine and generators for writing Rails applications using the Shopify API. The Engine provides a SessionsController and all the required code for authenticating with a shop via OAuth (other authentication methods are not supported).
|
22
|
+
<!-- This section is linked to in `templates/shopify_app.rb.tt`. Be careful renaming this heading. -->
|
23
|
+
## Requirements
|
38
24
|
|
39
|
-
|
25
|
+
> **Rails compatibility**
|
26
|
+
> * Rails 6.1 or above is not yet supported due to the new `cookies_same_site_protection` setting.
|
27
|
+
> * Use Shopify App `<= v7.2.8` if you need to work with Rails 4.
|
40
28
|
|
41
|
-
|
29
|
+
To become a Shopify app developer, you will need a [Shopify Partners](https://www.shopify.com/partners) account. Explore the [Shopify dev docs](https://shopify.dev/concepts/shopify-introduction) to learn more about [building Shopify apps](https://shopify.dev/concepts/apps).
|
42
30
|
|
43
|
-
|
31
|
+
This gem requires that you have the following credentials:
|
44
32
|
|
45
|
-
|
46
|
-
|
47
|
-
To become a Shopify App Developer, you'll need a [Shopify Partner account.](http://shopify.com/partners) If you don't have a Shopify Partner account, head to http://shopify.com/partners to create one before you start.
|
33
|
+
- **Shopify API key:** The API key app credential specified in your [Shopify Partners dashboard](https://partners.shopify.com/organizations).
|
34
|
+
- **Shopify API secret:** The API secret key app credential specified in your [Shopify Partners dashboard](https://partners.shopify.com/organizations).
|
48
35
|
|
49
|
-
|
36
|
+
## Usage
|
50
37
|
|
51
|
-
To
|
52
|
-
|
53
|
-
Installation
|
54
|
-
------------
|
55
|
-
To get started, add `shopify_app` to your Gemfile and run `bundle install`:
|
38
|
+
1. To get started, create a new Rails app:
|
56
39
|
|
57
40
|
``` sh
|
58
|
-
# Create a new rails app
|
59
41
|
$ rails new my_shopify_app
|
60
|
-
$ cd my_shopify_app
|
61
|
-
|
62
|
-
# Add the gem shopify_app to your Gemfile
|
63
|
-
$ bundle add shopify_app
|
64
|
-
```
|
65
|
-
|
66
|
-
Now we are ready to run any of the [generators](#generators) included with `shopify_app`. The following section explains the generators and what you can do with them.
|
67
|
-
|
68
|
-
|
69
|
-
#### Rails Compatibility
|
70
|
-
|
71
|
-
The latest version of shopify_app is compatible with Rails `>= 5`. Use version `<= v7.2.8` if you need to work with Rails 4.
|
72
|
-
|
73
|
-
|
74
|
-
Generators
|
75
|
-
----------
|
76
|
-
|
77
|
-
### API Keys
|
78
|
-
<!-- This anchor name `#api-keys` is linked to from user output in `templates/shopify_app.rb.tt` so beware of changing -->
|
79
|
-
Before starting the app, you'll need to ensure it can read the Shopify environment variables `SHOPIFY_API_KEY` and `SHOPIFY_API_SECRET`.
|
80
|
-
|
81
|
-
In a development environment, a common approach is to use the [dotenv-rails](https://github.com/bkeepers/dotenv) gem, along with an `.env` file in the following format:
|
82
|
-
|
83
|
-
```
|
84
|
-
SHOPIFY_API_KEY=your api key
|
85
|
-
SHOPIFY_API_SECRET=your api secret
|
86
|
-
```
|
87
|
-
|
88
|
-
These values can be found on the "App Setup" page in the [Shopify Partners Dashboard][dashboard].
|
89
|
-
(If you are using [shopify-app-cli](https://github.com/Shopify/shopify-app-cli) this `.env` file will be created automatically).
|
90
|
-
If you are checking your code into a code repository, ensure your `.gitignore` prevents your `.env` file from being checked into any publicly accessible code.
|
91
|
-
|
92
|
-
### Default Generator
|
93
|
-
|
94
|
-
The default generator will run the `install`, `shop`, `authenticated_controller`, and `home_controller` generators. This is the recommended way to start a new app from scratch:
|
95
|
-
|
96
|
-
```sh
|
97
|
-
$ rails generate shopify_app
|
98
|
-
```
|
99
|
-
|
100
|
-
After running the generator, you will need to run `rails db:migrate` to add new tables to your database. You can start your app with `bundle exec rails server` and install your app by visiting `http://localhost` in your web browser.
|
101
|
-
|
102
|
-
|
103
|
-
### Install Generator
|
104
|
-
|
105
|
-
```sh
|
106
|
-
$ rails generate shopify_app:install
|
107
|
-
```
|
108
|
-
|
109
|
-
Options include:
|
110
|
-
* `application_name` - the name of your app, it can be supplied with or without double-quotes if a whitespace is present. (e.g. `--application_name Example App` or `--application_name "Example App"`)
|
111
|
-
* `scope` - the OAuth access scope required for your app, e.g. **read_products, write_orders**. *Multiple options* need to be delimited by a comma-space and can be supplied with or without double-quotes
|
112
|
-
(e.g. `--scope read_products, write_orders, write_products` or `--scope "read_products, write_orders, write_products"`)
|
113
|
-
For more information, refer to the [docs](http://docs.shopify.com/api/tutorials/oauth).
|
114
|
-
* `embedded` - the default is to generate an [embedded app](http://docs.shopify.com/embedded-app-sdk), if you want a legacy non-embedded app then set this to false, `--embedded false`
|
115
|
-
* __[Not recommended for embedded apps]__ `with-cookie-authentication` - sets up the authentication strategy of the app to use cookies. By default, it uses JWT based session tokens.
|
116
|
-
|
117
|
-
You can update any of these settings later on easily; the arguments are simply for convenience.
|
118
|
-
|
119
|
-
The generator adds ShopifyApp and the required initializers to the host Rails application.
|
120
|
-
|
121
|
-
After running the `install` generator, you can start your app with `bundle exec rails server` and install your app by visiting localhost.
|
122
|
-
|
123
|
-
|
124
|
-
### Home Controller Generator
|
125
|
-
|
126
|
-
```sh
|
127
|
-
$ rails generate shopify_app:home_controller
|
128
|
-
```
|
129
|
-
|
130
|
-
This generator creates an example home controller and view which fetches and displays products using the Shopify API. By default, this generator creates an unauthenticated home_controller and a sample protected products_controller.
|
131
|
-
|
132
|
-
Options include:
|
133
|
-
* __[Not recommended for embedded apps]__ `with-cookie-authentication` - This flag generates an authenticated home_controller, where the authentication strategy relies on cookies. By default, this generator creates an unauthenticated home_controller and protected sample products_controller.
|
134
|
-
|
135
|
-
### Products Controller Generator
|
136
|
-
|
137
|
-
```sh
|
138
|
-
$ rails generate shopify_app:products_controller
|
139
|
-
```
|
140
|
-
|
141
|
-
This generator creates an example products API controller to fetch products using the Shopify API.
|
142
|
-
|
143
|
-
### App Proxy Controller Generator
|
144
|
-
|
145
|
-
```sh
|
146
|
-
$ rails generate shopify_app:app_proxy_controller
|
147
42
|
```
|
148
43
|
|
149
|
-
|
150
|
-
|
151
|
-
### Marketing Extension Generator
|
44
|
+
2. Add the Shopify App gem to `my_shopify_app`'s Gemfile.
|
152
45
|
|
153
46
|
```sh
|
154
|
-
$
|
155
|
-
```
|
156
|
-
|
157
|
-
This will create a controller with the endpoints required to build a [marketing activities extension](https://help.shopify.com/en/api/embedded-apps/app-extensions/shopify-admin/marketing-activities). The extension will be generated with a base URL at `/marketing_activities`, which should also be configured in partners.
|
158
|
-
|
159
|
-
### Controllers, Routes and Views
|
160
|
-
|
161
|
-
The last group of generators are for your convenience if you want to start overriding code included as part of the Rails engine. For example, by default the engine provides a simple SessionController, if you run the `rails generate shopify_app:controllers` generator then this code gets copied out into your app so you can start adding to it. Routes and views follow the exact same pattern.
|
162
|
-
|
163
|
-
Mounting the Engine
|
164
|
-
-------------------
|
165
|
-
|
166
|
-
Mounting the Engine will provide the basic routes to authenticating a shop with your application. By default it will provide:
|
167
|
-
|
168
|
-
| Verb | Route | Action |
|
169
|
-
|--------|-------------------------------|------------------------------|
|
170
|
-
|GET |'/login' |Login |
|
171
|
-
|POST |'/login' |Login |
|
172
|
-
|GET |'/auth/shopify/callback' |Authenticate Callback |
|
173
|
-
|GET |'/logout' |Logout |
|
174
|
-
|POST |'/webhooks/:type' |Webhook Callback |
|
175
|
-
|
176
|
-
### Nested Routes
|
177
|
-
|
178
|
-
The engine may also be mounted at a nested route, for example:
|
179
|
-
|
180
|
-
```ruby
|
181
|
-
mount ShopifyApp::Engine, at: '/nested'
|
182
|
-
```
|
183
|
-
|
184
|
-
This will create the Shopify engine routes under the specified subpath. You'll also need to make some updates to your `shopify_app.rb` and `omniauth.rb` initializers. First, update the shopify_app initializer to include a custom `root_url` e.g.:
|
185
|
-
|
186
|
-
```ruby
|
187
|
-
ShopifyApp.configure do |config|
|
188
|
-
config.root_url = '/nested'
|
189
|
-
end
|
190
|
-
```
|
191
|
-
|
192
|
-
then update the omniauth initializer to include a custom `callback_path` e.g.:
|
193
|
-
|
194
|
-
```ruby
|
195
|
-
provider :shopify,
|
196
|
-
ShopifyApp.configuration.api_key,
|
197
|
-
ShopifyApp.configuration.secret,
|
198
|
-
scope: ShopifyApp.configuration.scope,
|
199
|
-
callback_path: '/nested/auth/shopify/callback'
|
47
|
+
$ bundle add shopify_app
|
200
48
|
```
|
201
49
|
|
202
|
-
|
50
|
+
3. Create a `.env` file in the root of `my_shopify_app` to specify your Shopify API credentials:
|
203
51
|
|
204
|
-
```ruby
|
205
|
-
# config/routes.rb
|
206
|
-
Rails.application.routes.draw do
|
207
|
-
root :to => 'something_else#index'
|
208
|
-
get "/nested", to: "home#index"
|
209
|
-
mount ShopifyApp::Engine, at: '/nested'
|
210
|
-
end
|
211
52
|
```
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
### Custom login URL
|
216
|
-
|
217
|
-
While you can customize the login view by creating a `/app/views/shopify_app/sessions/new.html.erb` file, you may also want to customize the URL entirely. You can modify your `shopify_app.rb` initializer to provide a custom `login_url` e.g.:
|
218
|
-
|
219
|
-
```ruby
|
220
|
-
ShopifyApp.configure do |config|
|
221
|
-
config.login_url = 'https://my.domain.com/nested/login'
|
222
|
-
end
|
53
|
+
SHOPIFY_API_KEY=<Your Shopify API key>
|
54
|
+
SHOPIFY_API_SECRET=<Your Shopify API secret>
|
223
55
|
```
|
224
56
|
|
225
|
-
|
226
|
-
--------------
|
227
|
-
|
228
|
-
### Callback
|
229
|
-
|
230
|
-
Upon completing the authentication flow, Shopify calls the app at the `callback_path` mentioned before. If the app needs to do some extra work, it can define and configure the route to a custom callback controller, inheriting from `ShopifyApp::CallbackController` and hook into or override any of the defined helper methods. The default callback controller already provides the following behaviour:
|
231
|
-
* Logging into the shop and resetting the session
|
232
|
-
* [Installing Webhooks](https://github.com/Shopify/shopify_app#webhooksmanager)
|
233
|
-
* [Setting Scripttags](https://github.com/Shopify/shopify_app#scripttagsmanager)
|
234
|
-
* [Performing the AfterAuthenticate Job](https://github.com/Shopify/shopify_app#afterauthenticatejob)
|
235
|
-
* Redirecting to the return address
|
236
|
-
|
237
|
-
**Note that starting with version 8.4.0, we have extracted the callback logic in its own controller. If you are upgrading from a version older than 8.4.0 the callback action and related helper methods were defined in `ShopifyApp::SessionsController` ==> you will have to extend `ShopifyApp::CallbackController` instead and port your logic to the new controller.**
|
57
|
+
> In a development environment, you can use a gem like `dotenv-rails` to manage environment variables.
|
238
58
|
|
239
|
-
|
59
|
+
4. Run the default Shopify App generator to create an app that can be embedded in the Shopify Admin:
|
240
60
|
|
241
|
-
`ShopifyApp::SessionRepository` allows you as a developer to define how your sessions are stored and retrieved for shops. The `SessionRepository` is configured in the `config/initializers/shopify_app.rb` file and can be set to any object that implements `self.store(auth_session, *args)` which stores the session and returns a unique identifier and `self.retrieve(id)` which returns a `ShopifyAPI::Session` for the passed id. These methods are already implemented as part of the `ShopifyApp::SessionStorage` concern but can be overridden for custom implementation.
|
242
|
-
|
243
|
-
#### Shop-based token storage
|
244
|
-
Storing tokens on the store model means that any user login associated with the store will have equal access levels to whatever the original user granted the app.
|
245
|
-
```sh
|
246
|
-
$ rails generate shopify_app:shop_model
|
247
|
-
```
|
248
|
-
This will generate a shop model which will be the storage for the tokens necessary for authentication.
|
249
|
-
|
250
|
-
#### User-based token storage
|
251
|
-
A more granular control over the level of access per user on an app might be necessary, to which the shop-based token strategy is not sufficient. Shopify supports a user-based token storage strategy where a unique token to each user can be managed. Shop tokens must still be maintained if you are running background jobs so that you can make use of them when necessary.
|
252
61
|
```sh
|
253
|
-
$ rails generate shopify_app
|
254
|
-
$ rails generate shopify_app:user_model
|
255
|
-
```
|
256
|
-
This will generate a shop model and user model, which will be the storage for the tokens necessary for authentication.
|
257
|
-
|
258
|
-
The current Shopify user will be stored in the rails session at `session[:shopify_user]`
|
259
|
-
|
260
|
-
Read more about Online vs. Offline access [here](https://help.shopify.com/api/getting-started/authentication/oauth).
|
261
|
-
|
262
|
-
#### Migrating from shop-based to user-based token strategy
|
263
|
-
1. Run the `user_model` generator as mentioned above.
|
264
|
-
2. Ensure that both your `Shop` model and `User` model includes the necessary concerns `ShopifyApp::ShopSessionStorage` and `ShopifyApp::UserSessionStorage`.
|
265
|
-
3. Make changes to 2 initializer files as shown below:
|
266
|
-
```ruby
|
267
|
-
# In the `omniauth.rb` initializer:
|
268
|
-
provider :shopify,
|
269
|
-
...
|
270
|
-
setup: lambda { |env|
|
271
|
-
...
|
272
|
-
# Add this line
|
273
|
-
strategy.options[:per_user_permissions] = strategy.session[:user_tokens]
|
274
|
-
...
|
275
|
-
}
|
276
|
-
|
277
|
-
# In the `shopify_app.rb` initializer:
|
278
|
-
config.shop_session_repository = {YOUR_SHOP_MODEL_CLASS}
|
279
|
-
config.user_session_repository = {YOUR_USER_MODEL_CLASS}
|
280
|
-
```
|
281
|
-
|
282
|
-
### Authenticated
|
283
|
-
|
284
|
-
The engine provides a `ShopifyApp::Authenticated` concern which should be included in any controller that is intended to be behind Shopify OAuth. It adds `before_action`s to ensure that the user is authenticated and will redirect to the Shopify login page if not. It is best practice to include this concern in a base controller inheriting from your `ApplicationController`, from which all controllers that require Shopify authentication inherit.
|
285
|
-
|
286
|
-
For backwards compatibility, the engine still provides a controller called `ShopifyApp::AuthenticatedController` which includes the `ShopifyApp::Authenticated` concern. Note that it inherits directly from `ActionController::Base`, so you will not be able to share functionality between it and your application's `ApplicationController`.
|
287
|
-
|
288
|
-
### EnsureAuthenticatedLinks
|
289
|
-
|
290
|
-
The `ShopifyApp::EnsureAuthenticatedLinks` concern helps authenticate users that access protected pages of your app directly.
|
291
|
-
|
292
|
-
Include this concern in your app's `AuthenticatedController` if your app uses session tokens with [Turbolinks](https://shopify.dev/tutorials/authenticate-server-side-rendered-apps-with-session-tokens-app-bridge-turbolinks). It adds a `before_action` filter that detects whether a session token is present or not. If a session is not found, the user is redirected to your app's splash page path (`root_path`) along with `return_to` and `shop` parameters.
|
293
|
-
|
294
|
-
Example `AuthenticatedController`:
|
295
|
-
|
296
|
-
```rb
|
297
|
-
class AuthenticatedController < ApplicationController
|
298
|
-
include ShopifyApp::EnsureAuthenticatedLinks
|
299
|
-
include ShopifyApp::Authenticated
|
300
|
-
end
|
301
|
-
```
|
302
|
-
|
303
|
-
### AfterAuthenticate Job
|
304
|
-
|
305
|
-
If your app needs to perform specific actions after the user is authenticated successfully (i.e. every time a new session is created), ShopifyApp can queue or run a job of your choosing (note that we already provide support for automatically creating Webhooks and Scripttags). To configure the after authenticate job, update your initializer as follows:
|
306
|
-
|
307
|
-
```ruby
|
308
|
-
ShopifyApp.configure do |config|
|
309
|
-
config.after_authenticate_job = { job: "Shopify::AfterAuthenticateJob" }
|
310
|
-
end
|
311
|
-
```
|
312
|
-
|
313
|
-
The job can be configured as either a class or a class name string.
|
314
|
-
|
315
|
-
If you need the job to run synchronously add the `inline` flag:
|
316
|
-
|
317
|
-
```ruby
|
318
|
-
ShopifyApp.configure do |config|
|
319
|
-
config.after_authenticate_job = { job: Shopify::AfterAuthenticateJob, inline: true }
|
320
|
-
end
|
321
|
-
```
|
322
|
-
|
323
|
-
We've also provided a generator which creates a skeleton job and updates the initializer for you:
|
324
|
-
|
325
|
-
```
|
326
|
-
bin/rails g shopify_app:add_after_authenticate_job
|
327
|
-
```
|
328
|
-
|
329
|
-
If you want to perform that action only once, e.g. send a welcome email to the user when they install the app, you should make sure that this action is idempotent, meaning that it won't have an impact if run multiple times.
|
330
|
-
|
331
|
-
API Versioning
|
332
|
-
--------------
|
333
|
-
|
334
|
-
Shopify's API is versioned, and you can [read about that process in the Shopify Developers documentation page](https://shopify.dev/concepts/about-apis/versioning).
|
335
|
-
|
336
|
-
Since shopify_app gem version 1.11.0, the included shopify_api gem has also been updated to allow you to easily set and switch what version of the Shopify API you want your app or service to use, as well as surface warnings to Rails apps about [deprecated endpoints, GraphQL fields and more](https://shopify.dev/concepts/about-apis/versioning#deprecation-practices).
|
337
|
-
|
338
|
-
See the [shopify_api gem README](https://github.com/Shopify/shopify_api/) for more details.
|
339
|
-
|
340
|
-
WebhooksManager
|
341
|
-
---------------
|
342
|
-
|
343
|
-
ShopifyApp can manage your app's webhooks for you if you set which webhooks you require in the initializer:
|
344
|
-
|
345
|
-
```ruby
|
346
|
-
ShopifyApp.configure do |config|
|
347
|
-
config.webhooks = [
|
348
|
-
{topic: 'carts/update', address: 'https://example-app.com/webhooks/carts_update'}
|
349
|
-
]
|
350
|
-
end
|
351
|
-
```
|
352
|
-
|
353
|
-
When the OAuth callback is completed successfully, ShopifyApp will queue a background job which will ensure all the specified webhooks exist for that shop. Because this runs on every OAuth callback, it means your app will always have the webhooks it needs even if the user uninstalls and re-installs the app.
|
354
|
-
|
355
|
-
ShopifyApp also provides a WebhooksController that receives webhooks and queues a job based on the received topic. For example, if you register the webhook from above, then all you need to do is create a job called `CartsUpdateJob`. The job will be queued with 2 params: `shop_domain` and `webhook` (which is the webhook body).
|
356
|
-
|
357
|
-
If you would like to namespace your jobs, you may set `webhook_jobs_namespace` in the config. For example, if your app handles webhooks from other ecommerce applications as well, and you want Shopify cart update webhooks to be processed by a job living in `jobs/shopify/webhooks/carts_update_job.rb` rather than `jobs/carts_update_job.rb`):
|
358
|
-
|
359
|
-
```ruby
|
360
|
-
ShopifyApp.configure do |config|
|
361
|
-
config.webhook_jobs_namespace = 'shopify/webhooks'
|
362
|
-
end
|
363
|
-
```
|
364
|
-
|
365
|
-
If you are only interested in particular fields, you can optionally filter the data sent by Shopify by specifying the `fields` parameter in `config/webhooks`. Note that you will still receive a webhook request from Shopify every time the resource is updated, but only the specified fields will be sent.
|
366
|
-
|
367
|
-
```ruby
|
368
|
-
ShopifyApp.configure do |config|
|
369
|
-
config.webhooks = [
|
370
|
-
{topic: 'products/update', address: 'https://example-app.com/webhooks/products_update', fields: ['title', 'vendor']}
|
371
|
-
]
|
372
|
-
end
|
373
|
-
```
|
374
|
-
|
375
|
-
If you'd rather implement your own controller then you'll want to use the WebhookVerification module to verify your webhooks, example:
|
376
|
-
|
377
|
-
```ruby
|
378
|
-
class CustomWebhooksController < ApplicationController
|
379
|
-
include ShopifyApp::WebhookVerification
|
380
|
-
|
381
|
-
def carts_update
|
382
|
-
params.permit!
|
383
|
-
SomeJob.perform_later(shop_domain: shop_domain, webhook: webhook_params.to_h)
|
384
|
-
head :no_content
|
385
|
-
end
|
386
|
-
|
387
|
-
private
|
388
|
-
|
389
|
-
def webhook_params
|
390
|
-
params.except(:controller, :action, :type)
|
391
|
-
end
|
392
|
-
end
|
393
|
-
```
|
394
|
-
|
395
|
-
The module skips the `verify_authenticity_token` before_action and adds an action to verify that the webhook came from Shopify. You can now add a post route to your application, pointing to the controller and action to accept the webhook data from Shopify.
|
396
|
-
|
397
|
-
The WebhooksManager uses ActiveJob. If ActiveJob is not configured then by default Rails will run the jobs inline. However, it is highly recommended to configure a proper background processing queue like Sidekiq or Resque in production.
|
398
|
-
|
399
|
-
ShopifyApp can create webhooks for you using the `add_webhook` generator. This will add the new webhook to your config and create the required job class for you.
|
400
|
-
|
401
|
-
```
|
402
|
-
rails g shopify_app:add_webhook -t carts/update -a https://example.com/webhooks/carts_update
|
403
|
-
```
|
404
|
-
|
405
|
-
Where `-t` is the topic and `-a` is the address the webhook should be sent to.
|
406
|
-
|
407
|
-
ScripttagsManager
|
408
|
-
-----------------
|
409
|
-
|
410
|
-
As with webhooks, ShopifyApp can manage your app's scripttags for you by setting which scripttags you require in the initializer:
|
411
|
-
|
412
|
-
```ruby
|
413
|
-
ShopifyApp.configure do |config|
|
414
|
-
config.scripttags = [
|
415
|
-
{event:'onload', src: 'https://my-shopifyapp.herokuapp.com/fancy.js'},
|
416
|
-
{event:'onload', src: ->(domain) { dynamic_tag_url(domain) } }
|
417
|
-
]
|
418
|
-
end
|
419
|
-
```
|
420
|
-
|
421
|
-
You also need to have write_script_tags permission in the config scope in order to add script tags automatically:
|
422
|
-
|
423
|
-
```ruby
|
424
|
-
config.scope = '... , write_script_tags'
|
425
|
-
```
|
426
|
-
|
427
|
-
Scripttags are created in the same way as the Webhooks, with a background job which will create the required scripttags.
|
428
|
-
|
429
|
-
If `src` responds to `call` its return value will be used as the scripttag's source. It will be called on scripttag creation and deletion.
|
430
|
-
|
431
|
-
RotateShopifyTokenJob
|
432
|
-
---------------------
|
433
|
-
|
434
|
-
If your Shopify secret key is leaked, you can use the RotateShopifyTokenJob to perform [API Credential Rotation](https://help.shopify.com/en/api/getting-started/authentication/oauth/api-credential-rotation).
|
435
|
-
|
436
|
-
Before running the job, you'll need to generate a new secret key from your Shopify Partner dashboard, and update the `/config/initializers/shopify_app.rb` to hold your new and old secret keys:
|
437
|
-
|
438
|
-
```ruby
|
439
|
-
config.secret = Rails.application.secrets.shopify_secret
|
440
|
-
config.old_secret = Rails.application.secrets.old_shopify_secret
|
62
|
+
$ rails generate shopify_app
|
441
63
|
```
|
442
64
|
|
443
|
-
|
65
|
+
5. Run a migration to create the necessary tables in your database:
|
444
66
|
|
445
67
|
```sh
|
446
|
-
|
68
|
+
$ rails db:migrate
|
447
69
|
```
|
448
70
|
|
449
|
-
|
450
|
-
|
451
|
-
⚠️ Note: if you are updating `shopify_app` from a version prior to 8.4.2 (and do not wish to run the default/install generator again), you will need to add [the following line](https://github.com/Shopify/shopify_app/blob/4f7e6cca2a472d8f7af44b938bd0fcafe4d8e88a/lib/generators/shopify_app/install/templates/shopify_provider.rb#L18) to `config/initializers/omniauth.rb`:
|
452
|
-
|
453
|
-
```ruby
|
454
|
-
strategy.options[:old_client_secret] = ShopifyApp.configuration.old_secret
|
455
|
-
```
|
456
|
-
|
457
|
-
App Tunneling
|
458
|
-
-------------
|
459
|
-
|
460
|
-
Your local app needs to be accessible from the public Internet in order to install it on a Shopify store, to use the [App Proxy Controller](#app-proxy-controller-generator) or receive Webhooks.
|
461
|
-
|
462
|
-
Use a tunneling service like [ngrok](https://ngrok.com/), [Forward](https://forwardhq.com/), [Beeceptor](https://beeceptor.com/), [Mockbin](http://mockbin.org/), or [Hookbin](https://hookbin.com/) to make your development environment accessible to the internet.
|
463
|
-
|
464
|
-
For example with [ngrok](https://ngrok.com/), run this command to set up a tunnel proxy to Rails' default port:
|
71
|
+
6. Run the app:
|
465
72
|
|
466
73
|
```sh
|
467
|
-
|
468
|
-
```
|
469
|
-
|
470
|
-
AppProxyVerification
|
471
|
-
--------------------
|
472
|
-
|
473
|
-
The engine provides a mixin for verifying incoming HTTP requests sent via an App Proxy. Any controller that `include`s `ShopifyApp::AppProxyVerification` will verify that each request has a valid `signature` query parameter that is calculated using the other query parameters and the app's shared secret.
|
474
|
-
|
475
|
-
### Recommended Usage
|
476
|
-
|
477
|
-
The App Proxy Controller Generator automatically adds the mixin to the generated app_proxy_controller.rb
|
478
|
-
Additional controllers for resources within the App_Proxy namespace, will need to include the mixin like so:
|
479
|
-
|
480
|
-
```ruby
|
481
|
-
# app/controllers/app_proxy/reviews_controller.rb
|
482
|
-
class ReviewsController < ApplicationController
|
483
|
-
include ShopifyApp::AppProxyVerification
|
484
|
-
# ...
|
485
|
-
end
|
486
|
-
```
|
487
|
-
|
488
|
-
Create your app proxy URL in the [Shopify Partners' Dashboard][dashboard], making sure to point it to `https://your_app_website.com/app_proxy`.
|
489
|
-
![Creating an App Proxy](/images/app-proxy-screenshot.png)
|
490
|
-
|
491
|
-
App Bridge
|
492
|
-
---
|
493
|
-
|
494
|
-
A basic example of using [App Bridge][app-bridge] is included in the install generator. An app instance is automatically initialized in [shopify_app.js](https://github.com/Shopify/shopify_app/blob/master/lib/generators/shopify_app/install/templates/shopify_app.js) and [flash_messages.js](https://github.com/Shopify/shopify_app/blob/master/lib/generators/shopify_app/install/templates/flash_messages.js) converts Rails [flash messages](https://api.rubyonrails.org/classes/ActionDispatch/Flash.html) to App Bridge Toast actions automatically. By default, this library is included via [unpkg in the embedded_app layout](https://github.com/Shopify/shopify_app/blob/master/lib/generators/shopify_app/install/templates/embedded_app.html.erb#L27). For more advanced uses it is recommended to [install App Bridge via npm or yarn](https://help.shopify.com/en/api/embedded-apps/app-bridge/getting-started#set-up-shopify-app-bridge-in-your-app).
|
495
|
-
|
496
|
-
Troubleshooting
|
497
|
-
---------------
|
498
|
-
|
499
|
-
see [TROUBLESHOOTING.md](https://github.com/Shopify/shopify_app/blob/master/docs/Troubleshooting.md)
|
500
|
-
|
501
|
-
Using Test Helpers inside your Application
|
502
|
-
-----------------------------------------
|
503
|
-
|
504
|
-
A test helper that will allow you to test `ShopifyApp::WebhookVerification` in the controller from your app, to use this test, you need to `require` it directly inside your app `test/controllers/webhook_verification_test.rb`.
|
505
|
-
|
506
|
-
```ruby
|
507
|
-
require 'test_helper'
|
508
|
-
require 'action_controller'
|
509
|
-
require 'action_controller/base'
|
510
|
-
require 'shopify_app/test_helpers/webhook_verification_helper'
|
511
|
-
```
|
512
|
-
|
513
|
-
Or you can require in your `test/test_helper.rb`.
|
514
|
-
|
515
|
-
```ruby
|
516
|
-
ENV['RAILS_ENV'] ||= 'test'
|
517
|
-
require_relative '../config/environment'
|
518
|
-
require 'rails/test_help'
|
519
|
-
require 'byebug'
|
520
|
-
require 'shopify_app/test_helpers/all'
|
521
|
-
```
|
522
|
-
|
523
|
-
With `lib/shopify_app/test_helpers/all'` more tests can be added and will only need to be required in once in your library.
|
524
|
-
|
525
|
-
|
526
|
-
Testing an embedded app outside the Shopify admin
|
527
|
-
-------------------------------------------------
|
528
|
-
|
529
|
-
By default, loading your embedded app will redirect to the Shopify admin, with the app view loaded in an `iframe`. If you need to load your app outside of the Shopify admin (e.g., for performance testing), you can change `forceRedirect: true` to `false` in `ShopifyApp.init` block in the `embedded_app` view. To keep the redirect on in production but off in your `development` and `test` environments, you can use:
|
530
|
-
|
531
|
-
```javascript
|
532
|
-
forceRedirect: <%= Rails.env.development? || Rails.env.test? ? 'false' : 'true' %>
|
74
|
+
$ rails server
|
533
75
|
```
|
534
76
|
|
535
|
-
|
536
|
-
-------------------
|
537
|
-
|
538
|
-
Version 13.0.0 adds the ability to use both user and shop sessions, concurrently. This however involved a large
|
539
|
-
change to how session stores work. Here are the steps to migrate to 13.x
|
540
|
-
|
541
|
-
### Changes to `config/initializers/shopify_app.rb`
|
542
|
-
- *REMOVE* `config.per_user_tokens = [true|false]` this is no longer needed
|
543
|
-
- *CHANGE* `config.session_repository = 'Shop'` To `config.shop_session_repository = 'Shop'`
|
544
|
-
- *ADD (optional)* User Session Storage `config.user_session_repository = 'User'`
|
545
|
-
|
546
|
-
### Shop Model Changes (normally `app/models/shop.rb`)
|
547
|
-
- *CHANGE* `include ShopifyApp::SessionStorage` to `include ShopifyApp::ShopSessionStorage`
|
548
|
-
|
549
|
-
### Changes to the @shop_session instance variable (normally in `app/controllers/*.rb`)
|
550
|
-
- *CHANGE* if you are using shop sessions, `@shop_session` will need to be changed to `@current_shopify_session`.
|
551
|
-
|
552
|
-
### Changes to Rails `session`
|
553
|
-
- *CHANGE* `session[:shopify]` is no longer set. Use `session[:user_id]` if your app uses user based tokens, or `session[:shop_id]` if your app uses shop based tokens.
|
554
|
-
|
555
|
-
### Changes to `ShopifyApp::LoginProtection`
|
556
|
-
`ShopifyApp::LoginProtection`
|
557
|
-
|
558
|
-
- CHANGE if you are using `ShopifyApp::LoginProtection#shopify_session` in your code, it will need to be
|
559
|
-
changed to `ShopifyApp::LoginProtection#activate_shopify_session`
|
560
|
-
- CHANGE if you are using `ShopifyApp::LoginProtection#clear_shop_session` in your code, it will need to be
|
561
|
-
changed to `ShopifyApp::LoginProtection#clear_shopify_session`
|
77
|
+
See [*Quickstart*](/docs/Quickstart.md) to learn how to install your app on a shop.
|
562
78
|
|
563
|
-
|
564
|
-
You do not need a user model; a shop session is fine for most applications.
|
79
|
+
This app implements [OAuth 2.0](https://shopify.dev/tutorials/authenticate-with-oauth) with Shopify to authenticate requests made to Shopify APIs. By default, this app is configured to use [session tokens](https://shopify.dev/concepts/apps/building-embedded-apps-using-session-tokens) to authenticate merchants when embedded in the Shopify Admin.
|
565
80
|
|
566
|
-
|
567
|
-
----------------------
|
81
|
+
See [*Generators*](/docs/shopify_app/generators.md) for a complete list of generators available to Shopify App.
|
568
82
|
|
569
|
-
|
570
|
-
- [Read the docs!](https://help.shopify.com/api/guides)
|
571
|
-
- And don't forget to check the [Changelog](https://github.com/Shopify/shopify_app/blob/master/CHANGELOG.md) too!
|
83
|
+
## Documentation
|
572
84
|
|
573
|
-
|
574
|
-
---------------------------
|
85
|
+
You can find documentation on gem usage, concepts, mixins, installation, and more in [`/docs`](/docs).
|
575
86
|
|
576
|
-
|
577
|
-
|
87
|
+
* Start with the [*Generators*](/docs/shopify_app/generators.md) document to learn more about the generators this gem offers.
|
88
|
+
* Check out the [*Changelog*](/CHANGELOG.md) for notes on the latest gem releases.
|
89
|
+
* See [*Troubleshooting*](/docs/Troubleshooting.md) for tips on common issues.
|
90
|
+
* If you are looking to upgrade your Shopify App version to a new major release, see [*Upgrading*](/docs/Upgrading.md) for important notes on breaking changes.
|
578
91
|
|
579
|
-
|
580
|
-
---------------------
|
92
|
+
### Overview
|
581
93
|
|
582
|
-
|
583
|
-
If you are using sprockets in rails 6 or want to generate a shopify_app without webpacker run the install task by running
|
94
|
+
[Quickstart](/docs/Quickstart.md)
|
584
95
|
|
585
|
-
|
586
|
-
SHOPIFY_APP_DISABLE_WEBPACKER=1 rails generate shopify_app
|
587
|
-
```
|
588
|
-
|
589
|
-
and then in your ShopifyApp configuration block, add
|
590
|
-
|
591
|
-
```
|
592
|
-
ShopifyApp.configure do |config|
|
593
|
-
config.disable_webpacker = true
|
594
|
-
end
|
595
|
-
```
|
96
|
+
[Troubleshooting](/docs/Troubleshooting.md)
|
596
97
|
|
597
|
-
Upgrading
|
598
|
-
---------------------------
|
98
|
+
[Upgrading](/docs/Upgrading.md)
|
599
99
|
|
600
|
-
|
100
|
+
[Shopify App](/docs/shopify_app)
|
101
|
+
* [Authentication](/docs/shopify_app/authentication.md)
|
102
|
+
* [Engine](/docs/shopify_app/engine.md)
|
103
|
+
* [Generators](/docs/shopify_app/generators.md)
|
104
|
+
* [ScriptTags](/docs/shopify_app/script-tags.md)
|
105
|
+
* [Session repository](/docs/shopify_app/session-repository.md)
|
106
|
+
* [Handling changes in access scopes](/docs/shopify_app/handling-access-scopes-changes.md)
|
107
|
+
* [Testing](/docs/shopify_app/testing.md)
|
108
|
+
* [Webhooks](/docs/shopify_app/webhooks.md)
|
601
109
|
|
602
|
-
|
603
|
-
Set this to the version you want to run against by default. See [Shopify API docs](https://help.shopify.com/en/api/versioning) for versions available.
|
604
|
-
```ruby
|
605
|
-
config.api_version = '2019-04'
|
606
|
-
```
|
607
|
-
|
608
|
-
### Session storage change
|
609
|
-
|
610
|
-
You will need to add an `api_version` method to your session storage object. The default implementation for this is.
|
611
|
-
```ruby
|
612
|
-
def api_version
|
613
|
-
ShopifyApp.configuration.api_version
|
614
|
-
end
|
615
|
-
```
|
110
|
+
### Engine
|
616
111
|
|
617
|
-
|
112
|
+
Mounting the Shopify App Rails Engine provides the following routes. These routes are configured to help install your application on shops and implement OAuth.
|
618
113
|
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
114
|
+
| Verb | Route | Action |
|
115
|
+
| ---: | :--- | :--- |
|
116
|
+
| `GET` | `/login` | Login |
|
117
|
+
| `POST` | `/login` | Login |
|
118
|
+
| `GET` | `/auth/shopify/callback` | OAuth redirect URI |
|
119
|
+
| `GET` | `/logout` | Logout |
|
120
|
+
| `POST` | `/webhooks/:type` | Webhook callback |
|
624
121
|
|
625
|
-
|
122
|
+
These routes are configurable. See the more detailed [*Engine*](/docs/shopify_app/engine.md) documentation to learn how you can customize the login URL or mount the Shopify App Rails engine at nested routes.
|
626
123
|
|
627
|
-
|
628
|
-
forceRedirect: true
|
629
|
-
});
|
630
|
-
</script>
|
631
|
-
```
|
632
|
-
is changed to
|
633
|
-
```erb
|
634
|
-
<script type="text/javascript">
|
635
|
-
ShopifyApp.init({
|
636
|
-
apiKey: "<%= ShopifyApp.configuration.api_key %>",
|
637
|
-
|
638
|
-
shopOrigin: "<%= "https://#{ @shop_session.domain }" if @shop_session %>",
|
639
|
-
|
640
|
-
debug: false,
|
641
|
-
forceRedirect: true
|
642
|
-
});
|
643
|
-
</script>
|
644
|
-
```
|
124
|
+
To learn more about how this gem authenticates with Shopify, see [*Authentication*](/docs/shopify_app/authentication.md).
|
645
125
|
|
646
|
-
###
|
126
|
+
### API Versioning
|
647
127
|
|
648
|
-
|
128
|
+
[Shopify's API is versioned](https://shopify.dev/concepts/about-apis/versioning). With Shopify App `v1.11.0`, the included Shopify API gem allows developers to specify and update the Shopify API version they want their app or service to use. The Shopify API gem also surfaces warnings to Rails apps about [deprecated endpoints, GraphQL fields and more](https://shopify.dev/concepts/about-apis/versioning#deprecation-practices).
|
649
129
|
|
650
|
-
[
|
651
|
-
[app-bridge]:https://help.shopify.com/en/api/embedded-apps/app-bridge
|
130
|
+
See the [Shopify API gem README](https://github.com/Shopify/shopify_api/) for more information.
|