Class: Pay::Asaas::Charge

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_attribute(obj, attr_name) ⇒ Object



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

def self.get_attribute(obj, attr_name)
  return obj[attr_name.to_s] if obj.is_a?(Hash)
  return obj.send(attr_name) if obj.respond_to?(attr_name)

  nil
end

.sync(charge_id, object: nil, try: 0, retries: 1) ⇒ Object



10
11
12
13
14
15
16
17
18
19
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
# File 'lib/pay/asaas/charge.rb', line 10

def self.sync(charge_id, object: nil, try: 0, retries: 1)
  object ||= Pay::Asaas::Api::Payment.find(id: charge_id)

  customer_processor_id = get_attribute(object, :customer)
  charge_processor_id = get_attribute(object, :id)
  pay_customer = Pay::Customer.find_by(processor: :asaas, processor_id: customer_processor_id)

  # rubocop:disable Layout/LineLength
  if pay_customer.blank?
    Rails.logger.debug do
      "Pay::Customer #{customer_processor_id} is not in the database while syncing Asaas Charge #{charge_processor_id}"
    end
    return
  end
  # rubocop:enable Layout/LineLength

  attrs = {
    amount: get_attribute(object, :value).to_f * 100,
    status: get_attribute(object, :status),
  }

  # Update the charge
  if (pay_charge = pay_customer.charges.find_by(processor_id: charge_processor_id))
    pay_charge.with_lock do
      pay_charge.update!(attrs)
    end
    pay_charge
  end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
  try += 1
  if try <= retries
    sleep 0.1
    retry
  else
    raise
  end
end

Instance Method Details

#api_recordObject



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

def api_record
  Pay::Asaas::Api::Payment.find(id: processor_id)
rescue ApiClient::ApiError => e
  raise Pay::Asaas::Error, e
end

#refund!(amount_to_refund) ⇒ Object

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/pay/asaas/charge.rb', line 61

def refund!(amount_to_refund)
  raise NotImplementedError, "Refunding charges is not supported yet by the Asaas processor"
end

#sync_pix_qr_codeObject



65
66
67
68
69
70
71
72
73
# File 'lib/pay/asaas/charge.rb', line 65

def sync_pix_qr_code
  return unless payment_method_type == "pix"

  response = Pay::Asaas::Api::Payment.fetch_qr_code(processor_id)

  update!(
    pix_code: response["payload"],
  )
end