-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial payment intents API complete
- Loading branch information
Showing
18 changed files
with
392 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
use Mix.Config | ||
|
||
config :stripe_mock, StripeMockWeb.Endpoint, | ||
server: false | ||
|
||
# Print only warnings and errors during test | ||
config :stripe_mock, StripeMockWeb.Endpoint, server: false | ||
config :stripe_mock, StripeMock.Repo, pool: Ecto.Adapters.SQL.Sandbox | ||
config :logger, level: :warn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,68 @@ | ||
defmodule StripeMock.API.Operations.PaymentIntent do | ||
import Ecto.Query | ||
|
||
alias StripeMock.Repo | ||
alias StripeMock.API.{Charge, PaymentIntent} | ||
|
||
@preload [payment_method: [:card, :source, token: [:card]]] | ||
|
||
def list_payment_intents() do | ||
Repo.all(PaymentIntent) | ||
PaymentIntent | ||
|> preload(^@preload) | ||
|> Repo.all() | ||
end | ||
|
||
def get_payment_intent(id), do: Repo.fetch(PaymentIntent, id) | ||
def get_payment_intent!(id), do: Repo.get!(PaymentIntent, id) | ||
def get_payment_intent(id) do | ||
PaymentIntent | ||
|> preload(^@preload) | ||
|> Repo.fetch(id) | ||
|> preload_payment_method() | ||
end | ||
|
||
def create_payment_intent(attrs \\ %{}) do | ||
def create_payment_intent(attrs) do | ||
%PaymentIntent{} | ||
|> PaymentIntent.changeset(attrs) | ||
|> Repo.insert() | ||
|> preload_payment_method() | ||
end | ||
|
||
def update_payment_intent(%PaymentIntent{} = payment_intent, attrs) do | ||
payment_intent | ||
|> PaymentIntent.changeset(attrs) | ||
|> Repo.update() | ||
|> preload_payment_method() | ||
end | ||
|
||
def confirm_payment_intent(%PaymentIntent{} = payment_intent) do | ||
payment_intent | ||
|> PaymentIntent.status_changeset("requires_capture") | ||
|> Repo.update() | ||
|> preload_payment_method() | ||
end | ||
|
||
def capture_payment_intent(%PaymentIntent{} = payment_intent) do | ||
charge = | ||
%Charge{} | ||
|> Charge.payment_intent_changeset(payment_intent) | ||
|> Charge.capture_changeset(payment_intent) | ||
|> Repo.insert!() | ||
|
||
payment_intent | ||
|> PaymentIntent.capture_changeset(charge) | ||
|> Repo.update!() | ||
|> preload_payment_method() | ||
end | ||
|
||
defp preload_payment_method({:ok, payment_intent}) do | ||
{:ok, preload_payment_method(payment_intent)} | ||
end | ||
|
||
defp preload_payment_method(%PaymentIntent{} = payment_intent) do | ||
Repo.preload(payment_intent, @preload) | ||
end | ||
|
||
defp preload_payment_method([_ | _] = payment_intents) do | ||
Repo.preload(payment_intents, @preload) | ||
end | ||
|
||
defp preload_payment_method(arg), do: arg | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,82 @@ | ||
defmodule StripeMock.API.PaymentIntent do | ||
use StripeMock.Schema | ||
|
||
@primary_key {:id, :binary_id, autogenerate: false} | ||
schema "payment_intents" do | ||
field :amount, :integer | ||
field :capture, :boolean, default: false | ||
field :capture_method, :string | ||
field :confirmation_method, :string | ||
field :capture_method, :string, default: "automatic" | ||
field :confirm, :boolean, virtual: true, default: false | ||
field :confirmation_method, :string, default: "automatic" | ||
field :currency, :string | ||
field :description, :string | ||
field :metadata, StripeMock.Type.Metadata, default: %{} | ||
field :payment_method_types, {:array, :string}, default: ["card"] | ||
field :statement_descriptor, :string | ||
field :status, :string | ||
field :transfer_data, :map | ||
field :transfer_group, :string | ||
|
||
belongs_to(:customer, API.Customer) | ||
belongs_to(:payment_method, API.PaymentMethod) | ||
has_many(:charges, API.Charge) | ||
|
||
common_fields() | ||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(payment_intent, attrs) do | ||
payment_intent | ||
|> cast(attrs, [:amount, :confirm, :confirmation_method, :currency]) | ||
|> cast(attrs, [ | ||
:amount, | ||
:confirm, | ||
:confirmation_method, | ||
:currency, | ||
:customer_id, | ||
:description, | ||
:metadata, | ||
:payment_method_id, | ||
:statement_descriptor, | ||
:transfer_data, | ||
:transfer_group | ||
]) | ||
|> validate_inclusion(:capture_method, ~w(automatic manual)) | ||
|> validate_inclusion(:confirmation_method, ~w(automatic manual)) | ||
|> set_payment_method() | ||
|> validate_required([:payment_method_id]) | ||
|> put_common_fields() | ||
end | ||
|
||
@doc false | ||
def status_changeset(payment_intent, status) do | ||
change(payment_intent, %{status: status}) | ||
payment_intent | ||
|> change(%{status: status}) | ||
|> 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 -> | ||
changeset | ||
|
||
id -> | ||
card = Repo.get(API.Card, id) | ||
token = Repo.get(API.Token, id) | ||
|
||
case find_payment_method(card || token) do | ||
nil -> add_error(changeset, :payment_method_id, "not found") | ||
payment_method -> put_change(changeset, :payment_method_id, payment_method.id) | ||
end | ||
end | ||
end | ||
|
||
defp find_payment_method(%API.Card{} = card), | ||
do: Repo.get_by(API.PaymentMethod, card_id: card.id) | ||
|
||
defp find_payment_method(%API.Token{} = token), | ||
do: Repo.get_by(API.PaymentMethod, token_id: token.id) | ||
|
||
defp find_payment_method(_), | ||
do: nil | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule StripeMock.API.PaymentMethod do | ||
use StripeMock.Schema | ||
|
||
schema "payment_methods" do | ||
belongs_to :card, API.Card | ||
belongs_to :token, API.Token | ||
belongs_to :source, API.Source | ||
|
||
common_fields() | ||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(token, attrs) do | ||
token | ||
|> cast(attrs, [:card_id, :token_id, :source_id]) | ||
|> put_common_fields() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule StripeMockWeb.PaymentIntentView do | ||
use StripeMockWeb, :view | ||
alias StripeMockWeb.PaymentIntentView | ||
|
||
def render("index.json", %{page: page}) do | ||
%{data: render_many(page.data, PaymentIntentView, "payment_intent.json")} | ||
end | ||
|
||
def render("show.json", %{payment_intent: payment_intent}) do | ||
render_one(payment_intent, PaymentIntentView, "payment_intent.json") | ||
end | ||
|
||
def render("payment_intent.json", %{payment_intent: payment_intent}) do | ||
payment_intent | ||
|> as_map() | ||
|> Map.take( | ||
~w(amount capture_method confirmation_method currency description id metadata payment_method_types statement_descriptor status transfer_group)a | ||
) | ||
|> Map.put("customer", payment_intent.customer_id) | ||
|> Map.merge(%{ | ||
object: "payment_intent", | ||
payment_method: render_payment_method(payment_intent.payment_method) | ||
}) | ||
end | ||
|
||
def render_payment_method(payment_method) do | ||
render(StripeMockWeb.PaymentMethodView, "payment_method.json", payment_method: payment_method) | ||
end | ||
end |
Oops, something went wrong.