From d9927ee84dbb3d0d10e16b218c255064a46bb098 Mon Sep 17 00:00:00 2001 From: Ancor Cruz Date: Mon, 19 Aug 2024 15:20:45 +0100 Subject: [PATCH] feat(dunning): Add payment requests endpoint (get all) (#205) --- lib/lago-ruby-client.rb | 1 + lib/lago/api/resources/payment_request.rb | 17 +++++ spec/fixtures/api/payment_request.json | 61 +++++++++++++++ .../api/resources/payment_request_spec.rb | 74 +++++++++++++++++++ spec/spec_helper.rb | 28 +------ 5 files changed, 154 insertions(+), 27 deletions(-) create mode 100644 lib/lago/api/resources/payment_request.rb create mode 100644 spec/fixtures/api/payment_request.json create mode 100644 spec/lago/api/resources/payment_request_spec.rb diff --git a/lib/lago-ruby-client.rb b/lib/lago-ruby-client.rb index add8264..c97d11b 100644 --- a/lib/lago-ruby-client.rb +++ b/lib/lago-ruby-client.rb @@ -27,6 +27,7 @@ require 'lago/api/resources/mrr' require 'lago/api/resources/organization' require 'lago/api/resources/overdue_balance' +require 'lago/api/resources/payment_request' require 'lago/api/resources/plan' require 'lago/api/resources/subscription' require 'lago/api/resources/tax' diff --git a/lib/lago/api/resources/payment_request.rb b/lib/lago/api/resources/payment_request.rb new file mode 100644 index 0000000..37b2376 --- /dev/null +++ b/lib/lago/api/resources/payment_request.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Lago + module Api + module Resources + class PaymentRequest < Base + def api_resource + 'payment_requests' + end + + def root_name + 'payment_request' + end + end + end + end +end diff --git a/spec/fixtures/api/payment_request.json b/spec/fixtures/api/payment_request.json new file mode 100644 index 0000000..9fb6e30 --- /dev/null +++ b/spec/fixtures/api/payment_request.json @@ -0,0 +1,61 @@ +{ + "payment_request": { + "lago_id": "89b6b61e-4dbc-4307-ac96-4abcfa9e3e2d", + "email": "gavin@overdue.test", + "amount_cents": 120, + "amount_currency": "EUR", + "created_at": "2024-06-30T10: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": "dinesh@piedpiper.test", + "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" + }, + "metadata": [] + }, + "invoices": [ + { + "lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "sequential_id": 15, + "number": "LAG-1234-001-002", + "issuing_date": "2022-06-02", + "payment_dispute_lost_at": "2022-04-29T08:59:51Z", + "payment_due_date": "2022-06-02", + "payment_overdue": true, + "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 + } + ] + } +} diff --git a/spec/lago/api/resources/payment_request_spec.rb b/spec/lago/api/resources/payment_request_spec.rb new file mode 100644 index 0000000..3445bf4 --- /dev/null +++ b/spec/lago/api/resources/payment_request_spec.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Lago::Api::Resources::PaymentRequest do + subject(:resource) { described_class.new(client) } + + let(:client) { Lago::Api::Client.new } + + let(:payment_request_response) { load_fixture('payment_request') } + let(:payment_request_id) { JSON.parse(payment_request_response)['payment_request']['lago_id'] } + + let(:error_response) do + { + 'status' => 422, + 'error' => 'Unprocessable Entity', + 'message' => 'Validation error on the record', + }.to_json + end + + describe '#get_all' do + let(:payment_requests_response) do + { + 'payment_requests' => [JSON.parse(payment_request_response)['payment_request']], + 'meta': { + 'current_page' => 1, + 'next_page' => 2, + 'prev_page' => nil, + 'total_pages' => 7, + 'total_count' => 63, + }, + }.to_json + end + + context 'when there is no options' do + before do + stub_request(:get, 'https://api.getlago.com/api/v1/payment_requests') + .to_return(body: payment_requests_response, status: 200) + end + + it 'returns payment requests on the first page' do + response = resource.get_all + + expect(response['payment_requests'].first['lago_id']).to eq(payment_request_id) + expect(response['meta']['current_page']).to eq(1) + end + end + + context 'when options are present' do + before do + stub_request(:get, 'https://api.getlago.com/api/v1/payment_requests?per_page=2&page=1') + .to_return(body: payment_requests_response, status: 200) + end + + it 'returns payment requests on selected page' do + response = resource.get_all({ per_page: 2, page: 1 }) + + expect(response['payment_requests'].first['lago_id']).to eq(payment_request_id) + expect(response['meta']['current_page']).to eq(1) + end + end + + context 'when there is an issue' do + before do + stub_request(:get, 'https://api.getlago.com/api/v1/payment_requests') + .to_return(body: error_response, status: 422) + end + + it 'raises an error' do + expect { resource.get_all }.to raise_error(Lago::Api::HttpError) + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 059945e..1509cd4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,33 +3,7 @@ require 'debug' require 'factory_bot' -require 'lago/version' -require 'lago/api/client' -require 'lago/api/connection' -require 'lago/api/http_error' -require 'lago/api/resources/base' -require 'lago/api/resources/add_on' -require 'lago/api/resources/applied_coupon' -require 'lago/api/resources/billable_metric' -require 'lago/api/resources/coupon' -require 'lago/api/resources/credit_note' -require 'lago/api/resources/customer' -require 'lago/api/resources/event' -require 'lago/api/resources/fee' -require 'lago/api/resources/gross_revenue' -require 'lago/api/resources/invoice' -require 'lago/api/resources/invoice_collection' -require 'lago/api/resources/invoiced_usage' -require 'lago/api/resources/mrr' -require 'lago/api/resources/organization' -require 'lago/api/resources/overdue_balance' -require 'lago/api/resources/plan' -require 'lago/api/resources/subscription' -require 'lago/api/resources/tax' -require 'lago/api/resources/wallet' -require 'lago/api/resources/wallet_transaction' -require 'lago/api/resources/webhook' -require 'lago/api/resources/webhook_endpoint' +require 'lago-ruby-client' require 'webmock/rspec'