| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use std::collections::HashMap; |
| 22 |
|
| 23 |
use stripe::{IdempotencyKey, RequestStrategy, StripeRequest}; |
| 24 |
use stripe_billing::subscription::{ |
| 25 |
CancelSubscription, CreateSubscription, CreateSubscriptionItems, |
| 26 |
CreateSubscriptionItemsPriceData, CreateSubscriptionItemsPriceDataRecurring, |
| 27 |
CreateSubscriptionItemsPriceDataRecurringInterval, RetrieveSubscription, UpdateSubscription, |
| 28 |
UpdateSubscriptionItems, UpdateSubscriptionItemsPriceData, |
| 29 |
UpdateSubscriptionItemsPriceDataRecurring, UpdateSubscriptionItemsPriceDataRecurringInterval, |
| 30 |
UpdateSubscriptionProrationBehavior, |
| 31 |
}; |
| 32 |
use stripe_core::customer::CreateCustomer; |
| 33 |
use stripe_product::product::CreateProduct; |
| 34 |
use stripe_types::Currency; |
| 35 |
|
| 36 |
use super::StripeClient; |
| 37 |
use crate::db::{SyncAppId, UserId}; |
| 38 |
use crate::error::{AppError, Result}; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
fn synckit_idempotency_key(prefix: &str, app_id: SyncAppId) -> Result<IdempotencyKey> { |
| 49 |
IdempotencyKey::new(format!("{prefix}-{app_id}")) |
| 50 |
.map_err(|e| AppError::Internal(anyhow::anyhow!("invalid idempotency key: {e}"))) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
pub struct SynckitSubResult { |
| 56 |
pub subscription_id: String, |
| 57 |
pub current_period_start: i64, |
| 58 |
pub current_period_end: i64, |
| 59 |
} |
| 60 |
|
| 61 |
fn parse_subscription_id(id: &str) -> Result<stripe_shared::SubscriptionId> { |
| 62 |
id.parse().map_err(|e| { |
| 63 |
AppError::Internal(anyhow::anyhow!( |
| 64 |
"Invalid Stripe subscription ID '{id}': {e}" |
| 65 |
)) |
| 66 |
}) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
fn synckit_customer_request( |
| 75 |
developer_user_id: UserId, |
| 76 |
email: &str, |
| 77 |
app_name: &str, |
| 78 |
) -> CreateCustomer { |
| 79 |
let mut metadata = HashMap::new(); |
| 80 |
metadata.insert("mnw_user_id".to_string(), developer_user_id.to_string()); |
| 81 |
metadata.insert("synckit_app_name".to_string(), app_name.to_string()); |
| 82 |
CreateCustomer::new() |
| 83 |
.email(email.to_string()) |
| 84 |
.name(format!("SyncKit: {app_name}")) |
| 85 |
.metadata(metadata) |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
fn synckit_product_request(app_name: &str) -> CreateProduct { |
| 91 |
CreateProduct::new(format!("SyncKit: {app_name}")) |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
fn synckit_subscription_request( |
| 98 |
customer_id: &str, |
| 99 |
app_id: SyncAppId, |
| 100 |
product_id: &str, |
| 101 |
price_cents: i64, |
| 102 |
) -> CreateSubscription { |
| 103 |
let price_data = CreateSubscriptionItemsPriceData { |
| 104 |
currency: Currency::USD, |
| 105 |
product: product_id.to_string(), |
| 106 |
recurring: CreateSubscriptionItemsPriceDataRecurring::new( |
| 107 |
CreateSubscriptionItemsPriceDataRecurringInterval::Month, |
| 108 |
), |
| 109 |
tax_behavior: None, |
| 110 |
unit_amount: Some(price_cents), |
| 111 |
unit_amount_decimal: None, |
| 112 |
}; |
| 113 |
let mut item = CreateSubscriptionItems::new(); |
| 114 |
item.price_data = Some(price_data); |
| 115 |
|
| 116 |
let mut metadata = HashMap::new(); |
| 117 |
metadata.insert("synckit_app_id".to_string(), app_id.to_string()); |
| 118 |
|
| 119 |
CreateSubscription::new() |
| 120 |
.customer(customer_id.to_string()) |
| 121 |
.items(vec![item]) |
| 122 |
.metadata(metadata) |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
fn reprice_request( |
| 129 |
sub_id: stripe_shared::SubscriptionId, |
| 130 |
item_id: &str, |
| 131 |
product_id: &str, |
| 132 |
new_price_cents: i64, |
| 133 |
interval: UpdateSubscriptionItemsPriceDataRecurringInterval, |
| 134 |
proration: UpdateSubscriptionProrationBehavior, |
| 135 |
) -> UpdateSubscription { |
| 136 |
let new_price_data = UpdateSubscriptionItemsPriceData { |
| 137 |
currency: Currency::USD, |
| 138 |
product: product_id.to_string(), |
| 139 |
recurring: UpdateSubscriptionItemsPriceDataRecurring::new(interval), |
| 140 |
tax_behavior: None, |
| 141 |
unit_amount: Some(new_price_cents), |
| 142 |
unit_amount_decimal: None, |
| 143 |
}; |
| 144 |
let item = UpdateSubscriptionItems { |
| 145 |
id: Some(item_id.to_string()), |
| 146 |
price_data: Some(new_price_data), |
| 147 |
..Default::default() |
| 148 |
}; |
| 149 |
UpdateSubscription::new(sub_id) |
| 150 |
.items(vec![item]) |
| 151 |
.proration_behavior(proration) |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
fn app_sub_interval( |
| 156 |
interval: super::SyncBillingInterval, |
| 157 |
) -> UpdateSubscriptionItemsPriceDataRecurringInterval { |
| 158 |
match interval { |
| 159 |
super::SyncBillingInterval::Monthly => { |
| 160 |
UpdateSubscriptionItemsPriceDataRecurringInterval::Month |
| 161 |
} |
| 162 |
super::SyncBillingInterval::Annual => { |
| 163 |
UpdateSubscriptionItemsPriceDataRecurringInterval::Year |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
impl StripeClient { |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
#[tracing::instrument(skip_all, name = "payments::create_synckit_customer")] |
| 174 |
pub async fn create_synckit_customer( |
| 175 |
&self, |
| 176 |
developer_user_id: UserId, |
| 177 |
app_id: SyncAppId, |
| 178 |
email: &str, |
| 179 |
app_name: &str, |
| 180 |
) -> Result<String> { |
| 181 |
let key = synckit_idempotency_key("synckit-customer", app_id)?; |
| 182 |
let customer = synckit_customer_request(developer_user_id, email, app_name) |
| 183 |
.customize() |
| 184 |
.request_strategy(RequestStrategy::Idempotent(key)) |
| 185 |
.send(&self.client) |
| 186 |
.await |
| 187 |
.map_err(|e| { |
| 188 |
tracing::error!(error = ?e, "failed to create SyncKit Stripe customer"); |
| 189 |
AppError::Internal(anyhow::anyhow!("Failed to create Stripe customer")) |
| 190 |
})?; |
| 191 |
|
| 192 |
Ok(customer.id.to_string()) |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
async fn create_synckit_product(&self, app_id: SyncAppId, app_name: &str) -> Result<String> { |
| 198 |
let key = synckit_idempotency_key("synckit-product", app_id)?; |
| 199 |
let product = synckit_product_request(app_name) |
| 200 |
.customize() |
| 201 |
.request_strategy(RequestStrategy::Idempotent(key)) |
| 202 |
.send(&self.client) |
| 203 |
.await |
| 204 |
.map_err(|e| { |
| 205 |
tracing::error!(error = ?e, "failed to create SyncKit Stripe product"); |
| 206 |
AppError::Internal(anyhow::anyhow!("Failed to create Stripe product")) |
| 207 |
})?; |
| 208 |
Ok(product.id.to_string()) |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
#[tracing::instrument(skip_all, name = "payments::create_synckit_subscription")] |
| 217 |
pub async fn create_synckit_subscription( |
| 218 |
&self, |
| 219 |
customer_id: &str, |
| 220 |
app_id: SyncAppId, |
| 221 |
app_name: &str, |
| 222 |
price_cents: i64, |
| 223 |
) -> Result<SynckitSubResult> { |
| 224 |
if price_cents <= 0 { |
| 225 |
return Err(AppError::BadRequest( |
| 226 |
"Subscription price must be positive".to_string(), |
| 227 |
)); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
let product_id = self.create_synckit_product(app_id, app_name).await?; |
| 232 |
|
| 233 |
let key = synckit_idempotency_key("synckit-sub", app_id)?; |
| 234 |
let subscription = synckit_subscription_request( |
| 235 |
customer_id, |
| 236 |
app_id, |
| 237 |
&product_id, |
| 238 |
price_cents, |
| 239 |
) |
| 240 |
.customize() |
| 241 |
.request_strategy(RequestStrategy::Idempotent(key)) |
| 242 |
.send(&self.client) |
| 243 |
.await |
| 244 |
.map_err(|e| { |
| 245 |
tracing::error!(error = ?e, app_id = %app_id, "failed to create SyncKit subscription"); |
| 246 |
AppError::Internal(anyhow::anyhow!("Failed to create Stripe subscription")) |
| 247 |
})?; |
| 248 |
|
| 249 |
let first_item = subscription.items.data.first().ok_or_else(|| { |
| 250 |
AppError::Internal(anyhow::anyhow!("Stripe subscription has no items")) |
| 251 |
})?; |
| 252 |
|
| 253 |
Ok(SynckitSubResult { |
| 254 |
subscription_id: subscription.id.to_string(), |
| 255 |
current_period_start: first_item.current_period_start, |
| 256 |
current_period_end: first_item.current_period_end, |
| 257 |
}) |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
#[tracing::instrument(skip_all, name = "payments::update_synckit_subscription_price")] |
| 265 |
pub async fn update_synckit_subscription_price( |
| 266 |
&self, |
| 267 |
subscription_id: &str, |
| 268 |
new_price_cents: i64, |
| 269 |
app_name: &str, |
| 270 |
) -> Result<()> { |
| 271 |
if new_price_cents <= 0 { |
| 272 |
return Err(AppError::BadRequest( |
| 273 |
"Subscription price must be positive".to_string(), |
| 274 |
)); |
| 275 |
} |
| 276 |
|
| 277 |
let sub_id = parse_subscription_id(subscription_id)?; |
| 278 |
|
| 279 |
|
| 280 |
let existing = RetrieveSubscription::new(sub_id.clone()) |
| 281 |
.send(&self.client) |
| 282 |
.await |
| 283 |
.map_err(|e| { |
| 284 |
tracing::error!(error = ?e, subscription_id = %subscription_id, "failed to retrieve SyncKit subscription"); |
| 285 |
AppError::Internal(anyhow::anyhow!("Failed to retrieve Stripe subscription")) |
| 286 |
})?; |
| 287 |
|
| 288 |
let existing_item = existing.items.data.first().ok_or_else(|| { |
| 289 |
AppError::Internal(anyhow::anyhow!( |
| 290 |
"Stripe subscription {subscription_id} has no items" |
| 291 |
)) |
| 292 |
})?; |
| 293 |
|
| 294 |
|
| 295 |
let product_id = existing_item.price.product.id().to_string(); |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
let _ = app_name; |
| 302 |
|
| 303 |
reprice_request( |
| 304 |
sub_id, |
| 305 |
existing_item.id.as_ref(), |
| 306 |
&product_id, |
| 307 |
new_price_cents, |
| 308 |
UpdateSubscriptionItemsPriceDataRecurringInterval::Month, |
| 309 |
UpdateSubscriptionProrationBehavior::CreateProrations, |
| 310 |
) |
| 311 |
.send(&self.client) |
| 312 |
.await |
| 313 |
.map_err(|e| { |
| 314 |
tracing::error!(error = ?e, subscription_id = %subscription_id, "failed to update SyncKit subscription price"); |
| 315 |
AppError::Internal(anyhow::anyhow!("Failed to update Stripe subscription")) |
| 316 |
})?; |
| 317 |
|
| 318 |
Ok(()) |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
#[tracing::instrument(skip_all, name = "payments::update_synckit_app_sub_price")] |
| 328 |
pub async fn update_synckit_app_sub_price( |
| 329 |
&self, |
| 330 |
subscription_id: &str, |
| 331 |
new_price_cents: i64, |
| 332 |
interval: super::SyncBillingInterval, |
| 333 |
product_name: &str, |
| 334 |
) -> Result<()> { |
| 335 |
if new_price_cents <= 0 { |
| 336 |
return Err(AppError::BadRequest( |
| 337 |
"Subscription price must be positive".to_string(), |
| 338 |
)); |
| 339 |
} |
| 340 |
|
| 341 |
let sub_id = parse_subscription_id(subscription_id)?; |
| 342 |
|
| 343 |
let existing = RetrieveSubscription::new(sub_id.clone()) |
| 344 |
.send(&self.client) |
| 345 |
.await |
| 346 |
.map_err(|e| { |
| 347 |
tracing::error!(error = ?e, subscription_id = %subscription_id, "failed to retrieve app sub"); |
| 348 |
AppError::Internal(anyhow::anyhow!("Failed to retrieve Stripe subscription")) |
| 349 |
})?; |
| 350 |
|
| 351 |
let existing_item = existing.items.data.first().ok_or_else(|| { |
| 352 |
AppError::Internal(anyhow::anyhow!( |
| 353 |
"Stripe subscription {subscription_id} has no items" |
| 354 |
)) |
| 355 |
})?; |
| 356 |
|
| 357 |
let product_id = existing_item.price.product.id().to_string(); |
| 358 |
let _ = product_name; |
| 359 |
|
| 360 |
reprice_request( |
| 361 |
sub_id, |
| 362 |
existing_item.id.as_ref(), |
| 363 |
&product_id, |
| 364 |
new_price_cents, |
| 365 |
app_sub_interval(interval), |
| 366 |
UpdateSubscriptionProrationBehavior::None, |
| 367 |
) |
| 368 |
.send(&self.client) |
| 369 |
.await |
| 370 |
.map_err(|e| { |
| 371 |
tracing::error!(error = ?e, subscription_id = %subscription_id, "failed to re-price app sub"); |
| 372 |
AppError::Internal(anyhow::anyhow!("Failed to update Stripe subscription")) |
| 373 |
})?; |
| 374 |
|
| 375 |
Ok(()) |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
#[tracing::instrument(skip_all, name = "payments::cancel_synckit_subscription")] |
| 386 |
pub async fn cancel_synckit_subscription(&self, subscription_id: &str) -> Result<()> { |
| 387 |
let sub_id = parse_subscription_id(subscription_id)?; |
| 388 |
CancelSubscription::new(sub_id) |
| 389 |
.send(&self.client) |
| 390 |
.await |
| 391 |
.map_err(|e| { |
| 392 |
tracing::error!(error = ?e, subscription_id = %subscription_id, "failed to cancel SyncKit subscription"); |
| 393 |
AppError::Internal(anyhow::anyhow!("Failed to cancel Stripe subscription")) |
| 394 |
})?; |
| 395 |
Ok(()) |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
#[tracing::instrument(skip_all, name = "payments::create_synckit_billing_portal")] |
| 401 |
pub async fn create_synckit_billing_portal( |
| 402 |
&self, |
| 403 |
customer_id: &str, |
| 404 |
return_url: &str, |
| 405 |
) -> Result<String> { |
| 406 |
|
| 407 |
|
| 408 |
self.create_billing_portal_session(customer_id, return_url) |
| 409 |
.await |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
#[cfg(test)] |
| 414 |
mod tests { |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
use super::*; |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
fn form(req: &impl StripeRequest) -> std::collections::BTreeMap<String, String> { |
| 425 |
let built = req.build(); |
| 426 |
let body = built.body.unwrap_or_default(); |
| 427 |
url::form_urlencoded::parse(body.as_bytes()) |
| 428 |
.map(|(k, v)| (k.into_owned(), v.into_owned())) |
| 429 |
.collect() |
| 430 |
} |
| 431 |
|
| 432 |
fn path_of(req: &impl StripeRequest) -> String { |
| 433 |
req.build().path |
| 434 |
} |
| 435 |
|
| 436 |
#[test] |
| 437 |
fn the_same_app_and_prefix_always_produce_the_same_key() { |
| 438 |
let app = SyncAppId::nil(); |
| 439 |
let a = synckit_idempotency_key("synckit-sub", app).expect("valid key"); |
| 440 |
let b = synckit_idempotency_key("synckit-sub", app).expect("valid key"); |
| 441 |
assert_eq!( |
| 442 |
format!("{a:?}"), |
| 443 |
format!("{b:?}"), |
| 444 |
"two racing activates must reuse one Stripe object, not create two" |
| 445 |
); |
| 446 |
} |
| 447 |
|
| 448 |
#[test] |
| 449 |
fn a_different_operation_on_one_app_gets_a_different_key() { |
| 450 |
let app = SyncAppId::nil(); |
| 451 |
let sub = synckit_idempotency_key("synckit-sub", app).expect("valid key"); |
| 452 |
let cust = synckit_idempotency_key("synckit-cust", app).expect("valid key"); |
| 453 |
assert_ne!( |
| 454 |
format!("{sub:?}"), |
| 455 |
format!("{cust:?}"), |
| 456 |
"sharing a key across operations would make Stripe replay the wrong response" |
| 457 |
); |
| 458 |
} |
| 459 |
|
| 460 |
#[test] |
| 461 |
fn an_over_long_prefix_is_an_error_rather_than_a_silently_truncated_key() { |
| 462 |
|
| 463 |
|
| 464 |
let err = synckit_idempotency_key(&"x".repeat(300), SyncAppId::nil()); |
| 465 |
assert!( |
| 466 |
matches!(err, Err(AppError::Internal(_))), |
| 467 |
"an unusable key must not reach Stripe" |
| 468 |
); |
| 469 |
} |
| 470 |
|
| 471 |
#[test] |
| 472 |
fn subscription_id_parsing_rejects_nothing_at_all() { |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
assert!(parse_subscription_id("").is_ok()); |
| 483 |
assert!(parse_subscription_id("not a sub id").is_ok()); |
| 484 |
assert!(parse_subscription_id("acct_wrong_type").is_ok()); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
#[test] |
| 495 |
fn a_synckit_customer_is_the_app_rather_than_the_developer() { |
| 496 |
let req = synckit_customer_request(UserId::nil(), "dev@example.com", "Notes"); |
| 497 |
assert_eq!(path_of(&req), "/customers"); |
| 498 |
let f = form(&req); |
| 499 |
assert_eq!(f.get("email").map(String::as_str), Some("dev@example.com")); |
| 500 |
assert_eq!( |
| 501 |
f.get("name").map(String::as_str), |
| 502 |
Some("SyncKit: Notes"), |
| 503 |
"one customer per app, so the dashboard has to name the app" |
| 504 |
); |
| 505 |
assert_eq!( |
| 506 |
f.get("metadata[synckit_app_name]").map(String::as_str), |
| 507 |
Some("Notes") |
| 508 |
); |
| 509 |
assert_eq!( |
| 510 |
f.get("metadata[mnw_user_id]").map(String::as_str), |
| 511 |
Some(UserId::nil().to_string()).as_deref() |
| 512 |
); |
| 513 |
} |
| 514 |
|
| 515 |
#[test] |
| 516 |
fn a_synckit_product_is_named_for_its_app() { |
| 517 |
let req = synckit_product_request("Notes"); |
| 518 |
assert_eq!(path_of(&req), "/products"); |
| 519 |
assert_eq!( |
| 520 |
form(&req).get("name").map(String::as_str), |
| 521 |
Some("SyncKit: Notes") |
| 522 |
); |
| 523 |
} |
| 524 |
|
| 525 |
#[test] |
| 526 |
fn a_developer_subscription_prices_inline_and_routes_by_app_id() { |
| 527 |
let app = SyncAppId::nil(); |
| 528 |
let req = synckit_subscription_request("cus_123", app, "prod_123", 2500); |
| 529 |
assert_eq!(path_of(&req), "/subscriptions"); |
| 530 |
let f = form(&req); |
| 531 |
assert_eq!(f.get("customer").map(String::as_str), Some("cus_123")); |
| 532 |
assert_eq!( |
| 533 |
f.get("items[0][price_data][product]").map(String::as_str), |
| 534 |
Some("prod_123") |
| 535 |
); |
| 536 |
assert_eq!( |
| 537 |
f.get("items[0][price_data][unit_amount]") |
| 538 |
.map(String::as_str), |
| 539 |
Some("2500") |
| 540 |
); |
| 541 |
assert_eq!( |
| 542 |
f.get("items[0][price_data][currency]").map(String::as_str), |
| 543 |
Some("usd") |
| 544 |
); |
| 545 |
assert_eq!( |
| 546 |
f.get("items[0][price_data][recurring][interval]") |
| 547 |
.map(String::as_str), |
| 548 |
Some("month"), |
| 549 |
"without `recurring` Stripe bills the developer once, not monthly" |
| 550 |
); |
| 551 |
assert_eq!( |
| 552 |
f.get("metadata[synckit_app_id]").map(String::as_str), |
| 553 |
Some(app.to_string()).as_deref(), |
| 554 |
"the webhook dispatcher routes on this; without it the event reads \ |
| 555 |
as a creator-tier one" |
| 556 |
); |
| 557 |
} |
| 558 |
|
| 559 |
#[test] |
| 560 |
fn a_developer_reprice_prorates_and_reuses_the_existing_item() { |
| 561 |
let req = reprice_request( |
| 562 |
"sub_1A2b3C".parse().unwrap(), |
| 563 |
"si_123", |
| 564 |
"prod_123", |
| 565 |
4000, |
| 566 |
UpdateSubscriptionItemsPriceDataRecurringInterval::Month, |
| 567 |
UpdateSubscriptionProrationBehavior::CreateProrations, |
| 568 |
); |
| 569 |
assert_eq!(path_of(&req), "/subscriptions/sub_1A2b3C"); |
| 570 |
let f = form(&req); |
| 571 |
assert_eq!( |
| 572 |
f.get("items[0][id]").map(String::as_str), |
| 573 |
Some("si_123"), |
| 574 |
"re-pricing the existing item rather than adding one is what keeps \ |
| 575 |
the developer on a single charge" |
| 576 |
); |
| 577 |
assert_eq!( |
| 578 |
f.get("items[0][price_data][product]").map(String::as_str), |
| 579 |
Some("prod_123") |
| 580 |
); |
| 581 |
assert_eq!( |
| 582 |
f.get("items[0][price_data][unit_amount]") |
| 583 |
.map(String::as_str), |
| 584 |
Some("4000") |
| 585 |
); |
| 586 |
assert_eq!( |
| 587 |
f.get("proration_behavior").map(String::as_str), |
| 588 |
Some("create_prorations"), |
| 589 |
"the developer is credited or charged the difference on the next \ |
| 590 |
invoice" |
| 591 |
); |
| 592 |
} |
| 593 |
|
| 594 |
#[test] |
| 595 |
fn an_end_user_reprice_waits_for_the_period_boundary() { |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
let req = reprice_request( |
| 600 |
"sub_1A2b3C".parse().unwrap(), |
| 601 |
"si_123", |
| 602 |
"prod_123", |
| 603 |
900, |
| 604 |
app_sub_interval(super::super::SyncBillingInterval::Monthly), |
| 605 |
UpdateSubscriptionProrationBehavior::None, |
| 606 |
); |
| 607 |
assert_eq!( |
| 608 |
form(&req).get("proration_behavior").map(String::as_str), |
| 609 |
Some("none") |
| 610 |
); |
| 611 |
} |
| 612 |
|
| 613 |
#[test] |
| 614 |
fn the_billing_interval_reaches_stripe_as_the_one_the_user_bought() { |
| 615 |
for (interval, want) in [ |
| 616 |
(super::super::SyncBillingInterval::Monthly, "month"), |
| 617 |
(super::super::SyncBillingInterval::Annual, "year"), |
| 618 |
] { |
| 619 |
let req = reprice_request( |
| 620 |
"sub_1A2b3C".parse().unwrap(), |
| 621 |
"si_123", |
| 622 |
"prod_123", |
| 623 |
900, |
| 624 |
app_sub_interval(interval), |
| 625 |
UpdateSubscriptionProrationBehavior::None, |
| 626 |
); |
| 627 |
assert_eq!( |
| 628 |
form(&req) |
| 629 |
.get("items[0][price_data][recurring][interval]") |
| 630 |
.map(String::as_str), |
| 631 |
Some(want), |
| 632 |
"an annual subscriber re-priced monthly is billed twelve times \ |
| 633 |
over" |
| 634 |
); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
#[test] |
| 639 |
fn a_synckit_cancel_is_immediate_rather_than_at_period_end() { |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
let req = CancelSubscription::new( |
| 644 |
"sub_1A2b3C" |
| 645 |
.parse::<stripe_shared::SubscriptionId>() |
| 646 |
.unwrap(), |
| 647 |
); |
| 648 |
assert_eq!(path_of(&req), "/subscriptions/sub_1A2b3C"); |
| 649 |
assert_eq!(format!("{:?}", req.build().method), "Delete"); |
| 650 |
} |
| 651 |
} |
| 652 |
|