Class: SolidusMpDois::MpPix

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

Instance Method Summary collapse

Instance Method Details

#auto_capture?Boolean

Returns:

  • (Boolean)


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

def auto_capture?
  true
end

#create_payment(order) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/solidus_mp_dois/mp_pix.rb', line 31

def create_payment order
  existing_payment = find_existing_payment(order)
  return existing_payment if payment_usable?(order, existing_payment)
  invalidate_valid_payments(order, existing_payment&.id)

  payment = order.payments.new(amount: order.total, payment_method: self)
  payment.source = init_source(order)
  payment.save
  payment_source = payment.source
  identification_type = (payment_source.tax_id.length > 14) ? "CNPJ" : "CPF"

  mp_payment = mp_client.create_pix_payment(
    amount: payment_source.amount.to_f,
    description: payment_source.description,
    statement_descriptor: preferences[:statement_descriptor],
    payment_method: "pix",
    payer_email: payment_source.email,
    payer_identification_type: identification_type,
    payer_identification_number: payment_source.tax_id,
    external_reference: payment.number || SecureRandom.hex(4)
  )

  process_payment_response(payment, mp_payment)
  payment
end

#find_payment(external_id) ⇒ Object



27
28
29
# File 'app/models/solidus_mp_dois/mp_pix.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_pix.rb', line 11

def gateway_class
  Gateway
end

#invalidate_payment(payment) ⇒ Object



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

def invalidate_payment payment
  external_id = payment.source&.external_id
  return false if external_id.nil?
  mp_payment = find_payment(payment.source.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

#partial_nameObject



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

def partial_name
  "mercado_pago_pix"
end

#payment_source_classObject



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

def payment_source_class
  PixSource
end

#pix_paid?(payment) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'app/models/solidus_mp_dois/mp_pix.rb', line 68

def pix_paid? payment
  mp_payment = find_payment(payment.source.external_id)
  paid_amount = Float(mp_payment.transaction_details.total_paid_amount)
  mp_payment.status == "approved" && paid_amount >= payment.amount
end

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



57
58
59
60
61
62
63
64
65
66
# File 'app/models/solidus_mp_dois/mp_pix.rb', line 57

def purchase(money, source, options = {})
  mp_payment = source.retrieve_from_api
  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



84
85
86
87
88
89
90
# File 'app/models/solidus_mp_dois/mp_pix.rb', line 84

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_pix.rb', line 15

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