Skip to content

Commit 7495e95

Browse files
authored
Merge pull request #237 from getlago/feat-invoice-preview
2 parents 570f6b3 + 9c09e7f commit 7495e95

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

lib/lago/api/resources/invoice.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ def payment_url(invoice_id)
7070
JSON.parse(response.to_json, object_class: OpenStruct)
7171
end
7272

73+
def preview(params)
74+
path = "/api/v1/invoices/preview"
75+
payload = params.slice(:customer, :plan_code, :subscription_at, :billing_time, :coupons)
76+
response = connection.post(payload, path)[root_name]
77+
78+
JSON.parse(response.to_json, object_class: OpenStruct)
79+
end
80+
7381
def whitelist_params(params)
7482
result = {
7583
payment_status: params[:payment_status],
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
{
2+
"invoice": {
3+
"lago_id": null,
4+
"sequential_id": null,
5+
"number": "",
6+
"issuing_date": "2022-06-02",
7+
"payment_dispute_lost_at": null,
8+
"payment_due_date": "2022-06-02",
9+
"payment_overdue": false,
10+
"invoice_type": "one_off",
11+
"version_number": 2,
12+
"status": "finalized",
13+
"payment_status": "pending",
14+
"currency": "EUR",
15+
"net_payment_term": 0,
16+
"fees_amount_cents": 100,
17+
"taxes_amount_cents": 20,
18+
"coupons_amount_cents": 0,
19+
"credit_notes_amount_cents": 0,
20+
"sub_total_excluding_taxes_amount_cents": 100,
21+
"sub_total_including_taxes_amount_cents": 120,
22+
"prepaid_credit_amount_cents": 0,
23+
"total_amount_cents": 120,
24+
"metadata": [
25+
{
26+
"key": "key",
27+
"value": "value",
28+
"display_in_invoice": true
29+
}
30+
],
31+
"applied_taxes": [
32+
{
33+
"lago_id": null,
34+
"lago_invoice_id": null,
35+
"lago_tax_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
36+
"tax_name": "tax name",
37+
"tax_code": "tax_code",
38+
"tax_rate": "20.0",
39+
"tax_description": "tax description",
40+
"amount_cents": 100,
41+
"amount_currency": "EUR",
42+
"fees_amount_cents": 500,
43+
"created_at": "2022-04-29T08:59:51Z"
44+
}
45+
],
46+
"customer": {
47+
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
48+
"external_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
49+
"address_line1": "5230 Penfield Ave",
50+
"address_line2": null,
51+
"city": "Woodland Hills",
52+
"country": "US",
53+
"created_at": "2022-04-29T08:59:51Z",
54+
"email": "[email protected]",
55+
"legal_name": "Coleman-Blair",
56+
"legal_number": "49-008-2965",
57+
"logo_url": "http://hooli.com/logo.png",
58+
"name": "Gavin Belson",
59+
"phone": "1-171-883-3711 x245",
60+
"state": "CA",
61+
"url": "http://hooli.com",
62+
"zipcode": "91364",
63+
"currency": "EUR",
64+
"timezone": "Europe/Paris",
65+
"applicable_timezone": "Europe/Paris",
66+
"billing_configuration": {
67+
"payment_provider": "stripe",
68+
"payment_provider_code": "stripe-eu-1",
69+
"provider_customer_id": "cus_12345"
70+
},
71+
"taxes": [
72+
{
73+
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
74+
"name": "tax_name",
75+
"code": "tax_code",
76+
"rate": 15.0,
77+
"description": "tax_desc",
78+
"add_ons_count": 0,
79+
"customers_count": 0,
80+
"plans_count": 0,
81+
"charges_count": 0,
82+
"applied_to_organization": false,
83+
"created_at": "2022-04-29T08:59:51Z"
84+
}
85+
]
86+
},
87+
"subscriptions": [
88+
{
89+
"lago_id": null,
90+
"lago_customer_id": null,
91+
"external_customer_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
92+
"canceled_at": "2022-04-29T08:59:51Z",
93+
"created_at": "2022-04-29T08:59:51Z",
94+
"plan_code": "eartha lynch",
95+
"started_at": "2022-04-29T08:59:51Z",
96+
"status": "active",
97+
"billing_time": "anniversary",
98+
"terminated_at": null,
99+
"subscription_at": "2022-04-29T12:27:30Z",
100+
"previous_plan_code": null,
101+
"next_plan_code": null,
102+
"downgrade_plan_date": null,
103+
"current_billing_period_started_at": "2022-04-29T08:59:51Z",
104+
"current_billing_period_ending_at": "2022-05-29T23:59:59Z"
105+
}
106+
],
107+
"fees": [
108+
{
109+
"lago_id": null,
110+
"lago_charge_id": null,
111+
"item": {
112+
"type": "charge",
113+
"code": "seats",
114+
"name": "User Seats",
115+
"invoice_display_name": "User Seats on Invoice"
116+
},
117+
"amount_cents": 100,
118+
"amount_currency": "EUR",
119+
"taxes_amount_cents": 20,
120+
"taxes_rate": "20.0",
121+
"total_amount_cents": 120,
122+
"total_amount_currency": "EUR",
123+
"units": 12.6,
124+
"events_count": 4,
125+
"applied_taxes": [
126+
{
127+
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
128+
"lago_fee_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
129+
"lago_tax_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
130+
"tax_name": "tax name",
131+
"tax_code": "tax_code",
132+
"tax_rate": "20.0",
133+
"tax_description": "tax description",
134+
"amount_cents": 100,
135+
"amount_currency": "EUR",
136+
"created_at": "2022-04-29T08:59:51Z"
137+
}
138+
]
139+
}
140+
],
141+
"credits": []
142+
}
143+
}

spec/lago/api/resources/invoice_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,56 @@
316316
expect(result.payment_url).to eq('https://example.com')
317317
end
318318
end
319+
320+
describe '#preview' do
321+
subject { resource.preview(params) }
322+
323+
let(:params) do
324+
{
325+
customer: {
326+
external_id: '_ID_'
327+
},
328+
plan_code: 'plan_code',
329+
coupons: [
330+
{
331+
code: 'coupon_preview'
332+
}
333+
]
334+
}
335+
end
336+
337+
context 'when invoice preview is created' do
338+
let(:invoice_response) { load_fixture('invoice_preview') }
339+
340+
before do
341+
stub_request(:post, 'https://api.getlago.com/api/v1/invoices/preview')
342+
.with(body: params)
343+
.to_return(body: invoice_response, status: 200)
344+
end
345+
346+
it 'returns invoice' do
347+
expect(subject).to have_attributes(lago_id: nil)
348+
end
349+
end
350+
351+
context 'when invoice preview is not created' do
352+
let(:error_response) do
353+
{
354+
'status' => 404,
355+
'error' => 'Not Found',
356+
'message' => 'plan_not_found',
357+
}.to_json
358+
end
359+
360+
before do
361+
stub_request(:post, 'https://api.getlago.com/api/v1/invoices/preview')
362+
.with(body: params)
363+
.to_return(body: error_response, status: 404)
364+
end
365+
366+
it 'raises an error' do
367+
expect { subject }.to raise_error(Lago::Api::HttpError)
368+
end
369+
end
370+
end
319371
end

0 commit comments

Comments
 (0)