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 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 new file mode 100644 index 0000000..baac651 --- /dev/null +++ b/spec/factories/payment_request.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :create_payment_request, class: OpenStruct do + email { "gavin@overdue.test" } + external_customer_id { "gavin_001" } + lago_invoice_ids { ["f8e194df-5d90-4382-b146-c881d2c67f28", "a20b1805-d54c-4e57-873d-721cc153035e"] } + end +end 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" } ] } diff --git a/spec/lago/api/resources/payment_request_spec.rb b/spec/lago/api/resources/payment_request_spec.rb index 3445bf4..627ae10 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.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.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 + + 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