-
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.
Merge branch 'master' of github.com:whitepaperclip/stripe_mock
- Loading branch information
Showing
16 changed files
with
237 additions
and
61 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
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,24 +1,55 @@ | ||
defmodule StripeMock.API.Operations.Charge do | ||
alias StripeMock.Repo | ||
alias StripeMock.API.Charge | ||
alias StripeMock.API.{Card, Charge, Token} | ||
|
||
def list_charges do | ||
Repo.all(Charge) | ||
def list_charges() do | ||
Charge | ||
|> Repo.all() | ||
|> preload_source() | ||
end | ||
|
||
def get_charge(id) do | ||
Repo.fetch(Charge, id) | ||
with {:ok, charge} <- Repo.fetch(Charge, id) do | ||
{:ok, preload_source(charge)} | ||
end | ||
end | ||
|
||
defp preload_source(charges) when is_list(charges) do | ||
Enum.map(charges, &preload_source/1) | ||
end | ||
|
||
defp preload_source({:ok, charge}) do | ||
{:ok, preload_source(charge)} | ||
end | ||
|
||
defp preload_source(%Charge{} = charge) do | ||
%{charge | source: fetch_source(charge.source_id)} | ||
end | ||
|
||
defp preload_source(any), do: any | ||
|
||
def create_charge(attrs \\ %{}) do | ||
%Charge{} | ||
|> Charge.create_changeset(attrs) | ||
|> Repo.insert() | ||
|> preload_source() | ||
end | ||
|
||
def update_charge(%Charge{} = charge, attrs) do | ||
charge | ||
|> Charge.create_changeset(attrs) | ||
|> Repo.update() | ||
|> preload_source() | ||
end | ||
|
||
defp fetch_source("card_" <> _ = card_id), do: Repo.get(Card, card_id) | ||
|
||
defp fetch_source("tok_" <> _ = token_id) do | ||
case Repo.get(Token, token_id) do | ||
%{card_id: card_id} -> fetch_source(card_id) | ||
_ -> nil | ||
end | ||
end | ||
|
||
defp fetch_source(nil), 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
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,9 @@ | ||
defmodule StripeMock.Schema do | ||
defmacro __using__(_) do | ||
quote do | ||
use Ecto.Schema | ||
import Ecto.Changeset, warn: false | ||
alias StripeMock.API | ||
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
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
Oops, something went wrong.