Skip to content

Commit

Permalink
Merge pull request #488 from code-corps/462-add-subscription-controll…
Browse files Browse the repository at this point in the history
…er-and-event-basics

Add subscription controller and event basics
  • Loading branch information
joshsmith committed Nov 25, 2016
2 parents 4f5f9af + 2d9675b commit 755e601
Show file tree
Hide file tree
Showing 48 changed files with 454 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.Stripe.Adapters.StripeConnectAccount do
defmodule CodeCorps.StripeService.Adapters.StripeConnectAccount do
import CodeCorps.MapUtils, only: [rename: 3, keys_to_string: 1]

@stripe_attributes [
Expand Down
35 changes: 35 additions & 0 deletions lib/code_corps/stripe_service/adapters/stripe_connect_card.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule CodeCorps.StripeService.Adapters.StripeConnectCard do
import CodeCorps.MapUtils, only: [rename: 3, keys_to_string: 1]

@stripe_attributes [:id]

def to_params(%Stripe.Card{} = stripe_card, %{} = attributes) do
result =
stripe_card
|> Map.from_struct
|> Map.take(@stripe_attributes)
|> rename(:id, :id_from_stripe)
|> keys_to_string
|> add_non_stripe_attributes(attributes)

{:ok, result}
end

@non_stripe_attributes ["stripe_connect_account_id", "stripe_platform_card_id"]

defp add_non_stripe_attributes(%{} = params, %{} = attributes) do
attributes
|> get_non_stripe_attributes
|> add_to(params)
end

defp get_non_stripe_attributes(%{} = attributes) do
attributes
|> Map.take(@non_stripe_attributes)
end

defp add_to(%{} = attributes, %{} = params) do
params
|> Map.merge(attributes)
end
end
33 changes: 33 additions & 0 deletions lib/code_corps/stripe_service/adapters/stripe_connect_customer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule CodeCorps.StripeService.Adapters.StripeConnectCustomer do
import CodeCorps.MapUtils, only: [rename: 3, keys_to_string: 1]

def to_params(%Stripe.Customer{} = customer, %{} = attributes) do
result =
customer
|> Map.from_struct
|> Map.take([:id])
|> rename(:id, :id_from_stripe)
|> keys_to_string
|> add_non_stripe_attributes(attributes)

{:ok, result}
end

@non_stripe_attributes ["stripe_connect_account_id", "stripe_platform_customer_id"]

defp add_non_stripe_attributes(%{} = params, %{} = attributes) do
attributes
|> get_non_stripe_attributes
|> add_to(params)
end

defp get_non_stripe_attributes(%{} = attributes) do
attributes
|> Map.take(@non_stripe_attributes)
end

defp add_to(%{} = attributes, %{} = params) do
params
|> Map.merge(attributes)
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.Stripe.Adapters.StripeConnectPlan do
defmodule CodeCorps.StripeService.Adapters.StripeConnectPlan do
@moduledoc """
Used for conversion between stripe api payload maps and maps
usable for creation of `StripeConnectPlan` records locally
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
defmodule CodeCorps.Stripe.Adapters.StripeConnectSubscription do
defmodule CodeCorps.StripeService.Adapters.StripeConnectSubscription do
@moduledoc """
Used for conversion between stripe api payload maps and maps
usable for creation of StripeConnectSubscription records locally
"""

alias CodeCorps.Stripe.Adapters

import CodeCorps.MapUtils, only: [rename: 3, keys_to_string: 1]

@stripe_attributes [
Expand Down Expand Up @@ -50,7 +48,7 @@ defmodule CodeCorps.Stripe.Adapters.StripeConnectSubscription do
|> Map.merge(attributes)
end

defp add_plan(subscription, %Stripe.Plan{id: id} = plan) do
defp add_plan(subscription, %Stripe.Plan{id: id}) do
subscription |> Map.put("plan_id_from_stripe", id)
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.Stripe.Adapters.StripePlatformCard do
defmodule CodeCorps.StripeService.Adapters.StripePlatformCard do
import CodeCorps.MapUtils, only: [rename: 3, keys_to_string: 1]

@stripe_attributes [:brand, :customer, :cvc_check, :exp_month, :exp_year, :id, :last4, :name, :user_id]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CodeCorps.Stripe.Adapters.StripePlatformCustomer do
defmodule CodeCorps.StripeService.Adapters.StripePlatformCustomer do
import CodeCorps.MapUtils, only: [rename: 3, keys_to_string: 1]

def to_params(%Stripe.Customer{} = customer, %{} = attributes) do
Expand Down
5 changes: 5 additions & 0 deletions lib/code_corps/stripe_service/events/account_updated.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule CodeCorps.StripeService.Events.AccountUpdated do
def perform(event) do
IO.inspect event
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule CodeCorps.StripeService.Events.CustomerSubscriptionUpdated do
def perform(event) do
IO.inspect event
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule CodeCorps.Stripe.StripeConnectAccount do
alias CodeCorps.Stripe.Adapters
defmodule CodeCorps.StripeService.StripeConnectAccount do
alias CodeCorps.StripeService.Adapters
alias Stripe.Connect.OAuth.TokenResponse

@api Application.get_env(:code_corps, :stripe)
Expand Down
62 changes: 62 additions & 0 deletions lib/code_corps/stripe_service/stripe_connect_card.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule CodeCorps.StripeService.StripeConnectCard do
alias CodeCorps.Repo
alias CodeCorps.StripeService.Adapters
alias CodeCorps.StripeConnectAccount
alias CodeCorps.StripeConnectCard
alias CodeCorps.StripeConnectCustomer
alias CodeCorps.StripePlatformCard
alias CodeCorps.StripePlatformCustomer

import CodeCorps.MapUtils, only: [rename: 3, keys_to_string: 1]
import Ecto.Query # needed for match

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

def find_or_create(%StripePlatformCard{} = platform_card, %StripeConnectCustomer{} = connect_customer, %StripePlatformCustomer{} = platform_customer, %StripeConnectAccount{} = connect_account) do
case get_from_db(connect_account.id, platform_card.id) do
%StripeConnectCard{} = existing_card ->
{:ok, existing_card}
nil ->
create(platform_card, connect_customer, platform_customer, connect_account)
end
end

defp create(%StripePlatformCard{} = platform_card, %StripeConnectCustomer{} = connect_customer, %StripePlatformCustomer{} = platform_customer, %StripeConnectAccount{} = connect_account) do
platform_customer_id = platform_customer.id_from_stripe
platform_card_id = platform_card.id_from_stripe
connect_customer_id = connect_customer.id_from_stripe
connect_account_id = connect_account.id_from_stripe

attributes =
platform_card
|> create_non_stripe_attributes(connect_account)

with {:ok, %Stripe.Token{} = connect_token} <-
@api.Token.create_on_connect_account(platform_customer_id, platform_card_id, connect_account: connect_account_id),
{:ok, %Stripe.Card{} = connect_card} <-
@api.Card.create(:customer, connect_customer_id, connect_token.id, connect_account: connect_account_id),
{:ok, params} <-
Adapters.StripeConnectCard.to_params(connect_card, attributes)
do
%StripeConnectCard{}
|> StripeConnectCard.create_changeset(params)
|> Repo.insert
end
end

defp get_from_db(connect_account_id, platform_card_id) do
StripeConnectCard
|> where([c], c.stripe_connect_account_id == ^connect_account_id)
|> where([c], c.stripe_platform_card_id == ^platform_card_id)
|> Repo.one
end

defp create_non_stripe_attributes(platform_card, connect_account) do
platform_card
|> Map.from_struct
|> Map.take([:id])
|> rename(:id, :stripe_platform_card_id)
|> Map.put(:stripe_connect_account_id, connect_account.id)
|> keys_to_string
end
end
51 changes: 51 additions & 0 deletions lib/code_corps/stripe_service/stripe_connect_customer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defmodule CodeCorps.StripeService.StripeConnectCustomer do
alias CodeCorps.Repo
alias CodeCorps.StripeService.Adapters
alias CodeCorps.StripeConnectAccount
alias CodeCorps.StripeConnectCustomer
alias CodeCorps.StripePlatformCustomer

import CodeCorps.MapUtils, only: [rename: 3, keys_to_string: 1]
import Ecto.Query # needed for match

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

def find_or_create(%StripePlatformCustomer{} = platform_customer, %StripeConnectAccount{} = connect_account) do
case get_from_db(connect_account.id, platform_customer.id) do
%StripeConnectCustomer{} = existing_customer ->
{:ok, existing_customer}
nil ->
create(platform_customer, connect_account)
end
end

defp create(%StripePlatformCustomer{} = platform_customer, %StripeConnectAccount{} = connect_account) do
attributes = platform_customer |> create_non_stripe_attributes(connect_account)

with {:ok, customer} <-
@api.Customer.create(%{}, connect_account: connect_account.id_from_stripe),
{:ok, params} <-
Adapters.StripeConnectCustomer.to_params(customer, attributes)
do
%StripeConnectCustomer{}
|> StripeConnectCustomer.create_changeset(params)
|> Repo.insert
end
end

defp get_from_db(connect_account_id, platform_customer_id) do
StripeConnectCustomer
|> where([c], c.stripe_connect_account_id == ^connect_account_id)
|> where([c], c.stripe_platform_customer_id == ^platform_customer_id)
|> Repo.one
end

defp create_non_stripe_attributes(platform_customer, connect_account) do
platform_customer
|> Map.from_struct
|> Map.take([:id])
|> rename(:id, :stripe_platform_customer_id)
|> Map.put(:stripe_connect_account_id, connect_account.id)
|> keys_to_string
end
end
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
defmodule CodeCorps.Stripe.StripeConnectPlan do
defmodule CodeCorps.StripeService.StripeConnectPlan do
alias CodeCorps.Organization
alias CodeCorps.Project
alias CodeCorps.Repo
alias CodeCorps.Stripe.Adapters
alias CodeCorps.StripeService.Adapters
alias CodeCorps.StripeConnectPlan

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

def create(%{"project_id" => project_id} = attributes) do
with %Project{} = project <- Repo.get(Project, project_id),
create_attributes <- to_create_attributes(project),
%Organization{} = organization <- get_organization(project),
{:ok, plan} <- @api.Plan.create(create_attributes, connect_account: organization.stripe_connect_account.id_from_stripe),
{:ok, params} <- Adapters.StripeConnectPlan.to_params(plan, attributes)
with %Project{} = project <-
Repo.get(Project, project_id),
%{} = create_attributes <-
get_create_attributes(),
%Organization{} = organization <-
get_organization(project),
{:ok, plan} <-
@api.Plan.create(create_attributes, connect_account: organization.stripe_connect_account.id_from_stripe),
{:ok, params} <-
Adapters.StripeConnectPlan.to_params(plan, attributes)
do
%StripeConnectPlan{}
|> StripeConnectPlan.create_changeset(params)
Expand All @@ -23,13 +28,13 @@ defmodule CodeCorps.Stripe.StripeConnectPlan do
end
end

defp to_create_attributes(%Project{title: title}) do
defp get_create_attributes do
%{
amount: 1,
amount: 1, # in cents
currency: "usd",
id: "month",
interval: "month",
name: "Monthly donation to #{title}."
name: "Monthly donation"
}
end

Expand Down
Loading

0 comments on commit 755e601

Please sign in to comment.