Webhook orders doesnt trigger in development mode

Hi, I’m using shopify_app gem, and the webhook related with orders doesnt trigger anything in a development store. I create and update orders directly in the store also using the API in my console without see any request.

The only that works are: app/uninstalled and products/update

my configuration:

ShopifyApp.configure do |config|
  config.webhooks = [
    { topic: "app/uninstalled", path: "webhooks/uninstalled" },
    { topic: "orders/create", path: "webhooks/orders_create" },
    { topic: "orders/paid", path: "webhooks/orders_paid" },
    { topic: "orders/fulfilled", path: "webhooks/orders_fulfilled" },
    { topic: "products/update", path: "webhooks/products_update" }
  ]
  config.application_name = "rave"
  config.old_secret = ""
  config.scope = "read_products, read_orders, write_orders" # Consult this page for more scope options:
                                  # https://help.shopify.com/en/api/getting-started/authentication/oauth/scopes
  config.embedded_app = true
  config.after_authenticate_job = false
  config.api_version = "2022-10"
  config.shop_session_repository = 'Shop'

  config.reauth_on_access_scope_changes = true

  config.api_key = ENV.fetch('SHOPIFY_API_KEY', '').presence
  config.secret = ENV.fetch('SHOPIFY_API_SECRET', '').presence

  if defined? Rails::Server
    raise('Missing SHOPIFY_API_KEY. See https://github.com/Shopify/shopify_app#requirements') unless config.api_key
    raise('Missing SHOPIFY_API_SECRET. See https://github.com/Shopify/shopify_app#requirements') unless config.secret
  end
end

Rails.application.config.after_initialize do
  if ShopifyApp.configuration.api_key.present? && ShopifyApp.configuration.secret.present?
    ShopifyAPI::Context.setup(
      api_key: ShopifyApp.configuration.api_key,
      api_secret_key: ShopifyApp.configuration.secret,
      api_version: ShopifyApp.configuration.api_version,
      host: ENV['HOST'],
      scope: ShopifyApp.configuration.scope,
      is_private: !ENV.fetch('SHOPIFY_APP_PRIVATE_SHOP', '').empty?,
      is_embedded: ShopifyApp.configuration.embedded_app,
      session_storage: ShopifyApp::SessionRepository,
      logger: Rails.logger,
      private_shop: ENV.fetch('SHOPIFY_APP_PRIVATE_SHOP', nil),
      user_agent_prefix: "ShopifyApp/#{ShopifyApp::VERSION}"
    )

    ShopifyApp::WebhooksManager.add_registrations
  end
end

This is my code for creating an order

order = ShopifyAPI::Order.new(session: session)
order.email = "foo1@example.com"
order.fulfillment_status = "fulfilled"
order.send_receipt = true
order.send_fulfillment_receipt = true
order.line_items = [
  {
    "variant_id" => 44100045144346,
    "quantity" => 1
  }
]
order.save!

Is there something that a missing? or what is the correct way to tests the orders webhooks?

1 Like

If you are using ngrok in free mode, your API endpoint changes in every debug mode initialization. You should check Shopify webhooks list and validate the retrieved URLs are the same as your current ngrok URL.

If this is the issue there are some things you may consider:

  • You can refresh your webhook endpoints periodically. You may not want to use it in production.

  • You can buy ngrok and register a static URL for your local environment. It is 20$ per month, and I didn’t try the pro version with Shopify CLI.

  • You can use ngrok alternatives (for example localtunnel with subdomain parameter [-s]) that enable static URLs for free. Shopify CLI doesn’t natively support it.

Hope this helps

1 Like

I just check that you commented, but I think my tunnel is not the problem.

I uninstalled and installed the app again in my test store.

And request to the webhooks endpoint I get only two webhooks registered, nothing for orders:

shop = Shop.last
session = ShopifyAPI::Auth::Session.new(shop: shop.shopify_domain, access_token: shop.shopify_token)

hooks = ShopifyAPI::Webhook.all(
  session: session
)

hooks.each { |h| puts "topic: #{h.topic} address: #{h.address}" }

topic: app/uninstalled address: https://big-parts-smoke-187-198-66-222.loca.lt/webhooks/uninstalled
topic: products/update address: https://big-parts-smoke-187-198-66-222.loca.lt/webhooks/products_update

The topic orders/create seems right according to the documentation:

https://shopify.dev/api/admin-rest/2022-04/resources/webhook#resource-description

And following the instructions for creating webhooks:

https://github.com/Shopify/shopify_app/blob/main/docs/shopify_app/webhooks.md

Any another suggestion?

Did you try to remove read_orders scope? write_orders permission contains read_orders scope and including both of the scopes may break your order-related webhook registrations.

I think I found the error that causes that I cant register that. I tried to register a webhook with the API:

webhook = ShopifyAPI::Webhook.new(session: session)
webhook.topic = "orders/create"
webhook.address = "https://big-parts-smoke-187-198-66-222.loca.lt/webhooks/orders_create"
webhook.format = "json"
webhook.save!

ShopifyAPI::Errors::HttpResponseError: You do not have permission to create webhooks with orders/create topic. This topic contains protected customer data. See https://shopify.dev/apps/store/data-protection for more details.

So, I have to request a new permission to shopify in order to use that webhooks.

More info:

https://shopify.dev/apps/store/data-protection/protected-customer-data

2 Likes