Skip to content

Instantly share code, notes, and snippets.

@victorgiraldes
Last active November 22, 2019 18:32
Show Gist options
  • Save victorgiraldes/2d08a4d371e4ff08b0094237c9db4ff8 to your computer and use it in GitHub Desktop.
Save victorgiraldes/2d08a4d371e4ff08b0094237c9db4ff8 to your computer and use it in GitHub Desktop.
class NfeioController < InheritedResources::Base
before_action :authenticate_user!, :except => [:webhook]
before_filter :resource
skip_before_action :verify_authenticity_token
def webhook
event = JSON.parse(request.body.read)
case event['action']
when 'issued_successfully'
invoice_issue_success
when 'issued_failed'
invoice_issued_failed
when 'issued_error'
invoice_issued_error
when 'cancelled_successfully'
invoice_cancelled_successfully
when 'cancelled_failed'
invoice_cancelled_failed
when 'cancelled_error'
invoice_cancelled_error
end
rescue Exception => ex
render :json => {:status => 400, :error => "Webhook failed"} and return
end
render :json => {:status => 200}
end
def invoice_issue_success
@nfe_production_sale.update_attribute(:status, 'Autorizada')
end
def invoice_issued_failed
@nfe_production_sale.update_attribute(:status, 'Falha ao Emitir')
end
def invoice_issued_error
@nfe_production_sale.update_attribute(:status, 'Erro ao Emitir')
end
def invoice_cancelled_successfully
@nfe_production_sale.update_attribute(:status, 'Cancelada')
end
def invoice_cancelled_failed
@nfe_production_sale.update_attribute(:status, 'Erro ao Cancelar')
end
def invoice_cancelled_error
@nfe_production_sale.update_attribute(:status, 'Erro ao Cancelar')
end
private
def resource
@nfe_production_sale = NfeProductionSale.find_by(params[:nfeio_product_invoice_id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment