Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(invoice-preview): add support for invoice preview #237

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/lago/api/resources/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ def payment_url(invoice_id)
JSON.parse(response.to_json, object_class: OpenStruct)
end

def preview(params)
path = "/api/v1/invoices/preview"
payload = params.slice(:customer, :plan_code, :subscription_at, :billing_time, :coupons)
response = connection.post(payload, path)[root_name]

JSON.parse(response.to_json, object_class: OpenStruct)
end

def whitelist_params(params)
result = {
payment_status: params[:payment_status],
Expand Down
143 changes: 143 additions & 0 deletions spec/fixtures/api/invoice_preview.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"invoice": {
"lago_id": null,
"sequential_id": null,
"number": "",
"issuing_date": "2022-06-02",
"payment_dispute_lost_at": null,
"payment_due_date": "2022-06-02",
"payment_overdue": false,
"invoice_type": "one_off",
"version_number": 2,
"status": "finalized",
"payment_status": "pending",
"currency": "EUR",
"net_payment_term": 0,
"fees_amount_cents": 100,
"taxes_amount_cents": 20,
"coupons_amount_cents": 0,
"credit_notes_amount_cents": 0,
"sub_total_excluding_taxes_amount_cents": 100,
"sub_total_including_taxes_amount_cents": 120,
"prepaid_credit_amount_cents": 0,
"total_amount_cents": 120,
"metadata": [
{
"key": "key",
"value": "value",
"display_in_invoice": true
}
],
"applied_taxes": [
{
"lago_id": null,
"lago_invoice_id": null,
"lago_tax_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"tax_name": "tax name",
"tax_code": "tax_code",
"tax_rate": "20.0",
"tax_description": "tax description",
"amount_cents": 100,
"amount_currency": "EUR",
"fees_amount_cents": 500,
"created_at": "2022-04-29T08:59:51Z"
}
],
"customer": {
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"external_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"address_line1": "5230 Penfield Ave",
"address_line2": null,
"city": "Woodland Hills",
"country": "US",
"created_at": "2022-04-29T08:59:51Z",
"email": "[email protected]",
"legal_name": "Coleman-Blair",
"legal_number": "49-008-2965",
"logo_url": "http://hooli.com/logo.png",
"name": "Gavin Belson",
"phone": "1-171-883-3711 x245",
"state": "CA",
"url": "http://hooli.com",
"zipcode": "91364",
"currency": "EUR",
"timezone": "Europe/Paris",
"applicable_timezone": "Europe/Paris",
"billing_configuration": {
"payment_provider": "stripe",
"payment_provider_code": "stripe-eu-1",
"provider_customer_id": "cus_12345"
},
"taxes": [
{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"name": "tax_name",
"code": "tax_code",
"rate": 15.0,
"description": "tax_desc",
"add_ons_count": 0,
"customers_count": 0,
"plans_count": 0,
"charges_count": 0,
"applied_to_organization": false,
"created_at": "2022-04-29T08:59:51Z"
}
]
},
"subscriptions": [
{
"lago_id": null,
"lago_customer_id": null,
"external_customer_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"canceled_at": "2022-04-29T08:59:51Z",
"created_at": "2022-04-29T08:59:51Z",
"plan_code": "eartha lynch",
"started_at": "2022-04-29T08:59:51Z",
"status": "active",
"billing_time": "anniversary",
"terminated_at": null,
"subscription_at": "2022-04-29T12:27:30Z",
"previous_plan_code": null,
"next_plan_code": null,
"downgrade_plan_date": null,
"current_billing_period_started_at": "2022-04-29T08:59:51Z",
"current_billing_period_ending_at": "2022-05-29T23:59:59Z"
}
],
"fees": [
{
"lago_id": null,
"lago_charge_id": null,
"item": {
"type": "charge",
"code": "seats",
"name": "User Seats",
"invoice_display_name": "User Seats on Invoice"
},
"amount_cents": 100,
"amount_currency": "EUR",
"taxes_amount_cents": 20,
"taxes_rate": "20.0",
"total_amount_cents": 120,
"total_amount_currency": "EUR",
"units": 12.6,
"events_count": 4,
"applied_taxes": [
{
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"lago_fee_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"lago_tax_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"tax_name": "tax name",
"tax_code": "tax_code",
"tax_rate": "20.0",
"tax_description": "tax description",
"amount_cents": 100,
"amount_currency": "EUR",
"created_at": "2022-04-29T08:59:51Z"
}
]
}
],
"credits": []
}
}
52 changes: 52 additions & 0 deletions spec/lago/api/resources/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,56 @@
expect(result.payment_url).to eq('https://example.com')
end
end

describe '#preview' do
subject { resource.preview(params) }

let(:params) do
{
customer: {
external_id: '_ID_'
},
plan_code: 'plan_code',
coupons: [
{
code: 'coupon_preview'
}
]
}
end

context 'when invoice preview is created' do
let(:invoice_response) { load_fixture('invoice_preview') }

before do
stub_request(:post, 'https://api.getlago.com/api/v1/invoices/preview')
.with(body: params)
.to_return(body: invoice_response, status: 200)
end

it 'returns invoice' do
expect(subject).to have_attributes(lago_id: nil)
end
end

context 'when invoice preview is not created' do
let(:error_response) do
{
'status' => 404,
'error' => 'Not Found',
'message' => 'plan_not_found',
}.to_json
end

before do
stub_request(:post, 'https://api.getlago.com/api/v1/invoices/preview')
.with(body: params)
.to_return(body: error_response, status: 404)
end

it 'raises an error' do
expect { subject }.to raise_error(Lago::Api::HttpError)
end
end
end
end
Loading