Class: Pay::Abacatepay::Customer

Inherits:
Customer
  • Object
show all
Defined in:
app/models/pay/abacatepay/customer.rb

Defined Under Namespace

Classes: CheckoutResult

Instance Method Summary collapse

Instance Method Details

#add_payment_method(payment_method_id, default: false) ⇒ Object

Raises:

  • (NotImplementedError)


81
82
83
# File 'app/models/pay/abacatepay/customer.rb', line 81

def add_payment_method(payment_method_id, default: false)
  raise NotImplementedError, "Pay::Abacatepay::Customer#add_payment_method is planned for a future release"
end

#api_recordObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/pay/abacatepay/customer.rb', line 18

def api_record
  with_lock do
    if processor_id?
      ::AbacatePay.customers.get(processor_id)
    else
      attrs = api_record_attributes
      resource = build_customer_resource(attrs)
      created = ::AbacatePay.customers.create(resource)
      update!(processor_id: created.id)
      created
    end
  end
rescue ::AbacatePay::Error => e
  raise Pay::Abacatepay::Error, e.message
end

#api_record_attributesObject



9
10
11
12
13
14
15
16
# File 'app/models/pay/abacatepay/customer.rb', line 9

def api_record_attributes
  {
    name: customer_name,
    email: email,
    cellphone: owner.try(:cellphone) || owner.try(:phone),
    tax_id: extract_document
  }
end

#charge(amount, product_id: nil, methods: ["PIX", "CARD"], return_url: nil, completion_url: nil, external_id: nil, product_name: "Cobrança avulsa", metadata: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/pay/abacatepay/customer.rb', line 47

def charge(amount, product_id: nil, methods: ["PIX", "CARD"], return_url: nil, completion_url: nil, external_id: nil, product_name: "Cobrança avulsa", metadata: nil)
  api_record unless processor_id?

  product_id ||= create_one_time_product(amount, product_name)
  resource = build_checkout_resource(
    product_id: product_id,
    quantity: 1,
    methods: methods,
    return_url: return_url,
    completion_url: completion_url,
    external_id: external_id
  )
  created = ::AbacatePay.checkouts.create(resource)

  pay_charge = Pay::Abacatepay::Charge.find_or_initialize_by(
    customer: self,
    processor_id: created.id
  )
  pay_charge.amount = amount
  pay_charge.currency ||= "BRL"
  pay_charge.status = "pending"
  pay_charge.checkout_url = created.url
  pay_charge. =  if .present?
  pay_charge.save!

  CheckoutResult.new(id: created.id, url: created.url, charge: pay_charge)
rescue ::AbacatePay::Error => e
  raise Pay::Abacatepay::Error, e.message
end

#subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options) ⇒ Object

Raises:

  • (NotImplementedError)


77
78
79
# File 'app/models/pay/abacatepay/customer.rb', line 77

def subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options)
  raise NotImplementedError, "Pay::Abacatepay::Customer#subscribe is planned for a future release"
end

#update_api_record(**attributes) ⇒ Object

AbacatePay’s API does not expose an update endpoint for customers (POST /v2/customers/create and DELETE are the only mutations). TODO: Remove this no-op once the API adds PATCH/PUT support.



37
38
39
40
41
42
43
# File 'app/models/pay/abacatepay/customer.rb', line 37

def update_api_record(**attributes)
  logger = defined?(Rails) ? Rails.logger : nil
  logger&.warn(
    "[pay-abacatepay] AbacatePay does not support customer updates; update_api_record is a no-op."
  )
  nil
end