Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/code_corps/conn_utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule CodeCorps.ConnUtils do
def extract_ip(%Plug.Conn{} = conn) do
conn.remote_ip |> Tuple.to_list |> Enum.join(".")
end

def extract_user_agent(%Plug.Conn{} = conn) do
conn |> Plug.Conn.get_req_header("user-agent") |> List.first
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ defmodule CodeCorps.StripeService.Adapters.StripeConnectAccountAdapter do
{:support_email, [:support_email]},
{:support_phone, [:support_phone]},
{:support_url, [:support_url]},
{:tos_acceptance_date, [:tos_acceptance, :date]},
{:tos_acceptance_ip, [:tos_acceptance, :ip]},
{:tos_acceptance_user_agent, [:tos_acceptance, :user_agent]},
{:transfers_enabled, [:transfers_enabled]},
{:verification_disabled_reason, [:verification, :disabled_reason]},
{:verification_due_by, [:verification, :due_by]},
Expand Down
5 changes: 3 additions & 2 deletions lib/code_corps/stripe_service/stripe_connect_account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ defmodule CodeCorps.StripeService.StripeConnectAccountService do

@api Application.get_env(:code_corps, :stripe)

def create(%{"country" => country_code, "organization_id" => _} = attributes) do
with {:ok, %Stripe.Account{} = account} <- @api.Account.create(%{country: country_code, managed: true}),
def create(attributes) do
with {:ok, from_params} <- StripeConnectAccountAdapter.from_params(attributes),
{:ok, %Stripe.Account{} = account} <- @api.Account.create(from_params),
{:ok, params} <- StripeConnectAccountAdapter.to_params(account, attributes)
do
%StripeConnectAccount{}
Expand Down
21 changes: 10 additions & 11 deletions lib/code_corps/stripe_testing/account.ex
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
defmodule CodeCorps.StripeTesting.Account do
def create(_map) do
{:ok, create_stripe_record(%{})}
def create(attributes) do
{:ok, create_stripe_record(attributes)}
end

def retrieve(id) do
{:ok, create_stripe_record(%{"id" => id})}
end

def update(id, attributes) do
attributes =
{:ok, create_stripe_record(attributes |> Map.merge(%{id: id}))}
end

defp create_stripe_record(attributes) do
transformed_attributes =
attributes
|> CodeCorps.MapUtils.keys_to_string
|> Map.merge(%{"id" => id})
|> Map.merge(account_fixture)
|> add_nestings

{:ok, create_stripe_record(attributes)}
end

defp create_stripe_record(attributes) do
with attributes <- account_fixture |> Map.merge(attributes) |> add_nestings
do
Stripe.Account |> Stripe.Converter.stripe_map_to_struct(attributes)
end
Stripe.Account |> Stripe.Converter.stripe_map_to_struct(transformed_attributes)
end

defp account_fixture do
Expand Down
8 changes: 2 additions & 6 deletions lib/code_corps/stripe_testing/customer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ defmodule CodeCorps.StripeTesting.Customer do
end

defp do_create(_) do
{:ok, created} = DateTime.from_unix(1479472835)

