Skip to content

Commit 06ffc7d

Browse files
authored
Merge pull request #629 from code-corps/cleanup-formatting-and-error-handling-in-stripe-services
Cleanup formatting & error handling in various stripe services
2 parents 6d29b42 + dff0b26 commit 06ffc7d

9 files changed

Lines changed: 80 additions & 70 deletions

lib/code_corps/stripe_service/stripe_connect_account.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ defmodule CodeCorps.StripeService.StripeConnectAccountService do
1414
{:ok, params} <- StripeConnectAccountAdapter.to_params(account, attributes)
1515
do
1616
%StripeConnectAccount{} |> StripeConnectAccount.create_changeset(params) |> Repo.insert
17+
else
18+
failure -> failure
1719
end
1820
end
1921

@@ -40,8 +42,8 @@ defmodule CodeCorps.StripeService.StripeConnectAccountService do
4042
do
4143
local_account |> StripeConnectAccount.webhook_update_changeset(params) |> Repo.update
4244
else
43-
# Not found locally
4445
nil -> {:error, :not_found}
46+
failure -> failure
4547
end
4648
end
4749
end

lib/code_corps/stripe_service/stripe_connect_card.ex

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ defmodule CodeCorps.StripeService.StripeConnectCardService do
1010

1111
def find_or_create(%StripePlatformCard{} = platform_card, %StripeConnectCustomer{} = connect_customer, %StripePlatformCustomer{} = platform_customer, %StripeConnectAccount{} = connect_account) do
1212
case get_from_db(connect_account.id, platform_card.id) do
13-
%StripeConnectCard{} = existing_card ->
14-
{:ok, existing_card}
15-
nil ->
16-
create(platform_card, connect_customer, platform_customer, connect_account)
13+
%StripeConnectCard{} = existing_card -> {:ok, existing_card}
14+
nil -> create(platform_card, connect_customer, platform_customer, connect_account)
1715
end
1816
end
1917

@@ -22,8 +20,7 @@ defmodule CodeCorps.StripeService.StripeConnectCardService do
2220
do
2321
{:ok, updated_stripe_card}
2422
else
25-
{:error, %Stripe.APIErrorResponse{} = error} -> {:error, error}
26-
_ -> {:error, :unhandled}
23+
failure -> failure
2724
end
2825
end
2926

lib/code_corps/stripe_service/stripe_connect_customer.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ defmodule CodeCorps.StripeService.StripeConnectCustomerService do
3232
%StripeConnectCustomer{}
3333
|> StripeConnectCustomer.create_changeset(params)
3434
|> Repo.insert
35+
else
36+
failure -> failure
3537
end
3638
end
3739

lib/code_corps/stripe_service/stripe_connect_external_account_service.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ defmodule CodeCorps.StripeService.StripeConnectExternalAccountService do
1212
%StripeExternalAccount{}
1313
|> StripeExternalAccount.changeset(params)
1414
|> Repo.insert
15+
else
16+
failure -> failure
1517
end
1618
end
1719

lib/code_corps/stripe_service/stripe_connect_plan.ex

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,31 @@ defmodule CodeCorps.StripeService.StripeConnectPlanService do
55

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

