Skip to content

Commit

Permalink
Capture/confirm payment intent
Browse files Browse the repository at this point in the history
  • Loading branch information
nkezhaya committed Oct 16, 2019
1 parent 9ff8cdf commit 0d8b044
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
21 changes: 11 additions & 10 deletions lib/stripe_mock/api/operations/payment_intent.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions lib/stripe_mock/api/payment_intent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
5 changes: 4 additions & 1 deletion lib/stripe_mock_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0d8b044

Please sign in to comment.