Class: Pay::LemonSqueezy::Charge
- Inherits:
-
Charge
- Object
- ApplicationRecord
- 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
Methods inherited from Charge
#amount_refunded_with_currency, #amount_with_currency, #captured?, #charged_to, find_by_processor_and_id, #full_refund?, #line_items, #partial_refund?, #refunded?
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 32 33 |
# 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::Customer.find_by(type: "Pay::LemonSqueezy::Customer", 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 = 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 |
.sync_subscription_invoice(subscription_invoice_id, object: nil) ⇒ Object
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 70 71 |
# File 'app/models/pay/lemon_squeezy/charge.rb', line 35 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::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) } # 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
73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/pay/lemon_squeezy/charge.rb', line 73 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 |