Skip to content

Commit

Permalink
Added amount_refunded to Charge
Browse files Browse the repository at this point in the history
  • Loading branch information
nkezhaya committed Mar 20, 2020
1 parent b1c6256 commit d73dbcf
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/stripe_mock/api/charge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lib/stripe_mock/api/refund.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule StripeMock.API.Refund do
|> validate_required(:amount)
|> validate_amount()
|> validate_reason()
|> update_charge()
|> put_common_fields()
end

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/stripe_mock_web/views/charge_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions priv/repo/migrations/20191015221147_create_tables.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule StripeMockWeb.ChargeControllerTest do
assert %{
"id" => "ch_" <> id,
"amount" => 5000,
"amount_refunded" => 0,
"currency" => "some currency",
"customer" => _,
"description" => "some description",
Expand Down
5 changes: 5 additions & 0 deletions test/stripe_mock_web/controllers/refund_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d73dbcf

Please sign in to comment.