diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index 3cab7b1..caf69a2 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -1,5 +1,5 @@ class WebhooksController < ApplicationController - skip_before_action :verify_authenticity_token, only: [:product_updated] + skip_before_action :verify_authenticity_token, only: %i[product_updated order_cancelled] def product_updated data = JSON.parse(request.body.read) @@ -18,6 +18,19 @@ def product_updated end end + def order_cancelled + data = JSON.parse(request.body.read) + order_id = data['id'] + @order = Order.find_by(shopify_id: order_id) + + if @order.present? + CanceledOrder.create(order: @order) + redirect_to orders_path + else + redirect_to @order, alert: 'Error while canceling the order' + end + end + private def assign_product_attributes(data) diff --git a/config/routes.rb b/config/routes.rb index fee72a2..b38e102 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ get '/auth/callback', to: 'shopify_auth#callback' get '/import_shopify_data', to: 'shopify_import#import' post '/webhooks/shopify/product_updated', to: 'webhooks#product_updated' + post '/webhooks/shopify/order_cancelled', to: 'webhooks#order_cancelled' resources :orders do member do post :cancel