From 0d8b04448ec084eaba2a711d398d6997efc0f0f1 Mon Sep 17 00:00:00 2001 From: Nick Kezhaya Date: Wed, 16 Oct 2019 18:08:34 -0500 Subject: [PATCH] Capture/confirm payment intent --- .../api/operations/payment_intent.ex | 21 ++++++++++--------- lib/stripe_mock/api/payment_intent.ex | 4 ---- lib/stripe_mock_web/router.ex | 5 ++++- .../payment_intent_controller_test.exs | 12 +++++++++++ 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/stripe_mock/api/operations/payment_intent.ex b/lib/stripe_mock/api/operations/payment_intent.ex index 9ff973f..24aced0 100644 --- a/lib/stripe_mock/api/operations/payment_intent.ex +++ b/lib/stripe_mock/api/operations/payment_intent.ex @@ -1,10 +1,11 @@ defmodule StripeMock.API.Operations.PaymentIntent do import Ecto.Query + alias Ecto.Multi alias StripeMock.Repo alias StripeMock.API.{Charge, PaymentIntent} - @preload [payment_method: [:card, :source, token: [:card]]] + @preload [:charges, payment_method: [:card, :source, token: [:card]]] def list_payment_intents() do PaymentIntent @@ -35,20 +36,20 @@ defmodule StripeMock.API.Operations.PaymentIntent do def confirm_payment_intent(%PaymentIntent{} = payment_intent) do payment_intent - |> PaymentIntent.status_changeset("requires_capture") + |> PaymentIntent.status_changeset("requires_action") |> Repo.update() |> preload_payment_method() end def capture_payment_intent(%PaymentIntent{} = payment_intent) do - charge = - %Charge{} - |> Charge.capture_changeset(payment_intent) - |> Repo.insert!() - - payment_intent - |> PaymentIntent.capture_changeset(charge) - |> Repo.update!() + Multi.new() + |> Multi.insert(:charge, Charge.capture_changeset(%Charge{}, payment_intent)) + |> Multi.update(:payment_intent, PaymentIntent.status_changeset(payment_intent, "succeeded")) + |> Repo.transaction() + |> case do + {:ok, %{payment_intent: payment_intent}} -> {:ok, payment_intent} + {:error, _, value, _} -> {:error, value} + end |> preload_payment_method() end diff --git a/lib/stripe_mock/api/payment_intent.ex b/lib/stripe_mock/api/payment_intent.ex index e9f9357..a79d338 100644 --- a/lib/stripe_mock/api/payment_intent.ex +++ b/lib/stripe_mock/api/payment_intent.ex @@ -51,10 +51,6 @@ defmodule StripeMock.API.PaymentIntent do |> put_common_fields() end - @doc false - def capture_changeset(payment_intent, charge) do - end - defp set_payment_method(changeset) do case get_change(changeset, :payment_method_id) do nil -> diff --git a/lib/stripe_mock_web/router.ex b/lib/stripe_mock_web/router.ex index 52ba46a..092fdc4 100644 --- a/lib/stripe_mock_web/router.ex +++ b/lib/stripe_mock_web/router.ex @@ -16,10 +16,13 @@ defmodule StripeMockWeb.Router do resources "/charges", ChargeController, except: [:delete] resources "/refunds", RefundController, except: [:delete] - resources "/payment_intents", PaymentIntentController, except: [:delete] resources "/sources", SourceController, only: [:show] resources "/tokens", TokenController, only: [:create, :show] + resources "/payment_intents", PaymentIntentController, except: [:delete] + post "/payment_intents/:id/capture", PaymentIntentController, :capture + post "/payment_intents/:id/confirm", PaymentIntentController, :confirm + post "/charges/:id", ChargeController, :update post "/customers/:id", CustomerController, :update post "/refunds/:id", RefundController, :update diff --git a/test/stripe_mock_web/controllers/payment_intent_controller_test.exs b/test/stripe_mock_web/controllers/payment_intent_controller_test.exs index 980a52e..38a271e 100644 --- a/test/stripe_mock_web/controllers/payment_intent_controller_test.exs +++ b/test/stripe_mock_web/controllers/payment_intent_controller_test.exs @@ -96,6 +96,18 @@ defmodule StripeMockWeb.PaymentIntentControllerTest do assert json_response(conn, 422)["errors"] != %{} end + + test "captures payment intent", %{conn: conn, payment_intent: payment_intent} do + conn = post(conn, Routes.payment_intent_path(conn, :capture, payment_intent), %{}) + + assert %{"status" => "succeeded"} = json_response(conn, 200) + end + + test "confirms payment intent", %{conn: conn, payment_intent: payment_intent} do + conn = post(conn, Routes.payment_intent_path(conn, :confirm, payment_intent), %{}) + + assert %{"status" => "requires_action"} = json_response(conn, 200) + end end def create_attrs() do