mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-04 09:28:11 +00:00
wip
This commit is contained in:
1 parent
f81d40b5bb
commit
176feda73b
4 files changed
+60
-8
No files matched your search
+16
-2
@@ -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<String>,
|
||||
}
|
||||
|
||||
// =============== 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<MySqlPool>,
|
||||
request: Query<CreateCheckoutSessionRequest>,
|
||||
stripe_config: web::Data<StripeConfig>,
|
||||
) -> Result<HttpResponse, CreateCheckoutSessionError>
|
||||
{
|
||||
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);
|
||||
|
||||
+12
-5
@@ -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<String>,
|
||||
}
|
||||
|
||||
// =============== 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<MySqlPool>,
|
||||
request: Query<CreateCheckoutSessionRequest>,
|
||||
stripe_config: web::Data<StripeConfig>,
|
||||
) -> Result<HttpResponse, CreateCheckoutSessionError>
|
||||
{
|
||||
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);
|
||||
|
||||
+11
-1
@@ -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<String> {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
+21
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user