Module: Pay::Lago

Extended by:
Env
Defined in:
lib/pay/lago.rb,
lib/pay/lago/error.rb,
lib/pay/lago/charge.rb,
lib/pay/lago/engine.rb,
lib/pay/lago/version.rb,
lib/pay/lago/billable.rb,
lib/pay/lago/attributes.rb,
lib/pay/lago/subscription.rb,
lib/pay/lago/payment_method.rb,
lib/pay/lago/webhook_extensions.rb,
lib/pay/lago/webhooks/invoice_created.rb,
lib/pay/lago/webhooks/invoice_drafted.rb,
lib/pay/lago/webhooks/subscription_started.rb,
app/models/concerns/pay/lago/pay_extensions.rb,
lib/pay/lago/webhooks/invoice_one_off_created.rb,
lib/pay/lago/webhooks/subscription_terminated.rb,
app/models/concerns/pay/lago/pay_customer_extensions.rb,
lib/pay/lago/webhooks/invoice_payment_status_updated.rb,
lib/pay/lago/webhooks/customer_payment_provider_created.rb,
app/models/concerns/pay/lago/pay_payment_method_extensions.rb

Defined Under Namespace

Modules: Attributes, PayCustomerExtensions, PayExtensions, PayPaymentMethodExtensions, WebhookExtensions, Webhooks Classes: Billable, Charge, Engine, Error, PaymentMethod, Subscription

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.api_keyObject



63
64
65
# File 'lib/pay/lago.rb', line 63

def api_key
  find_value_by_name(:lago, :api_key)
end

.api_urlObject



67
68
69
# File 'lib/pay/lago.rb', line 67

def api_url
  find_value_by_name(:lago, :api_url)
end

.clientObject



36
37
38
# File 'lib/pay/lago.rb', line 36

def client
  @client ||= ::Lago::Api::Client.new(api_key: api_key, api_url: api_url)
end

.configure_webhooksObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pay/lago.rb', line 71

def configure_webhooks
  Pay::Webhooks.configure do |events|
    events.subscribe "lago.customer.payment_provider_created", Pay::Lago::Webhooks::CustomerPaymentProviderCreated.new
    events.subscribe "lago.invoice.created", Pay::Lago::Webhooks::InvoiceCreated.new
    events.subscribe "lago.invoice.drafted", Pay::Lago::Webhooks::InvoiceDrafted.new
    events.subscribe "lago.invoice.one_off_created", Pay::Lago::Webhooks::InvoiceOneOffCreated.new
    events.subscribe "lago.invoice.payment_status_updated", Pay::Lago::Webhooks::InvoicePaymentStatusUpdated.new
    events.subscribe "lago.subscription.started", Pay::Lago::Webhooks::SubscriptionStarted.new
    events.subscribe "lago.subscription.terminated", Pay::Lago::Webhooks::SubscriptionTerminated.new
  end
end

.create_webhook!(url, mount = Pay.routes_path) ⇒ Object



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

def create_webhook!(url, mount = Pay.routes_path)
  uri = URI(url)
  raise Pay::Lago::Error.new("Invalid URI: #{uri}") unless uri.host.present?
  uri.scheme = "https" unless %w[http https].include?(uri.scheme)
  uri.path = "/#{mount}/webhooks/lago".squeeze("/")
  client.webhook_endpoints.create(signature_algo: "jwt", webhook_url: uri.to_s)
  true
rescue ::Lago::Api::HttpError => e
  return true if e.error_code == 422
  raise Pay::Lago::Error, e
end

.enabled?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/pay/lago.rb', line 32

def enabled?
  defined?(::Lago)
end

.openstruct_to_h(ostruct) ⇒ Object

Helpers for working with Lago client



84
85
86
87
88
# File 'lib/pay/lago.rb', line 84

def openstruct_to_h(ostruct)
  return ostruct.to_h.transform_values { |value| openstruct_to_h(value) } if ostruct.is_a?(OpenStruct) || ostruct.is_a?(Hash)
  return ostruct.map { |value| openstruct_to_h(value) } if ostruct.is_a?(Array)
  ostruct
end

.valid_auth?Boolean

Returns:

  • (Boolean)


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

def valid_auth?
  webhook_public_key
  true
rescue
  false
end

.webhook_public_keyObject



59
60
61
# File 'lib/pay/lago.rb', line 59

def webhook_public_key
  @webhook_public_key ||= client.webhooks.public_key
end