@@ -361,20 +361,79 @@ pub async fn index_subscriptions(
361
361
)
362
362
} ) ?;
363
363
364
- let stripe_customer_id = DBUser :: get_id ( c. user_id , & pg, & redis)
365
- . await ?
366
- . ok_or_else ( || {
367
- ApiError :: from ( DatabaseError :: Database (
368
- sqlx:: Error :: RowNotFound ,
369
- ) )
370
- } )
371
- . and_then ( |user| {
372
- user. stripe_customer_id . ok_or_else ( || {
364
+ let customer_address = ' a: {
365
+ let stripe_id: stripe:: PaymentIntentId = c
366
+ . payment_platform_id
367
+ . as_ref ( )
368
+ . and_then ( |x| x. parse ( ) . ok ( ) )
369
+ . ok_or_else ( || {
373
370
ApiError :: InvalidInput (
374
- "User has no Stripe customer ID" . to_owned ( ) ,
371
+ "Charge has no payment platform ID" . to_owned ( ) ,
375
372
)
376
- } )
377
- } ) ?;
373
+ } ) ?;
374
+
375
+ // Attempt retrieving the address via the payment intent's payment method
376
+
377
+ let pi = stripe:: PaymentIntent :: retrieve (
378
+ & stripe_client,
379
+ & stripe_id,
380
+ & [ "payment_method" ] ,
381
+ )
382
+ . await ?;
383
+
384
+ let pi_stripe_address = pi
385
+ . payment_method
386
+ . and_then ( |x| x. into_object ( ) )
387
+ . and_then ( |x| x. billing_details . address ) ;
388
+
389
+ match pi_stripe_address {
390
+ Some ( address) => break ' a address,
391
+ None => {
392
+ warn ! ( "PaymentMethod had no address" ) ;
393
+ }
394
+ } ;
395
+
396
+ let stripe_customer_id =
397
+ DBUser :: get_id ( c. user_id , & pg, & redis)
398
+ . await ?
399
+ . ok_or_else ( || {
400
+ ApiError :: from ( DatabaseError :: Database (
401
+ sqlx:: Error :: RowNotFound ,
402
+ ) )
403
+ } )
404
+ . and_then ( |user| {
405
+ user. stripe_customer_id . ok_or_else ( || {
406
+ ApiError :: InvalidInput (
407
+ "User has no Stripe customer ID"
408
+ . to_owned ( ) ,
409
+ )
410
+ } )
411
+ } ) ?;
412
+
413
+ let Ok ( customer_id) : Result < stripe:: CustomerId , _ > =
414
+ stripe_customer_id. parse ( )
415
+ else {
416
+ return Err ( ApiError :: InvalidInput (
417
+ "Charge's Stripe customer ID was invalid"
418
+ . to_owned ( ) ,
419
+ ) ) ;
420
+ } ;
421
+
422
+ let customer = stripe:: Customer :: retrieve (
423
+ & stripe_client,
424
+ & customer_id,
425
+ & [ ] ,
426
+ )
427
+ . await ?;
428
+
429
+ let Some ( stripe_address) = customer. address else {
430
+ return Err ( ApiError :: InvalidInput (
431
+ "Stripe customer had no address" . to_owned ( ) ,
432
+ ) ) ;
433
+ } ;
434
+
435
+ stripe_address
436
+ } ;
378
437
379
438
let tax_id =
380
439
DBProductsTaxIdentifier :: get_price ( c. price_id , & pg)
@@ -383,30 +442,6 @@ pub async fn index_subscriptions(
383
442
DatabaseError :: Database ( sqlx:: Error :: RowNotFound )
384
443
} ) ?;
385
444
386
- let Ok ( customer_id) : Result < stripe:: CustomerId , _ > =
387
- stripe_customer_id. parse ( )
388
- else {
389
- return Err ( ApiError :: InvalidInput (
390
- "Charge's Stripe customer ID was invalid" . to_owned ( ) ,
391
- ) ) ;
392
- } ;
393
-
394
- let customer = stripe:: Customer :: retrieve (
395
- & stripe_client,
396
- & customer_id,
397
- & [ ] ,
398
- )
399
- . await ?;
400
-
401
- let Some ( stripe_address) = customer. address else {
402
- return Err ( ApiError :: InvalidInput (
403
- "Stripe customer had no address" . to_owned ( ) ,
404
- ) ) ;
405
- } ;
406
-
407
- let customer_address =
408
- anrok:: Address :: from_stripe_address ( & stripe_address) ;
409
-
410
445
let tax_platform_id =
411
446
anrok:: transaction_id_stripe_pi ( & payment_intent_id) ;
412
447
@@ -417,7 +452,10 @@ pub async fn index_subscriptions(
417
452
. create_or_update_txn ( & anrok:: Transaction {
418
453
id : tax_platform_id. clone ( ) ,
419
454
fields : anrok:: TransactionFields {
420
- customer_address,
455
+ customer_address :
456
+ anrok:: Address :: from_stripe_address (
457
+ & customer_address,
458
+ ) ,
421
459
currency_code : c. currency_code . clone ( ) ,
422
460
accounting_time : c. due ,
423
461
accounting_time_zone :
0 commit comments