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)


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

def auto_capture?
  true
end

#create_payment(order) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/solidus_mp_dois/mp_pix.rb', line 27

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
  )

  process_payment_response(payment, mp_payment)
  payment
end

#find_payment(external_id) ⇒ Object



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

def find_payment external_id
  mp_client.get_payment(external_id)
end

#invalidate_payment(payment) ⇒ Object



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

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

#paid?(payment) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'app/models/solidus_mp_dois/mp_pix.rb', line 62

def paid? payment
  purchase(payment)
  payment.completed?
end

#partial_nameObject



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

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

#purchase(payment) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'app/models/solidus_mp_dois/mp_pix.rb', line 52

def purchase(payment)
  return unless payment.source&.external_id
  10.times do
    mp_payment = find_payment(payment.source.external_id)
    break if ["approved", "rejected"].include? mp_payment.status
  end
  mp_payment = find_payment(payment.source.external_id)
  sync(payment, mp_payment)
end

#supports?(source) ⇒ Boolean

Returns:

  • (Boolean)


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

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