@@ -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
0 commit comments