From 176feda73b6520ec09a784f0b2f81fd39c2cc471 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sat, 10 Sep 2022 17:41:45 -0400 Subject: [PATCH] wip --- ...pe_create_checkout_session_json_handler.rs | 18 ++++++++++++++-- ...reate_checkout_session_redirect_handler.rs | 17 ++++++++++----- .../stripe_create_checkout_session_shared.rs | 12 ++++++++++- .../http_endpoints/stripe_webhook_handler.rs | 21 +++++++++++++++++++ 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_json_handler.rs b/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_json_handler.rs index c517f052b3..022db464f0 100644 --- a/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_json_handler.rs +++ b/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_json_handler.rs @@ -1,7 +1,7 @@ use actix_web::error::ResponseError; use actix_web::error::UrlencodedError::ContentType; use actix_web::http::{header, StatusCode}; -use actix_web::web::Path; +use actix_web::web::{Path, Query}; use actix_web::{web, HttpResponse, HttpRequest}; use chrono::{DateTime, Utc}; use crate::stripe::http_endpoints::checkout::stripe_create_checkout_session_shared::stripe_create_checkout_session_shared; @@ -14,12 +14,23 @@ use std::collections::HashMap; use std::fmt; use stripe::{CheckoutSession, CheckoutSessionMode, CreateCheckoutSession, CreateCheckoutSessionLineItems}; +// =============== Request =============== + +#[derive(Deserialize)] +pub struct CreateCheckoutSessionRequest { + price_key: Option, +} + +// =============== Success Response =============== + #[derive(Serialize)] pub struct CreateCheckoutSessionSuccessResponse { pub success: bool, pub stripe_checkout_redirect_url: String, } +// =============== Error Response =============== + #[derive(Debug, Serialize)] pub enum CreateCheckoutSessionError { ServerError, @@ -47,12 +58,15 @@ impl fmt::Display for CreateCheckoutSessionError { pub async fn stripe_create_checkout_session_json_handler( _http_request: HttpRequest, _mysql_pool: web::Data, + request: Query, stripe_config: web::Data, ) -> Result { + let price_key = request.price_key.as_deref().unwrap_or("unknown"); + let user_token = Some("U:TEST"); - let url = stripe_create_checkout_session_shared(&stripe_config, user_token) + let url = stripe_create_checkout_session_shared(&stripe_config, price_key, user_token) .await .map_err(|err| { error!("Error creating Stripe checkout session: {:?}", err); diff --git a/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_redirect_handler.rs b/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_redirect_handler.rs index fa1ea08606..192b99b59f 100644 --- a/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_redirect_handler.rs +++ b/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_redirect_handler.rs @@ -1,7 +1,7 @@ use actix_web::error::ResponseError; use actix_web::error::UrlencodedError::ContentType; use actix_web::http::{header, StatusCode}; -use actix_web::web::Path; +use actix_web::web::{Path, Query}; use actix_web::{web, HttpResponse, HttpRequest}; use chrono::{DateTime, Utc}; use crate::stripe::http_endpoints::checkout::stripe_create_checkout_session_shared::stripe_create_checkout_session_shared; @@ -13,11 +13,15 @@ use std::collections::HashMap; use std::fmt; use stripe::{CheckoutSession, CheckoutSessionMode, CreateCheckoutSession, CreateCheckoutSessionLineItems}; -#[derive(Serialize)] -pub struct CreateCheckoutSessionSuccessResponse { - pub success: bool, +// =============== Request =============== + +#[derive(Deserialize)] +pub struct CreateCheckoutSessionRequest { + price_key: Option, } +// =============== Error Response =============== + #[derive(Debug, Serialize)] pub enum CreateCheckoutSessionError { ServerError, @@ -45,12 +49,15 @@ impl fmt::Display for CreateCheckoutSessionError { pub async fn stripe_create_checkout_session_redirect_handler( _http_request: HttpRequest, _mysql_pool: web::Data, + request: Query, stripe_config: web::Data, ) -> Result { + let price_key = request.price_key.as_deref().unwrap_or("unknown"); + let user_token = Some("U:TEST"); - let url = stripe_create_checkout_session_shared(&stripe_config, user_token) + let url = stripe_create_checkout_session_shared(&stripe_config, price_key, user_token) .await .map_err(|err| { error!("Error creating Stripe checkout session: {:?}", err); diff --git a/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_shared.rs b/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_shared.rs index 78219da3e2..d8d848a866 100644 --- a/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_shared.rs +++ b/crates/component_subsystem/billing/src/stripe/http_endpoints/checkout/stripe_create_checkout_session_shared.rs @@ -12,10 +12,14 @@ use crate::stripe::helpers::common_metadata_keys::{METADATA_EMAIL, METADATA_USER pub const PRODUCT_FAKEYOU_BASIC_ID : &'static str = "prod_MMxi2J5y69VPbO"; pub const PRODUCT_FAKEYOU_BASIC_PRICE_ID : &'static str = "price_1LeDnKEU5se17MekVr1iYYNf"; +pub const PRODUCT_ONE_TIME_PURCHASE_ID : &'static str = "prod_MPQ6nWJ4k6lJmw"; +pub const PRODUCT_ONE_TIME_PURCHASE_PRICE_ID : &'static str = "price_1LgbG9EU5se17MekZIw95gEO"; + /// Create a checkout session and return the URL /// If anything fails, treat it as a 500 server error. pub async fn stripe_create_checkout_session_shared( stripe_config: &StripeConfig, + price_key: &str, user_token: Option<&str>, ) -> AnyhowResult { @@ -34,6 +38,12 @@ pub async fn stripe_create_checkout_session_shared( .as_deref() .ok_or(anyhow!("Checkout Cancel URL not configured"))?; + let price_id = match price_key { + "subscription" => PRODUCT_FAKEYOU_BASIC_PRICE_ID, + "one-time" => PRODUCT_ONE_TIME_PURCHASE_PRICE_ID, + _ => return Err(anyhow!("wrong price key!")), + }; + let checkout_session = { let mut params = CreateCheckoutSession::new( cancel_url, @@ -65,7 +75,7 @@ pub async fn stripe_create_checkout_session_shared( params.line_items = Some(vec![ CreateCheckoutSessionLineItems { - price: Some(PRODUCT_FAKEYOU_BASIC_PRICE_ID.to_string()), + price: Some(price_id.to_string()), quantity: Some(1), ..Default::default() } diff --git a/crates/component_subsystem/billing/src/stripe/http_endpoints/stripe_webhook_handler.rs b/crates/component_subsystem/billing/src/stripe/http_endpoints/stripe_webhook_handler.rs index b6b32c44ea..552a804c6c 100644 --- a/crates/component_subsystem/billing/src/stripe/http_endpoints/stripe_webhook_handler.rs +++ b/crates/component_subsystem/billing/src/stripe/http_endpoints/stripe_webhook_handler.rs @@ -32,6 +32,27 @@ pub struct StripeWebhookSuccessResponse { pub success: bool, } +/* + +[2022-09-10T09:41:16Z WARN billing_component::stripe::http_endpoints::stripe_webhook_handler] UNHANDLED STRIPE WEBHOOK EVENT TYPE: ChargeSucceeded +[2022-09-10T09:41:16Z WARN billing_component::stripe::http_endpoints::stripe_webhook_handler] UNHANDLED STRIPE WEBHOOK EVENT TYPE: PaymentMethodAttached +[2022-09-10T09:41:16Z WARN billing_component::stripe::http_endpoints::stripe_webhook_handler] UNHANDLED STRIPE WEBHOOK EVENT TYPE: InvoiceFinalized +[2022-09-10T09:41:17Z WARN billing_component::stripe::http_endpoints::stripe_webhook_handler] UNHANDLED STRIPE WEBHOOK EVENT TYPE: PaymentIntentCreated + + +[2022-09-10T09:41:16Z INFO billing_component::stripe::http_endpoints::stripe_webhook_handler] Stripe webhook event type: InvoiceCreated + +[2022-09-10T09:41:16Z WARN billing_component::stripe::webhook_event_handlers::customer::customer_created_handler] >>> customer.created: "cus_MPEexz47x94d3X", None +[2022-09-10T09:41:16Z ERROR billing_component::stripe::webhook_event_handlers::checkout_session::checkout_session_completed_handler] >>> checkout.session.completed: Some("cus_MPEexz47x94d3X"), None +[2022-09-10T09:41:16Z WARN billing_component::stripe::webhook_event_handlers::customer::customer_updated_handler] >>> customer.updated: "cus_MPEexz47x94d3X", None +[2022-09-10T09:41:16Z ERROR billing_component::stripe::webhook_event_handlers::customer_subscription::customer_subscription_created_handler] >>> customer.subscription.created: "sub_1LgQBZEU5se17MeksLCPvfcq", "cus_MPEexz47x94d3X", Some("U:TEST"), "incomplete" +[2022-09-10T09:41:17Z WARN billing_component::stripe::webhook_event_handlers::invoice::invoice_updated_handler] >>> invoice.updated: Some(true), Some(Paid), Some("cus_MPEexz47x94d3X"), None +[2022-09-10T09:41:17Z ERROR billing_component::stripe::webhook_event_handlers::customer_subscription::customer_subscription_updated_handler] >>> customer.subscription.updated: "sub_1LgQBZEU5se17MeksLCPvfcq", "cus_MPEexz47x94d3X", Some("U:TEST"), "active" +[2022-09-10T09:41:17Z WARN billing_component::stripe::webhook_event_handlers::invoice::invoice_paid_handler] >>> invoice.paid: Some(Paid), Some("cus_MPEexz47x94d3X"), None +[2022-09-10T09:41:17Z WARN billing_component::stripe::webhook_event_handlers::invoice::invoice_payment_succeeded_handler] >>> invoice.payment_succeeded: Some(Paid), Some("cus_MPEexz47x94d3X"), None + + + */ pub async fn stripe_webhook_handler( http_request: HttpRequest, request_body_bytes: Bytes,