Class: Pay::Lago::Charge

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pay_charge) ⇒ Charge

Returns a new instance of Charge.



47
48
49
# File 'lib/pay/lago/charge.rb', line 47

def initialize(pay_charge)
  @pay_charge = pay_charge
end

Instance Attribute Details

#pay_chargeObject (readonly)

Returns the value of attribute pay_charge.



4
5
6
# File 'lib/pay/lago/charge.rb', line 4

def pay_charge
  @pay_charge
end

Class Method Details

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



8
9
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
# File 'lib/pay/lago/charge.rb', line 8

def self.sync(charge_id, object: nil, try: 0, retries: 1)
  object ||= Lago.client.invoices.get(charge_id)

  pay_customer = Pay::Customer.find_by(processor: :lago, processor_id: object.customer.external_id)
  if pay_customer.blank?
    Rails.logger.debug "Pay::Customer #{object.customer} is not in the database while syncing Lago Invoice #{object.lago_id}"
    return
  end

  attrs = {
    processor_id: object.lago_id,
    amount: object.total_amount_cents,
    data: Lago.openstruct_to_h(object)
  }

  if object.subscriptions.present?
    subscription = object.subscriptions.first
    attrs[:subscription] = pay_customer.subscriptions.find_by(processor_id: subscription.try(:lago_id))
  end

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

Instance Method Details

#chargeObject



51
52
53
54
55
# File 'lib/pay/lago/charge.rb', line 51

def charge
  Lago.client.invoices.get(processor_id)
rescue ::Lago::Api::HttpError => e
  raise Pay::Lago::Error, e
end

#refund!(amount_to_refund, **options) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pay/lago/charge.rb', line 63

def refund!(amount_to_refund, **options)
  attributes = {
    refund_amount_cents: amount_to_refund,
    invoice_id: processor_id,
    items: [{fee_id: charge.fees.first.try(:lago_id), amount_cents: amount_to_refund}]
  }
  begin
    Lago.client.credit_notes.create(options.merge(attributes))
  rescue ::Lago::Api::HttpError => e
    if e.error_code == 403
      raise Pay::Lago::Error.new("Creating a credit note requires Lago Premium.")
    else
      raise Pay::Lago::Error, e
    end
  end
  pay_charge.update!(amount_refunded: pay_charge.amount_refunded.to_i + amount_to_refund)
end

#update_charge!(**attributes) ⇒ Object



57
58
59
60
61
# File 'lib/pay/lago/charge.rb', line 57

def update_charge!(**attributes)
  Lago.client.invoices.update({metadata: []}.merge(attributes), charge.lago_id)
rescue ::Lago::Api::HttpError => e
  raise Pay::Lago::Error, e
end