Skip to content

Commit

Permalink
Merge pull request #213 from getlago/feat/dunning-create-payment-requ…
Browse files Browse the repository at this point in the history
…ests

feat(dunning): Add create payment requests endpoint
  • Loading branch information
ancorcruz authored Sep 4, 2024
2 parents dc40de9 + 3619498 commit 6abee8c
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
lago-ruby-client (1.9.0)
lago-ruby-client (1.10.0)
jwt
openssl

Expand Down
10 changes: 10 additions & 0 deletions lib/lago/api/resources/payment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/factories/payment_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

FactoryBot.define do
factory :create_payment_request, class: OpenStruct do
email { "[email protected]" }
external_customer_id { "gavin_001" }
lago_invoice_ids { ["f8e194df-5d90-4382-b146-c881d2c67f28", "a20b1805-d54c-4e57-873d-721cc153035e"] }
end
end
76 changes: 65 additions & 11 deletions spec/fixtures/api/payment_request.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
"payment_request": {
"lago_id": "89b6b61e-4dbc-4307-ac96-4abcfa9e3e2d",
"email": "[email protected]",
"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": "[email protected]",
"legal_name": "Coleman-Blair",
"legal_number": "49-008-2965",
Expand All @@ -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",
Expand All @@ -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"
}
]
}
Expand Down
52 changes: 52 additions & 0 deletions spec/lago/api/resources/payment_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]")
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

0 comments on commit 6abee8c

Please sign in to comment.