asdhfahsdf

This commit is contained in:
Brandon Thomas committed 2022-09-10 04:26:24 -04:00
1 parent e1d4c0f5f3
commit 4acec068aa
10 files changed
+190 -13

No files matched your search

@@ -6,9 +6,14 @@ use actix_web::{web, HttpResponse, HttpRequest};
use chrono::{DateTime, Utc};
use crate::stripe::stripe_config::StripeConfig;
use crate::stripe::webhook_event_handlers::checkout_session_completed_handler::checkout_session_completed_handler;
use crate::stripe::webhook_event_handlers::customer::customer_created_handler::customer_created_handler;
use crate::stripe::webhook_event_handlers::customer::customer_updated_handler::customer_updated_handler;
use crate::stripe::webhook_event_handlers::customer_subscription::customer_subscription_created_handler::customer_subscription_created_handler;
use crate::stripe::webhook_event_handlers::customer_subscription::customer_subscription_deleted_handler::customer_subscription_deleted_handler;
use crate::stripe::webhook_event_handlers::customer_subscription::customer_subscription_updated_handler::customer_subscription_updated_handler;
use crate::stripe::webhook_event_handlers::invoice_paid_handler::invoice_paid_handler;
use crate::stripe::webhook_event_handlers::invoice::invoice_paid_handler::invoice_paid_handler;
use crate::stripe::webhook_event_handlers::invoice::invoice_payment_failed::invoice_payment_failed_handler;
use crate::stripe::webhook_event_handlers::invoice::invoice_payment_succeeded_handler::invoice_payment_succeeded_handler;
use crate::stripe::webhook_event_handlers::stripe_webhook_error::StripeWebhookError;
use http_server_common::request::get_request_header_optional::get_request_header_optional;
use http_server_common::response::serialize_as_json_error::serialize_as_json_error;
@@ -18,7 +23,7 @@ use sqlx::MySqlPool;
use std::collections::HashMap;
use std::fmt;
use stripe::{EventObject, EventType, PaymentIntentStatus, Webhook};
use crate::stripe::webhook_event_handlers::customer_subscription::customer_subscription_deleted_handler::customer_subscription_deleted_handler;
use crate::stripe::webhook_event_handlers::customer::customer_deleted_handler::customer_deleted_handler;
#[derive(Serialize)]
pub struct StripeWebhookSuccessResponse {
@@ -58,18 +63,18 @@ pub async fn stripe_webhook_handler(
//
// CustomerSubscriptionCreated ------
// CustomerSubscriptionUpdated -------
// InvoicePaymentSucceeded
// CustomerCreated
// CustomerUpdated
//
// Events seen (not yet handled):
//
// CheckoutSessionCompleted
// ChargeSucceeded
// PaymentMethodAttached
// CustomerCreated
// CustomerUpdated
// InvoiceCreated
// InvoiceFinalized
// InvoiceUpdated
// InvoicePaymentSucceeded
// PaymentIntentSucceeded
// PaymentIntentCreated
@@ -99,6 +104,28 @@ pub async fn stripe_webhook_handler(
}
}
// EventType::CheckoutSessionExpired
// EventType::CheckoutSessionAsyncPaymentFailed
// EventType::CheckoutSessionAsyncPaymentSucceeded
// =============== CUSTOMERS ===============
EventType::CustomerCreated => {
if let EventObject::Customer(customer) = webhook_payload.data.object {
let _r = customer_created_handler(&customer)?;
}
}
EventType::CustomerUpdated => {
if let EventObject::Customer(customer) = webhook_payload.data.object {
let _r = customer_updated_handler(&customer)?;
}
}
EventType::CustomerDeleted => {
if let EventObject::Customer(customer) = webhook_payload.data.object {
let _r = customer_deleted_handler(&customer)?;
}
}
// =============== CUSTOMER SUBSCRIPTIONS ===============
EventType::CustomerSubscriptionCreated => {
@@ -116,10 +143,9 @@ pub async fn stripe_webhook_handler(
let _r = customer_subscription_deleted_handler(&subscription)?;
}
}
EventType::CustomerSubscriptionPendingUpdateApplied => {
}
EventType::CustomerSubscriptionPendingUpdateExpired => {
}
// EventType::CustomerSubscriptionPendingUpdateApplied
// EventType::CustomerSubscriptionPendingUpdateExpired
// EventType::CustomerSubscriptionTrialWillEnd
// =============== INVOICES ===============
@@ -127,17 +153,41 @@ pub async fn stripe_webhook_handler(
// TODO: We need to respond to this so we don't hold payments up by 72 hours!
// See: https://stripe.com/docs/billing/subscriptions/webhooks
}
EventType::InvoicePaymentSucceeded => {
if let EventObject::Invoice(invoice) = webhook_payload.data.object {
let _r = invoice_payment_succeeded_handler(&invoice)?;
}
}
EventType::InvoicePaid => {
if let EventObject::Invoice(invoice) = webhook_payload.data.object {
let _r = invoice_paid_handler(&invoice)?;
}
}
EventType::InvoicePaymentFailed => {
if let EventObject::Invoice(invoice) = webhook_payload.data.object {
let _r = invoice_payment_failed_handler(&invoice)?;
}
}
// EventType::InvoiceDeleted
// EventType::InvoiceFinalizationFailed
// EventType::InvoiceFinalized
// EventType::InvoiceItemCreated
// EventType::InvoiceItemDeleted
// EventType::InvoiceItemUpdated
// EventType::InvoiceMarkedUncollectible
// EventType::InvoicePaymentActionRequired
// EventType::InvoiceSent
// EventType::InvoiceUpcoming
// EventType::InvoiceUpdated
// EventType::InvoiceVoided
// =============== PAYMENT INTENTS ===============
EventType::PaymentIntentSucceeded => {
if let EventObject::PaymentIntent(payment_intent) = webhook_payload.data.object {
}
}
EventType::InvoicePaymentFailed => {
}
_ => {},
}
@@ -0,0 +1,22 @@
use container_common::anyhow_result::AnyhowResult;
use crate::stripe::helpers::common_metadata_keys::METADATA_USER_TOKEN;
use crate::stripe::helpers::expand_customer_id::expand_customer_id;
use crate::stripe::webhook_event_handlers::stripe_webhook_error::StripeWebhookError;
use log::warn;
use stripe::Customer;
// Handle event type: 'customer.created'
pub fn customer_created_handler(customer: &Customer) -> Result<(), StripeWebhookError> {
// NB: We'll need this to send them to the "customer portal", which is how they can modify
// or cancel their subscriptions.
let customer_id = customer.id.to_string();
// NB: Our internal user token.
let maybe_user_token = customer.metadata.get(METADATA_USER_TOKEN)
.map(|t| t.to_string());
warn!(">>> customer.created: {:?}, {:?}", customer_id, maybe_user_token);
Ok(())
}
@@ -0,0 +1,22 @@
use container_common::anyhow_result::AnyhowResult;
use crate::stripe::helpers::common_metadata_keys::METADATA_USER_TOKEN;
use crate::stripe::helpers::expand_customer_id::expand_customer_id;
use crate::stripe::webhook_event_handlers::stripe_webhook_error::StripeWebhookError;
use log::warn;
use stripe::Customer;
// Handle event type: 'customer.deleted'
pub fn customer_deleted_handler(customer: &Customer) -> Result<(), StripeWebhookError> {
// NB: We'll need this to send them to the "customer portal", which is how they can modify
// or cancel their subscriptions.
let customer_id = customer.id.to_string();
// NB: Our internal user token.
let maybe_user_token = customer.metadata.get(METADATA_USER_TOKEN)
.map(|t| t.to_string());
warn!(">>> customer.deleted : {:?}, {:?}", customer_id, maybe_user_token);
Ok(())
}
@@ -0,0 +1,22 @@
use container_common::anyhow_result::AnyhowResult;
use crate::stripe::helpers::common_metadata_keys::METADATA_USER_TOKEN;
use crate::stripe::helpers::expand_customer_id::expand_customer_id;
use crate::stripe::webhook_event_handlers::stripe_webhook_error::StripeWebhookError;
use log::warn;
use stripe::Customer;
// Handle event type: 'customer.updated'
pub fn customer_updated_handler(customer: &Customer) -> Result<(), StripeWebhookError> {
// NB: We'll need this to send them to the "customer portal", which is how they can modify
// or cancel their subscriptions.
let customer_id = customer.id.to_string();
// NB: Our internal user token.
let maybe_user_token = customer.metadata.get(METADATA_USER_TOKEN)
.map(|t| t.to_string());
warn!(">>> customer.updated: {:?}, {:?}", customer_id, maybe_user_token);
Ok(())
}
@@ -0,0 +1,3 @@
pub (crate) mod customer_created_handler;
pub (crate) mod customer_deleted_handler;
pub (crate) mod customer_updated_handler;
@@ -5,10 +5,12 @@ use stripe::Invoice;
use crate::stripe::helpers::common_metadata_keys::METADATA_USER_TOKEN;
use crate::stripe::webhook_event_handlers::stripe_webhook_error::StripeWebhookError;
// Handle event type: 'invoice.paid'
//
// https://stripe.com/docs/billing/subscriptions/webhooks :
//
// Sent when the invoice is successfully paid. You can provision access to your product when you
// receive this event and the subscription status is active.
// receive this event and the subscription `status` is `active`.
//
// https://stripe.com/docs/billing/subscriptions/webhooks#active-subscriptions :
//
@@ -0,0 +1,26 @@
use container_common::anyhow_result::AnyhowResult;
use crate::stripe::helpers::common_metadata_keys::METADATA_USER_TOKEN;
use crate::stripe::helpers::expand_customer_id::expand_customer_id;
use crate::stripe::webhook_event_handlers::stripe_webhook_error::StripeWebhookError;
use log::warn;
use stripe::Invoice;
// Handle event type: 'invoice.payment_failed'
pub fn invoice_payment_failed_handler(invoice: &Invoice) -> Result<(), StripeWebhookError> {
let paid_status = invoice.status;
// 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 = invoice.customer
.as_ref()
.map(|c| expand_customer_id(c));
// NB: Our internal user token.
let maybe_user_token = invoice.metadata.get(METADATA_USER_TOKEN)
.map(|t| t.to_string());
warn!(">>> invoice.payment_failed: {:?}, {:?}, {:?}", paid_status, maybe_stripe_customer_id, maybe_user_token);
Ok(())
}
@@ -0,0 +1,26 @@
use container_common::anyhow_result::AnyhowResult;
use crate::stripe::helpers::common_metadata_keys::METADATA_USER_TOKEN;
use crate::stripe::helpers::expand_customer_id::expand_customer_id;
use crate::stripe::webhook_event_handlers::stripe_webhook_error::StripeWebhookError;
use log::warn;
use stripe::Invoice;
// Handle event type: 'invoice.payment_succeeded'
pub fn invoice_payment_succeeded_handler(invoice: &Invoice) -> Result<(), StripeWebhookError> {
let paid_status = invoice.status;
// 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 = invoice.customer
.as_ref()
.map(|c| expand_customer_id(c));
// NB: Our internal user token.
let maybe_user_token = invoice.metadata.get(METADATA_USER_TOKEN)
.map(|t| t.to_string());
warn!(">>> invoice.payment_succeeded: {:?}, {:?}, {:?}", paid_status, maybe_stripe_customer_id, maybe_user_token);
Ok(())
}
@@ -0,0 +1,3 @@
pub (crate) mod invoice_paid_handler;
pub (crate) mod invoice_payment_failed;
pub (crate) mod invoice_payment_succeeded_handler;
@@ -1,5 +1,6 @@
pub mod stripe_webhook_error;
pub (crate) mod checkout_session_completed_handler;
pub (crate) mod customer;
pub (crate) mod customer_subscription;
pub (crate) mod invoice_paid_handler;
pub (crate) mod invoice;