-
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 #508 from code-corps/475-handle-customer-source-up…
…dated Handle customer.source.updated, propagate platform card updates to connect card
- Loading branch information
Showing
14 changed files
with
319 additions
and
22 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
lib/code_corps/stripe_service/events/customer_source_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,7 @@ | ||
defmodule CodeCorps.StripeService.Events.CustomerSourceUpdated do | ||
def handle(%{"data" => %{"object" => %{"id" => card_id, "object" => "card"}}}) do | ||
CodeCorps.StripeService.StripePlatformCardService.update_from_stripe(card_id) | ||
end | ||
|
||
def handle(%{"data" => %{"object" => %{"id" => _, "object" => _}}}), do: {:error, :unsupported_object} | ||
end |
2 changes: 0 additions & 2 deletions
2
lib/code_corps/stripe_service/events/customer_subscription_deleted.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: 0 additions & 2 deletions
2
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
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,27 +1,95 @@ | ||
defmodule CodeCorps.StripeService.StripePlatformCardService do | ||
alias CodeCorps.Repo | ||
alias CodeCorps.StripeService.Adapters.StripePlatformCardAdapter | ||
alias CodeCorps.StripePlatformCard | ||
alias CodeCorps.StripePlatformCustomer | ||
alias CodeCorps.StripeService.StripeConnectCardService | ||
alias CodeCorps.{StripeConnectCard, StripePlatformCard, StripePlatformCustomer} | ||
|
||
alias Ecto.Multi | ||
|
||
@api Application.get_env(:code_corps, :stripe) | ||
|
||
def create(%{"stripe_token" => stripe_token, "user_id" => user_id} = attributes) do | ||
with %StripePlatformCustomer{} = customer <- get_customer(user_id), | ||
{:ok, card} <- @api.Card.create(:customer, customer.id_from_stripe, stripe_token), | ||
{:ok, params} <- StripePlatformCardAdapter.to_params(card, attributes) | ||
{:ok, card} <- @api.Card.create(:customer, customer.id_from_stripe, stripe_token), | ||
{:ok, params} <- StripePlatformCardAdapter.to_params(card, attributes) | ||
do | ||
%StripePlatformCard{} | ||
|> StripePlatformCard.create_changeset(params) | ||
|> Repo.insert | ||
else | ||
{:error, error} -> {:error, error} | ||
nil -> {:error, :not_found} | ||
{:error, %Stripe.APIErrorResponse{} = error} -> {:error, error} | ||
{:error, %Ecto.Changeset{} = changeset} -> {:error, changeset} | ||
end | ||
end | ||
|
||
def update_from_stripe(card_id) do | ||
with {:ok, %StripePlatformCard{} = record} <- get_card(card_id), | ||
{:ok, %Stripe.Card{} = stripe_card} <- get_card_from_stripe(record), | ||
{:ok, params} <- StripePlatformCardAdapter.to_params(stripe_card, %{}) | ||
do | ||
perform_update(record, params) | ||
else | ||
nil -> {:error, :not_found} | ||
{:error, %Stripe.APIErrorResponse{} = error} -> {:error, error} | ||
{:error, %Ecto.Changeset{} = changeset} -> {:error, changeset} | ||
end | ||
end | ||
|
||
defp get_customer(user_id) do | ||
StripePlatformCustomer | ||
|> CodeCorps.Repo.get_by(user_id: user_id) | ||
end | ||
|
||
defp get_card(card_id_from_stripe) do | ||
record = Repo.get_by(StripePlatformCard, id_from_stripe: card_id_from_stripe) | ||
{:ok, record} | ||
end | ||
|
||
defp get_card_from_stripe(%StripePlatformCard{id_from_stripe: stripe_id, customer_id_from_stripe: owner_id}) do | ||
@api.Card.retrieve(:customer, owner_id, stripe_id) | ||
end | ||
|
||
defp perform_update(record, params) do | ||
changeset = record |> StripePlatformCard.update_changeset(params) | ||
|
||
multi = | ||
Multi.new | ||
|> Multi.update(:update_platform_card, changeset) | ||
|> Multi.run(:update_connect_cards, &update_connect_cards/1) | ||
|
||
case Repo.transaction(multi) do | ||
{:ok, %{update_platform_card: platform_card_update, update_connect_cards: connect_card_updates}} -> | ||
{:ok, platform_card_update, connect_card_updates} | ||
{:error, :update_platform_card, %Ecto.Changeset{} = changeset, %{}} -> | ||
{:error, changeset} | ||
{:error, _failed_operation, _failed_value, _changes_so_far} -> | ||
{:error, :unhandled} | ||
end | ||
end | ||
|
||
defp update_connect_cards(%{update_platform_card: %StripePlatformCard{} = stripe_platform_card}) do | ||
attributes = connect_card_attributes(stripe_platform_card) | ||
|
||
case do_update_connect_cards(stripe_platform_card, attributes) do | ||
[_h | _t] = results -> {:ok, results} | ||
[] -> {:ok, nil} | ||
end | ||
end | ||
|
||
defp connect_card_attributes(stripe_platform_card) do | ||
stripe_platform_card |> Map.take([:exp_month, :exp_year, :name]) | ||
end | ||
|
||
defp do_update_connect_cards(stripe_platform_card, attributes) when attributes == %{}, do: [] | ||
defp do_update_connect_cards(stripe_platform_card, attributes) do | ||
stripe_platform_card | ||
|> Repo.preload([stripe_connect_cards: [:stripe_connect_account, :stripe_platform_card]]) | ||
|> Map.get(:stripe_connect_cards) | ||
|> Enum.map(&do_update_connect_card(&1, attributes)) | ||
end | ||
|
||
defp do_update_connect_card(%StripeConnectCard{} = stripe_connect_card, attributes) do | ||
stripe_connect_card |> StripeConnectCardService.update(attributes) | ||
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
35 changes: 35 additions & 0 deletions
35
test/lib/code_corps/stripe_service/stripe_connect_card_service_test.exs
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.StripeConnectCardServiceTest do | ||
use ExUnit.Case, async: true | ||
|
||
use CodeCorps.ModelCase | ||
|
||
alias CodeCorps.StripeConnectCard | ||
alias CodeCorps.StripeService.StripeConnectCardService | ||
|
||
describe "update/1" do | ||
@attributes %{name: "John Doe", exp_month: 6, exp_year: 2030} | ||
|
||
test "it just updates the connect card on Stripe API, not locally" do | ||
connect_card = insert(:stripe_connect_card) | ||
|
||
connect_card = | ||
StripeConnectCard | ||
|> Repo.get(connect_card.id) | ||
|> Repo.preload([:stripe_platform_card, :stripe_connect_account]) | ||
|
||
updated_at = connect_card.updated_at | ||
|
||
{:ok, %Stripe.Card{} = stripe_card} = | ||
StripeConnectCardService.update(connect_card, @attributes) | ||
|
||
assert stripe_card.id == connect_card.id_from_stripe | ||
assert stripe_card.name == "John Doe" | ||
assert stripe_card.exp_year == 2030 | ||
assert stripe_card.exp_month == 6 | ||
|
||
connect_card = Repo.get(StripeConnectCard, connect_card.id) | ||
|
||
assert connect_card.updated_at == updated_at | ||
end | ||
end | ||
end |
53 changes: 53 additions & 0 deletions
53
test/lib/code_corps/stripe_service/stripe_platform_card_service_test.exs
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,53 @@ | ||
defmodule CodeCorps.StripeService.StripePlatformCardServiceTest do | ||
use ExUnit.Case, async: true | ||
|
||
use CodeCorps.ModelCase | ||
|
||
alias CodeCorps.StripePlatformCard | ||
alias CodeCorps.StripeService.StripePlatformCardService | ||
|
||
describe "update_from_stripe/1" do | ||
test "it just updates the platform card if there is nothing associated to update" do | ||
platform_card = insert(:stripe_platform_card) | ||
|
||
{:ok, %StripePlatformCard{} = platform_card, nil} = | ||
StripePlatformCardService.update_from_stripe(platform_card.id_from_stripe) | ||
|
||
assert platform_card.exp_year == 2020 | ||
end | ||
|
||
# TODO: We can't really do this test until we are able to mock stripe API data | ||
# test "it returns an {:error, changeset} if there are validation errors with the platform_card" do | ||
# platform_card = insert(:stripe_platform_card) | ||
|
||
# {:error, changeset} = | ||
# StripePlatformCardService.update_from_stripe(platform_card.id_from_stripe) | ||
|
||
# refute changeset.valid? | ||
# end | ||
|
||
test "it also updates the associated connect cards if there are any" do | ||
platform_card = insert(:stripe_platform_card) | ||
|
||
[connect_card_1, connect_card_2] = insert_pair(:stripe_connect_card, stripe_platform_card: platform_card) | ||
|
||
{:ok, %StripePlatformCard{} = platform_card, connect_updates} = | ||
StripePlatformCardService.update_from_stripe(platform_card.id_from_stripe) | ||
|
||
assert platform_card.exp_year == 2020 | ||
|
||
platform_card = Repo.get(StripePlatformCard, platform_card.id) | ||
assert platform_card.exp_year == 2020 | ||
|
||
[ | ||
{:ok, %Stripe.Card{} = stripe_record_1}, | ||
{:ok, %Stripe.Card{} = stripe_record_2} | ||
] = connect_updates | ||
|
||
assert stripe_record_1.id == connect_card_1.id_from_stripe | ||
assert stripe_record_1.exp_year == 2020 | ||
assert stripe_record_2.id == connect_card_2.id_from_stripe | ||
assert stripe_record_2.exp_year == 2020 | ||
end | ||
end | ||
end |
Oops, something went wrong.