11defmodule CodeCorps.StripeService.Adapters.StripeConnectAccountAdapter do
22 import CodeCorps.MapUtils , only: [ keys_to_string: 1 ]
3- import CodeCorps.StripeService.Util , only: [ transform_map: 2 ]
3+ import CodeCorps.StripeService.Util , only: [ transform_attributes: 2 , transform_map: 2 ]
44
5- @ stripe_attributes [
6- :business_name , :business_url , :charges_enabled , :country , :default_currency , :details_submitted , :email , :id , :managed ,
7- :support_email , :support_phone , :support_url , :transfers_enabled
8- ]
9-
10- @ doc """
11- Mapping of stripe record attributes to locally stored attributes
12- Format is {:local_key, [:nesting, :of, :stripe, :keys]}
13- """
5+ # Mapping of stripe record attributes to locally stored attributes
6+ # Format is {:local_key, [:nesting, :of, :stripe, :keys]}
147 @ stripe_mapping [
158 { :id_from_stripe , [ :id ] } ,
169 { :business_name , [ :business_name ] } ,
@@ -45,7 +38,9 @@ defmodule CodeCorps.StripeService.Adapters.StripeConnectAccountAdapter do
4538 { :legal_entity_personal_address_postal_code , [ :legal_entity , :personal_address , :postal_code ] } ,
4639 { :legal_entity_personal_address_state , [ :legal_entity , :personal_address , :state ] } ,
4740 { :legal_entity_phone_number , [ :legal_entity , :phone_number ] } ,
41+ { :legal_entity_personal_id_number , [ :legal_entity , :personal_id_number ] } ,
4842 { :legal_entity_personal_id_number_provided , [ :legal_entity , :personal_id_number_provided ] } ,
43+ { :legal_entity_ssn_last_4 , [ :legal_entity , :ssn_last_4 ] } ,
4944 { :legal_entity_ssn_last_4_provided , [ :legal_entity , :ssn_last_4_provided ] } ,
5045 { :legal_entity_type , [ :legal_entity , :type ] } ,
5146 { :legal_entity_verification_details , [ :legal_entity , :verification , :details ] } ,
@@ -62,6 +57,19 @@ defmodule CodeCorps.StripeService.Adapters.StripeConnectAccountAdapter do
6257 { :verification_fields_needed , [ :verification , :fields_needed ] }
6358 ]
6459
60+ @ doc """
61+ Transforms a set of local attributes into a map of parameters used to
62+ update a `%Stripe.Account{}`.
63+ """
64+ def from_params ( % { } = attributes ) do
65+ result =
66+ attributes
67+ |> remove_attributes ( )
68+ |> transform_attributes ( @ stripe_mapping )
69+
70+ { :ok , result }
71+ end
72+
6573 @ doc """
6674 Transforms a `%Stripe.Account{}` and a set of local attributes into a
6775 map of parameters used to create or update a `StripeConnectAccount` record.
@@ -89,21 +97,23 @@ defmodule CodeCorps.StripeService.Adapters.StripeConnectAccountAdapter do
8997 end
9098
9199 defp get_non_stripe_attributes ( % { } = attributes ) do
92- attributes
93- |> Map . take ( @ non_stripe_attributes )
100+ attributes |> Map . take ( @ non_stripe_attributes )
94101 end
95102
96103 defp add_to ( % { } = attributes , % { } = params ) do
97- params
98- |> Map . merge ( attributes )
104+ params |> Map . merge ( attributes )
99105 end
100106
101107 defp add_nested_attributes ( map , stripe_account ) do
102- map
103- |> add_external_account ( stripe_account )
104- end
108+ map |> add_external_account ( stripe_account )
109+ end
105110
106111 defp add_external_account ( map , % Stripe.Account { external_accounts: % { data: [ ] } } ) , do: map
107112 defp add_external_account ( map , % Stripe.Account { external_accounts: % { data: [ head | _ ] } } ) , do: map |> do_add_external_account ( head )
108113 defp do_add_external_account ( map , % { "id" => id } ) , do: map |> Map . put ( :external_account , id )
114+
115+ defp remove_attributes ( % { "legal_entity_verification_status" => "verified" } = attributes ) do
116+ attributes |> Map . delete ( "legal_entity_verification_document" )
117+ end
118+ defp remove_attributes ( attributes ) , do: attributes
109119end
0 commit comments