Skip to content

Commit

Permalink
Send charges as paginated list back with payment intents
Browse files Browse the repository at this point in the history
  • Loading branch information
nkezhaya committed Oct 18, 2019
1 parent 86a7df3 commit 07cd9b0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/stripe_mock/pagination.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule StripeMock.Pagination do

defmacro __using__(_opts) do
quote do
import unquote(__MODULE__)

def render_page(conn, page, view, template) do
%{
object: "list",
Expand Down
4 changes: 4 additions & 0 deletions lib/stripe_mock_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ defmodule StripeMockWeb do
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
quote do
Module.put_attribute(unquote(__MODULE__), :moduledoc, false)
end

apply(__MODULE__, which, [])
end
end
15 changes: 8 additions & 7 deletions lib/stripe_mock_web/views/payment_intent_view.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
defmodule StripeMockWeb.PaymentIntentView do
use StripeMockWeb, :view
alias StripeMockWeb.PaymentIntentView
alias StripeMockWeb.{ChargeView, PaymentIntentView}

def render("index.json", %{page: page}) do
%{data: render_many(page.data, PaymentIntentView, "payment_intent.json")}
def render("index.json", %{conn: conn, page: page}) do
%{data: render_many(page.data, PaymentIntentView, "payment_intent.json", conn: conn)}
end

def render("show.json", %{payment_intent: payment_intent}) do
render_one(payment_intent, PaymentIntentView, "payment_intent.json")
def render("show.json", %{conn: conn, payment_intent: payment_intent}) do
render_one(payment_intent, PaymentIntentView, "payment_intent.json", conn: conn)
end

def render("payment_intent.json", %{payment_intent: payment_intent}) do
def render("payment_intent.json", %{conn: conn, payment_intent: payment_intent}) do
payment_intent
|> as_map("payment_intent")
|> Map.take(
Expand All @@ -19,7 +19,8 @@ defmodule StripeMockWeb.PaymentIntentView do
|> Map.put("customer", payment_intent.customer_id)
|> Map.merge(%{
object: "payment_intent",
payment_method: render_payment_method(payment_intent.payment_method)
payment_method: render_payment_method(payment_intent.payment_method),
charges: render_page(conn, paginate(payment_intent.charges), ChargeView, "charge.json")
})
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ defmodule StripeMockWeb.PaymentIntentControllerTest do
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)
assert %{"status" => "succeeded", "charges" => %{"data" => charges}} =
json_response(conn, 200)

assert is_list(charges)
end

test "confirms payment intent", %{conn: conn, payment_intent: payment_intent} do
Expand Down

0 comments on commit 07cd9b0

Please sign in to comment.