Home > Mobile >  how to refactor receive webhooks in controller in rails
how to refactor receive webhooks in controller in rails

Time:06-01

I'm developing a controller to receive a webhooks I wanted to understand how I can refactor this create method in the controller. the parameters are almost always repeated. can i create a private moto maybe?

module Webhooks
  module Payments
    class ConfirmationsController < Webhooks::BaseController
      def create
        confirmation_payment = {
          customer_code: params[:event][:data][:bill][:customer][:code],
          customer_name: params[:event][:data][:bill][:customer][:name],
          customer_email: params[:event][:data][:bill][:customer][:email],
          payment_company_id: params[:event][:data][:bill][:payment_profile [:payment_company][:id],
          payment_company_name: params[:event][:data][:bill][:payment_profile][:payment_company][:name],
          payment_profile_card_number_last_four: params[:event][:data][:bill][:payment_profile][:card_number_last_four],
          payment_time: params[:event][:data][:bill][:updated_at]
        }
      end
    end
  end
end

CodePudding user response:

I would started with:

module Webhooks
  module Payments
    class ConfirmationsController < Webhooks::BaseController
      def create
        bill     = params.dig(:event, :data, :bill)
        payment  = bill.fetch(:payment_profile)
        customer = bill.fetch(:customer)
        company  = payment.fetch(:payment_company)

        confirmation_payment = {
          customer_code: customer[:code],
          customer_name: customer[:name],
          customer_email: customer[:email],
          payment_company_id: company[:id],
          payment_company_name: company[:name],
          payment_profile_card_number_last_four: payment[:card_number_last_four],
          payment_time: bill[:updated_at]
        }
      end
    end
  end
end
  • Related