| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use std::collections::HashMap; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 37 |
pub enum SubscriptionProduct { |
| 38 |
|
| 39 |
SyncKit, |
| 40 |
|
| 41 |
SyncKitAppSub, |
| 42 |
|
| 43 |
FanPlus, |
| 44 |
|
| 45 |
CreatorTier, |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
Undetermined, |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 71 |
pub enum CheckoutKind { |
| 72 |
FanPlus, |
| 73 |
CreatorTier, |
| 74 |
SyncKitAppSub, |
| 75 |
|
| 76 |
ProjectSubscription, |
| 77 |
Tip, |
| 78 |
Guest, |
| 79 |
Cart, |
| 80 |
|
| 81 |
Purchase, |
| 82 |
} |
| 83 |
|
| 84 |
impl CheckoutKind { |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
pub fn captures_funds_at_checkout(self) -> bool { |
| 92 |
match self { |
| 93 |
CheckoutKind::FanPlus |
| 94 |
| CheckoutKind::CreatorTier |
| 95 |
| CheckoutKind::SyncKitAppSub |
| 96 |
| CheckoutKind::ProjectSubscription => false, |
| 97 |
CheckoutKind::Tip |
| 98 |
| CheckoutKind::Guest |
| 99 |
| CheckoutKind::Cart |
| 100 |
| CheckoutKind::Purchase => true, |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 114 |
pub enum MnwEventName { |
| 115 |
CheckoutCompletedCart, |
| 116 |
CheckoutCompletedCreatorTier, |
| 117 |
CheckoutCompletedFanPlus, |
| 118 |
CheckoutCompletedPurchase, |
| 119 |
CheckoutCompletedSubscription, |
| 120 |
CheckoutCompletedTip, |
| 121 |
SubscriptionUpdated(SubscriptionProduct), |
| 122 |
SubscriptionDeleted(SubscriptionProduct), |
| 123 |
InvoicePaymentSucceeded(SubscriptionProduct), |
| 124 |
InvoicePaymentFailed(SubscriptionProduct), |
| 125 |
} |
| 126 |
|
| 127 |
impl MnwEventName { |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
pub fn as_str(self) -> &'static str { |
| 135 |
use SubscriptionProduct as P; |
| 136 |
match self { |
| 137 |
MnwEventName::CheckoutCompletedCart => "checkout.session.completed.cart", |
| 138 |
MnwEventName::CheckoutCompletedCreatorTier => "checkout.session.completed.creator_tier", |
| 139 |
MnwEventName::CheckoutCompletedFanPlus => "checkout.session.completed.fan_plus", |
| 140 |
MnwEventName::CheckoutCompletedPurchase => "checkout.session.completed.purchase", |
| 141 |
MnwEventName::CheckoutCompletedSubscription => { |
| 142 |
"checkout.session.completed.subscription" |
| 143 |
} |
| 144 |
MnwEventName::CheckoutCompletedTip => "checkout.session.completed.tip", |
| 145 |
|
| 146 |
MnwEventName::SubscriptionUpdated(P::SyncKit) => { |
| 147 |
"customer.subscription.updated.synckit" |
| 148 |
} |
| 149 |
MnwEventName::SubscriptionUpdated(P::SyncKitAppSub) => { |
| 150 |
"customer.subscription.updated.synckit_app_sub" |
| 151 |
} |
| 152 |
MnwEventName::SubscriptionUpdated(P::FanPlus) => { |
| 153 |
"customer.subscription.updated.fan_plus" |
| 154 |
} |
| 155 |
MnwEventName::SubscriptionUpdated(P::CreatorTier) => { |
| 156 |
"customer.subscription.updated.creator_tier" |
| 157 |
} |
| 158 |
MnwEventName::SubscriptionUpdated(P::Undetermined) => "customer.subscription.updated", |
| 159 |
|
| 160 |
MnwEventName::SubscriptionDeleted(P::SyncKit) => { |
| 161 |
"customer.subscription.deleted.synckit" |
| 162 |
} |
| 163 |
MnwEventName::SubscriptionDeleted(P::SyncKitAppSub) => { |
| 164 |
"customer.subscription.deleted.synckit_app_sub" |
| 165 |
} |
| 166 |
MnwEventName::SubscriptionDeleted(P::FanPlus) => { |
| 167 |
"customer.subscription.deleted.fan_plus" |
| 168 |
} |
| 169 |
MnwEventName::SubscriptionDeleted(P::CreatorTier) => { |
| 170 |
"customer.subscription.deleted.creator_tier" |
| 171 |
} |
| 172 |
MnwEventName::SubscriptionDeleted(P::Undetermined) => "customer.subscription.deleted", |
| 173 |
|
| 174 |
MnwEventName::InvoicePaymentSucceeded(P::SyncKit) => { |
| 175 |
"invoice.payment_succeeded.synckit" |
| 176 |
} |
| 177 |
MnwEventName::InvoicePaymentSucceeded(P::SyncKitAppSub) => { |
| 178 |
"invoice.payment_succeeded.synckit_app_sub" |
| 179 |
} |
| 180 |
MnwEventName::InvoicePaymentSucceeded(P::FanPlus) => { |
| 181 |
"invoice.payment_succeeded.fan_plus" |
| 182 |
} |
| 183 |
MnwEventName::InvoicePaymentSucceeded(P::CreatorTier) => { |
| 184 |
"invoice.payment_succeeded.creator_tier" |
| 185 |
} |
| 186 |
MnwEventName::InvoicePaymentSucceeded(P::Undetermined) => "invoice.payment_succeeded", |
| 187 |
|
| 188 |
MnwEventName::InvoicePaymentFailed(P::SyncKit) => "invoice.payment_failed.synckit", |
| 189 |
MnwEventName::InvoicePaymentFailed(P::FanPlus) => "invoice.payment_failed.fan_plus", |
| 190 |
MnwEventName::InvoicePaymentFailed(P::CreatorTier) => { |
| 191 |
"invoice.payment_failed.creator_tier" |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
MnwEventName::InvoicePaymentFailed(P::SyncKitAppSub | P::Undetermined) => { |
| 196 |
"invoice.payment_failed" |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
impl std::fmt::Display for MnwEventName { |
| 203 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 204 |
f.write_str(self.as_str()) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
#[derive(Debug, Default, Clone)] |
| 216 |
pub struct Presentment { |
| 217 |
pub amount: Option<i64>, |
| 218 |
pub currency: Option<String>, |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
#[derive(Debug, Default, Clone)] |
| 228 |
pub struct CheckoutCompletion { |
| 229 |
pub session_id: String, |
| 230 |
|
| 231 |
|
| 232 |
pub metadata: Option<HashMap<String, String>>, |
| 233 |
pub payment_intent_id: Option<String>, |
| 234 |
pub subscription_id: Option<String>, |
| 235 |
pub customer_id: Option<String>, |
| 236 |
|
| 237 |
pub customer_email: Option<String>, |
| 238 |
|
| 239 |
|
| 240 |
pub amount_subtotal: Option<i64>, |
| 241 |
pub presentment: Option<Presentment>, |
| 242 |
pub currency: Option<String>, |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
pub settled: bool, |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
#[derive(Debug, Clone)] |
| 253 |
pub struct SubscriptionLifecycle { |
| 254 |
pub stripe_subscription_id: String, |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
pub status: String, |
| 263 |
pub cancel_at_period_end: bool, |
| 264 |
|
| 265 |
|
| 266 |
pub current_period: Option<(i64, i64)>, |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
#[derive(Debug, Clone)] |
| 271 |
pub struct InvoiceOutcome { |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
pub subscription_id: Option<String>, |
| 276 |
pub period_start: i64, |
| 277 |
pub period_end: i64, |
| 278 |
|
| 279 |
|
| 280 |
pub is_renewal: bool, |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
#[derive(Debug, Clone)] |
| 285 |
pub struct RefundOutcome { |
| 286 |
pub amount: i64, |
| 287 |
|
| 288 |
pub succeeded: bool, |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
pub mnw_transaction_id: Option<String>, |
| 293 |
pub payment_intent_id: Option<String>, |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
#[derive(Debug)] |
| 302 |
pub enum MnwEvent { |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
Checkout { |
| 311 |
kind: CheckoutKind, |
| 312 |
session: Box<CheckoutCompletion>, |
| 313 |
}, |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
CheckoutAsyncPaymentFailed { |
| 318 |
session_id: String, |
| 319 |
}, |
| 320 |
SubscriptionUpdated(Box<SubscriptionLifecycle>), |
| 321 |
SubscriptionDeleted(Box<SubscriptionLifecycle>), |
| 322 |
InvoicePaymentSucceeded(Box<InvoiceOutcome>), |
| 323 |
InvoicePaymentFailed(Box<InvoiceOutcome>), |
| 324 |
AccountUpdated(Box<super::AccountUpdate>), |
| 325 |
|
| 326 |
|
| 327 |
ChargeRefunded(Option<Box<super::ChargeRefundData>>), |
| 328 |
|
| 329 |
|
| 330 |
RefundSettled(Box<RefundOutcome>), |
| 331 |
|
| 332 |
|
| 333 |
Unhandled { |
| 334 |
stripe_type: String, |
| 335 |
}, |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
use super::{CheckoutSessionView, InvoiceView, RefundView, SubscriptionView}; |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
pub(in crate::payments) fn checkout_kind(meta: Option<&HashMap<String, String>>) -> CheckoutKind { |
| 353 |
use super::{ |
| 354 |
is_cart_checkout, is_creator_tier_checkout, is_fan_plus_checkout, is_guest_checkout, |
| 355 |
is_subscription_checkout, is_synckit_app_sub_checkout, is_tip_checkout, |
| 356 |
}; |
| 357 |
if is_fan_plus_checkout(meta) { |
| 358 |
CheckoutKind::FanPlus |
| 359 |
} else if is_creator_tier_checkout(meta) { |
| 360 |
CheckoutKind::CreatorTier |
| 361 |
} else if is_synckit_app_sub_checkout(meta) { |
| 362 |
CheckoutKind::SyncKitAppSub |
| 363 |
} else if is_subscription_checkout(meta) { |
| 364 |
CheckoutKind::ProjectSubscription |
| 365 |
} else if is_tip_checkout(meta) { |
| 366 |
CheckoutKind::Tip |
| 367 |
} else if is_guest_checkout(meta) { |
| 368 |
CheckoutKind::Guest |
| 369 |
} else if is_cart_checkout(meta) { |
| 370 |
CheckoutKind::Cart |
| 371 |
} else { |
| 372 |
CheckoutKind::Purchase |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
impl From<CheckoutSessionView> for CheckoutCompletion { |
| 377 |
fn from(v: CheckoutSessionView) -> Self { |
| 378 |
let settled = v.payment_settled(); |
| 379 |
CheckoutCompletion { |
| 380 |
session_id: v.id, |
| 381 |
metadata: v.metadata, |
| 382 |
payment_intent_id: v.payment_intent, |
| 383 |
subscription_id: v.subscription, |
| 384 |
customer_id: v.customer, |
| 385 |
customer_email: v.customer_details.and_then(|d| d.email), |
| 386 |
amount_subtotal: v.amount_subtotal, |
| 387 |
presentment: v.presentment_details.map(|p| Presentment { |
| 388 |
amount: p.presentment_amount, |
| 389 |
currency: p.presentment_currency, |
| 390 |
}), |
| 391 |
currency: v.currency, |
| 392 |
settled, |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
impl From<SubscriptionView> for SubscriptionLifecycle { |
| 398 |
fn from(v: SubscriptionView) -> Self { |
| 399 |
let current_period = v.current_period(); |
| 400 |
SubscriptionLifecycle { |
| 401 |
stripe_subscription_id: v.id, |
| 402 |
status: v.status, |
| 403 |
cancel_at_period_end: v.cancel_at_period_end, |
| 404 |
current_period, |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
impl From<InvoiceView> for InvoiceOutcome { |
| 410 |
fn from(v: InvoiceView) -> Self { |
| 411 |
InvoiceOutcome { |
| 412 |
subscription_id: v.subscription_id().map(str::to_string), |
| 413 |
period_start: v.period_start, |
| 414 |
period_end: v.period_end, |
| 415 |
is_renewal: v.is_renewal(), |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
impl From<RefundView> for RefundOutcome { |
| 421 |
fn from(v: RefundView) -> Self { |
| 422 |
RefundOutcome { |
| 423 |
amount: v.amount, |
| 424 |
succeeded: v.is_succeeded(), |
| 425 |
mnw_transaction_id: v.mnw_transaction_id().map(str::to_string), |
| 426 |
payment_intent_id: v.payment_intent, |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
#[cfg(test)] |
| 432 |
mod tests { |
| 433 |
use super::*; |
| 434 |
use SubscriptionProduct as P; |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
#[test] |
| 441 |
fn every_name_is_the_one_already_in_the_log() { |
| 442 |
let expected = [ |
| 443 |
( |
| 444 |
MnwEventName::CheckoutCompletedCart, |
| 445 |
"checkout.session.completed.cart", |
| 446 |
), |
| 447 |
( |
| 448 |
MnwEventName::CheckoutCompletedCreatorTier, |
| 449 |
"checkout.session.completed.creator_tier", |
| 450 |
), |
| 451 |
( |
| 452 |
MnwEventName::CheckoutCompletedFanPlus, |
| 453 |
"checkout.session.completed.fan_plus", |
| 454 |
), |
| 455 |
( |
| 456 |
MnwEventName::CheckoutCompletedPurchase, |
| 457 |
"checkout.session.completed.purchase", |
| 458 |
), |
| 459 |
( |
| 460 |
MnwEventName::CheckoutCompletedSubscription, |
| 461 |
"checkout.session.completed.subscription", |
| 462 |
), |
| 463 |
( |
| 464 |
MnwEventName::CheckoutCompletedTip, |
| 465 |
"checkout.session.completed.tip", |
| 466 |
), |
| 467 |
( |
| 468 |
MnwEventName::SubscriptionUpdated(P::SyncKit), |
| 469 |
"customer.subscription.updated.synckit", |
| 470 |
), |
| 471 |
( |
| 472 |
MnwEventName::SubscriptionUpdated(P::SyncKitAppSub), |
| 473 |
"customer.subscription.updated.synckit_app_sub", |
| 474 |
), |
| 475 |
( |
| 476 |
MnwEventName::SubscriptionUpdated(P::FanPlus), |
| 477 |
"customer.subscription.updated.fan_plus", |
| 478 |
), |
| 479 |
( |
| 480 |
MnwEventName::SubscriptionUpdated(P::CreatorTier), |
| 481 |
"customer.subscription.updated.creator_tier", |
| 482 |
), |
| 483 |
( |
| 484 |
MnwEventName::SubscriptionUpdated(P::Undetermined), |
| 485 |
"customer.subscription.updated", |
| 486 |
), |
| 487 |
( |
| 488 |
MnwEventName::SubscriptionDeleted(P::SyncKit), |
| 489 |
"customer.subscription.deleted.synckit", |
| 490 |
), |
| 491 |
( |
| 492 |
MnwEventName::SubscriptionDeleted(P::SyncKitAppSub), |
| 493 |
"customer.subscription.deleted.synckit_app_sub", |
| 494 |
), |
| 495 |
( |
| 496 |
MnwEventName::SubscriptionDeleted(P::FanPlus), |
| 497 |
"customer.subscription.deleted.fan_plus", |
| 498 |
), |
| 499 |
( |
| 500 |
MnwEventName::SubscriptionDeleted(P::CreatorTier), |
| 501 |
"customer.subscription.deleted.creator_tier", |
| 502 |
), |
| 503 |
( |
| 504 |
MnwEventName::SubscriptionDeleted(P::Undetermined), |
| 505 |
"customer.subscription.deleted", |
| 506 |
), |
| 507 |
( |
| 508 |
MnwEventName::InvoicePaymentSucceeded(P::SyncKit), |
| 509 |
"invoice.payment_succeeded.synckit", |
| 510 |
), |
| 511 |
( |
| 512 |
MnwEventName::InvoicePaymentSucceeded(P::SyncKitAppSub), |
| 513 |
"invoice.payment_succeeded.synckit_app_sub", |
| 514 |
), |
| 515 |
( |
| 516 |
MnwEventName::InvoicePaymentSucceeded(P::FanPlus), |
| 517 |
"invoice.payment_succeeded.fan_plus", |
| 518 |
), |
| 519 |
( |
| 520 |
MnwEventName::InvoicePaymentSucceeded(P::CreatorTier), |
| 521 |
"invoice.payment_succeeded.creator_tier", |
| 522 |
), |
| 523 |
( |
| 524 |
MnwEventName::InvoicePaymentSucceeded(P::Undetermined), |
| 525 |
"invoice.payment_succeeded", |
| 526 |
), |
| 527 |
( |
| 528 |
MnwEventName::InvoicePaymentFailed(P::SyncKit), |
| 529 |
"invoice.payment_failed.synckit", |
| 530 |
), |
| 531 |
( |
| 532 |
MnwEventName::InvoicePaymentFailed(P::FanPlus), |
| 533 |
"invoice.payment_failed.fan_plus", |
| 534 |
), |
| 535 |
( |
| 536 |
MnwEventName::InvoicePaymentFailed(P::CreatorTier), |
| 537 |
"invoice.payment_failed.creator_tier", |
| 538 |
), |
| 539 |
( |
| 540 |
MnwEventName::InvoicePaymentFailed(P::Undetermined), |
| 541 |
"invoice.payment_failed", |
| 542 |
), |
| 543 |
]; |
| 544 |
for (name, want) in expected { |
| 545 |
assert_eq!(name.as_str(), want, "{name:?} changed spelling"); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
#[test] |
| 550 |
fn the_undetermined_product_is_the_bare_name_not_a_siblings_name() { |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
for bare in [ |
| 555 |
MnwEventName::SubscriptionUpdated(P::Undetermined), |
| 556 |
MnwEventName::SubscriptionDeleted(P::Undetermined), |
| 557 |
MnwEventName::InvoicePaymentSucceeded(P::Undetermined), |
| 558 |
] { |
| 559 |
assert!(!bare.as_str().ends_with("_tier")); |
| 560 |
assert!(!bare.as_str().ends_with("fan_plus")); |
| 561 |
assert!(!bare.as_str().ends_with("synckit")); |
| 562 |
assert!(!bare.as_str().ends_with("synckit_app_sub")); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
#[test] |
| 567 |
fn subscription_mode_checkouts_do_not_wait_on_settlement() { |
| 568 |
for kind in [ |
| 569 |
CheckoutKind::FanPlus, |
| 570 |
CheckoutKind::CreatorTier, |
| 571 |
CheckoutKind::SyncKitAppSub, |
| 572 |
CheckoutKind::ProjectSubscription, |
| 573 |
] { |
| 574 |
assert!(!kind.captures_funds_at_checkout(), "{kind:?}"); |
| 575 |
} |
| 576 |
for kind in [ |
| 577 |
CheckoutKind::Tip, |
| 578 |
CheckoutKind::Guest, |
| 579 |
CheckoutKind::Cart, |
| 580 |
CheckoutKind::Purchase, |
| 581 |
] { |
| 582 |
assert!(kind.captures_funds_at_checkout(), "{kind:?}"); |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
|