-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #488 from code-corps/462-add-subscription-controll…
…er-and-event-basics Add subscription controller and event basics
- Loading branch information
Showing
48 changed files
with
454 additions
and
54 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...stripe/adapters/stripe_connect_account.ex → ...ervice/adapters/stripe_connect_account.ex
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
35 changes: 35 additions & 0 deletions
35
lib/code_corps/stripe_service/adapters/stripe_connect_card.ex
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,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
33
lib/code_corps/stripe_service/adapters/stripe_connect_customer.ex
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,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 |
2 changes: 1 addition & 1 deletion
2
...ps/stripe/adapters/stripe_connect_plan.ex → ...e_service/adapters/stripe_connect_plan.ex
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
2 changes: 1 addition & 1 deletion
2
...s/stripe/adapters/stripe_platform_card.ex → ..._service/adapters/stripe_platform_card.ex
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
2 changes: 1 addition & 1 deletion
2
...ripe/adapters/stripe_platform_customer.ex → ...vice/adapters/stripe_platform_customer.ex
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,5 @@ | ||
defmodule CodeCorps.StripeService.Events.AccountUpdated do | ||
def perform(event) do | ||
IO.inspect event | ||
end | ||
end |
5 changes: 5 additions & 0 deletions
5
lib/code_corps/stripe_service/events/customer_subscription_updated.ex
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,5 @@ | ||
defmodule CodeCorps.StripeService.Events.CustomerSubscriptionUpdated do | ||
def perform(event) do | ||
IO.inspect event | ||
end | ||
end |
4 changes: 2 additions & 2 deletions
4
...de_corps/stripe/stripe_connect_account.ex → .../stripe_service/stripe_connect_account.ex
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,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 |
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,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 |
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.