From 2467d6f2c71b0719a09b49ca495e342d5afbde52 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 5 Sep 2025 05:28:17 -0400 Subject: [PATCH] wip --- .../payment_intent_succeeded_handler.rs | 63 ++++++++++--------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/payment_intent/payment_intent_succeeded_handler.rs b/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/payment_intent/payment_intent_succeeded_handler.rs index cc70783da3..c5a9e54063 100644 --- a/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/payment_intent/payment_intent_succeeded_handler.rs +++ b/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/payment_intent/payment_intent_succeeded_handler.rs @@ -13,37 +13,9 @@ pub async fn payment_intent_succeeded_handler( payment_intent: &PaymentIntent, stripe_client: &Client ) -> Result { + let payment_intent_id = payment_intent.id.to_string(); - let mut should_ignore_retry = false; - let mut action_was_taken = false; - - // Payment intent events are bare. They have absolutely no context about what they were for. - // No products, no checkout sessions, etc. We'll have to look them up on success. - - info!("Look up payment..."); - - match payment_intent.status { - stripe_shared::PaymentIntentStatus::Succeeded => { - - let purchase = lookup_purchase_from_payment_intent_success(&payment_intent_id, stripe_client) - .await - .map_err(|err| { - error!("Error looking up purchase from payment intent {}: {:?}", &payment_intent_id, err); - StripeArtcraftWebhookError::ServerError("error looking up purchase".to_string()) - })?; - - info!("Found purchase: {:?}", purchase); - info!("Found purchase: {:?}", purchase); - info!("Found purchase: {:?}", purchase); - info!("Found purchase: {:?}", purchase); - info!("Found purchase: {:?}", purchase); - - //should_ignore_retry = true; - }, - _ => {} // Fall-through - } - // NB: We'll need this to send them to the "customer portal", which is how they can modify // or cancel their subscriptions. let maybe_stripe_customer_id = payment_intent.customer @@ -54,6 +26,39 @@ pub async fn payment_intent_succeeded_handler( let maybe_user_token = payment_intent.metadata.get(STRIPE_ARTCRAFT_METADATA_USER_TOKEN) .map(|t| t.to_string()); + let mut should_ignore_retry = false; + let mut action_was_taken = false; + + // Payment intent events are bare. They have absolutely no context about what they were for. + // No products, no checkout sessions, etc. We'll have to look them up on success. + + let payment_succeeded = match payment_intent.status { + stripe_shared::PaymentIntentStatus::Succeeded => true, + _ => false, + }; + + if payment_succeeded { + info!("Payment intent succeeded. Looking up payment..."); + + let purchase = lookup_purchase_from_payment_intent_success(&payment_intent_id, stripe_client) + .await + .map_err(|err| { + error!("Error looking up purchase from payment intent {}: {:?}", &payment_intent_id, err); + StripeArtcraftWebhookError::ServerError("error looking up purchase".to_string()) + })?; + + if purchase.has_invoice { + // Subscription purchase. Let `invoice.paid` event handle this instead. + should_ignore_retry = true; + action_was_taken = false; + } else { + // TODO: Add credits. + + should_ignore_retry = true; + action_was_taken = true; + } + } + Ok(StripeArtcraftWebhookSummary { maybe_user_token, maybe_event_entity_id: Some(payment_intent_id),