Class: SolidusMpDois::MpGateway

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus_mp_dois/mp_gateway.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MpGateway

Returns a new instance of MpGateway.



3
4
5
# File 'app/models/solidus_mp_dois/mp_gateway.rb', line 3

def initialize(options)
  MpApi.configuration.access_token = options[:access_token]
end

Instance Method Details

#purchase(money, source, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/solidus_mp_dois/mp_gateway.rb', line 7

def purchase money, source, options = {}
  mp_payment = source.retrieve_from_api
  10.times do
    break if ["approved", "rejected"].include? mp_payment.status
    mp_payment = source.retrieve_from_api
  end
  if mp_payment.status == "approved"
    source.update(status: "approved")
    successful_response("Pagamento realizado", mp_payment.id)
  else
    failure_response(mp_payment.status_detail || mp_payment.status)
  end
end

#successful_response(message, transaction_id) ⇒ Object



26
27
28
29
30
31
32
33
# File 'app/models/solidus_mp_dois/mp_gateway.rb', line 26

def successful_response message, transaction_id
  ActiveMerchant::Billing::Response.new(
    true,
    message,
    {},
    authorization: transaction_id
  )
end

#void(transaction_id, options = {}) ⇒ Object



21
22
23
24
# File 'app/models/solidus_mp_dois/mp_gateway.rb', line 21

def void(transaction_id, options = {})
  # Respondendo sempre com successful_response para funcionar o botão de "Cancelar" do pedido. Reembolso deve ser feito por fora.
  successful_response("Pagamento cancelado. Se necessário, realize o reembolso.", transaction_id)
end