8+
@doc """
9+
Creates a new `Stripe.Plan` record on Stripe API, as well as an associated local
10+
`StripeConnectPlan` record
11+
12+
# Possible return values
13+
14+
- `{:ok, %StripeConnectPlan{}}` - the created record.
15+
- `{:error, %Ecto.Changeset{}}` - the record was not created due to validation issues.
16+
- `{:error, :project_not_ready}` - the associated project does not meed the prerequisites for creating a plan.
17+
- `{:error, %Stripe.APIErrorResponse{}}` - there was a problem with the stripe request
18+
- `{:error, :not_found}` - one of the associated records was not found
19+
"""
820
def create(%{"project_id" => project_id} = attributes) do
921
with {:ok, %Project{} = project} <- get_project(project_id) |> ProjectCanEnableDonations.validate,
10-
%{} = create_attributes <- get_create_attributes(project_id),
11-
connect_account_id <- project.organization.stripe_connect_account.id_from_stripe,
12-
{:ok, plan} <- @api.Plan.create(create_attributes, connect_account: connect_account_id),
13-
{:ok, params} <- StripeConnectPlanAdapter.to_params(plan, attributes)
22+
%{} = create_attributes <- get_create_attributes(project_id),
23+
connect_account_id <- project.organization.stripe_connect_account.id_from_stripe,
24+
{:ok, plan} <- @api.Plan.create(create_attributes, connect_account: connect_account_id),
25+
{:ok, params} <- StripeConnectPlanAdapter.to_params(plan, attributes)
1426
do
1527
%StripeConnectPlan{}
1628
|> StripeConnectPlan.create_changeset(params)
1729
|> Repo.insert
1830
else
19-
{:error, :project_not_ready} -> {:error, :project_not_ready}
20-
{:error, error} -> {:error, error}
2131
nil -> {:error, :not_found}
32+
failure -> failure
2233
end
2334
end
2435

lib/code_corps/stripe_service/stripe_connect_subscription.ex

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ defmodule CodeCorps.StripeService.StripeConnectSubscriptionService do
1212

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

15+
@doc """
16+
Finds or creates a new `Stripe.Subscription` record on Stripe API, as well as an associated local
17+
`StripeConnectSubscription` record
18+
19+
# Possible return values
20+
21+
- `{:ok, %StripeConnectSubscription{}}` - the created record.
22+
- `{:error, %Ecto.Changeset{}}` - the record was not created due to validation issues.
23+
- `{:error, :project_not_ready}` - the associated project does not meed the prerequisites for receiving donations.
24+
- `{:error, :user_not_ready}` - the associated user does not meet the prerequisits to donate.
25+
- `{:error, %Stripe.APIErrorResponse{}}` - there was a problem with the stripe request
26+
- `{:error, :not_found}` - one of the associated records was not found
27+
28+
# Side effects
29+
30+
- If the subscription is created or found, associated project totals will get updated
31+
- If the subscription is created or found, associated donation goal states will be updated
32+
"""
1533
def find_or_create(%{"project_id" => project_id, "quantity" => _, "user_id" => user_id} = attributes) do
1634
with {:ok, %Project{} = project} <- get_project(project_id) |> ProjectSubscribable.validate,
1735
{:ok, %User{} = user} <- get_user(user_id) |> UserCanSubscribe.validate
@@ -23,18 +41,21 @@ defmodule CodeCorps.StripeService.StripeConnectSubscriptionService do
2341

2442
{:ok, subscription}
2543
else
26-
# possible errors
27-
# {:error, :project_not_ready} - `CodeCorps.ProjectSubscribable.validate/1` failed
28-
# {:error, :user_not_ready} - `CodeCorps.UserCanSubscribe.validate/1` failed
29-
# {:error, %Ecto.Changeset{}} - Record creation failed due to validation errors
30-
# {:error, %Stripe.APIErrorResponse{}} - Stripe request failed
31-
# {:error, :not_found} - One of the associated records was not found
32-
{:error, error} -> {:error, error}
3344
nil -> {:error, :not_found}
34-
_ -> {:error, :unexpected}
45+
failure -> failure
3546
end
3647
end
3748

49+
@doc """
50+
Updates an existing `StripeConnectSubscription` record by retrieving a `Stripe.Subscription` record and
51+
using that data as update parameters
52+
53+
# Possible return values
54+
- `{:ok, %StripeConnectSubscription{}}` - the updated record.
55+
- `{:error, %Stripe.APIErrorResponse{}}` - there was a problem with the stripe request
56+
- `{:error, :not_found}` - one of the associated records was not found
57+
58+
"""
3859
def update_from_stripe(stripe_id, connect_customer_id) do
3960
with {:ok, %StripeConnectAccount{} = connect_account} <- retrieve_connect_account(connect_customer_id),
4061
{:ok, %Stripe.Subscription{} = stripe_subscription} <- @api.Subscription.retrieve(stripe_id, connect_account: connect_account.id),
@@ -49,13 +70,8 @@ defmodule CodeCorps.StripeService.StripeConnectSubscriptionService do
4970

