Skip to content

Commit

Permalink
Initial payment intent skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
nkezhaya committed Oct 15, 2019
1 parent 5d56835 commit 667487c
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/stripe_mock/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ defmodule StripeMock.API do
# defdelegate attach_source(source, customer), to: Ops.Source
# defdelegate detach_source(source, customer), to: Ops.Source

# PaymentIntents
defdelegate list_payment_intents(), to: Ops.PaymentIntent
defdelegate get_payment_intent(id), to: Ops.PaymentIntent
defdelegate get_payment_intent!(id), to: Ops.PaymentIntent
defdelegate create_payment_intent(attrs \\ %{}), to: Ops.PaymentIntent
defdelegate update_payment_intent(payment_intent, attrs \\ %{}), to: Ops.PaymentIntent
defdelegate confirm_payment_intent(payment_intent), to: Ops.PaymentIntent
defdelegate capture_payment_intent(payment_intent), to: Ops.PaymentIntent

# Charges
defdelegate list_charges(), to: Ops.Charge
defdelegate get_charge(id), to: Ops.Charge
Expand Down
4 changes: 4 additions & 0 deletions lib/stripe_mock/api/charge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ defmodule StripeMock.API.Charge do
|> validate_required([:amount, :currency])
end

@doc false
def capture_changeset(payment_intent, charge) do
end

defp set_customer_and_source(changeset) do
customer =
with customer_id when not is_nil(customer_id) <- get_field(changeset, :customer_id) do
Expand Down
40 changes: 40 additions & 0 deletions lib/stripe_mock/api/operations/payment_intent.ex
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
32 changes: 32 additions & 0 deletions lib/stripe_mock/api/payment_intent.ex
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
2 changes: 2 additions & 0 deletions lib/stripe_mock/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,14 @@ defmodule StripeMock.Repo do
def type(API.Card), do: :card
def type(API.Charge), do: :charge
def type(API.Customer), do: :customer
def type(API.PaymentIntent), do: :payment_intent
def type(API.Refund), do: :refund
def type(API.Token), do: :token

def prefix(%API.Card{}), do: "card"
def prefix(%API.Charge{}), do: "ch"
def prefix(%API.Customer{}), do: "cus"
def prefix(%API.PaymentIntent{}), do: "pi"
def prefix(%API.Refund{}), do: "re"
def prefix(%API.Token{}), do: "tok"
end
53 changes: 53 additions & 0 deletions lib/stripe_mock_web/controllers/payment_intent_controller.ex
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
1 change: 1 addition & 0 deletions lib/stripe_mock_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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]

Expand Down

0 comments on commit 667487c

Please sign in to comment.