Skip to content

Commit f2db343

Browse files
committed
Retrieve stripe address from PM
1 parent ed44d3f commit f2db343

File tree

1 file changed

+75
-37
lines changed

1 file changed

+75
-37
lines changed

apps/labrinth/src/queue/billing.rs

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -361,20 +361,79 @@ pub async fn index_subscriptions(
361361
)
362362
})?;
363363

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(|| {
373370
ApiError::InvalidInput(
374-
"User has no Stripe customer ID".to_owned(),
371+
"Charge has no payment platform ID".to_owned(),
375372
)
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+
};
378437

379438
let tax_id =
380439
DBProductsTaxIdentifier::get_price(c.price_id, &pg)
@@ -383,30 +442,6 @@ pub async fn index_subscriptions(
383442
DatabaseError::Database(sqlx::Error::RowNotFound)
384443
})?;
385444

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-
410445
let tax_platform_id =
411446
anrok::transaction_id_stripe_pi(&payment_intent_id);
412447

@@ -417,7 +452,10 @@ pub async fn index_subscriptions(
417452
.create_or_update_txn(&anrok::Transaction {
418453
id: tax_platform_id.clone(),
419454
fields: anrok::TransactionFields {
420-
customer_address,
455+
customer_address:
456+
anrok::Address::from_stripe_address(
457+
&customer_address,
458+
),
421459
currency_code: c.currency_code.clone(),
422460
accounting_time: c.due,
423461
accounting_time_zone:

0 commit comments

Comments
 (0)