%Stripe.Customer{
id: "cus_9aMOFmqy1esIRE",
account_balance: 0,
created: created,
created: 1479472835,
currency: "usd",
default_source: nil,
delinquent: false,
Expand All @@ -29,12 +27,10 @@ defmodule CodeCorps.StripeTesting.Customer do
end

defp do_update(id, map) do
{:ok, created} = DateTime.from_unix(1479472835)

%Stripe.Customer{
id: id,
account_balance: 0,
created: created,
created: 1479472835,
currency: "usd",
default_source: nil,
delinquent: false,
Expand Down
7 changes: 2 additions & 5 deletions lib/code_corps/stripe_testing/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ defmodule CodeCorps.StripeTesting.Event do
end

defp do_retrieve(_) do
{:ok, created} = DateTime.from_unix(1479472835)

%Stripe.Event{
api_version: "2016-07-06",
created: created,
created: 1479472835,
id: "evt_123",
livemode: false,
object: "event",
Expand All @@ -22,11 +21,9 @@ defmodule CodeCorps.StripeTesting.Event do
end

defp do_retrieve_connect(_) do
{:ok, created} = DateTime.from_unix(1479472835)

%Stripe.Event{
api_version: "2016-07-06",
created: created,
created: 1479472835,
id: "evt_123",
livemode: false,
object: "event",
Expand Down
4 changes: 1 addition & 3 deletions lib/code_corps/stripe_testing/plan.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ defmodule CodeCorps.StripeTesting.Plan do
end

defp do_create(_) do
{:ok, created} = DateTime.from_unix(1479472835)

%Stripe.Plan{
id: "plan_9aMOFmqy1esIRE",
amount: 5000,
created: created,
created: 1479472835,
currency: "usd",
interval: "month",
interval_count: 1,
Expand Down
20 changes: 8 additions & 12 deletions lib/code_corps/stripe_testing/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ defmodule CodeCorps.StripeTesting.Subscription do
end

defp do_create(%{quantity: quantity}) do
{:ok, date} = DateTime.from_unix(1479472835)

{:ok, plan} = CodeCorps.StripeTesting.Plan.create(%{}, [])

%Stripe.Subscription{
application_fee_percent: 5.0,
cancel_at_period_end: false,
canceled_at: nil,
created: date,
current_period_end: date,
current_period_start: date,
created: 1479472835,
current_period_end: 1479472835,
current_period_start: 1479472835,
customer: "cus_123",
ended_at: nil,
id: "sub_123",
Expand All @@ -27,7 +25,7 @@ defmodule CodeCorps.StripeTesting.Subscription do
plan: plan,
quantity: quantity,
source: nil,
start: date,
start: 1479472835,
status: "active",
tax_percent: nil,
trial_end: nil,
Expand All @@ -36,17 +34,15 @@ defmodule CodeCorps.StripeTesting.Subscription do
end

defp do_retrieve(_) do
{:ok, date} = DateTime.from_unix(1479472835)

{:ok, plan} = CodeCorps.StripeTesting.Plan.create(%{}, [])

%Stripe.Subscription{
application_fee_percent: 5.0,
cancel_at_period_end: false,
canceled_at: nil,
created: date,
current_period_end: date,
current_period_start: date,
created: 1479472835,
current_period_end: 1479472835,
current_period_start: 1479472835,
customer: "cus_123",
ended_at: nil,
id: "sub_123",
Expand All @@ -55,7 +51,7 @@ defmodule CodeCorps.StripeTesting.Subscription do
plan: plan,
quantity: 1000,
source: nil,
start: date,
start: 1479472835,
status: "canceled",
tax_percent: nil,
trial_end: nil,
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"segment": {:hex, :segment, "0.1.1", "47bf9191590e7a533c105d1e21518e0d6da47c91e8d98ebb649c624db5dfc359", [:mix], [{:httpoison, "~> 0.8", [hex: :httpoison, optional: false]}, {:poison, "~> 1.3 or ~> 2.0", [hex: :poison, optional: false]}]},
"sentry": {:hex, :sentry, "2.1.0", "51e7ca261b519294ac73b30763893c4a7ad2005205514aefa5bf37ccb83e44ea", [:mix], [{:hackney, "~> 1.6.1", [hex: :hackney, optional: false]}, {:plug, "~> 1.0", [hex: :plug, optional: true]}, {:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, optional: false]}, {:uuid, "~> 1.0", [hex: :uuid, optional: false]}]},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.0", "edee20847c42e379bf91261db474ffbe373f8acb56e9079acb6038d4e0bf414f", [:make, :rebar], []},
"stripity_stripe": {:git, "https://github.com/code-corps/stripity_stripe.git", "88698ce6582574d5c7070db3551099c2439248c9", [branch: "add-update-changesets"]},
"stripity_stripe": {:git, "https://github.com/code-corps/stripity_stripe.git", "bffefa3195d4fc75dafece4094d697506dc72c07", [branch: "2.0"]},
"sweet_xml": {:hex, :sweet_xml, "0.6.3", "814265792baeb163421811c546581c522dfdcb9d1767b1e59959c52906414e80", [:mix], []},
"timber": {:hex, :timber, "0.4.7", "df3fcd79bcb4eb4b53874d906ef5f3a212937b4bc7b7c5b244745202cc389443", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, optional: true]}, {:phoenix, "~> 1.2", [hex: :phoenix, optional: true]}, {:plug, "~> 1.2", [hex: :plug, optional: true]}, {:poison, "~> 2.0 or ~> 3.0", [hex: :poison, optional: false]}]},
"timex": {:hex, :timex, "3.1.5", "413d6d8d6f0162a5d47080cb8ca520d790184ac43e097c95191c7563bf25b428", [:mix], [{:combine, "~> 0.7", [hex: :combine, optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5", [hex: :tzdata, optional: false]}]},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule CodeCorps.Repo.Migrations.AddTosAcceptanceFieldsToStripeConnectAccounts do
use Ecto.Migration

def change do
alter table(:stripe_connect_accounts) do
add :tos_acceptance_date, :datetime
add :tos_acceptance_ip, :string
add :tos_acceptance_user_agent, :string
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
defmodule CodeCorps.Repo.Migrations.ConvertStripeTimeStampsToIntegers do
use Ecto.Migration

def up do
alter table(:stripe_connect_accounts) do
remove :tos_acceptance_date
remove :verification_due_by

add :tos_acceptance_date, :integer
add :verification_due_by, :integer
end

alter table(:stripe_platform_customers) do
remove :created
add :created, :integer
end

alter table(:stripe_connect_plans) do
remove :created
add :created, :integer
end

alter table(:stripe_connect_subscriptions) do
remove :cancelled_at
remove :created
remove :current_period_end
remove :current_period_start
remove :ended_at
remove :start

add :cancelled_at, :integer
add :created, :integer
add :current_period_end, :integer
add :current_period_start, :integer
add :ended_at, :integer
add :start, :integer
end

alter table(:stripe_file_upload) do
remove :created

add :created, :integer
end
end

def down do
alter table(:stripe_connect_accounts) do
remove :tos_acceptance_date
remove :verification_due_by

add :tos_acceptance_date, :datetime
add :verification_due_by, :datetime
end

alter table(:stripe_platform_customers) do
remove :created
add :created, :datetime
end

alter table(:stripe_connect_plans) do
remove :created
add :created, :datetime
end

alter table(:stripe_connect_subscriptions) do
remove :cancelled_at
remove :created
remove :current_period_end
remove :current_period_start
remove :ended_at
remove :start

add :cancelled_at, :datetime
add :created, :datetime
add :current_period_end, :datetime
add :current_period_start, :datetime
add :ended_at, :datetime
add :start, :datetime
end

alter table(:stripe_file_upload) do
remove :created

add :created, :datetime
end
end
end
15 changes: 13 additions & 2 deletions test/controllers/stripe_connect_account_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,22 @@ defmodule CodeCorps.StripeConnectAccountControllerTest do
test "creates and renders resource when user is authenticated and authorized", %{conn: conn, current_user: current_user} do
organization = insert(:organization)
insert(:organization_membership, member: current_user, organization: organization, role: "owner")
attrs = %{organization: organization, country: "US"}
assert conn |> request_create(attrs) |> json_response(201)

attrs = %{organization: organization, country: "US", tos_acceptance_date: 123456}

response = conn |> put_req_header("user-agent", "Test agent") |> request_create(attrs)
assert response |> json_response(201)

user_id = current_user.id
assert_received {:track, ^user_id, "Created Stripe Connect Account", %{}}

account = StripeConnectAccount |> Repo.one

assert account.tos_acceptance_date
request_ip = CodeCorps.ConnUtils.extract_ip(response)
assert account.tos_acceptance_ip == request_ip
request_user_agent = CodeCorps.ConnUtils.extract_user_agent(response)
assert account.tos_acceptance_user_agent == request_user_agent
end

test "does not create resource and renders 401 when unauthenticated", %{conn: conn} do
Expand Down
21 changes: 21 additions & 0 deletions test/lib/code_corps/conn_utils_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule CodeCorps.ConnUtilsTest do
use CodeCorps.ConnCase

alias CodeCorps.ConnUtils

defp conn_with_ip(ip) do
%Plug.Conn{remote_ip: ip}
end

describe "extract_ip/1" do
test "extracts IP address from the request properly" do
assert conn_with_ip({151, 236, 219, 228}) |> ConnUtils.extract_ip == "151.236.219.228"
end
end

describe "extract_user_agent/1" do
test "extracts User Agent from the request properly", %{conn: conn} do
assert conn |> put_req_header("user-agent", "Some agent") |> ConnUtils.extract_user_agent == "Some agent"
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was going to ask for these tests, but here they are! ❤️

end
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ defmodule CodeCorps.StripeService.Adapters.StripeConnectAccountTestAdapter do
support_email: nil,
support_phone: "1234567890",
timezone: "US/Pacific",
tos_acceptance: %{
date: 123456,
ip: "127.0.0.1",
user_agent: "Chrome"
},
transfers_enabled: false,
verification: %{
disabled_reason: "fields_needed",
Expand Down Expand Up @@ -141,6 +146,10 @@ defmodule CodeCorps.StripeService.Adapters.StripeConnectAccountTestAdapter do

"transfers_enabled" => false,

"tos_acceptance_date" => 123456,
"tos_acceptance_ip" => "127.0.0.1",
"tos_acceptance_user_agent" => "Chrome",

"verification_disabled_reason" => "fields_needed",
"verification_due_by" => nil,
"verification_fields_needed" => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ defmodule CodeCorps.StripeService.StripeConnectAccountServiceTest do
test "creates a StripeConnectAccount" do
organization = insert(:organization)

attributes = %{"country" => "US", "organization_id" => organization.id}
attributes = %{
"country" => "US",
"organization_id" => organization.id,
"tos_acceptance_date" => 123456
}

{:ok, %StripeConnectAccount{} = connect_account} =
StripeConnectAccountService.create(attributes)

assert connect_account.country == "US"
assert connect_account.organization_id == organization.id
assert connect_account.managed == true
assert connect_account.tos_acceptance_date == 123456
end
end

Expand Down
Loading