Tuesday, 10 December 2019

Shopify App (with shopify gem in rails)

Shopify App 

https://github.com/Shopify/shopify_app

1- # Create a new rails app 
        $ rails new my_shopify_app 
        $ cd my_shopify_app
 

2- # Add the gem shopify_app to your Gemfile  

          "gem 'shopify_app'"
           bundle install
         
Now we are ready to run any of the shopify_app generators. The following section explains the generators and what they can do.

3- Default Generator

The default generator will run the install, shop, and home_controller generators. This is the recommended way to start your app.
     
        $ rails generate shopify_app
 
 After run this cmd generated file list is here.

 generate  shopify_app:install
Running via Spring preloader in process 7234
     gemfile  dotenv-rails
      create  config/initializers/shopify_app.rb
      create  config/initializers/session_store.rb
      create  config/initializers/omniauth.rb
      insert  config/initializers/omniauth.rb
      create  app/views/layouts/embedded_app.html.erb
      create  app/views/layouts/_flash_messages.html.erb
      create  app/assets/javascripts/shopify_app.js
      create  app/assets/javascripts/flash_messages.js
      create  config/initializers/user_agent.rb
       route  mount ShopifyApp::Engine, at: '/'
      insert  config/environments/development.rb
    generate  shopify_app:shop_model
Running via Spring preloader in process 7254
      create  app/models/shop.rb
      create  db/migrate/20191210084729_create_shops.rb
        gsub  config/initializers/shopify_app.rb
      create  test/fixtures/shops.yml
    generate  shopify_app:authenticated_controller
Running via Spring preloader in process 7277
      create  app/controllers/authenticated_controller.rb
    generate  shopify_app:home_controller
Running via Spring preloader in process 7281
      create  app/controllers/home_controller.rb
      create  app/views/home/index.html.erb
     prepend  app/views/home/index.html.erb
       route  root :to => 'home#index'

4-  After running the generator, you will need to run rake db:migrate to add 
     tables to your database. You can start your app with bundle exec rails server 
     and install your app by visiting localhost.

5- The default and install generators have been updated to source Shopify API 
    key and secret from a .env file, which you will need to create with the 
    following format:
       SHOPIFY_API_KEY=your api key
       SHOPIFY_API_SECRET=your api secret
  
These values can be found on the "App Setup" page in the Shopify Partners Dashboard.(when you create a account on shopify and after create a app then u get api key and api secret )

6- Install Generator
        rails generate shopify_app:install

After running the install generator, you can start your app with bundle exec rails server and install your app by visiting localhost.

7- Shop Model Generator
       rails generate shopify_app:shop_model

This generator creates a shop model and a migration. This model includes the ShopifyApp::SessionStorage concern which adds two methods to make it compatible as a SessionRepository. After running this generator you'll notice the session_repository in your config/initializers/shopify_app.rb will be set to the Shop model. This means that internally ShopifyApp will try and load tokens from this model.
           identical  app/models/shop.rb
           identical  db/migrate/20191210084729_create_shops.rb
           gsub  config/initializers/shopify_app.rb
           identical  test/fixtures/shops.yml



Note that you will need to run rake db:migrate after this generator
 




8- Home Controller Generator
         rails generate shopify_app:home_controller

This generator creates an example home controller and view which fetches and displays products using the ShopifyAPI

         identical  app/controllers/home_controller.rb
         force  app/views/home/index.html.erb
         prepend  app/views/home/index.html.erb
         route  root :to => 'home#index' 


9- App Proxy Controller Generator
          rails generate shopify_app:app_proxy_controller

 This optional generator, not included with the default generator, creates the app proxy controller to handle proxy requests to the app from your shop storefront, modifies 'config/routes.rb' with a namespace route, and an example view which displays current shop information using the LiquidAPI.

           create  app/controllers/app_proxy_controller.rb
           create  app/views/app_proxy/index.html.erb
           insert  config/routes.rb

 
 10- Controllers, Routes and Views

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.

after run this cmd create files:
      create  app/controllers/shopify_app/authenticated_controller.rb
      create  app/controllers/shopify_app/callback_controller.rb
      create  app/controllers/shopify_app/sessions_controller.rb
      create  app/controllers/shopify_app/webhooks_controller.rb

  
 11- Routes
           mount ShopifyApp::Engine, at: '/'


     * update the omniauth initializer to include a custom callback_path
 

          provider :shopify,
             ShopifyApp.configuration.api_key, 
             ShopifyApp.configuration.secret, 
             scope: ShopifyApp.configuration.scope, 
             callback_path: '/auth/shopify/callback'




  
   
Finally, note that if you do this, to add your app to a store, you must navigate to / in order to render the Enter your shop domain to log in or install this app.

          UI  (example.myshopify.com) when run localhost:3000/login

12- Custom login URL

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

           ShopifyApp.configure do |config|
              config.login_url = 'https://my.domain.com/nested/login'
           end
 



















13- Per User Authentication 

To enable per user authentication you need to update  the omniauth.rb  initializer:

        provider :shopify,
          ShopifyApp.configuration.api_key,
          ShopifyApp.configuration.secret,
          scope: ShopifyApp.configuration.scope,
          per_user_permissions: true
 
 
The current Shopify user will be stored in the rails session at session[:shopify_user]

This will change the type of token that Shopify returns and it will only be valid for a short time. Read more about Online access here. Note that this means you won't be able to use this token to respond to Webhooks.

14- WebhooksManager

rails g shopify_app:add_webhook -t carts/update -a http://localhost:3000/webhooks/carts_update

invoke  test_unit
      create    test/jobs/carts_update_job_test.rb
      insert  config/initializers/shopify_app.rb
      insert  config/initializers/shopify_app.rb
      create  app/jobs/carts_update_job.rb


ShopifyApp can manage your app's webhooks for you if you set which webhooks you require in the initializer:

 ShopifyApp.configure do |config|
     config.webhooks = [
          {topic: 'carts/update', address: 'http://localhost:3000/webhooks 

          /carts_update', format: 'json'},
     ]

 end

15- AfterAuthenticate Job

We've also provided a generator which creates a skeleton job and updates the initializer for you:
       bin/rails g shopify_app:add_after_authenticate_job

invoke  test_unit
      create    test/jobs/shopify/after_authenticate_job_test.rb
      insert  config/initializers/shopify_app.rb
Error adding after_authenticate_job to config. Add this line manually:   config.after_authenticate_job = { job: "Shopify::AfterAuthenticateJob", inline: false }
      create  app/jobs/shopify/after_authenticate_job.rb

 

 To configure the after authenticate job update your initializer as follows:

         ShopifyApp.configure do |config|
              config.after_authenticate_job = { job:"Shopify::AfterAuthenticateJob", 
               inline: false }
         end










 

  Common Git Commands   Take clone of a repository git clone <remote-repository-url> ex: git clone  https://github.com/agricor/RegTech...