5071
{:ok, subscription}
5172
else
52-
# possible errors
53-
# {:error, %Ecto.Changeset{}} - Record creation failed due to validation errors
54-
# {:error, %Stripe.APIErrorResponse{}} - Stripe request failed
55-
# {:error, :not_found} - One of the associated records was not found
56-
{:error, error} -> {:error, error}
5773
nil -> {:error, :not_found}
58-
_ -> {:error, :unexpected}
74+
failure -> failure
5975
end
6076
end
6177

lib/code_corps/stripe_service/stripe_invoice_service.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ defmodule CodeCorps.StripeService.StripeInvoiceService do
1515
|> StripeInvoice.create_changeset(params)
1616
|> Repo.insert
1717
|> CodeCorps.Analytics.Segment.track(:payment_succeeded, nil)
18+
else
19+
failure -> failure
1820
end
1921
end
2022

lib/code_corps/stripe_service/stripe_platform_card.ex

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,29 @@ defmodule CodeCorps.StripeService.StripePlatformCardService do
99
@api Application.get_env(:code_corps, :stripe)
1010

1111
def create(%{"stripe_token" => stripe_token, "user_id" => user_id} = attributes) do
12-
with %StripePlatformCustomer{} = customer <- get_customer(user_id),
13-
{:ok, card} <- @api.Card.create(:customer, customer.id_from_stripe, stripe_token),
14-
{:ok, params} <- StripePlatformCardAdapter.to_params(card, attributes)
12+
with %StripePlatformCustomer{} = customer <- StripePlatformCustomer |> CodeCorps.Repo.get_by(user_id: user_id),
13+
{:ok, %Stripe.Card{} = card} <- @api.Card.create(:customer, customer.id_from_stripe, stripe_token),
14+
{:ok, params} <- StripePlatformCardAdapter.to_params(card, attributes)
1515
do
16-
%StripePlatformCard{}
17-
|> StripePlatformCard.create_changeset(params)
18-
|> Repo.insert
16+
%StripePlatformCard{} |> StripePlatformCard.create_changeset(params) |> Repo.insert
1917
else
2018
nil -> {:error, :not_found}
21-
{:error, %Stripe.APIErrorResponse{} = error} -> {:error, error}
22-
{:error, %Ecto.Changeset{} = changeset} -> {:error, changeset}
19+
failure -> failure
2320
end
2421
end
2522

2623
def update_from_stripe(card_id) do
27-
with {:ok, %StripePlatformCard{} = record} <- get_card(card_id),
28-
{:ok, %Stripe.Card{} = stripe_card} <- get_card_from_stripe(record),
29-
{:ok, params} <- StripePlatformCardAdapter.to_params(stripe_card, %{})
24+
with %StripePlatformCard{} = record <- Repo.get_by(StripePlatformCard, id_from_stripe: card_id),
25+
{:ok, %Stripe.Card{} = stripe_card} <- @api.Card.retrieve(:customer, record.customer_id_from_stripe, card_id),
26+
{:ok, params} <- StripePlatformCardAdapter.to_params(stripe_card, %{})
3027
do
3128
perform_update(record, params)
3229
else
3330
nil -> {:error, :not_found}
34-
{:error, %Stripe.APIErrorResponse{} = error} -> {:error, error}
35-
{:error, %Ecto.Changeset{} = changeset} -> {:error, changeset}
31+
failure -> failure
3632
end
3733
end
3834

39-
defp get_customer(user_id) do
40-
StripePlatformCustomer
41-
|> CodeCorps.Repo.get_by(user_id: user_id)
42-
end
43-
44-
defp get_card(card_id_from_stripe) do
45-
record = Repo.get_by(StripePlatformCard, id_from_stripe: card_id_from_stripe)
46-
{:ok, record}
47-
end
48-
49-
defp get_card_from_stripe(%StripePlatformCard{id_from_stripe: stripe_id, customer_id_from_stripe: owner_id}) do
50-
@api.Card.retrieve(:customer, owner_id, stripe_id)
51-
end
52-
5335
defp perform_update(record, params) do
5436
changeset = record |> StripePlatformCard.update_changeset(params)
5537

