diff --git a/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/handle_webhook_payload.rs b/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/handle_webhook_payload.rs index 46faa3fd35..af819bbbe4 100644 --- a/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/handle_webhook_payload.rs +++ b/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/handle_webhook_payload.rs @@ -165,7 +165,11 @@ pub async fn handle_webhook_payload( EventObject::PaymentIntentSucceeded(payment_intent) => { info!("Event: {}, data: {:?}", webhook_payload.type_, payment_intent); - webhook_summary = payment_intent_succeeded_handler(&payment_intent, stripe_client).await?; + webhook_summary = payment_intent_succeeded_handler( + &payment_intent, + server_environment, + stripe_client + ).await?; } // =============== Ignored =============== 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 7cc49daa89..0c96b5db4b 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 @@ -1,16 +1,22 @@ +use crate::configs::get_artcraft_product_by_stripe_id_and_env::get_artcraft_product_by_stripe_id_and_env; use crate::configs::stripe_artcraft_metadata_keys::STRIPE_ARTCRAFT_METADATA_USER_TOKEN; use crate::endpoints::webhook::webhook_event_handlers::stripe_artcraft_webhook_error::StripeArtcraftWebhookError; use crate::endpoints::webhook::webhook_event_handlers::stripe_artcraft_webhook_summary::StripeArtcraftWebhookSummary; use crate::requests::lookup_purchase_from_payment_intent_success::lookup_purchase_from_payment_intent_success; use crate::utils::expand_ids::expand_customer_id::expand_customer_id; -use log::{error, info}; +use log::{error, info, warn}; +use reusable_types::server_environment::ServerEnvironment; use stripe::Client; use stripe_checkout::checkout_session::ListCheckoutSession; use stripe_shared::PaymentIntent; +use crate::configs::stripe_artcraft_generic_product_info::StripeArtcraftGenericProductInfo; +use crate::configs::subscriptions::stripe_artcraft_subscription_info::StripeArtcraftSubscriptionInfo; +use crate::fulfillment::credits_pack::complete_credits_pack_purchase::complete_credits_pack_purchase; // Handle event type: 'payment_intent.succeeded' pub async fn payment_intent_succeeded_handler( payment_intent: &PaymentIntent, + server_environment: ServerEnvironment, stripe_client: &Client ) -> Result { @@ -52,13 +58,22 @@ pub async fn payment_intent_succeeded_handler( should_ignore_retry = true; action_was_taken = false; } else { - // TODO: Add credits. - - info!(">>> ONE OFF PURCHASE : {:?}", &purchase); - info!(">>> ONE OFF PURCHASE : {:?}", &purchase); - info!(">>> ONE OFF PURCHASE : {:?}", &purchase); - info!(">>> ONE OFF PURCHASE : {:?}", &purchase); - info!(">>> ONE OFF PURCHASE : {:?}", &purchase); + match get_artcraft_product_by_stripe_id_and_env(&purchase.product_id, server_environment) { + None => { + error!("Could not find product for ID: {}", &purchase.product_id); + return Err(StripeArtcraftWebhookError::ServerError("unknown product".to_string())); + } + Some(StripeArtcraftGenericProductInfo::Subscription(subscription)) => { + info!("Do not handle subscriptions as one-off payments: {}", purchase.product_id); + should_ignore_retry = true; + action_was_taken = false; + } + Some(StripeArtcraftGenericProductInfo::CreditsPack(credits_pack)) => { + info!("Fulfilling one-time payment..."); + complete_credits_pack_purchase(credits_pack, purchase.quantity) + .await?; + } + } should_ignore_retry = true; action_was_taken = true; diff --git a/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/stripe_artcraft_webhook_error.rs b/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/stripe_artcraft_webhook_error.rs index 0954accb5d..4567565b8c 100644 --- a/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/stripe_artcraft_webhook_error.rs +++ b/crates/service/plugins/billing_artcraft/src/endpoints/webhook/webhook_event_handlers/stripe_artcraft_webhook_error.rs @@ -41,3 +41,9 @@ impl From for StripeArtcraftWebhookError { StripeArtcraftWebhookError::ServerError(format!("SQLX Error: {:?}", err)) } } + +impl From for StripeArtcraftWebhookError { + fn from(err: anyhow::Error) -> Self { + StripeArtcraftWebhookError::ServerError(format!("Anyhow Error: {:?}", err)) + } +} diff --git a/crates/service/plugins/billing_artcraft/src/fulfillment/credits_pack/mod.rs b/crates/service/plugins/billing_artcraft/src/fulfillment/credits_pack/mod.rs index c4e478fea3..69a87a49ad 100644 --- a/crates/service/plugins/billing_artcraft/src/fulfillment/credits_pack/mod.rs +++ b/crates/service/plugins/billing_artcraft/src/fulfillment/credits_pack/mod.rs @@ -1 +1 @@ -mod complete_credits_pack_purchase; +pub mod complete_credits_pack_purchase; diff --git a/crates/service/plugins/billing_artcraft/src/fulfillment/mod.rs b/crates/service/plugins/billing_artcraft/src/fulfillment/mod.rs index a3c4b91336..ce9ffe768b 100644 --- a/crates/service/plugins/billing_artcraft/src/fulfillment/mod.rs +++ b/crates/service/plugins/billing_artcraft/src/fulfillment/mod.rs @@ -1,2 +1,2 @@ -mod subscriptions; -mod credits_pack; \ No newline at end of file +pub mod subscriptions; +pub mod credits_pack; \ No newline at end of file