require 'mercadopago' require 'app_configuration' class MercadoPagoHelper APPROVED = :approved PENDING = :pending IN_PROCESS = :in_process REJECTED = :rejected REFUNDED = :refunded CANCELLED = :cancelled IN_MEDIATION = :in_mediation class << self @@mp = MercadoPago.new(AppConfiguration.for(:mercadopago).client_id, AppConfiguration.for(:mercadopago).client_secret) def mp @@mp end def set_handlers(status_handlers = {}) validate_presence(status_handlers) @contexts = status_handlers.dup end def get_context(response, obj) status = response['response']['collection']['status'].to_sym @contexts[status].new(response, obj) end private def validate_presence(status_handlers) if status_handlers[APPROVED].nil? || status_handlers[PENDING].nil? || status_handlers[IN_PROCESS].nil? || status_handlers[REJECTED].nil? || status_handlers[REFUNDED].nil? || status_handlers[CANCELLED].nil? || status_handlers[IN_MEDIATION].nil? raise "MissingStatusHandler" end end end end