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)


107
108
109
# File 'app/models/pay/abacatepay/customer.rb', line 107

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, coupons: 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
76
# 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, coupons: 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,
    coupons: coupons
  )
  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: nil, methods: ["PIX", "CARD"], quantity: 1, external_id: nil, metadata: nil, cycle: nil, **options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/pay/abacatepay/customer.rb', line 78

def subscribe(name: Pay.default_product_name, plan: nil, methods: ["PIX", "CARD"], quantity: 1, external_id: nil, metadata: nil, cycle: nil, **options)
  methods = Array(methods).reject(&:blank?)
  validate_subscribe_args!(plan: plan, methods: methods, quantity: quantity, cycle: cycle, options: options)
  api_record unless processor_id?

  resource = build_subscription_resource(plan: plan, methods: methods, external_id: external_id, quantity: quantity)
  created = ::AbacatePay.subscriptions.create(resource)

  period_start = Time.current
  period_end = cycle ? period_start + Pay::Abacatepay::Frequency.to_interval(cycle) : nil

  pay_subscription = Pay::Abacatepay::Subscription.find_or_initialize_by(customer: self, processor_id: created.id)
  pay_subscription.assign_attributes(
    name: name,
    processor_plan: plan,
    quantity: quantity,
    status: Pay::Abacatepay::Subscription::API_STATUS_MAP[created.status&.to_s&.upcase] || "incomplete",
    current_period_start: period_start,
    current_period_end: period_end,
    checkout_url: created.url
  )
  pay_subscription. =  if .present?
  pay_subscription.save!

  pay_subscription
rescue ::AbacatePay::Error => e
  raise Pay::Abacatepay::Error, e.message
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