Class: SolidusMpDois::MpCard

Inherits:
Spree::PaymentMethod
  • Object
show all
Defined in:
app/models/solidus_mp_dois/mp_card.rb

Instance Method Summary collapse

Instance Method Details

#auto_capture?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/models/solidus_mp_dois/mp_card.rb', line 19

def auto_capture?
  true
end

#cancel!(payment) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/solidus_mp_dois/mp_card.rb', line 64

def cancel!(payment)
  external_id = payment.source&.external_id
  return unless external_id
  sleep(1)
  mp_payment = find_payment(external_id)
  sync(payment, mp_payment)
  return false unless ["checkout", "pending"].include? payment.state

  mp_payment = mp_client.update_payment(payment_id: external_id, status: "cancelled")
  sync(payment, mp_payment)
end

#create_payment(order, source_params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/solidus_mp_dois/mp_card.rb', line 31

def create_payment order, source_params
  customer = find_or_create_customer(order, source_params)
  save_credit_card(source_params[:token], customer) if customer && source_params[:saved_card] == "false" # Se o cartão usado ja for um cartão salvo, não é necessario executar save_credit_card
  invalidate_valid_payments(order)

  payment = order.payments.new(amount: order.total, payment_method: self)
  payment.source = init_source(order, source_params, customer)
  payment.save

  mp_payment = if payment.source.saved_card
    create_payment_with_saved_card(payment.source, customer.try(:external_id))
  else
    create_payment_with_new_card(payment.source)
  end
  process_payment_response(payment, mp_payment)
  payment
end

#find_payment(external_id) ⇒ Object



27
28
29
# File 'app/models/solidus_mp_dois/mp_card.rb', line 27

def find_payment external_id
  mp_client.get_payment(external_id)
end

#gateway_classObject



11
12
13
# File 'app/models/solidus_mp_dois/mp_card.rb', line 11

def gateway_class
  Gateway
end

#partial_nameObject



23
24
25
# File 'app/models/solidus_mp_dois/mp_card.rb', line 23

def partial_name
  "mercado_pago_card"
end

#payment_source_classObject



7
8
9
# File 'app/models/solidus_mp_dois/mp_card.rb', line 7

def payment_source_class
  CreditCardSource
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/solidus_mp_dois/mp_card.rb', line 49

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
  sync(source.payments.sole, mp_payment)
  if mp_payment.status == "approved"
    source.update(status: "approved")
    successful_response("Pagamento aprovado", status: source.status, internal_detail: source.internal_details)
  else
    failure_response(internal_error(source.internal_details), status: source.status, internal_detail: source.internal_details)
  end
end

#refund_payment(payment, amount = nil) ⇒ Object



76
77
78
79
80
81
82
# File 'app/models/solidus_mp_dois/mp_card.rb', line 76

def refund_payment(payment, amount = nil)
  amount = payment.source.amount if amount.nil?
  refund = mp_client.create_refund(id: payment.source.external_id, amount: amount.to_f)
  mp_payment = payment.source.retrieve_from_api
  payment.source.update(status: mp_payment.status, internal_details: mp_payment.status_detail)
  refund
end

#supports?(source) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'app/models/solidus_mp_dois/mp_card.rb', line 15

def supports?(source)
  source.is_a?(payment_source_class)
end