@@ -11,8 +11,8 @@ defmodule Algora.Payments do
1111 alias Algora.Payments.Jobs
1212 alias Algora.Payments.PaymentMethod
1313 alias Algora.Payments.Transaction
14+ alias Algora.PSP
1415 alias Algora.Repo
15- alias Algora.Stripe.ConnectCountries
1616 alias Algora.Util
1717
1818 require Logger
@@ -28,12 +28,12 @@ defmodule Algora.Payments do
2828 end
2929
3030 @ spec create_stripe_session (
31- line_items :: [ Stripe .Session. line_item_data ( ) ] ,
32- payment_intent_data :: Stripe .Session. payment_intent_data ( )
31+ line_items :: [ PSP .Session. line_item_data ( ) ] ,
32+ payment_intent_data :: PSP .Session. payment_intent_data ( )
3333 ) ::
34- { :ok , Stripe.Session . t ( ) } | { :error , Stripe.Error . t ( ) }
34+ { :ok , PSP . session ( ) } | { :error , PSP . error ( ) }
3535 def create_stripe_session ( line_items , payment_intent_data ) do
36- Algora.Stripe .Session. create ( % {
36+ PSP .Session. create ( % {
3737 mode: "payment" ,
3838 billing_address_collection: "required" ,
3939 line_items: line_items ,
@@ -59,7 +59,7 @@ defmodule Algora.Payments do
5959 end
6060
6161 def get_provider_fee_from_invoice ( % { id: id } ) do
62- case Stripe .Invoice. retrieve ( id , expand: [ "charge.balance_transaction" ] ) do
62+ case PSP .Invoice. retrieve ( id , expand: [ "charge.balance_transaction" ] ) do
6363 { :ok , invoice } ->
6464 get_provider_fee_from_balance_transaction ( invoice . charge . balance_transaction )
6565
@@ -71,7 +71,7 @@ defmodule Algora.Payments do
7171 # TODO: This is not used anymore
7272 def get_provider_fee_from_payment_intent ( pi ) do
7373 with [ ch ] <- pi . charges . data ,
74- { :ok , txn } <- Stripe .BalanceTransaction. retrieve ( ch . balance_transaction ) do
74+ { :ok , txn } <- PSP .BalanceTransaction. retrieve ( ch . balance_transaction ) do
7575 get_provider_fee_from_balance_transaction ( txn )
7676 else
7777 _ -> nil
@@ -153,7 +153,7 @@ defmodule Algora.Payments do
153153 end
154154
155155 @ spec fetch_or_create_customer ( user :: User . t ( ) ) ::
156- { :ok , Customer . t ( ) } | { :error , Ecto.Changeset . t ( ) } | { :error , Stripe.Error . t ( ) }
156+ { :ok , Customer . t ( ) } | { :error , Ecto.Changeset . t ( ) } | { :error , PSP . error ( ) }
157157 def fetch_or_create_customer ( user ) do
158158 case fetch_customer_by ( user_id: user . id ) do
159159 { :ok , customer } -> { :ok , customer }
@@ -162,9 +162,9 @@ defmodule Algora.Payments do
162162 end
163163
164164 @ spec create_customer ( user :: User . t ( ) ) ::
165- { :ok , Customer . t ( ) } | { :error , Ecto.Changeset . t ( ) } | { :error , Stripe.Error . t ( ) }
165+ { :ok , Customer . t ( ) } | { :error , Ecto.Changeset . t ( ) } | { :error , PSP . error ( ) }
166166 def create_customer ( user ) do
167- with { :ok , stripe_customer } <- Stripe .Customer. create ( % { name: user . name } ) do
167+ with { :ok , stripe_customer } <- PSP .Customer. create ( % { name: user . name } ) do
168168 % Customer { }
169169 |> Customer . changeset ( % {
170170 provider: "stripe" ,
@@ -177,7 +177,7 @@ defmodule Algora.Payments do
177177 end
178178 end
179179
180- @ spec create_payment_method ( customer :: Customer . t ( ) , payment_method :: Stripe.PaymentMethod . t ( ) ) ::
180+ @ spec create_payment_method ( customer :: Customer . t ( ) , payment_method :: PSP . payment_method ( ) ) ::
181181 { :ok , PaymentMethod . t ( ) } | { :error , Ecto.Changeset . t ( ) }
182182 def create_payment_method ( customer , payment_method ) do
183183 % PaymentMethod { }
@@ -193,9 +193,9 @@ defmodule Algora.Payments do
193193 end
194194
195195 @ spec create_stripe_setup_session ( customer :: Customer . t ( ) , success_url :: String . t ( ) , cancel_url :: String . t ( ) ) ::
196- { :ok , Stripe.Session . t ( ) } | { :error , Stripe.Error . t ( ) }
196+ { :ok , PSP . session ( ) } | { :error , PSP . error ( ) }
197197 def create_stripe_setup_session ( customer , success_url , cancel_url ) do
198- Stripe .Session. create ( % {
198+ PSP .Session. create ( % {
199199 billing_address_collection: "required" ,
200200 mode: "setup" ,
201201 payment_method_types: [ "card" ] ,
@@ -223,7 +223,7 @@ defmodule Algora.Payments do
223223 @ spec create_account ( user :: User . t ( ) , country :: String . t ( ) ) ::
224224 { :ok , Account . t ( ) } | { :error , Ecto.Changeset . t ( ) }
225225 def create_account ( user , country ) do
226- type = ConnectCountries . account_type ( country )
226+ type = PSP. ConnectCountries. account_type ( country )
227227
228228 with { :ok , stripe_account } <- create_stripe_account ( % { country: country , type: type } ) do
229229 attrs = % {
@@ -242,18 +242,18 @@ defmodule Algora.Payments do
242242 end
243243
244244 @ spec create_stripe_account ( attrs :: map ( ) ) ::
245- { :ok , Stripe.Account . t ( ) } | { :error , Stripe.Error . t ( ) }
245+ { :ok , PSP . account ( ) } | { :error , PSP . error ( ) }
246246 defp create_stripe_account ( % { country: country , type: type } ) do
247- case Stripe .Account. create ( % { country: country , type: to_string ( type ) } ) do
247+ case PSP .Account. create ( % { country: country , type: to_string ( type ) } ) do
248248 { :ok , account } -> { :ok , account }
249- { :error , _reason } -> Stripe .Account. create ( % { type: to_string ( type ) } )
249+ { :error , _reason } -> PSP .Account. create ( % { type: to_string ( type ) } )
250250 end
251251 end
252252
253253 @ spec create_account_link ( account :: Account . t ( ) , base_url :: String . t ( ) ) ::
254- { :ok , Stripe.AccountLink . t ( ) } | { :error , Stripe.Error . t ( ) }
254+ { :ok , PSP . account_link ( ) } | { :error , PSP . error ( ) }
255255 def create_account_link ( account , base_url ) do
256- Stripe .AccountLink. create ( % {
256+ PSP .AccountLink. create ( % {
257257 account: account . provider_id ,
258258 refresh_url: "#{ base_url } /callbacks/stripe/refresh" ,
259259 return_url: "#{ base_url } /callbacks/stripe/return" ,
@@ -262,12 +262,12 @@ defmodule Algora.Payments do
262262 end
263263
264264 @ spec create_login_link ( account :: Account . t ( ) ) ::
265- { :ok , Stripe.LoginLink . t ( ) } | { :error , Stripe.Error . t ( ) }
265+ { :ok , PSP . login_link ( ) } | { :error , PSP . error ( ) }
266266 def create_login_link ( account ) do
267- Stripe .LoginLink. create ( account . provider_id , % { } )
267+ PSP .LoginLink. create ( account . provider_id )
268268 end
269269
270- @ spec update_account ( account :: Account . t ( ) , stripe_account :: Stripe.Account . t ( ) ) ::
270+ @ spec update_account ( account :: Account . t ( ) , stripe_account :: PSP . account ( ) ) ::
271271 { :ok , Account . t ( ) } | { :error , Ecto.Changeset . t ( ) }
272272 def update_account ( account , stripe_account ) do
273273 account
@@ -288,10 +288,10 @@ defmodule Algora.Payments do
288288 end
289289
290290 @ spec refresh_stripe_account ( user :: User . t ( ) ) ::
291- { :ok , Account . t ( ) } | { :error , Ecto.Changeset . t ( ) } | { :error , :not_found } | { :error , Stripe.Error . t ( ) }
291+ { :ok , Account . t ( ) } | { :error , Ecto.Changeset . t ( ) } | { :error , :not_found } | { :error , PSP . error ( ) }
292292 def refresh_stripe_account ( user ) do
293293 with { :ok , account } <- fetch_account ( user ) ,
294- { :ok , stripe_account } <- Stripe .Account. retrieve ( account . provider_id , [ ] ) ,
294+ { :ok , stripe_account } <- PSP .Account. retrieve ( account . provider_id ) ,
295295 { :ok , updated_account } <- update_account ( account , stripe_account ) do
296296 user = Accounts . get_user ( account . user_id )
297297
@@ -303,25 +303,25 @@ defmodule Algora.Payments do
303303 end
304304 end
305305
306- @ spec get_service_agreement ( account :: Stripe.Account . t ( ) ) :: String . t ( )
306+ @ spec get_service_agreement ( account :: PSP . account ( ) ) :: String . t ( )
307307 defp get_service_agreement ( % { tos_acceptance: % { service_agreement: agreement } } = _account ) when not is_nil ( agreement ) do
308308 agreement
309309 end
310310
311- @ spec get_service_agreement ( account :: Stripe.Account . t ( ) ) :: String . t ( )
311+ @ spec get_service_agreement ( account :: PSP . account ( ) ) :: String . t ( )
312312 defp get_service_agreement ( % { capabilities: capabilities } ) do
313313 if is_nil ( capabilities [ :card_payments ] ) , do: "recipient" , else: "full"
314314 end
315315
316316 @ spec delete_account ( account :: Account . t ( ) ) :: { :ok , Account . t ( ) } | { :error , Ecto.Changeset . t ( ) }
317317 def delete_account ( account ) do
318- with { :ok , _stripe_account } <- Stripe .Account. delete ( account . provider_id ) do
318+ with { :ok , _stripe_account } <- PSP .Account. delete ( account . provider_id ) do
319319 Repo . delete ( account )
320320 end
321321 end
322322
323323 @ spec execute_pending_transfer ( credit_id :: String . t ( ) ) ::
324- { :ok , Stripe.Transfer . t ( ) } | { :error , :not_found } | { :error , :duplicate_transfer_attempt }
324+ { :ok , PSP . transfer ( ) } | { :error , :not_found } | { :error , :duplicate_transfer_attempt }
325325 def execute_pending_transfer ( credit_id ) do
326326 with { :ok , credit } <- Repo . fetch_by ( Transaction , id: credit_id , type: :credit , status: :succeeded ) do
327327 transfers =
@@ -391,7 +391,7 @@ defmodule Algora.Payments do
391391 end
392392 end
393393
394- @ spec initialize_and_execute_transfer ( credit :: Transaction . t ( ) ) :: { :ok , Stripe.Transfer . t ( ) } | { :error , term ( ) }
394+ @ spec initialize_and_execute_transfer ( credit :: Transaction . t ( ) ) :: { :ok , PSP . transfer ( ) } | { :error , term ( ) }
395395 defp initialize_and_execute_transfer ( % Transaction { } = credit ) do
396396 case fetch_active_account ( credit . user_id ) do
397397 { :ok , account } ->
@@ -452,7 +452,7 @@ defmodule Algora.Payments do
452452 |> Map . merge ( if charge && charge . provider_id , do: % { source_transaction: charge . provider_id } , else: % { } )
453453
454454 # TODO: provide idempotency key
455- case Algora.Stripe .Transfer. create ( transfer_params ) do
455+ case PSP .Transfer. create ( transfer_params ) do
456456 { :ok , transfer } ->
457457 # it's fine if this fails since we'll receive a webhook
458458 transaction
0 commit comments