-
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.
- Loading branch information
Showing
7 changed files
with
141 additions
and
0 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
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,40 @@ | ||
defmodule StripeMock.API.Operations.PaymentIntent do | ||
alias StripeMock.Repo | ||
alias StripeMock.API.{Charge, PaymentIntent} | ||
|
||
def list_payment_intents() do | ||
Repo.all(PaymentIntent) | ||
end | ||
|
||
def get_payment_intent(id), do: Repo.fetch(PaymentIntent, id) | ||
def get_payment_intent!(id), do: Repo.get!(PaymentIntent, id) | ||
|
||
def create_payment_intent(attrs \\ %{}) do | ||
%PaymentIntent{} | ||
|> PaymentIntent.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
def update_payment_intent(%PaymentIntent{} = payment_intent, attrs) do | ||
payment_intent | ||
|> PaymentIntent.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
def confirm_payment_intent(%PaymentIntent{} = payment_intent) do | ||
payment_intent | ||
|> PaymentIntent.status_changeset("requires_capture") | ||
|> Repo.update() | ||
end | ||
|
||
def capture_payment_intent(%PaymentIntent{} = payment_intent) do | ||
charge = | ||
%Charge{} | ||
|> Charge.payment_intent_changeset(payment_intent) | ||
|> Repo.insert!() | ||
|
||
payment_intent | ||
|> PaymentIntent.capture_changeset(charge) | ||
|> Repo.update!() | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 :currency, :string | ||
field :description, :string | ||
field :metadata, StripeMock.Type.Metadata, default: %{} | ||
field :payment_method_types, {:array, :string}, default: ["card"] | ||
field :statement_descriptor, :string | ||
field :transfer_group, :string | ||
end | ||
|
||
@doc false | ||
def changeset(payment_intent, attrs) do | ||
payment_intent | ||
|> cast(attrs, [:amount, :confirm, :confirmation_method, :currency]) | ||
end | ||
|
||
@doc false | ||
def status_changeset(payment_intent, status) do | ||
change(payment_intent, %{status: status}) | ||
end | ||
|
||
@doc false | ||
def capture_changeset(payment_intent, charge) do | ||
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
53 changes: 53 additions & 0 deletions
53
lib/stripe_mock_web/controllers/payment_intent_controller.ex
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,53 @@ | ||
defmodule StripeMockWeb.PaymentIntentController do | ||
use StripeMockWeb, :controller | ||
|
||
alias StripeMock.API | ||
alias StripeMock.API.PaymentIntent | ||
|
||
action_fallback StripeMockWeb.FallbackController | ||
|
||
def index(conn, params) do | ||
page = API.list_payment_intents() |> paginate(params) | ||
render(conn, "index.json", page: page) | ||
end | ||
|
||
def create(conn, payment_intent_params) do | ||
with {:ok, payment_intent} <- API.create_payment_intent(payment_intent_params) do | ||
conn | ||
|> put_status(:created) | ||
|> put_resp_header("location", Routes.payment_intent_path(conn, :show, payment_intent)) | ||
|> render("show.json", payment_intent: payment_intent) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
with {:ok, payment_intent} <- API.get_payment_intent(id) do | ||
render(conn, "show.json", payment_intent: payment_intent) | ||
end | ||
end | ||
|
||
def update(conn, %{"id" => id} = payment_intent_params) do | ||
payment_intent = API.get_payment_intent!(id) | ||
|
||
with {:ok, payment_intent} <- | ||
API.update_payment_intent(payment_intent, payment_intent_params) do | ||
render(conn, "show.json", payment_intent: payment_intent) | ||
end | ||
end | ||
|
||
def confirm(conn, %{"id" => id} = payment_intent_params) do | ||
payment_intent = API.get_payment_intent!(id) | ||
|
||
with {:ok, payment_intent} <- API.confirm_payment_intent(payment_intent) do | ||
render(conn, "show.json", payment_intent: payment_intent) | ||
end | ||
end | ||
|
||
def capture(conn, %{"id" => id} = payment_intent_params) do | ||
payment_intent = API.get_payment_intent!(id) | ||
|
||
with {:ok, payment_intent} <- API.capture_payment_intent(payment_intent) do | ||
render(conn, "show.json", payment_intent: payment_intent) | ||
end | ||
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