Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nkezhaya committed Jun 3, 2019
0 parents commit 922a844
Show file tree
Hide file tree
Showing 64 changed files with 2,307 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
import_deps: [:phoenix],
inputs: ["*.{ex,exs}", "{config,lib,test}/**/*.{ex,exs}"],
locals_without_parens: [
field: :*,
belongs_to: :*
]
]
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
stripe_mock-*.tar

# Since we are building assets from assets/,
# we ignore priv/static. You may want to comment
# this depending on your deployment strategy.
/priv/static/

# Files matching config/*.secret.exs pattern contain sensitive
# data and you should not commit them into version control.
#
# Alternatively, you may comment the line below and commit the
# secrets files as long as you replace their contents by environment
# variables.
/config/*.secret.exs
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Nick Kezhaya

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# StripeMock

Starts a server that pretends to be Stripe. Docs here: https://hexdocs.pm/stripe_mock/StripeMock.html
28 changes: 28 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
#
# This configuration file is loaded before any dependency and
# is restricted to this project.

# General application configuration
use Mix.Config

# Configures the endpoint
config :stripe_mock, StripeMockWeb.Endpoint,
url: [host: "localhost"],
http: [:inet6, port: System.get_env("PORT") || 12111],
secret_key_base: "ug9ATr9o7f/N2inxnW+SlrNVP7Ok+f9gAP43yHfqqm/bgFZSLeY6vQOY+wp562Iz",
render_errors: [view: StripeMockWeb.ErrorView, accepts: ~w(json)],
pubsub: [name: StripeMock.PubSub, adapter: Phoenix.PubSub.PG2]

# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"
47 changes: 47 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use Mix.Config

# For development, we disable any cache and enable
# debugging and code reloading.
#
# The watchers configuration can be used to run external
# watchers to your application. For example, we use it
# with webpack to recompile .js and .css sources.
config :stripe_mock, StripeMockWeb.Endpoint,
debug_errors: true,
code_reloader: true,
check_origin: false,
watchers: []

# ## SSL Support
#
# In order to use HTTPS in development, a self-signed
# certificate can be generated by running the following
# Mix task:
#
# mix phx.gen.cert
#
# Note that this task requires Erlang/OTP 20 or later.
# Run `mix help phx.gen.cert` for more information.
#
# The `http:` config above can be replaced with:
#
# https: [
# port: 4001,
# cipher_suite: :strong,
# keyfile: "priv/cert/selfsigned_key.pem",
# certfile: "priv/cert/selfsigned.pem"
# ],
#
# If desired, both `http:` and `https:` keys can be
# configured to run both http and https servers on
# different ports.

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"

# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20

# Initialize plugs at runtime for faster development compilation
config :phoenix, :plug_init_mode, :runtime
7 changes: 7 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use Mix.Config

config :stripe_mock, StripeMockWeb.Endpoint,
server: false

# Print only warnings and errors during test
config :logger, level: :warn
27 changes: 27 additions & 0 deletions lib/stripe_mock.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule StripeMock do
@moduledoc """
StripeMock is a service that duplicates some of the core functionality of the
Stripe API, without doing enough to receive a cease-and-desist letter from
Stripe's legal team.
This has been created for testing purposes only. MY GOD will it speed up your
test suite.
First, add the dependency to your `mix.exs`:
{:stripe_mock, "~> 0.1.0"}
(or whatever the latest version is; I probably won't be updating this moduledoc.)
In your app's `config/test.exs` file:
config :stripity_stripe, :api_base_url, "http://localhost:12111/v1/"
config :stripe_mock, StripeMockWeb.Endpoint, http: [port: 12111], server: true
That should get the StripeMock server to start in your test environment.
No database connection is needed. `StripeMock.Repo` is just a GenServer that
stores everything in its state. It'd be nice if `ecto_mnesia` was updated for
Ecto 3, but as of right now this is the next best option.
"""
end
39 changes: 39 additions & 0 deletions lib/stripe_mock/api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule StripeMock.API do
alias StripeMock.API.Operations, as: Ops

# Customers
defdelegate list_customers(), to: Ops.Customer
defdelegate get_customer(id), to: Ops.Customer
defdelegate get_customer!(id), to: Ops.Customer
defdelegate create_customer(attrs \\ %{}), to: Ops.Customer
defdelegate update_customer(customer, attrs \\ %{}), to: Ops.Customer
defdelegate delete_customer(customer), to: Ops.Customer

# Tokens
defdelegate get_token(id), to: Ops.Token
defdelegate create_token(attrs \\ %{}), to: Ops.Token

# Cards
defdelegate list_cards(customer), to: Ops.Card
defdelegate get_card!(id), to: Ops.Card
defdelegate create_card(customer, attrs \\ %{}), to: Ops.Card
defdelegate create_customer_card_from_source(customer, source, metadata \\ %{}), to: Ops.Card
defdelegate update_card(card, attrs \\ %{}), to: Ops.Card
defdelegate delete_card(card), to: Ops.Card

# Sources - TODO
# defdelegate attach_source(source, customer), to: Ops.Source
# defdelegate detach_source(source, customer), to: Ops.Source

# Charges
defdelegate list_charges(), to: Ops.Charge
defdelegate get_charge(id), to: Ops.Charge
defdelegate create_charge(attrs \\ %{}), to: Ops.Charge
defdelegate update_charge(charge, attrs \\ %{}), to: Ops.Charge

# Refunds
defdelegate list_refunds(), to: Ops.Refund
defdelegate get_refund(id), to: Ops.Refund
defdelegate create_refund(attrs \\ %{}), to: Ops.Refund
defdelegate update_refund(refund, attrs \\ %{}), to: Ops.Refund
end
79 changes: 79 additions & 0 deletions lib/stripe_mock/api/card.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
defmodule StripeMock.API.Card do
use Ecto.Schema
import Ecto.Changeset
alias StripeMock.API

schema "cards" do
field :brand, :string
field :created, :integer
field :deleted, :boolean
field :metadata, StripeMock.Metadata, default: %{}
field :last4, :string
field :source, :string

field :number, :string
field :exp_month, :integer
field :exp_year, :integer
field :cvc, :string

belongs_to :customer, API.Customer
end

@doc false
def create_changeset(card, attrs) do
card
|> cast(attrs, [:source, :metadata])
|> validate_required([:source, :metadata])
|> set_brand()
|> set_last4()
end

@doc false
def update_changeset(card, attrs) do
card
|> cast(attrs, [])
|> validate_required([])
end

@doc false
def token_changeset(card, attrs) do
card
|> cast(attrs, [:number, :exp_month, :exp_year, :cvc])
|> validate_required([:number, :exp_month, :exp_year, :cvc])
|> set_brand()
|> set_last4()
end

defp set_brand(changeset) do
brand =
case get_field(changeset, :number) do
"4242424242424242" -> "Visa"
"4000056655665556" -> "Visa"
"5555555555554444" -> "Mastercard"
"2223003122003222" -> "Mastercard"
"5200828282828210" -> "Mastercard"
"5105105105105100" -> "Mastercard"
"378282246310005" -> "American Express"
"371449635398431" -> "American Express"
"6011111111111117" -> "Discover"
"6011000990139424" -> "Discover"
"30569309025904" -> "Diners Club"
"38520000023237" -> "Diners Club"
"3566002020360505" -> "JCB"
"6200000000000005" -> "UnionPay"
end

put_change(changeset, :brand, brand)
end

defp set_last4(changeset) do
case get_field(changeset, :number) do
number when is_bitstring(number) ->
last4 = String.split_at(number, -6) |> elem(1)
put_change(changeset, :last4, last4)

_ ->
changeset
end
end
end
47 changes: 47 additions & 0 deletions lib/stripe_mock/api/charge.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule StripeMock.API.Charge do
use Ecto.Schema
import Ecto.Changeset

alias StripeMock.API

@foreign_key_type :binary_id
schema "charges" do
field :amount, :integer
field :capture, :boolean, default: false
field :currency, :string
field :description, :string
field :metadata, StripeMock.Metadata, default: %{}
field :statement_descriptor, :string
field :transfer_group, :string

belongs_to :customer, API.Customer
end

@doc false
def create_changeset(charge, attrs) do
charge
|> cast(attrs, [
:amount,
:currency,
:capture,
:customer_id,
:description,
:metadata,
:statement_descriptor,
:transfer_group
])
|> validate_required([:amount, :currency])
end

@doc false
def update_changeset(charge, attrs) do
charge
|> cast(attrs, [
:customer_id,
:description,
:metadata,
:transfer_group
])
|> validate_required([:amount, :currency])
end
end
30 changes: 30 additions & 0 deletions lib/stripe_mock/api/customer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule StripeMock.API.Customer do
use Ecto.Schema
import Ecto.Changeset

@primary_key {:id, :binary_id, autogenerate: false}
schema "customers" do
field :created, :integer
field :currency, :string, default: "usd"
field :deleted, :boolean, default: false
field :description, :string
field :email, :string
field :metadata, StripeMock.Metadata, default: %{}
field :name, :string
field :phone, :string
end

@doc false
def changeset(customer, attrs) do
customer
|> cast(attrs, [:created, :currency, :description, :email, :name, :phone])
|> validate_email()
end

defp validate_email(changeset) do
case get_field(changeset, :email) do
nil -> changeset
_ -> validate_format(changeset, :email, ~r/.+@.+\..+/i)
end
end
end
Loading

0 comments on commit 922a844

Please sign in to comment.