class ApiRecurlyWebHookController < ApiController

  http_basic_authenticate_with name: Rails.application.config.recurly_webhook_user, password: Rails.application.config.recurly_webhook_pass

  before_filter :api_signed_in_user, only: []
  #respond_to :xml


  def on_hook
    begin

      document = Nokogiri::XML(request.body)

      if RecurlyTransactionWebHook.is_transaction_web_hook?(document)
        transaction = RecurlyTransactionWebHook.create_from_xml(document)
      end

    rescue Exception => e
      Stats.write('web.recurly.webhook.transaction.error', {message: e.to_s, value: 1})

      log.error("unable to process webhook: #{e.to_s}")

      raise JamArgumentError.new("unable to parse webhook #{e.to_s}")
    end

    Stats.write('web.recurly.webhook.transaction.success', {value: 1})

    render xml: { success: true }, :status => 200
  end
end
