Class: Pay::LemonSqueezy::Charge
- Inherits:
-
Charge
- Object
- Charge
- Pay::LemonSqueezy::Charge
- Defined in:
- app/models/pay/lemon_squeezy/charge.rb
Class Method Summary collapse
-
.sync_order(order_id, object: nil, try: 0, retries: 1) ⇒ Object
LemonSqueezy uses Order for one-time payments and Order + Subscription + SubscriptionInvoice for subscriptions.
- .sync_subscription_invoice(subscription_invoice_id, object: nil) ⇒ Object
Instance Method Summary collapse
Class Method Details
.sync_order(order_id, object: nil, try: 0, retries: 1) ⇒ Object
LemonSqueezy uses Order for one-time payments and Order + Subscription + SubscriptionInvoice for subscriptions
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/pay/lemon_squeezy/charge.rb', line 6 def self.sync_order(order_id, object: nil, try: 0, retries: 1) object ||= ::LemonSqueezy::Order.retrieve(id: order_id) pay_customer = Pay::LemonSqueezy::Customer.find_by(processor_id: object.customer_id) return unless pay_customer processor_id = "order:#{object.id}" attributes = { processor_id: processor_id, currency: object.currency, subtotal: object.subtotal, tax: object.tax, amount: object.total, amount_refunded: object.refunded_amount, created_at: (object.created_at ? Time.parse(object.created_at) : nil), updated_at: (object.updated_at ? Time.parse(object.updated_at) : nil) } # Update or create the charge if (pay_charge = find_by(customer: pay_customer, processor_id: processor_id)) pay_charge.with_lock { pay_charge.update!(attributes) } pay_charge else create!(attributes.merge(customer: pay_customer, processor_id: processor_id)) end end |
.sync_subscription_invoice(subscription_invoice_id, object: nil) ⇒ Object
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) # Skip loading the latest subscription invoice details from the API if we already have it object ||= ::LemonSqueezy::SubscriptionInvoice.retrieve(id: subscription_invoice_id) pay_customer = Pay::LemonSqueezy::Customer.find_by(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) } # Update customer's payment method Pay::LemonSqueezy::PaymentMethod.sync(pay_customer: pay_customer, attributes: object) # Update or create the charge 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 |
Instance Method Details
#api_record ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/models/pay/lemon_squeezy/charge.rb', line 81 def api_record ls_type, ls_id = processor_id.split(":", 2) case ls_type when "order" ::LemonSqueezy::Order.retrieve(id: ls_id) when "subscription_invoice" ::LemonSqueezy::SubscriptionInvoice.retrieve(id: ls_id) end rescue ::LemonSqueezy::Error => e raise Pay::LemonSqueezy::Error, e end |
#save ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'app/models/pay/lemon_squeezy/charge.rb', line 71 def save ls_type, ls_id = processor_id.split(":", 2) case ls_type when "order" self.class.sync_order(ls_id) when "subscription_invoice" self.class.sync_subscription_invoice(ls_id) end end |