@@ -63,8 +45,8 @@ defmodule CodeCorps.StripeService.StripePlatformCardService do
6345
{:ok, platform_card_update, connect_card_updates}
6446
{:error, :update_platform_card, %Ecto.Changeset{} = changeset, %{}} ->
6547
{:error, changeset}
66-
{:error, _failed_operation, _failed_value, _changes_so_far} ->
67-
{:error, :unhandled}
48+
{:error, failed_operation, failed_value, _changes_so_far} ->
49+
{:error, failed_operation, failed_value}
6850
end
6951
end
7052

lib/code_corps/stripe_service/stripe_platform_customer.ex

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ defmodule CodeCorps.StripeService.StripePlatformCustomerService do
1616
%StripePlatformCustomer{}
1717
|> StripePlatformCustomer.create_changeset(params)
1818
|> Repo.insert
19+
else
20+
failure -> failure
1921
end
2022
end
2123

@@ -37,9 +39,7 @@ defmodule CodeCorps.StripeService.StripePlatformCustomerService do
3739
do
3840
{:ok, updated_customer, stripe_customer}
3941
else
40-
{:error, %Ecto.Changeset{} = changeset} -> {:error, changeset}
41-
{:error, %Stripe.APIErrorResponse{} = error} -> {:error, error}
42-
_ -> {:error, :unhandled}
42+
failure -> failure
4343
end
4444
end
4545

@@ -56,17 +56,14 @@ defmodule CodeCorps.StripeService.StripePlatformCustomerService do
5656
- `{:error, :unhandled}` -if something unexpected went wrong
5757
"""
5858
def update_from_stripe(id_from_stripe) do
59-
with %StripePlatformCustomer{} = customer <- Repo.get_by(StripePlatformCustomer, id_from_stripe: id_from_stripe),
60-
{:ok, %Stripe.Customer{} = stripe_customer} <- @api.Customer.retrieve(id_from_stripe),
61-
{:ok, params} <- StripePlatformCustomerAdapter.to_params(stripe_customer, %{}),
62-
{:ok, %StripePlatformCustomer{} = platform_customer, connect_customer_updates} <- perform_update(customer, params)
59+
with %StripePlatformCustomer{} = customer <- Repo.get_by(StripePlatformCustomer, id_from_stripe: id_from_stripe),
60+
{:ok, %Stripe.Customer{} = stripe_customer} <- @api.Customer.retrieve(id_from_stripe),
61+
{:ok, params} <- StripePlatformCustomerAdapter.to_params(stripe_customer, %{})
6362
do
64-
{:ok, platform_customer, connect_customer_updates}
63+
perform_update(customer, params)
6564
else
6665
nil -> {:error, :not_found}
67-
{:error, %Ecto.Changeset{} = changeset} -> {:error, changeset}
68-
{:error, %Stripe.APIErrorResponse{} = error} -> {:error, error}
69-
_ -> {:error, :unhandled}
66+
failure -> failure
7067
end
7168
end
7269

@@ -91,8 +88,8 @@ defmodule CodeCorps.StripeService.StripePlatformCustomerService do
9188
{:ok, platform_customer, update_connect_customers_results}
9289
{:error, :update_platform_customer, %Ecto.Changeset{} = changeset, %{}} ->
9390
{:error, changeset}
94-
{:error, _failed_operation, _failed_value, _changes_so_far} ->
95-
{:error, :unhandled}
91+
{:error, failed_operation, failed_value, _changes_so_far} ->
92+
{:error, failed_operation, failed_value}
9693
end
9794
end
9895

@@ -101,7 +98,6 @@ defmodule CodeCorps.StripeService.StripePlatformCustomerService do
10198
{:ok, platform_customer, nil}
10299
else
103100
{:error, changeset} -> {:error, changeset}
104-
_ -> {:error, :unhandled}
105101
end
106102
end
107103

0 commit comments

Comments
 (0)