From d73dbcfdde8a71545aecffb0fe5219fa8ad7f8f4 Mon Sep 17 00:00:00 2001 From: Nick Kezhaya Date: Fri, 20 Mar 2020 12:15:45 -0500 Subject: [PATCH] Added amount_refunded to Charge --- lib/stripe_mock/api/charge.ex | 1 + lib/stripe_mock/api/refund.ex | 14 ++++++++++++++ lib/stripe_mock_web/views/charge_view.ex | 2 +- .../migrations/20191015221147_create_tables.exs | 1 + .../controllers/charge_controller_test.exs | 1 + .../controllers/refund_controller_test.exs | 5 +++++ 6 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/stripe_mock/api/charge.ex b/lib/stripe_mock/api/charge.ex index e0e393f..66d4b97 100644 --- a/lib/stripe_mock/api/charge.ex +++ b/lib/stripe_mock/api/charge.ex @@ -3,6 +3,7 @@ defmodule StripeMock.API.Charge do schema "charges" do field :amount, :integer + field :amount_refunded, :integer, default: 0 field :capture, :boolean, virtual: true, default: false field :captured, :boolean, default: false field :currency, :string diff --git a/lib/stripe_mock/api/refund.ex b/lib/stripe_mock/api/refund.ex index 1780d96..20e05a9 100644 --- a/lib/stripe_mock/api/refund.ex +++ b/lib/stripe_mock/api/refund.ex @@ -21,6 +21,7 @@ defmodule StripeMock.API.Refund do |> validate_required(:amount) |> validate_amount() |> validate_reason() + |> update_charge() |> put_common_fields() end @@ -75,4 +76,17 @@ defmodule StripeMock.API.Refund do _ -> nil end end + + defp update_charge(changeset) do + prepare_changes(changeset, fn prepared_changeset -> + if charge_id = get_field(prepared_changeset, :charge_id) do + amount = get_field(prepared_changeset, :amount) + + from(c in API.Charge, where: c.id == ^charge_id) + |> prepared_changeset.repo.update_all([inc: [amount_refunded: amount]], []) + end + + prepared_changeset + end) + end end diff --git a/lib/stripe_mock_web/views/charge_view.ex b/lib/stripe_mock_web/views/charge_view.ex index 731a6c0..728ef5a 100644 --- a/lib/stripe_mock_web/views/charge_view.ex +++ b/lib/stripe_mock_web/views/charge_view.ex @@ -14,7 +14,7 @@ defmodule StripeMockWeb.ChargeView do charge |> as_map("charge") |> Map.take( - ~w(id object amount currency capture description metadata statement_descriptor transfer_group)a + ~w(id object amount amount_refunded currency capture description metadata statement_descriptor transfer_group)a ) |> Map.merge(%{ customer: charge.customer_id, diff --git a/priv/repo/migrations/20191015221147_create_tables.exs b/priv/repo/migrations/20191015221147_create_tables.exs index aac06b0..7b4e9eb 100644 --- a/priv/repo/migrations/20191015221147_create_tables.exs +++ b/priv/repo/migrations/20191015221147_create_tables.exs @@ -77,6 +77,7 @@ defmodule StripeMock.Repo.Migrations.CreateTables do create table(:charges, primary_key: false) do add(:amount, :integer) + add(:amount_refunded, :integer, default: 0) add(:captured, :boolean, default: false) add(:currency, :string) add(:statement_descriptor, :string) diff --git a/test/stripe_mock_web/controllers/charge_controller_test.exs b/test/stripe_mock_web/controllers/charge_controller_test.exs index ac70890..03b07a7 100644 --- a/test/stripe_mock_web/controllers/charge_controller_test.exs +++ b/test/stripe_mock_web/controllers/charge_controller_test.exs @@ -32,6 +32,7 @@ defmodule StripeMockWeb.ChargeControllerTest do assert %{ "id" => "ch_" <> id, "amount" => 5000, + "amount_refunded" => 0, "currency" => "some currency", "customer" => _, "description" => "some description", diff --git a/test/stripe_mock_web/controllers/refund_controller_test.exs b/test/stripe_mock_web/controllers/refund_controller_test.exs index b9dfe28..58fea9c 100644 --- a/test/stripe_mock_web/controllers/refund_controller_test.exs +++ b/test/stripe_mock_web/controllers/refund_controller_test.exs @@ -45,6 +45,11 @@ defmodule StripeMockWeb.RefundControllerTest do "charge" => ^charge_id, "metadata" => %{} } = json_response(conn, 200) + + # Ensure charge.amount_refunded is updated + + conn = get(conn, Routes.charge_path(conn, :show, charge.id)) + assert %{"amount" => ^amount, "amount_refunded" => ^amount} = json_response(conn, 200) end test "refunds limited to charge amount", %{conn: conn, charge: charge} do