Skip to content

Commit a851fd8

Browse files
authored
feat: #81 add webhooks for invoices (#106)
* Add Webhook url to organizations, create Lago Http Client * add sequential id into serializer * fix last review * fix spec
1 parent ab42acc commit a851fd8

32 files changed

+431
-42
lines changed

.dockerignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
/tmp/*
1+
/tmp/*
2+
.rsa_private.pem
3+
.rsa_public.pem

.github/workflows/spec.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ jobs:
2525
with:
2626
ruby-version: 3.0.1
2727
bundler-cache: true
28+
- name: Generate RSA keys
29+
run: ./scripts/generate.rsa.sh
2830
- name: Set up database schema
2931
run: bin/rails db:schema:load
3032
- name: Run tests

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@
3737

3838
# Ignore Simplecov coverage status
3939
/coverage/*
40+
41+
.rsa_private.pem
42+
.rsa_public.pem

.rspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--require spec_helper
1+
--require spec_helper

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ RUN apk add --no-cache \
1414
nodejs \
1515
python2 \
1616
tzdata \
17+
openssl \
1718
postgresql-dev
1819

1920
RUN bundle config build.nokogiri --use-system-libraries &&\

Dockerfile.dev

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ WORKDIR /app
44

55
COPY ./Gemfile /app/Gemfile
66
COPY ./Gemfile.lock /app/Gemfile.lock
7+
COPY ./scripts/generate.rsa.sh /app/scripts/generate.rsa.sh
78

89
RUN apk add --no-cache \
910
git \
@@ -15,9 +16,12 @@ RUN apk add --no-cache \
1516
python2 \
1617
tzdata \
1718
postgresql-dev \
19+
openssl \
1820
gcompat
1921

2022
RUN bundle config build.nokogiri --use-system-libraries &&\
2123
bundle install
2224

23-
CMD ["./start.dev.sh"]
25+
RUN ./scripts/generate.rsa.sh
26+
27+
CMD ["./scripts/start.dev.sh"]

app/jobs/send_webhook_job.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require Rails.root.join('lib/lago_http_client/lago_http_client')
4+
5+
class SendWebhookJob < ApplicationJob
6+
queue_as 'webhook'
7+
8+
retry_on LagoHttpClient::HttpError, wait: :exponentially_longer, attempts: 3
9+
10+
def perform(webhook_type, object)
11+
case webhook_type
12+
when :invoice
13+
Webhooks::InvoicesService.new(object).call
14+
else
15+
raise NotImplementedError
16+
end
17+
end
18+
end

app/models/fee.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,22 @@ def compute_vat
3232
self.vat_amount_cents = (amount_cents * vat_rate / 100).to_i
3333
self.vat_amount_currency = amount_currency
3434
end
35+
36+
def item_type
37+
return 'charge' if charge_fee?
38+
39+
'subscription'
40+
end
41+
42+
def item_code
43+
return billable_metric.code if charge_fee?
44+
45+
subscription.plan.code
46+
end
47+
48+
def item_name
49+
return billable_metric.name if charge_fee?
50+
51+
subscription.plan.name
52+
end
3553
end

app/models/organization.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Organization < ApplicationRecord
1313
before_create :generate_api_key
1414

1515
validates :name, presence: true
16+
validates :webhook_url, url: true, allow_nil: true
1617

1718
private
1819

app/serializers/v1/fee_serializer.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module V1
4+
class FeeSerializer < ModelSerializer
5+
def serialize
6+
{
7+
item: {
8+
type: model.item_type,
9+
code: model.item_code,
10+
name: model.item_name,
11+
},
12+
amount_cents: model.amount_cents,
13+
amount_currency: model.amount_currency,
14+
vat_amount_cents: model.vat_amount_cents,
15+
vat_amount_currency: model.vat_amount_currency,
16+
}
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)