Class: Pay::LemonSqueezy::Charge

Inherits:
Charge
  • Object
show all
Extended by:
Sync
Defined in:
app/models/pay/lemon_squeezy/charge.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sync

find_pay_customer, pay_processor, sync_with_retries

Class Method Details

.sync(processor_id, object: nil) ⇒ Object

LemonSqueezy uses Order for one-time payments and Order + Subscription + SubscriptionInvoice for subscriptions Charges are stored with a "order:123" or "subscription_invoice:123" processor_id so both can be synced by ID



9
10
11
12
13
14
15
16
17
# File 'app/models/pay/lemon_squeezy/charge.rb', line 9

def self.sync(processor_id, object: nil)
  type, id = processor_id.split(":", 2)
  case type
  when "order"
    sync_order(id, object: object)
  when "subscription_invoice"
    sync_subscription_invoice(id, object: object)
  end
end

.sync_order(order_id, object: nil) ⇒ Object



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
# File 'app/models/pay/lemon_squeezy/charge.rb', line 19

def self.sync_order(order_id, object: nil)
  sync_with_retries do
    order = object || ::LemonSqueezy::Order.retrieve(id: order_id)
    return unless (pay_customer = find_pay_customer(order.customer_id))

    processor_id = "order:#{order.id}"
    attributes = {
      processor_id: processor_id,
      currency: order.currency,
      subtotal: order.subtotal,
      tax: order.tax,
      amount: order.total,
      amount_refunded: order.refunded_amount,
      created_at: (order.created_at ? Time.parse(order.created_at) : nil),
      updated_at: (order.updated_at ? Time.parse(order.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
end

.sync_subscription_invoice(subscription_invoice_id, object: nil) ⇒ Object



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
72
73
74
75
76
77
78
79
80
81
# File 'app/models/pay/lemon_squeezy/charge.rb', line 46

def self.sync_subscription_invoice(subscription_invoice_id, object: nil)
  sync_with_retries do
    invoice = object || ::LemonSqueezy::SubscriptionInvoice.retrieve(id: subscription_invoice_id)
    return unless (pay_customer = find_pay_customer(invoice.customer_id))

    processor_id = "subscription_invoice:#{invoice.id}"
    subscription = Pay::LemonSqueezy::Subscription.find_by(processor_id: invoice.subscription_id)
    attributes = {
      processor_id: processor_id,
      currency: invoice.currency,
      amount: invoice.total,
      amount_refunded: invoice.refunded_amount,
      subtotal: invoice.subtotal,
      tax: invoice.tax,
      subscription: subscription,
      payment_method_type: ("card" if invoice.card_brand.present?),
      brand: invoice.card_brand,
      last4: invoice.card_last_four,
      created_at: (invoice.created_at ? Time.parse(invoice.created_at) : nil),
      updated_at: (invoice.updated_at ? Time.parse(invoice.updated_at) : nil)
    }

    # Update customer's payment method
    Pay::LemonSqueezy::PaymentMethod.sync(pay_customer: pay_customer, attributes: invoice)

    # 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
end

Instance Method Details

#api_recordObject



83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/pay/lemon_squeezy/charge.rb', line 83

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