33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'app/models/pay/lemon_squeezy/charge.rb', line 33
def self.sync_subscription_invoice(subscription_invoice_id, object: nil)
object ||= ::LemonSqueezy::SubscriptionInvoice.retrieve(id: subscription_invoice_id)
pay_customer = Pay::Customer.find_by(type: "Pay::LemonSqueezy::Customer", processor_id: object.customer_id)
return unless pay_customer
processor_id = "subscription_invoice:#{object.id}"
subscription = Pay::LemonSqueezy::Subscription.find_by(processor_id: object.subscription_id)
attributes = {
processor_id: processor_id,
currency: object.currency,
amount: object.total,
amount_refunded: object.refunded_amount,
subtotal: object.subtotal,
tax: object.tax,
subscription: subscription,
payment_method_type: ("card" if object.card_brand.present?),
brand: object.card_brand,
last4: object.card_last_four,
created_at: (object.created_at ? Time.parse(object.created_at) : nil),
updated_at: (object.updated_at ? Time.parse(object.updated_at) : nil)
}
Pay::LemonSqueezy::PaymentMethod.sync(pay_customer: pay_customer, attributes: object)
if (pay_charge = pay_customer.charges.find_by(processor_id: processor_id))
pay_charge.with_lock do
pay_charge.update!(attributes)
end
pay_charge
else
pay_customer.charges.create!(attributes.merge(processor_id: processor_id))
end
end
|