Class: Pay::Abacatepay::Charge

Inherits:
Charge
  • Object
show all
Defined in:
lib/pay/abacatepay/charge.rb

Constant Summary collapse

STATUS_MAP =

AbacatePay checkout status → Pay::Charge status (stored in data). PENDING is created eagerly by Customer#charge so the app can render “payment in progress” UI before the webhook arrives. EXPIRED and CANCELLED never become a Pay::Charge — the payment never succeeded and there is nothing to track.

{
  "PENDING" => "pending",
  "PAID" => "paid",
  "REFUNDED" => "refunded",
  "DISPUTED" => "disputed"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sync(charge_id, object: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pay/abacatepay/charge.rb', line 20

def self.sync(charge_id, object: nil)
  object ||= ::AbacatePay.checkouts.get(charge_id)

  customer_id = extract_customer_id(object)
  if customer_id.blank?
    Rails.logger.debug("[pay-abacatepay] checkout #{charge_id} has no customer; skipping sync")
    return
  end

  pay_customer = Pay::Customer.find_by(processor: "abacatepay", processor_id: customer_id)
  if pay_customer.nil?
    Rails.logger.debug("[pay-abacatepay] Pay::Customer #{customer_id} not found while syncing checkout #{charge_id}")
    return
  end

  charge = Pay::Abacatepay::Charge.find_or_initialize_by(
    customer: pay_customer,
    processor_id: charge_id
  )
  charge.amount = object.amount if object.respond_to?(:amount) && object.amount
  charge.currency ||= "BRL"
  charge.status = STATUS_MAP[object.status] || charge.status || "pending" if object.respond_to?(:status)
  charge.checkout_url = object.url if object.respond_to?(:url) && object.url
  charge.save!
  charge
rescue ::AbacatePay::Error => e
  raise Pay::Abacatepay::Error, e.message
end

Instance Method Details

#api_recordObject



49
50
51
52
53
# File 'lib/pay/abacatepay/charge.rb', line 49

def api_record
  ::AbacatePay.checkouts.get(processor_id)
rescue ::AbacatePay::Error => e
  raise Pay::Abacatepay::Error, e.message
end

#charged_back?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/pay/abacatepay/charge.rb', line 62

def charged_back?
  status == "disputed"
end

#refund!(_amount_to_refund = nil) ⇒ Object



55
56
57
58
59
60
# File 'lib/pay/abacatepay/charge.rb', line 55

def refund!(_amount_to_refund = nil)
  raise Pay::Abacatepay::Error,
    "AbacatePay does not expose a refund endpoint. " \
    "Process the refund in the AbacatePay dashboard; the checkout.refunded " \
    "webhook will update this Pay::Charge automatically."
end