From 4ff356df1d5781a3c0fbe240220ee00b6d3f375e Mon Sep 17 00:00:00 2001 From: Igor Fernandes Date: Mon, 19 May 2025 15:04:14 -0300 Subject: [PATCH 1/2] Add list invoices support --- lib/chargebeex/invoice/invoice.ex | 13 +++++++++ test/chargebeex/invoice_test.exs | 44 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/lib/chargebeex/invoice/invoice.ex b/lib/chargebeex/invoice/invoice.ex index 781ab1a..75c9923 100644 --- a/lib/chargebeex/invoice/invoice.ex +++ b/lib/chargebeex/invoice/invoice.ex @@ -76,4 +76,17 @@ defmodule Chargebeex.Invoice do opts ) end + + @doc """ + Allows to list Invoices + + Available filters can be found here: https://apidocs.chargebee.com/docs/api/invoices?lang=curl#list_invoices + + ## Examples + + iex> filters = %{limit: 2} + iex(2)> Chargebeex.Invoice.list(filters) + {:ok, [%Chargebeex.Invoice{...}, %Chargebeex.Invoice{...}], %{"next_offset" => nil}} + """ + def list(params), do: super(params) end diff --git a/test/chargebeex/invoice_test.exs b/test/chargebeex/invoice_test.exs index 0b98964..3ae8c39 100644 --- a/test/chargebeex/invoice_test.exs +++ b/test/chargebeex/invoice_test.exs @@ -8,6 +8,50 @@ defmodule Chargebeex.InvoiceTest do setup :verify_on_exit! + describe ".list/1" do + test "fails for invalid authentication data" do + unauthorized = Common.unauthorized() + + expect( + Chargebeex.HTTPClientMock, + :get, + fn url, body, headers -> + assert url == "https://test-namespace.chargebee.com/api/v2/invoices" + assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}] + assert body == "" + + {:ok, 401, [], Jason.encode!(unauthorized)} + end + ) + + assert {:error, 401, [], ^unauthorized} = Invoice.list() + end + + test "allows filters" do + expect( + Chargebeex.HTTPClientMock, + :get, + fn url, body, headers -> + assert url == + "https://test-namespace.chargebee.com/api/v2/invoices?limit=1&status%5Bis%5D=not_paid" + + assert headers == [{"Authorization", "Basic dGVzdF9jaGFyZ2VlYmVlX2FwaV9rZXk6"}] + assert body == "" + + result = %{ + list: [%{invoice: %{id: 0000}}], + next_offset: "foobar" + } + + {:ok, 200, [], Jason.encode!(result)} + end + ) + + assert {:ok, [%Invoice{}], %{"next_offset" => "foobar"}} = + Invoice.list(%{"limit" => 1, "status[is]" => "not_paid"}) + end + end + describe "create_for_charge_items_and_charges" do test "with bad authentication should fail" do unauthorized = Common.unauthorized() From 68dc4d1ebcc1d761f238161ab97aee5345f3d40b Mon Sep 17 00:00:00 2001 From: Igor Fernandes Date: Mon, 19 May 2025 15:19:59 -0300 Subject: [PATCH 2/2] refactor --- lib/chargebeex/invoice/invoice.ex | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/chargebeex/invoice/invoice.ex b/lib/chargebeex/invoice/invoice.ex index 75c9923..781ab1a 100644 --- a/lib/chargebeex/invoice/invoice.ex +++ b/lib/chargebeex/invoice/invoice.ex @@ -76,17 +76,4 @@ defmodule Chargebeex.Invoice do opts ) end - - @doc """ - Allows to list Invoices - - Available filters can be found here: https://apidocs.chargebee.com/docs/api/invoices?lang=curl#list_invoices - - ## Examples - - iex> filters = %{limit: 2} - iex(2)> Chargebeex.Invoice.list(filters) - {:ok, [%Chargebeex.Invoice{...}, %Chargebeex.Invoice{...}], %{"next_offset" => nil}} - """ - def list(params), do: super(params) end