From c1951090d1c982b785b39b675267427b692d089f Mon Sep 17 00:00:00 2001 From: Ancor Cruz Date: Wed, 4 Sep 2024 11:25:44 +0100 Subject: [PATCH 1/5] update Gemfile.lock --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 597e57e..fda23cd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - lago-ruby-client (1.9.0) + lago-ruby-client (1.10.0) jwt openssl From 1b80c46011aa84c23b800e43756cced3925abf81 Mon Sep 17 00:00:00 2001 From: Ancor Cruz Date: Wed, 4 Sep 2024 11:25:58 +0100 Subject: [PATCH 2/5] update payment_request json fixture --- spec/fixtures/api/payment_request.json | 76 ++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 11 deletions(-) diff --git a/spec/fixtures/api/payment_request.json b/spec/fixtures/api/payment_request.json index 9fb6e30..5a2ec63 100644 --- a/spec/fixtures/api/payment_request.json +++ b/spec/fixtures/api/payment_request.json @@ -2,17 +2,19 @@ "payment_request": { "lago_id": "89b6b61e-4dbc-4307-ac96-4abcfa9e3e2d", "email": "gavin@overdue.test", - "amount_cents": 120, + "amount_cents": 19955, "amount_currency": "EUR", + "payment_status": "pending", "created_at": "2024-06-30T10:59:51Z", "customer": { "lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", - "external_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "external_id": "gavin_001", "address_line1": "5230 Penfield Ave", - "address_line2": null, + "address_line2": "fzufuzfuz", "city": "Woodland Hills", "country": "US", "created_at": "2022-04-29T08:59:51Z", + "updated_at": "2022-04-29T08:59:51Z", "email": "dinesh@piedpiper.test", "legal_name": "Coleman-Blair", "legal_number": "49-008-2965", @@ -25,16 +27,41 @@ "currency": "EUR", "timezone": "Europe/Paris", "applicable_timezone": "Europe/Paris", + "net_payment_term": 30, + "tax_identification_number": "EU123456789", + "sequential_id": 101, + "slug": "customer_101_slug", + "external_salesforce_id": "salesforce_999", + "finalize_zero_amount_invoice": "skip", "billing_configuration": { + "invoice_grace_period": 3, "payment_provider": "stripe", "payment_provider_code": "stripe-eu-1", - "provider_customer_id": "cus_12345" + "provider_customer_id": "cus_12345", + "sync_with_provider": true, + "document_locale": "fr", + "provider_payment_methods": ["card"] }, - "metadata": [] + "shipping_address": { + "address_line1": "5230 Penfield Ave", + "city": "Woodland Hills", + "zipcode": "91364", + "state": "CA", + "country": "US" + }, + "metadata": [ + { + "lago_id": "7317916c-b64b-45df-8bfd-2fc80ed1679d", + "key": "lead_name", + "value": "John Doe", + "display_in_invoice": true, + "created_at": "2022-04-29T08:59:51Z" + } + ] }, "invoices": [ { - "lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90", + "lago_id": "f8e194df-5d90-4382-b146-c881d2c67f28", "sequential_id": 15, "number": "LAG-1234-001-002", "issuing_date": "2022-06-02", @@ -47,14 +74,41 @@ "payment_status": "pending", "currency": "EUR", "net_payment_term": 0, - "fees_amount_cents": 100, - "taxes_amount_cents": 20, + "fees_amount_cents": 10000, + "taxes_amount_cents": 2000, + "coupons_amount_cents": 0, + "credit_notes_amount_cents": 0, + "sub_total_excluding_taxes_amount_cents": 10000, + "sub_total_including_taxes_amount_cents": 12000, + "prepaid_credit_amount_cents": 0, + "total_amount_cents": 12000, + "progressive_billing_credit_amount_cents": 0, + "file_url": "https://lago-files/invoice_002.pdf" + }, + { + "lago_id": "a20b1805-d54c-4e57-873d-721cc153035e", + "sequential_id": 22, + "number": "LAG-1234-009-012", + "issuing_date": "2022-07-08", + "payment_dispute_lost_at": null, + "payment_due_date": "2022-07-08", + "payment_overdue": true, + "invoice_type": "one_off", + "version_number": 3, + "status": "finalized", + "payment_status": "failed", + "currency": "EUR", + "net_payment_term": 0, + "fees_amount_cents": 7000, + "taxes_amount_cents": 955, "coupons_amount_cents": 0, "credit_notes_amount_cents": 0, - "sub_total_excluding_taxes_amount_cents": 100, - "sub_total_including_taxes_amount_cents": 120, + "sub_total_excluding_taxes_amount_cents": 7000, + "sub_total_including_taxes_amount_cents": 7955, "prepaid_credit_amount_cents": 0, - "total_amount_cents": 120 + "total_amount_cents": 7955, + "progressive_billing_credit_amount_cents": 0, + "file_url": "https://lago-files/invoice_012.pdf" } ] } From f87fc4450e27b608c03a6568279b4567ada8f9a4 Mon Sep 17 00:00:00 2001 From: Ancor Cruz Date: Wed, 4 Sep 2024 11:26:14 +0100 Subject: [PATCH 3/5] Add payment request create factory --- spec/factories/payment_request.rb | 123 ++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 spec/factories/payment_request.rb diff --git a/spec/factories/payment_request.rb b/spec/factories/payment_request.rb new file mode 100644 index 0000000..3575b20 --- /dev/null +++ b/spec/factories/payment_request.rb @@ -0,0 +1,123 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :create_payment_request, class: OpenStruct do + lago_id { "89b6b61e-4dbc-4307-ac96-4abcfa9e3e2d" } + email { "gavin@overdue.test" } + amount_cents { 199_55 } + amount_currency { "EUR" } + payment_status { "pending" } + created_at { "2024-06-30T10:59:51Z" } + + customer do + { + lago_id: "1a901a90-1a90-1a90-1a90-1a901a901a90", + external_id: "gavin_001", + name: "Gavin Belson", + country: "US", + address_line1: "5230 Penfield Ave", + address_line2: "fzufuzfuz", + state: "CA", + zipcode: "91364", + email: "dinesh@piedpiper.test", + city: "Woodland Hills", + url: "http://hooli.com", + phone: "1-171-883-3711 x245", + logo_url: "http://hooli.com/logo.png", + legal_name: "Coleman-Blair", + legal_number: "49-008-2965", + net_payment_term: 30, + tax_identification_number: "EU123456789", + currency: "EUR", + timezone: "Europe/Paris", + sequential_id: 101, + slug: "customer_101_slug", + created_at: "2022-04-29T08:59:51Z", + updated_at: "2022-04-29T08:59:51Z", + applicable_timezone: "UTC", + external_salesforce_id: "salesforce_999", + finalize_zero_amount_invoice: "skip", + billing_configuration: { + invoice_grace_period: 3, + payment_provider: "stripe", + payment_provider_code: "stripe-eu-1", + provider_customer_id: "cus_12345", + sync_with_provider: true, + document_locale: "fr", + provider_payment_methods: ["card"] + }, + shipping_address: { + address_line1: "5230 Penfield Ave", + city: "Woodland Hills", + zipcode: "91364", + state: "CA", + country: "US" + }, + metadata: [ + { + lago_id: "7317916c-b64b-45df-8bfd-2fc80ed1679d", + key: "lead_name", + value: "John Doe", + display_in_invoice: true, + "created_at": "2022-04-29T08:59:51Z" + } + ] + } + end + + invoices do + [ + { + lago_id: "f8e194df-5d90-4382-b146-c881d2c67f28", + 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_00, + taxes_amount_cents: 20_00, + coupons_amount_cents: 0, + credit_notes_amount_cents: 0, + sub_total_excluding_taxes_amount_cents: 100_00, + sub_total_including_taxes_amount_cents: 120_00, + prepaid_credit_amount_cents: 0, + total_amount_cents: 120_00, + progressive_billing_credit_amount_cents: 0, + file_url: "https://lago-files/invoice_002.pdf", + }, + { + lago_id: "a20b1805-d54c-4e57-873d-721cc153035e", + sequential_id: 22, + number: "LAG-1234-009-012", + issuing_date: "2022-07-08", + payment_dispute_lost_at: nil, + payment_due_date: "2022-07-08", + payment_overdue: true, + invoice_type: "one_off", + version_number: 3, + status: "finalized", + payment_status: "failed", + currency: "EUR", + net_payment_term: 0, + fees_amount_cents: 70_00, + taxes_amount_cents: 9_55, + coupons_amount_cents: 0, + credit_notes_amount_cents: 0, + sub_total_excluding_taxes_amount_cents: 70_00, + sub_total_including_taxes_amount_cents: 79_55, + prepaid_credit_amount_cents: 0, + total_amount_cents: 79_55, + progressive_billing_credit_amount_cents: 0, + file_url: "https://lago-files/invoice_012.pdf", + } + ] + end + end +end From 509306c1b5bd4a6df8fbca1ca918ca526d38ac38 Mon Sep 17 00:00:00 2001 From: Ancor Cruz Date: Wed, 4 Sep 2024 11:26:27 +0100 Subject: [PATCH 4/5] Add test for payment request create endpoint --- .../api/resources/payment_request_spec.rb | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/spec/lago/api/resources/payment_request_spec.rb b/spec/lago/api/resources/payment_request_spec.rb index 3445bf4..770a346 100644 --- a/spec/lago/api/resources/payment_request_spec.rb +++ b/spec/lago/api/resources/payment_request_spec.rb @@ -71,4 +71,56 @@ end end end + + describe "#create" do + let(:params) { create(:create_payment_request).to_h } + + context "when payment request is successfully created" do + before do + stub_request(:post, "https://api.getlago.com/api/v1/payment_requests") + .with(body: { payment_request: params }) + .to_return(body: payment_request_response, status: 200) + end + + it "returns a payment request", :aggregate_failures do + payment_request = resource.create(params) + + expect(payment_request.customer[:external_id]).to eq("1a901a90-1a90-1a90-1a90-1a901a901a90") + expect(payment_request.customer[:name]).to eq("Gavin Belson") + expect(payment_request.customer[:currency]).to eq("EUR") + expect(payment_request.customer[:net_payment_term]).to eq(nil) + expect(payment_request.customer[:tax_identification_number]).to eq("EU123456789") + expect(payment_request.customer[:billing_configuration].invoice_grace_period).to eq(3) + expect(payment_request.customer[:billing_configuration].provider_customer_id).to eq("cus_12345") + expect(payment_request.customer[:billing_configuration].provider_payment_methods).to eq(["card"]) + expect(payment_request.customer[:shipping_address].city).to eq("Woodland Hills") + expect(payment_request.customer[:shipping_address].country).to eq("US") + expect(payment_request.customer[:integration_customers].first.external_customer_id).to eq("123456789") + expect(payment_request.customer[:integration_customers].first.type).to eq("netsuite") + expect(payment_request.customer[:metadata].first.key).to eq("key") + expect(payment_request.customer[:metadata].first.value).to eq("value") + expect(payment_request.customer[:taxes].map(&:code)).to eq(["tax_code"]) + end + end + + context "when payment request is not successfully created" do + let(:response) do + { + "status" => 422, + "error" => "Unprocessable Entity", + "message" => "Validation error on the record", + }.to_json + end + + before do + stub_request(:post, "https://api.getlago.com/api/v1/payment_requests") + .with(body: { payment_request: params }) + .to_return(body: response, status: 422) + end + + it "raises an error" do + expect { resource.create(params) }.to raise_error(Lago::Api::HttpError) + end + end + end end From 36194982ee3f177f92d4b384b2d3781d5916cc34 Mon Sep 17 00:00:00 2001 From: Ancor Cruz Date: Wed, 4 Sep 2024 12:12:27 +0100 Subject: [PATCH 5/5] feat(dunning): Add create payment requests endpoint --- lib/lago/api/resources/payment_request.rb | 10 ++ spec/factories/payment_request.rb | 118 +----------------- .../api/resources/payment_request_spec.rb | 26 ++-- 3 files changed, 25 insertions(+), 129 deletions(-) diff --git a/lib/lago/api/resources/payment_request.rb b/lib/lago/api/resources/payment_request.rb index 37b2376..577e93f 100644 --- a/lib/lago/api/resources/payment_request.rb +++ b/lib/lago/api/resources/payment_request.rb @@ -11,6 +11,16 @@ def api_resource def root_name 'payment_request' end + + def whitelist_params(params) + result_hash = { + email: params[:email], + external_customer_id: params[:external_customer_id], + lago_invoice_ids: params[:lago_invoice_ids] + }.compact + + { root_name => result_hash } + end end end end diff --git a/spec/factories/payment_request.rb b/spec/factories/payment_request.rb index 3575b20..baac651 100644 --- a/spec/factories/payment_request.rb +++ b/spec/factories/payment_request.rb @@ -2,122 +2,8 @@ FactoryBot.define do factory :create_payment_request, class: OpenStruct do - lago_id { "89b6b61e-4dbc-4307-ac96-4abcfa9e3e2d" } email { "gavin@overdue.test" } - amount_cents { 199_55 } - amount_currency { "EUR" } - payment_status { "pending" } - created_at { "2024-06-30T10:59:51Z" } - - customer do - { - lago_id: "1a901a90-1a90-1a90-1a90-1a901a901a90", - external_id: "gavin_001", - name: "Gavin Belson", - country: "US", - address_line1: "5230 Penfield Ave", - address_line2: "fzufuzfuz", - state: "CA", - zipcode: "91364", - email: "dinesh@piedpiper.test", - city: "Woodland Hills", - url: "http://hooli.com", - phone: "1-171-883-3711 x245", - logo_url: "http://hooli.com/logo.png", - legal_name: "Coleman-Blair", - legal_number: "49-008-2965", - net_payment_term: 30, - tax_identification_number: "EU123456789", - currency: "EUR", - timezone: "Europe/Paris", - sequential_id: 101, - slug: "customer_101_slug", - created_at: "2022-04-29T08:59:51Z", - updated_at: "2022-04-29T08:59:51Z", - applicable_timezone: "UTC", - external_salesforce_id: "salesforce_999", - finalize_zero_amount_invoice: "skip", - billing_configuration: { - invoice_grace_period: 3, - payment_provider: "stripe", - payment_provider_code: "stripe-eu-1", - provider_customer_id: "cus_12345", - sync_with_provider: true, - document_locale: "fr", - provider_payment_methods: ["card"] - }, - shipping_address: { - address_line1: "5230 Penfield Ave", - city: "Woodland Hills", - zipcode: "91364", - state: "CA", - country: "US" - }, - metadata: [ - { - lago_id: "7317916c-b64b-45df-8bfd-2fc80ed1679d", - key: "lead_name", - value: "John Doe", - display_in_invoice: true, - "created_at": "2022-04-29T08:59:51Z" - } - ] - } - end - - invoices do - [ - { - lago_id: "f8e194df-5d90-4382-b146-c881d2c67f28", - 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_00, - taxes_amount_cents: 20_00, - coupons_amount_cents: 0, - credit_notes_amount_cents: 0, - sub_total_excluding_taxes_amount_cents: 100_00, - sub_total_including_taxes_amount_cents: 120_00, - prepaid_credit_amount_cents: 0, - total_amount_cents: 120_00, - progressive_billing_credit_amount_cents: 0, - file_url: "https://lago-files/invoice_002.pdf", - }, - { - lago_id: "a20b1805-d54c-4e57-873d-721cc153035e", - sequential_id: 22, - number: "LAG-1234-009-012", - issuing_date: "2022-07-08", - payment_dispute_lost_at: nil, - payment_due_date: "2022-07-08", - payment_overdue: true, - invoice_type: "one_off", - version_number: 3, - status: "finalized", - payment_status: "failed", - currency: "EUR", - net_payment_term: 0, - fees_amount_cents: 70_00, - taxes_amount_cents: 9_55, - coupons_amount_cents: 0, - credit_notes_amount_cents: 0, - sub_total_excluding_taxes_amount_cents: 70_00, - sub_total_including_taxes_amount_cents: 79_55, - prepaid_credit_amount_cents: 0, - total_amount_cents: 79_55, - progressive_billing_credit_amount_cents: 0, - file_url: "https://lago-files/invoice_012.pdf", - } - ] - end + external_customer_id { "gavin_001" } + lago_invoice_ids { ["f8e194df-5d90-4382-b146-c881d2c67f28", "a20b1805-d54c-4e57-873d-721cc153035e"] } end end diff --git a/spec/lago/api/resources/payment_request_spec.rb b/spec/lago/api/resources/payment_request_spec.rb index 770a346..627ae10 100644 --- a/spec/lago/api/resources/payment_request_spec.rb +++ b/spec/lago/api/resources/payment_request_spec.rb @@ -85,21 +85,21 @@ it "returns a payment request", :aggregate_failures do payment_request = resource.create(params) - expect(payment_request.customer[:external_id]).to eq("1a901a90-1a90-1a90-1a90-1a901a901a90") + expect(payment_request.lago_id).to eq("89b6b61e-4dbc-4307-ac96-4abcfa9e3e2d") + expect(payment_request.email).to eq("gavin@overdue.test") + expect(payment_request.amount_cents).to eq(199_55) + expect(payment_request.amount_currency).to eq("EUR") + expect(payment_request.payment_status).to eq("pending") + expect(payment_request.created_at).to eq("2024-06-30T10:59:51Z") + + expect(payment_request.customer[:lago_id]).to eq("1a901a90-1a90-1a90-1a90-1a901a901a90") + expect(payment_request.customer[:external_id]).to eq("gavin_001") expect(payment_request.customer[:name]).to eq("Gavin Belson") expect(payment_request.customer[:currency]).to eq("EUR") - expect(payment_request.customer[:net_payment_term]).to eq(nil) - expect(payment_request.customer[:tax_identification_number]).to eq("EU123456789") - expect(payment_request.customer[:billing_configuration].invoice_grace_period).to eq(3) - expect(payment_request.customer[:billing_configuration].provider_customer_id).to eq("cus_12345") - expect(payment_request.customer[:billing_configuration].provider_payment_methods).to eq(["card"]) - expect(payment_request.customer[:shipping_address].city).to eq("Woodland Hills") - expect(payment_request.customer[:shipping_address].country).to eq("US") - expect(payment_request.customer[:integration_customers].first.external_customer_id).to eq("123456789") - expect(payment_request.customer[:integration_customers].first.type).to eq("netsuite") - expect(payment_request.customer[:metadata].first.key).to eq("key") - expect(payment_request.customer[:metadata].first.value).to eq("value") - expect(payment_request.customer[:taxes].map(&:code)).to eq(["tax_code"]) + + expect(payment_request.invoices.size).to eq(2) + expect(payment_request.invoices.first[:lago_id]).to eq("f8e194df-5d90-4382-b146-c881d2c67f28") + expect(payment_request.invoices.last[:lago_id]).to eq("a20b1805-d54c-4e57-873d-721cc153035e") end end