| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use super::super::{ |
| 8 |
Cents, ClaimToken, DbTransaction, DownloadToken, ItemId, PgPool, ProjectId, PromoCodeId, |
| 9 |
Result, TransactionId, UserId, |
| 10 |
}; |
| 11 |
|
| 12 |
|
| 13 |
pub struct CreateTransactionParams<'a> { |
| 14 |
pub buyer_id: Option<UserId>, |
| 15 |
pub seller_id: UserId, |
| 16 |
|
| 17 |
pub item_id: Option<ItemId>, |
| 18 |
pub amount_cents: Cents, |
| 19 |
pub platform_fee_cents: Cents, |
| 20 |
pub stripe_checkout_session_id: &'a str, |
| 21 |
pub item_title: &'a str, |
| 22 |
pub seller_username: &'a str, |
| 23 |
pub share_contact: bool, |
| 24 |
|
| 25 |
pub project_id: Option<ProjectId>, |
| 26 |
|
| 27 |
pub promo_code_id: Option<PromoCodeId>, |
| 28 |
|
| 29 |
pub guest_email: Option<&'a str>, |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
pub platform_credit_cents: i64, |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
#[tracing::instrument(skip_all)] |
| 39 |
pub async fn create_transaction<'e>( |
| 40 |
executor: impl sqlx::PgExecutor<'e>, |
| 41 |
params: &CreateTransactionParams<'_>, |
| 42 |
) -> Result<DbTransaction> { |
| 43 |
let tx = sqlx::query_as!( |
| 44 |
DbTransaction, |
| 45 |
r#" |
| 46 |
INSERT INTO transactions (buyer_id, seller_id, item_id, amount_cents, platform_fee_cents, stripe_checkout_session_id, item_title, seller_username, share_contact, project_id, promo_code_id, guest_email, platform_credit_cents) |
| 47 |
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) |
| 48 |
RETURNING |
| 49 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 50 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 51 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 52 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 53 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 54 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 55 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 56 |
download_token AS "download_token: DownloadToken", |
| 57 |
presentment_amount_cents, presentment_currency |
| 58 |
"#, |
| 59 |
params.buyer_id as Option<UserId>, |
| 60 |
params.seller_id as UserId, |
| 61 |
params.item_id as Option<ItemId>, |
| 62 |
params.amount_cents as Cents, |
| 63 |
params.platform_fee_cents as Cents, |
| 64 |
params.stripe_checkout_session_id, |
| 65 |
params.item_title, |
| 66 |
params.seller_username, |
| 67 |
params.share_contact, |
| 68 |
params.project_id as Option<ProjectId>, |
| 69 |
params.promo_code_id as Option<PromoCodeId>, |
| 70 |
params.guest_email, |
| 71 |
params.platform_credit_cents, |
| 72 |
) |
| 73 |
.fetch_one(executor) |
| 74 |
.await?; |
| 75 |
|
| 76 |
Ok(tx) |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#[tracing::instrument(skip_all)] |
| 84 |
pub async fn complete_transaction<'e>( |
| 85 |
executor: impl sqlx::PgExecutor<'e>, |
| 86 |
stripe_checkout_session_id: &str, |
| 87 |
stripe_payment_intent_id: Option<&str>, |
| 88 |
presentment: Option<(i64, &str)>, |
| 89 |
) -> Result<Option<DbTransaction>> { |
| 90 |
|
| 91 |
|
| 92 |
let tx = sqlx::query_as!( |
| 93 |
DbTransaction, |
| 94 |
r#" |
| 95 |
UPDATE transactions |
| 96 |
SET status = 'completed', |
| 97 |
stripe_payment_intent_id = $2, |
| 98 |
presentment_amount_cents = $3, |
| 99 |
presentment_currency = $4, |
| 100 |
completed_at = NOW() |
| 101 |
WHERE stripe_checkout_session_id = $1 |
| 102 |
AND status = 'pending' |
| 103 |
RETURNING |
| 104 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 105 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 106 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 107 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 108 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 109 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 110 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 111 |
download_token AS "download_token: DownloadToken", |
| 112 |
presentment_amount_cents, presentment_currency |
| 113 |
"#, |
| 114 |
stripe_checkout_session_id, |
| 115 |
stripe_payment_intent_id, |
| 116 |
presentment.map(|(cents, _)| cents), |
| 117 |
presentment.map(|(_, currency)| currency), |
| 118 |
) |
| 119 |
.fetch_optional(executor) |
| 120 |
.await?; |
| 121 |
|
| 122 |
Ok(tx) |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
#[tracing::instrument(skip_all)] |
| 128 |
pub async fn complete_cart_transactions<'e>( |
| 129 |
executor: impl sqlx::PgExecutor<'e>, |
| 130 |
stripe_checkout_session_id: &str, |
| 131 |
stripe_payment_intent_id: Option<&str>, |
| 132 |
) -> Result<Vec<DbTransaction>> { |
| 133 |
let txs = sqlx::query_as!( |
| 134 |
DbTransaction, |
| 135 |
r#" |
| 136 |
UPDATE transactions |
| 137 |
SET status = 'completed', |
| 138 |
stripe_payment_intent_id = $2, |
| 139 |
completed_at = NOW() |
| 140 |
WHERE stripe_checkout_session_id = $1 |
| 141 |
AND status = 'pending' |
| 142 |
RETURNING |
| 143 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 144 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 145 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 146 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 147 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 148 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 149 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 150 |
download_token AS "download_token: DownloadToken", |
| 151 |
presentment_amount_cents, presentment_currency |
| 152 |
"#, |
| 153 |
stripe_checkout_session_id, |
| 154 |
stripe_payment_intent_id, |
| 155 |
) |
| 156 |
.fetch_all(executor) |
| 157 |
.await?; |
| 158 |
|
| 159 |
Ok(txs) |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
#[tracing::instrument(skip_all)] |
| 170 |
pub async fn get_completed_transactions_for_session<'e>( |
| 171 |
executor: impl sqlx::PgExecutor<'e>, |
| 172 |
stripe_checkout_session_id: &str, |
| 173 |
) -> Result<Vec<DbTransaction>> { |
| 174 |
let txs = sqlx::query_as!( |
| 175 |
DbTransaction, |
| 176 |
r#" |
| 177 |
SELECT |
| 178 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 179 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 180 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 181 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 182 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 183 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 184 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 185 |
download_token AS "download_token: DownloadToken", |
| 186 |
presentment_amount_cents, presentment_currency |
| 187 |
FROM transactions |
| 188 |
WHERE stripe_checkout_session_id = $1 AND status = 'completed' |
| 189 |
"#, |
| 190 |
stripe_checkout_session_id, |
| 191 |
) |
| 192 |
.fetch_all(executor) |
| 193 |
.await?; |
| 194 |
|
| 195 |
Ok(txs) |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
pub struct CreateProjectTransactionParams<'a> { |
| 200 |
pub buyer_id: UserId, |
| 201 |
pub seller_id: UserId, |
| 202 |
pub project_id: ProjectId, |
| 203 |
pub amount_cents: i32, |
| 204 |
pub stripe_checkout_session_id: &'a str, |
| 205 |
pub project_title: &'a str, |
| 206 |
pub seller_username: &'a str, |
| 207 |
pub share_contact: bool, |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
#[tracing::instrument(skip_all)] |
| 212 |
pub async fn create_project_transaction( |
| 213 |
pool: &PgPool, |
| 214 |
params: &CreateProjectTransactionParams<'_>, |
| 215 |
) -> Result<DbTransaction> { |
| 216 |
let tx = sqlx::query_as!( |
| 217 |
DbTransaction, |
| 218 |
r#" |
| 219 |
INSERT INTO transactions (buyer_id, seller_id, project_id, amount_cents, platform_fee_cents, stripe_checkout_session_id, item_title, seller_username, share_contact) |
| 220 |
VALUES ($1, $2, $3, $4, 0, $5, $6, $7, $8) |
| 221 |
RETURNING |
| 222 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 223 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 224 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 225 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 226 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 227 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 228 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 229 |
download_token AS "download_token: DownloadToken", |
| 230 |
presentment_amount_cents, presentment_currency |
| 231 |
"#, |
| 232 |
params.buyer_id as UserId, |
| 233 |
params.seller_id as UserId, |
| 234 |
params.project_id as ProjectId, |
| 235 |
params.amount_cents, |
| 236 |
params.stripe_checkout_session_id, |
| 237 |
params.project_title, |
| 238 |
params.seller_username, |
| 239 |
params.share_contact, |
| 240 |
) |
| 241 |
.fetch_one(pool) |
| 242 |
.await?; |
| 243 |
|
| 244 |
Ok(tx) |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
#[tracing::instrument(skip_all)] |
| 252 |
pub async fn create_subscription_pending_transaction( |
| 253 |
pool: &PgPool, |
| 254 |
buyer_id: UserId, |
| 255 |
seller_id: UserId, |
| 256 |
project_id: ProjectId, |
| 257 |
stripe_checkout_session_id: &str, |
| 258 |
promo_code_id: PromoCodeId, |
| 259 |
) -> Result<()> { |
| 260 |
sqlx::query!( |
| 261 |
r#" |
| 262 |
INSERT INTO transactions (buyer_id, seller_id, project_id, amount_cents, platform_fee_cents, |
| 263 |
stripe_checkout_session_id, item_title, seller_username, share_contact, promo_code_id) |
| 264 |
VALUES ($1, $2, $3, 0, 0, $4, 'subscription-promo-hold', '', false, $5) |
| 265 |
"#, |
| 266 |
buyer_id as UserId, |
| 267 |
seller_id as UserId, |
| 268 |
project_id as ProjectId, |
| 269 |
stripe_checkout_session_id, |
| 270 |
promo_code_id as PromoCodeId, |
| 271 |
) |
| 272 |
.execute(pool) |
| 273 |
.await?; |
| 274 |
|
| 275 |
Ok(()) |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
#[tracing::instrument(skip_all)] |
| 281 |
pub async fn delete_subscription_pending_transaction<'e>( |
| 282 |
executor: impl sqlx::PgExecutor<'e>, |
| 283 |
stripe_checkout_session_id: &str, |
| 284 |
) -> Result<()> { |
| 285 |
sqlx::query!( |
| 286 |
"DELETE FROM transactions WHERE stripe_checkout_session_id = $1 AND status = 'pending'", |
| 287 |
stripe_checkout_session_id, |
| 288 |
) |
| 289 |
.execute(executor) |
| 290 |
.await?; |
| 291 |
|
| 292 |
Ok(()) |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
#[tracing::instrument(skip_all)] |
| 302 |
pub async fn cleanup_stale_pending( |
| 303 |
pool: &PgPool, |
| 304 |
older_than: chrono::Duration, |
| 305 |
) -> Result<Vec<Option<crate::db::PromoCodeId>>> { |
| 306 |
let cutoff = chrono::Utc::now() - older_than; |
| 307 |
|
| 308 |
let rows: Vec<(Option<crate::db::PromoCodeId>,)> = sqlx::query_as( |
| 309 |
r" |
| 310 |
DELETE FROM transactions |
| 311 |
WHERE status = 'pending' |
| 312 |
AND created_at < $1 |
| 313 |
RETURNING promo_code_id |
| 314 |
", |
| 315 |
) |
| 316 |
.bind(cutoff) |
| 317 |
.fetch_all(pool) |
| 318 |
.await?; |
| 319 |
|
| 320 |
Ok(rows.into_iter().map(|(id,)| id).collect()) |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
#[tracing::instrument(skip_all)] |
| 328 |
pub async fn pending_subset( |
| 329 |
pool: &PgPool, |
| 330 |
buyer_id: UserId, |
| 331 |
item_ids: &[ItemId], |
| 332 |
) -> Result<std::collections::HashSet<ItemId>> { |
| 333 |
if item_ids.is_empty() { |
| 334 |
return Ok(std::collections::HashSet::new()); |
| 335 |
} |
| 336 |
let rows = sqlx::query_scalar!( |
| 337 |
r#"SELECT DISTINCT item_id AS "item_id!: ItemId" FROM transactions |
| 338 |
WHERE buyer_id = $1 AND status = 'pending' AND item_id = ANY($2)"#, |
| 339 |
buyer_id as UserId, |
| 340 |
item_ids as &[ItemId], |
| 341 |
) |
| 342 |
.fetch_all(pool) |
| 343 |
.await?; |
| 344 |
Ok(rows.into_iter().collect()) |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
#[tracing::instrument(skip_all)] |
| 350 |
pub async fn get_pending_item_purchase( |
| 351 |
pool: &PgPool, |
| 352 |
buyer_id: UserId, |
| 353 |
item_id: ItemId, |
| 354 |
) -> Result<Option<(TransactionId, chrono::DateTime<chrono::Utc>)>> { |
| 355 |
let row = sqlx::query!( |
| 356 |
r#" |
| 357 |
SELECT id AS "id: TransactionId", created_at AS "created_at: chrono::DateTime<chrono::Utc>" |
| 358 |
FROM transactions |
| 359 |
WHERE buyer_id = $1 AND item_id = $2 AND status = 'pending' |
| 360 |
LIMIT 1 |
| 361 |
"#, |
| 362 |
buyer_id as UserId, |
| 363 |
item_id as ItemId, |
| 364 |
) |
| 365 |
.fetch_optional(pool) |
| 366 |
.await?; |
| 367 |
|
| 368 |
Ok(row.map(|r| (r.id, r.created_at))) |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
#[tracing::instrument(skip_all)] |
| 375 |
pub async fn delete_pending_item_purchase( |
| 376 |
pool: &PgPool, |
| 377 |
buyer_id: UserId, |
| 378 |
item_id: ItemId, |
| 379 |
) -> Result<Option<crate::db::PromoCodeId>> { |
| 380 |
let row: Option<Option<crate::db::PromoCodeId>> = sqlx::query_scalar!( |
| 381 |
r#" |
| 382 |
DELETE FROM transactions |
| 383 |
WHERE buyer_id = $1 AND item_id = $2 AND status = 'pending' |
| 384 |
RETURNING promo_code_id AS "promo_code_id: crate::db::PromoCodeId" |
| 385 |
"#, |
| 386 |
buyer_id as UserId, |
| 387 |
item_id as ItemId, |
| 388 |
) |
| 389 |
.fetch_optional(pool) |
| 390 |
.await?; |
| 391 |
|
| 392 |
Ok(row.flatten()) |
| 393 |
} |
| 394 |
|