Module: Pay::LemonSqueezy

Extended by:
Env
Defined in:
lib/pay/lemon_squeezy.rb,
app/models/pay/lemon_squeezy/charge.rb,
lib/pay/lemon_squeezy/webhooks/order.rb,
app/models/pay/lemon_squeezy/customer.rb,
app/models/pay/lemon_squeezy/subscription.rb,
app/models/pay/lemon_squeezy/payment_method.rb,
lib/pay/lemon_squeezy/webhooks/subscription.rb,
lib/pay/lemon_squeezy/webhooks/subscription_payment.rb

Defined Under Namespace

Modules: Webhooks Classes: Charge, Customer, Error, PaymentMethod, Subscription

Class Method Summary collapse

Class Method Details

.api_keyObject



25
26
27
# File 'lib/pay/lemon_squeezy.rb', line 25

def self.api_key
  find_value_by_name(:lemon_squeezy, :api_key)
end

.configure_webhooksObject



47
48
49
50
51
52
53
54
55
# File 'lib/pay/lemon_squeezy.rb', line 47

def self.configure_webhooks
  Pay::Webhooks.configure do |events|
    events.subscribe "lemon_squeezy.order_created", Pay::LemonSqueezy::Webhooks::Order.new
    events.subscribe "lemon_squeezy.subscription_created", Pay::LemonSqueezy::Webhooks::Subscription.new
    events.subscribe "lemon_squeezy.subscription_updated", Pay::LemonSqueezy::Webhooks::Subscription.new
    events.subscribe "lemon_squeezy.subscription_payment_refunded", Pay::LemonSqueezy::Webhooks::SubscriptionPayment.new
    events.subscribe "lemon_squeezy.subscription_payment_success", Pay::LemonSqueezy::Webhooks::SubscriptionPayment.new
  end
end

.construct_from_webhook_event(event) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pay/lemon_squeezy.rb', line 57

def self.construct_from_webhook_event(event)
  data = event["data"]
  case data
  when Array
    data.map do |object|
      construct_from_webhook_event(object)
    end
  when Hash
    type = {
      "orders" => ::LemonSqueezy::Order,
      "subscriptions" => ::LemonSqueezy::Subscription,
      "subscription-invoices" => ::LemonSqueezy::SubscriptionInvoice
    }.fetch(data["type"])

    object = type.new(data)
    object.meta = event["meta"]
    object
  end
end

.enabled?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/pay/lemon_squeezy.rb', line 15

def self.enabled?
  return false unless Pay.enabled_processors.include?(:lemon_squeezy) && defined?(::LemonSqueezy)

  Pay::Engine.version_matches?(required: "~> 1.0", current: ::LemonSqueezy::VERSION) || (raise "[Pay] lemonsqueezy gem must be version ~> 1.0")
end

.owner_from_passthrough(passthrough) ⇒ Object



41
42
43
44
45
# File 'lib/pay/lemon_squeezy.rb', line 41

def self.owner_from_passthrough(passthrough)
  GlobalID::Locator.locate_signed passthrough
rescue JSON::ParserError
  nil
end

.passthrough(owner:, **options) ⇒ Object



37
38
39
# File 'lib/pay/lemon_squeezy.rb', line 37

def self.passthrough(owner:, **options)
  owner.to_sgid.to_s
end

.setupObject



21
22
23
# File 'lib/pay/lemon_squeezy.rb', line 21

def self.setup
  ::LemonSqueezy.config.api_key = api_key
end

.signing_secretObject



33
34
35
# File 'lib/pay/lemon_squeezy.rb', line 33

def self.signing_secret
  find_value_by_name(:lemon_squeezy, :signing_secret)
end

.store_idObject



29
30
31
# File 'lib/pay/lemon_squeezy.rb', line 29

def self.store_id
  find_value_by_name(:lemon_squeezy, :store_id)
end

.sync_order(order_id, object: nil) ⇒ Object

An Order may have subscriptions or be a one-time purchase



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pay/lemon_squeezy.rb', line 78

def self.sync_order(order_id, object: nil)
  subscriptions = ::LemonSqueezy::Subscription.list(order_id: order_id).data
  subscriptions.each do |subscription|
    Pay::LemonSqueezy::Subscription.sync(subscription.id, object: subscription)
    ::LemonSqueezy::SubscriptionInvoice.list(subscription_id: subscription.id).data.each do |invoice|
      Pay::LemonSqueezy::Charge.sync_subscription_invoice(invoice.id, object: invoice)
    end
  end

  if subscriptions.empty?
    Pay::LemonSqueezy::Charge.sync_order(order_id, object: object)
  end
end