| 1 |
use super::{ |
| 2 |
Cents, ClaimToken, DbPurchaseRow, DbTransaction, DbTransactionExportRow, DownloadToken, ItemId, |
| 3 |
KeyCode, PgPool, ProjectId, PromoCodeId, Result, TransactionId, UserId, |
| 4 |
}; |
| 5 |
|
| 6 |
|
| 7 |
pub struct CreateTransactionParams<'a> { |
| 8 |
pub buyer_id: Option<UserId>, |
| 9 |
pub seller_id: UserId, |
| 10 |
|
| 11 |
pub item_id: Option<ItemId>, |
| 12 |
pub amount_cents: Cents, |
| 13 |
pub platform_fee_cents: Cents, |
| 14 |
pub stripe_checkout_session_id: &'a str, |
| 15 |
pub item_title: &'a str, |
| 16 |
pub seller_username: &'a str, |
| 17 |
pub share_contact: bool, |
| 18 |
|
| 19 |
pub project_id: Option<ProjectId>, |
| 20 |
|
| 21 |
pub promo_code_id: Option<PromoCodeId>, |
| 22 |
|
| 23 |
pub guest_email: Option<&'a str>, |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
pub platform_credit_cents: i64, |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
pub struct ClaimParams<'a> { |
| 33 |
pub buyer_id: UserId, |
| 34 |
pub item_id: ItemId, |
| 35 |
pub seller_id: UserId, |
| 36 |
pub item_title: &'a str, |
| 37 |
pub seller_username: &'a str, |
| 38 |
pub share_contact: bool, |
| 39 |
|
| 40 |
pub parent_transaction_id: Option<TransactionId>, |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
pub platform_credit_cents: i64, |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
#[tracing::instrument(skip_all)] |
| 50 |
pub async fn create_transaction<'e>( |
| 51 |
executor: impl sqlx::PgExecutor<'e>, |
| 52 |
params: &CreateTransactionParams<'_>, |
| 53 |
) -> Result<DbTransaction> { |
| 54 |
let tx = sqlx::query_as!( |
| 55 |
DbTransaction, |
| 56 |
r#" |
| 57 |
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) |
| 58 |
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) |
| 59 |
RETURNING |
| 60 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 61 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 62 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 63 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 64 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 65 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 66 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 67 |
download_token AS "download_token: DownloadToken" |
| 68 |
"#, |
| 69 |
params.buyer_id as Option<UserId>, |
| 70 |
params.seller_id as UserId, |
| 71 |
params.item_id as Option<ItemId>, |
| 72 |
params.amount_cents as Cents, |
| 73 |
params.platform_fee_cents as Cents, |
| 74 |
params.stripe_checkout_session_id, |
| 75 |
params.item_title, |
| 76 |
params.seller_username, |
| 77 |
params.share_contact, |
| 78 |
params.project_id as Option<ProjectId>, |
| 79 |
params.promo_code_id as Option<PromoCodeId>, |
| 80 |
params.guest_email, |
| 81 |
params.platform_credit_cents, |
| 82 |
) |
| 83 |
.fetch_one(executor) |
| 84 |
.await?; |
| 85 |
|
| 86 |
Ok(tx) |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#[tracing::instrument(skip_all)] |
| 97 |
pub async fn complete_guest_transaction<'e>( |
| 98 |
executor: impl sqlx::PgExecutor<'e>, |
| 99 |
stripe_checkout_session_id: &str, |
| 100 |
stripe_payment_intent_id: Option<&str>, |
| 101 |
guest_email: &str, |
| 102 |
) -> Result<Option<DbTransaction>> { |
| 103 |
let claim_token = ClaimToken::new(); |
| 104 |
|
| 105 |
let tx = sqlx::query_as!( |
| 106 |
DbTransaction, |
| 107 |
r#" |
| 108 |
UPDATE transactions |
| 109 |
SET status = 'completed', |
| 110 |
stripe_payment_intent_id = $2, |
| 111 |
completed_at = NOW(), |
| 112 |
guest_email = $3, |
| 113 |
claim_token = $4, |
| 114 |
buyer_id = NULL |
| 115 |
WHERE stripe_checkout_session_id = $1 |
| 116 |
AND status = 'pending' |
| 117 |
RETURNING |
| 118 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 119 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 120 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 121 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 122 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 123 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 124 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 125 |
download_token AS "download_token: DownloadToken" |
| 126 |
"#, |
| 127 |
stripe_checkout_session_id, |
| 128 |
stripe_payment_intent_id, |
| 129 |
guest_email, |
| 130 |
claim_token as ClaimToken, |
| 131 |
) |
| 132 |
.fetch_optional(executor) |
| 133 |
.await?; |
| 134 |
|
| 135 |
Ok(tx) |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
#[tracing::instrument(skip_all)] |
| 141 |
pub async fn attach_guest_purchases_by_email( |
| 142 |
pool: &PgPool, |
| 143 |
email: &str, |
| 144 |
user_id: UserId, |
| 145 |
) -> Result<u64> { |
| 146 |
let result = sqlx::query!( |
| 147 |
r#" |
| 148 |
UPDATE transactions |
| 149 |
SET buyer_id = $1, claimed_by = $1, claim_token = NULL |
| 150 |
WHERE LOWER(guest_email) = LOWER($2) |
| 151 |
AND buyer_id IS NULL |
| 152 |
AND status = 'completed' |
| 153 |
"#, |
| 154 |
user_id as UserId, |
| 155 |
email, |
| 156 |
) |
| 157 |
.execute(pool) |
| 158 |
.await?; |
| 159 |
|
| 160 |
Ok(result.rows_affected()) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
#[tracing::instrument(skip_all)] |
| 165 |
pub async fn claim_guest_purchase( |
| 166 |
pool: &PgPool, |
| 167 |
claim_token: ClaimToken, |
| 168 |
user_id: UserId, |
| 169 |
) -> Result<Option<DbTransaction>> { |
| 170 |
let tx = sqlx::query_as!( |
| 171 |
DbTransaction, |
| 172 |
r#" |
| 173 |
UPDATE transactions |
| 174 |
SET buyer_id = $2, claimed_by = $2, claim_token = NULL |
| 175 |
WHERE claim_token = $1 |
| 176 |
AND buyer_id IS NULL |
| 177 |
AND status = 'completed' |
| 178 |
RETURNING |
| 179 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 180 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 181 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 182 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 183 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 184 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 185 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 186 |
download_token AS "download_token: DownloadToken" |
| 187 |
"#, |
| 188 |
claim_token as ClaimToken, |
| 189 |
user_id as UserId, |
| 190 |
) |
| 191 |
.fetch_optional(pool) |
| 192 |
.await?; |
| 193 |
|
| 194 |
Ok(tx) |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
#[tracing::instrument(skip_all)] |
| 199 |
pub async fn get_transaction_by_download_token( |
| 200 |
pool: &PgPool, |
| 201 |
download_token: DownloadToken, |
| 202 |
) -> Result<Option<DbTransaction>> { |
| 203 |
let tx = sqlx::query_as!( |
| 204 |
DbTransaction, |
| 205 |
r#" |
| 206 |
SELECT |
| 207 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 208 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 209 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 210 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 211 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 212 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 213 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 214 |
download_token AS "download_token: DownloadToken" |
| 215 |
FROM transactions WHERE download_token = $1 AND status = 'completed' |
| 216 |
"#, |
| 217 |
download_token as DownloadToken, |
| 218 |
) |
| 219 |
.fetch_optional(pool) |
| 220 |
.await?; |
| 221 |
|
| 222 |
Ok(tx) |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
#[tracing::instrument(skip_all)] |
| 230 |
pub async fn complete_transaction<'e>( |
| 231 |
executor: impl sqlx::PgExecutor<'e>, |
| 232 |
stripe_checkout_session_id: &str, |
| 233 |
stripe_payment_intent_id: Option<&str>, |
| 234 |
) -> Result<Option<DbTransaction>> { |
| 235 |
|
| 236 |
|
| 237 |
let tx = sqlx::query_as!( |
| 238 |
DbTransaction, |
| 239 |
r#" |
| 240 |
UPDATE transactions |
| 241 |
SET status = 'completed', |
| 242 |
stripe_payment_intent_id = $2, |
| 243 |
completed_at = NOW() |
| 244 |
WHERE stripe_checkout_session_id = $1 |
| 245 |
AND status = 'pending' |
| 246 |
RETURNING |
| 247 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 248 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 249 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 250 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 251 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 252 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 253 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 254 |
download_token AS "download_token: DownloadToken" |
| 255 |
"#, |
| 256 |
stripe_checkout_session_id, |
| 257 |
stripe_payment_intent_id, |
| 258 |
) |
| 259 |
.fetch_optional(executor) |
| 260 |
.await?; |
| 261 |
|
| 262 |
Ok(tx) |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
#[tracing::instrument(skip_all)] |
| 268 |
pub async fn complete_cart_transactions<'e>( |
| 269 |
executor: impl sqlx::PgExecutor<'e>, |
| 270 |
stripe_checkout_session_id: &str, |
| 271 |
stripe_payment_intent_id: Option<&str>, |
| 272 |
) -> Result<Vec<DbTransaction>> { |
| 273 |
let txs = sqlx::query_as!( |
| 274 |
DbTransaction, |
| 275 |
r#" |
| 276 |
UPDATE transactions |
| 277 |
SET status = 'completed', |
| 278 |
stripe_payment_intent_id = $2, |
| 279 |
completed_at = NOW() |
| 280 |
WHERE stripe_checkout_session_id = $1 |
| 281 |
AND status = 'pending' |
| 282 |
RETURNING |
| 283 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 284 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 285 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 286 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 287 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 288 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 289 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 290 |
download_token AS "download_token: DownloadToken" |
| 291 |
"#, |
| 292 |
stripe_checkout_session_id, |
| 293 |
stripe_payment_intent_id, |
| 294 |
) |
| 295 |
.fetch_all(executor) |
| 296 |
.await?; |
| 297 |
|
| 298 |
Ok(txs) |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
#[tracing::instrument(skip_all)] |
| 309 |
pub async fn get_completed_transactions_for_session<'e>( |
| 310 |
executor: impl sqlx::PgExecutor<'e>, |
| 311 |
stripe_checkout_session_id: &str, |
| 312 |
) -> Result<Vec<DbTransaction>> { |
| 313 |
let txs = sqlx::query_as!( |
| 314 |
DbTransaction, |
| 315 |
r#" |
| 316 |
SELECT |
| 317 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 318 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 319 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 320 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 321 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 322 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 323 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 324 |
download_token AS "download_token: DownloadToken" |
| 325 |
FROM transactions |
| 326 |
WHERE stripe_checkout_session_id = $1 AND status = 'completed' |
| 327 |
"#, |
| 328 |
stripe_checkout_session_id, |
| 329 |
) |
| 330 |
.fetch_all(executor) |
| 331 |
.await?; |
| 332 |
|
| 333 |
Ok(txs) |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
#[tracing::instrument(skip_all)] |
| 340 |
pub async fn get_transactions_by_buyer( |
| 341 |
pool: &PgPool, |
| 342 |
buyer_id: UserId, |
| 343 |
limit: Option<i64>, |
| 344 |
) -> Result<Vec<DbTransaction>> { |
| 345 |
let txs = sqlx::query_as!( |
| 346 |
DbTransaction, |
| 347 |
r#" |
| 348 |
SELECT |
| 349 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 350 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 351 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 352 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 353 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 354 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 355 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 356 |
download_token AS "download_token: DownloadToken" |
| 357 |
FROM transactions WHERE buyer_id = $1 ORDER BY created_at DESC LIMIT $2 |
| 358 |
"#, |
| 359 |
buyer_id as UserId, |
| 360 |
limit, |
| 361 |
) |
| 362 |
.fetch_all(pool) |
| 363 |
.await?; |
| 364 |
|
| 365 |
Ok(txs) |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
pub async fn get_buyer_transactions_for_export_page( |
| 374 |
pool: &PgPool, |
| 375 |
buyer_id: UserId, |
| 376 |
limit: i64, |
| 377 |
offset: i64, |
| 378 |
) -> Result<Vec<DbTransaction>> { |
| 379 |
let txs = sqlx::query_as!( |
| 380 |
DbTransaction, |
| 381 |
r#" |
| 382 |
SELECT |
| 383 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 384 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 385 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 386 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 387 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 388 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 389 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 390 |
download_token AS "download_token: DownloadToken" |
| 391 |
FROM transactions WHERE buyer_id = $1 |
| 392 |
ORDER BY created_at DESC, id DESC |
| 393 |
LIMIT $2 OFFSET $3 |
| 394 |
"#, |
| 395 |
buyer_id as UserId, |
| 396 |
limit, |
| 397 |
offset, |
| 398 |
) |
| 399 |
.fetch_all(pool) |
| 400 |
.await?; |
| 401 |
|
| 402 |
Ok(txs) |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
#[tracing::instrument(skip_all)] |
| 409 |
pub async fn get_transactions_by_seller( |
| 410 |
pool: &PgPool, |
| 411 |
seller_id: UserId, |
| 412 |
limit: Option<i64>, |
| 413 |
) -> Result<Vec<DbTransaction>> { |
| 414 |
let txs = sqlx::query_as!( |
| 415 |
DbTransaction, |
| 416 |
r#" |
| 417 |
SELECT |
| 418 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 419 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 420 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 421 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 422 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 423 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 424 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 425 |
download_token AS "download_token: DownloadToken" |
| 426 |
FROM transactions WHERE seller_id = $1 ORDER BY created_at DESC LIMIT $2 |
| 427 |
"#, |
| 428 |
seller_id as UserId, |
| 429 |
limit, |
| 430 |
) |
| 431 |
.fetch_all(pool) |
| 432 |
.await?; |
| 433 |
|
| 434 |
Ok(txs) |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
#[tracing::instrument(skip_all)] |
| 439 |
pub async fn has_purchased_item(pool: &PgPool, user_id: UserId, item_id: ItemId) -> Result<bool> { |
| 440 |
let count: i64 = sqlx::query_scalar!( |
| 441 |
r#"SELECT COUNT(*) AS "count!" FROM transactions WHERE buyer_id = $1 AND item_id = $2 AND status = 'completed'"#, |
| 442 |
user_id as UserId, |
| 443 |
item_id as ItemId, |
| 444 |
) |
| 445 |
.fetch_one(pool) |
| 446 |
.await?; |
| 447 |
|
| 448 |
Ok(count > 0) |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
#[tracing::instrument(skip_all)] |
| 454 |
pub async fn purchased_subset( |
| 455 |
pool: &PgPool, |
| 456 |
user_id: UserId, |
| 457 |
item_ids: &[ItemId], |
| 458 |
) -> Result<std::collections::HashSet<ItemId>> { |
| 459 |
if item_ids.is_empty() { |
| 460 |
return Ok(std::collections::HashSet::new()); |
| 461 |
} |
| 462 |
let rows = sqlx::query_scalar!( |
| 463 |
r#"SELECT DISTINCT item_id AS "item_id!: ItemId" FROM transactions |
| 464 |
WHERE buyer_id = $1 AND status = 'completed' AND item_id = ANY($2)"#, |
| 465 |
user_id as UserId, |
| 466 |
item_ids as &[ItemId], |
| 467 |
) |
| 468 |
.fetch_all(pool) |
| 469 |
.await?; |
| 470 |
Ok(rows.into_iter().collect()) |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
#[tracing::instrument(skip_all)] |
| 475 |
pub async fn get_user_purchased_item_ids(pool: &PgPool, user_id: UserId) -> Result<Vec<ItemId>> { |
| 476 |
let item_ids: Vec<ItemId> = sqlx::query_scalar!( |
| 477 |
r#"SELECT DISTINCT item_id AS "item_id!: ItemId" FROM transactions WHERE buyer_id = $1 AND status = 'completed' AND item_id IS NOT NULL"#, |
| 478 |
user_id as UserId, |
| 479 |
) |
| 480 |
.fetch_all(pool) |
| 481 |
.await?; |
| 482 |
|
| 483 |
Ok(item_ids) |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
#[tracing::instrument(skip_all)] |
| 493 |
pub async fn claim_free_item<'e>( |
| 494 |
executor: impl sqlx::PgExecutor<'e>, |
| 495 |
params: &ClaimParams<'_>, |
| 496 |
) -> Result<bool> { |
| 497 |
let claim_id = format!("free-claim-{}-{}", params.buyer_id, params.item_id); |
| 498 |
let result = sqlx::query!( |
| 499 |
r#" |
| 500 |
INSERT INTO transactions (buyer_id, seller_id, item_id, amount_cents, platform_fee_cents, stripe_checkout_session_id, status, completed_at, item_title, seller_username, share_contact, parent_transaction_id, platform_credit_cents) |
| 501 |
VALUES ($1, $2, $3, 0, 0, $4, 'completed', NOW(), $5, $6, $7, $8, $9) |
| 502 |
ON CONFLICT (buyer_id, item_id) WHERE status = 'completed' AND item_id IS NOT NULL DO NOTHING |
| 503 |
"#, |
| 504 |
params.buyer_id as UserId, |
| 505 |
params.seller_id as UserId, |
| 506 |
params.item_id as ItemId, |
| 507 |
claim_id, |
| 508 |
params.item_title, |
| 509 |
params.seller_username, |
| 510 |
params.share_contact, |
| 511 |
params.parent_transaction_id as Option<TransactionId>, |
| 512 |
params.platform_credit_cents, |
| 513 |
) |
| 514 |
.execute(executor) |
| 515 |
.await?; |
| 516 |
|
| 517 |
Ok(result.rows_affected() > 0) |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
#[tracing::instrument(skip_all)] |
| 527 |
pub async fn claim_free_items_batch<'e>( |
| 528 |
executor: impl sqlx::PgExecutor<'e>, |
| 529 |
buyer_id: UserId, |
| 530 |
seller_id: UserId, |
| 531 |
seller_username: &str, |
| 532 |
parent_transaction_id: Option<TransactionId>, |
| 533 |
items: &[(ItemId, &str)], |
| 534 |
) -> Result<u64> { |
| 535 |
if items.is_empty() { |
| 536 |
return Ok(0); |
| 537 |
} |
| 538 |
let item_ids: Vec<ItemId> = items.iter().map(|(id, _)| *id).collect(); |
| 539 |
let item_titles: Vec<&str> = items.iter().map(|(_, title)| *title).collect(); |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
let result = sqlx::query( |
| 544 |
r" |
| 545 |
INSERT INTO transactions |
| 546 |
(buyer_id, seller_id, item_id, amount_cents, platform_fee_cents, |
| 547 |
stripe_checkout_session_id, status, completed_at, item_title, |
| 548 |
seller_username, share_contact, parent_transaction_id) |
| 549 |
SELECT |
| 550 |
$1, $2, t.item_id, 0, 0, |
| 551 |
'free-claim-' || $1::text || '-' || t.item_id::text, |
| 552 |
'completed', NOW(), t.item_title, $3, false, $4 |
| 553 |
FROM UNNEST($5::uuid[], $6::text[]) AS t(item_id, item_title) |
| 554 |
ON CONFLICT (buyer_id, item_id) WHERE status = 'completed' AND item_id IS NOT NULL DO NOTHING |
| 555 |
", |
| 556 |
) |
| 557 |
.bind(buyer_id) |
| 558 |
.bind(seller_id) |
| 559 |
.bind(seller_username) |
| 560 |
.bind(parent_transaction_id) |
| 561 |
.bind(&item_ids) |
| 562 |
.bind(&item_titles) |
| 563 |
.execute(executor) |
| 564 |
.await?; |
| 565 |
|
| 566 |
Ok(result.rows_affected()) |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
pub struct LicenseKeyParams<'a> { |
| 571 |
pub key_code: &'a KeyCode, |
| 572 |
pub max_activations: Option<i32>, |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
#[tracing::instrument(skip_all)] |
| 588 |
pub async fn claim_free_with_promo_code( |
| 589 |
pool: &PgPool, |
| 590 |
promo_code_id: PromoCodeId, |
| 591 |
params: &ClaimParams<'_>, |
| 592 |
license_key_params: Option<&LicenseKeyParams<'_>>, |
| 593 |
) -> Result<(bool, bool)> { |
| 594 |
let mut tx = pool.begin().await?; |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
let claim_id = format!("free-claim-{}-{}", params.buyer_id, params.item_id); |
| 601 |
let result = sqlx::query!( |
| 602 |
r#" |
| 603 |
INSERT INTO transactions (buyer_id, seller_id, item_id, amount_cents, platform_fee_cents, stripe_checkout_session_id, status, completed_at, item_title, seller_username, share_contact, promo_code_id, platform_credit_cents) |
| 604 |
VALUES ($1, $2, $3, 0, 0, $4, 'completed', NOW(), $5, $6, $7, $8, $9) |
| 605 |
ON CONFLICT (buyer_id, item_id) WHERE status = 'completed' AND item_id IS NOT NULL DO NOTHING |
| 606 |
"#, |
| 607 |
params.buyer_id as UserId, |
| 608 |
params.seller_id as UserId, |
| 609 |
params.item_id as ItemId, |
| 610 |
claim_id, |
| 611 |
params.item_title, |
| 612 |
params.seller_username, |
| 613 |
params.share_contact, |
| 614 |
promo_code_id as PromoCodeId, |
| 615 |
params.platform_credit_cents, |
| 616 |
) |
| 617 |
.execute(&mut *tx) |
| 618 |
.await?; |
| 619 |
|
| 620 |
let claimed = result.rows_affected() > 0; |
| 621 |
|
| 622 |
|
| 623 |
if !claimed { |
| 624 |
tx.rollback().await?; |
| 625 |
return Ok((true, false)); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
let code_result = sqlx::query!( |
| 634 |
r#" |
| 635 |
UPDATE promo_codes SET use_count = use_count + 1 |
| 636 |
WHERE id = $1 |
| 637 |
AND (max_uses IS NULL OR use_count < max_uses) |
| 638 |
AND (starts_at IS NULL OR starts_at <= NOW()) |
| 639 |
AND (expires_at IS NULL OR expires_at > NOW()) |
| 640 |
"#, |
| 641 |
promo_code_id as PromoCodeId, |
| 642 |
) |
| 643 |
.execute(&mut *tx) |
| 644 |
.await?; |
| 645 |
|
| 646 |
|
| 647 |
if code_result.rows_affected() == 0 { |
| 648 |
tx.rollback().await?; |
| 649 |
return Ok((false, false)); |
| 650 |
} |
| 651 |
|
| 652 |
crate::db::items::increment_sales_count(&mut *tx, params.item_id).await?; |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
if let Some(lk) = license_key_params { |
| 659 |
let attempt = sqlx::query!( |
| 660 |
r#" |
| 661 |
INSERT INTO license_keys (item_id, owner_id, transaction_id, key_code, max_activations) |
| 662 |
VALUES ($1, $2, NULL, $3, $4) |
| 663 |
"#, |
| 664 |
params.item_id as ItemId, |
| 665 |
params.buyer_id as UserId, |
| 666 |
lk.key_code as &KeyCode, |
| 667 |
lk.max_activations, |
| 668 |
) |
| 669 |
.execute(&mut *tx) |
| 670 |
.await; |
| 671 |
|
| 672 |
if let Err(sqlx::Error::Database(e)) = &attempt |
| 673 |
&& e.code().as_deref() == Some("23505") |
| 674 |
{ |
| 675 |
let retry_code = crate::helpers::generate_key_code(); |
| 676 |
tracing::warn!(item_id = %params.item_id, "license key 23505 collision; retrying once"); |
| 677 |
sqlx::query!( |
| 678 |
r#" |
| 679 |
INSERT INTO license_keys (item_id, owner_id, transaction_id, key_code, max_activations) |
| 680 |
VALUES ($1, $2, NULL, $3, $4) |
| 681 |
"#, |
| 682 |
params.item_id as ItemId, |
| 683 |
params.buyer_id as UserId, |
| 684 |
retry_code as KeyCode, |
| 685 |
lk.max_activations, |
| 686 |
) |
| 687 |
.execute(&mut *tx) |
| 688 |
.await?; |
| 689 |
} else { |
| 690 |
attempt?; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
tx.commit().await?; |
| 695 |
Ok((true, true)) |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
#[tracing::instrument(skip_all)] |
| 702 |
pub async fn has_purchased_project( |
| 703 |
pool: &PgPool, |
| 704 |
user_id: UserId, |
| 705 |
project_id: ProjectId, |
| 706 |
) -> Result<bool> { |
| 707 |
let count: i64 = sqlx::query_scalar!( |
| 708 |
r#"SELECT COUNT(*) AS "count!" FROM transactions WHERE buyer_id = $1 AND project_id = $2 AND status = 'completed'"#, |
| 709 |
user_id as UserId, |
| 710 |
project_id as ProjectId, |
| 711 |
) |
| 712 |
.fetch_one(pool) |
| 713 |
.await?; |
| 714 |
|
| 715 |
Ok(count > 0) |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
pub struct CreateProjectTransactionParams<'a> { |
| 720 |
pub buyer_id: UserId, |
| 721 |
pub seller_id: UserId, |
| 722 |
pub project_id: ProjectId, |
| 723 |
pub amount_cents: i32, |
| 724 |
pub stripe_checkout_session_id: &'a str, |
| 725 |
pub project_title: &'a str, |
| 726 |
pub seller_username: &'a str, |
| 727 |
pub share_contact: bool, |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
#[tracing::instrument(skip_all)] |
| 732 |
pub async fn create_project_transaction( |
| 733 |
pool: &PgPool, |
| 734 |
params: &CreateProjectTransactionParams<'_>, |
| 735 |
) -> Result<DbTransaction> { |
| 736 |
let tx = sqlx::query_as!( |
| 737 |
DbTransaction, |
| 738 |
r#" |
| 739 |
INSERT INTO transactions (buyer_id, seller_id, project_id, amount_cents, platform_fee_cents, stripe_checkout_session_id, item_title, seller_username, share_contact) |
| 740 |
VALUES ($1, $2, $3, $4, 0, $5, $6, $7, $8) |
| 741 |
RETURNING |
| 742 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 743 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 744 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 745 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 746 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 747 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 748 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 749 |
download_token AS "download_token: DownloadToken" |
| 750 |
"#, |
| 751 |
params.buyer_id as UserId, |
| 752 |
params.seller_id as UserId, |
| 753 |
params.project_id as ProjectId, |
| 754 |
params.amount_cents, |
| 755 |
params.stripe_checkout_session_id, |
| 756 |
params.project_title, |
| 757 |
params.seller_username, |
| 758 |
params.share_contact, |
| 759 |
) |
| 760 |
.fetch_one(pool) |
| 761 |
.await?; |
| 762 |
|
| 763 |
Ok(tx) |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
#[tracing::instrument(skip_all)] |
| 774 |
pub async fn get_user_purchases(pool: &PgPool, user_id: UserId) -> Result<Vec<DbPurchaseRow>> { |
| 775 |
let purchases = sqlx::query_as!( |
| 776 |
DbPurchaseRow, |
| 777 |
r#" |
| 778 |
SELECT |
| 779 |
transaction_id AS "transaction_id!: TransactionId", |
| 780 |
item_id AS "item_id!: ItemId", |
| 781 |
title AS "title!", |
| 782 |
creator AS "creator!", |
| 783 |
item_type AS "item_type!: crate::db::ItemType", |
| 784 |
purchased_at AS "purchased_at!: chrono::DateTime<chrono::Utc>", |
| 785 |
is_free AS "is_free!", |
| 786 |
license_key_code AS "license_key_code?: KeyCode", |
| 787 |
has_new_version AS "has_new_version!" |
| 788 |
FROM ( |
| 789 |
SELECT DISTINCT ON (p.item_id) |
| 790 |
p.transaction_id, |
| 791 |
p.item_id, |
| 792 |
i.title, |
| 793 |
u.username as creator, |
| 794 |
i.item_type, |
| 795 |
p.purchased_at, |
| 796 |
-- Badge from what the buyer actually paid, not the item's current |
| 797 |
-- price: a later re-price to $0 must not retroactively badge a paid |
| 798 |
-- purchase "Free" (nor vice-versa). The purchases view carries the |
| 799 |
-- transaction's own amount_cents. |
| 800 |
(p.amount_cents = 0) as is_free, |
| 801 |
lk.key_code as license_key_code, |
| 802 |
(vc.total_versions > 0 AND vc.total_versions > COALESCE(dc.downloaded_count, 0)) as has_new_version |
| 803 |
FROM purchases p |
| 804 |
JOIN items i ON p.item_id = i.id |
| 805 |
JOIN projects proj ON i.project_id = proj.id |
| 806 |
JOIN users u ON proj.user_id = u.id |
| 807 |
LEFT JOIN license_keys lk ON lk.item_id = p.item_id AND lk.owner_id = p.buyer_id AND lk.revoked_at IS NULL |
| 808 |
LEFT JOIN LATERAL ( |
| 809 |
SELECT COUNT(*) AS total_versions |
| 810 |
FROM versions v |
| 811 |
WHERE v.item_id = i.id AND v.s3_key IS NOT NULL |
| 812 |
) vc ON true |
| 813 |
LEFT JOIN LATERAL ( |
| 814 |
SELECT COUNT(*) AS downloaded_count |
| 815 |
FROM user_downloads ud |
| 816 |
WHERE ud.user_id = p.buyer_id AND ud.item_id = i.id |
| 817 |
) dc ON true |
| 818 |
WHERE p.buyer_id = $1 |
| 819 |
ORDER BY p.item_id, p.purchased_at DESC |
| 820 |
) deduped |
| 821 |
ORDER BY purchased_at DESC |
| 822 |
LIMIT 20 |
| 823 |
"#, |
| 824 |
user_id as UserId, |
| 825 |
) |
| 826 |
.fetch_all(pool) |
| 827 |
.await?; |
| 828 |
|
| 829 |
Ok(purchases) |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
#[tracing::instrument(skip_all)] |
| 835 |
pub async fn remove_free_item_from_library( |
| 836 |
pool: &PgPool, |
| 837 |
user_id: UserId, |
| 838 |
item_id: ItemId, |
| 839 |
) -> Result<bool> { |
| 840 |
|
| 841 |
let row: Option<Option<crate::db::PromoCodeId>> = sqlx::query_scalar!( |
| 842 |
r#" |
| 843 |
DELETE FROM transactions |
| 844 |
WHERE buyer_id = $1 AND item_id = $2 AND amount_cents = 0 AND status = 'completed' |
| 845 |
RETURNING promo_code_id AS "promo_code_id: crate::db::PromoCodeId" |
| 846 |
"#, |
| 847 |
user_id as UserId, |
| 848 |
item_id as ItemId, |
| 849 |
) |
| 850 |
.fetch_optional(pool) |
| 851 |
.await?; |
| 852 |
|
| 853 |
let deleted = row.is_some(); |
| 854 |
|
| 855 |
if let Some(Some(pc_id)) = row { |
| 856 |
crate::db::promo_codes::release_use_count(pool, pc_id) |
| 857 |
.await |
| 858 |
.ok(); |
| 859 |
} |
| 860 |
|
| 861 |
Ok(deleted) |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
#[tracing::instrument(skip_all)] |
| 876 |
pub async fn get_transaction_by_id( |
| 877 |
pool: &PgPool, |
| 878 |
id: TransactionId, |
| 879 |
) -> Result<Option<DbTransaction>> { |
| 880 |
let tx = sqlx::query_as!( |
| 881 |
DbTransaction, |
| 882 |
r#" |
| 883 |
SELECT |
| 884 |
id AS "id: TransactionId", buyer_id AS "buyer_id: UserId", seller_id AS "seller_id: UserId", |
| 885 |
item_id AS "item_id: ItemId", amount_cents AS "amount_cents: Cents", platform_fee_cents AS "platform_fee_cents: Cents", |
| 886 |
currency, status AS "status: crate::db::TransactionStatus", stripe_payment_intent_id, stripe_checkout_session_id, |
| 887 |
created_at AS "created_at: chrono::DateTime<chrono::Utc>", completed_at AS "completed_at: chrono::DateTime<chrono::Utc>", |
| 888 |
item_title, seller_username, share_contact, project_id AS "project_id: ProjectId", |
| 889 |
parent_transaction_id AS "parent_transaction_id: TransactionId", promo_code_id AS "promo_code_id: PromoCodeId", |
| 890 |
guest_email, claim_token AS "claim_token: ClaimToken", claimed_by AS "claimed_by: UserId", |
| 891 |
download_token AS "download_token: DownloadToken" |
| 892 |
FROM transactions WHERE id = $1 |
| 893 |
"#, |
| 894 |
id as TransactionId, |
| 895 |
) |
| 896 |
.fetch_optional(pool) |
| 897 |
.await?; |
| 898 |
Ok(tx) |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
#[tracing::instrument(skip_all)] |
| 911 |
pub async fn claim_transaction_for_refund( |
| 912 |
pool: &PgPool, |
| 913 |
id: TransactionId, |
| 914 |
) -> Result<Option<TransactionId>> { |
| 915 |
let row = sqlx::query_scalar!( |
| 916 |
r#" |
| 917 |
UPDATE transactions |
| 918 |
SET status = 'refunding' |
| 919 |
WHERE id = $1 AND status = 'completed' |
| 920 |
RETURNING id AS "id: TransactionId" |
| 921 |
"#, |
| 922 |
id as TransactionId, |
| 923 |
) |
| 924 |
.fetch_optional(pool) |
| 925 |
.await?; |
| 926 |
|
| 927 |
Ok(row) |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
#[tracing::instrument(skip_all)] |
| 934 |
pub async fn release_refund_claim(pool: &PgPool, id: TransactionId) -> Result<()> { |
| 935 |
sqlx::query!( |
| 936 |
r#" |
| 937 |
UPDATE transactions |
| 938 |
SET status = 'completed' |
| 939 |
WHERE id = $1 AND status = 'refunding' |
| 940 |
"#, |
| 941 |
id as TransactionId, |
| 942 |
) |
| 943 |
.execute(pool) |
| 944 |
.await?; |
| 945 |
|
| 946 |
Ok(()) |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
#[tracing::instrument(skip_all)] |
| 964 |
pub(crate) async fn refund_transaction_by_payment_intent<'e>( |
| 965 |
executor: impl sqlx::PgExecutor<'e>, |
| 966 |
payment_intent_id: &str, |
| 967 |
) -> Result<Vec<(crate::db::TransactionId, Option<ItemId>)>> { |
| 968 |
|
| 969 |
|
| 970 |
let rows = sqlx::query!( |
| 971 |
r#" |
| 972 |
UPDATE transactions |
| 973 |
SET status = 'refunded' |
| 974 |
WHERE stripe_payment_intent_id = $1 AND status IN ('completed', 'refunding') |
| 975 |
RETURNING id AS "id: crate::db::TransactionId", item_id AS "item_id: ItemId" |
| 976 |
"#, |
| 977 |
payment_intent_id, |
| 978 |
) |
| 979 |
.fetch_all(executor) |
| 980 |
.await?; |
| 981 |
|
| 982 |
Ok(rows.into_iter().map(|r| (r.id, r.item_id)).collect()) |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
#[tracing::instrument(skip_all)] |
| 994 |
pub(crate) async fn refund_transaction_by_id<'e>( |
| 995 |
executor: impl sqlx::PgExecutor<'e>, |
| 996 |
id: TransactionId, |
| 997 |
) -> Result<Option<(crate::db::TransactionId, Option<ItemId>)>> { |
| 998 |
let row = sqlx::query!( |
| 999 |
r#" |
| 1000 |
UPDATE transactions |
| 1001 |
SET status = 'refunded' |
| 1002 |
WHERE id = $1 AND status IN ('completed', 'refunding') |
| 1003 |
RETURNING id AS "id: crate::db::TransactionId", item_id AS "item_id: ItemId" |
| 1004 |
"#, |
| 1005 |
id as TransactionId, |
| 1006 |
) |
| 1007 |
.fetch_optional(executor) |
| 1008 |
.await?; |
| 1009 |
|
| 1010 |
Ok(row.map(|r| (r.id, r.item_id))) |
| 1011 |
} |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
pub async fn transaction_exists_for_payment_intent<'e>( |
| 1017 |
executor: impl sqlx::PgExecutor<'e>, |
| 1018 |
payment_intent_id: &str, |
| 1019 |
) -> Result<bool> { |
| 1020 |
let exists = sqlx::query_scalar!( |
| 1021 |
r#"SELECT EXISTS(SELECT 1 FROM transactions WHERE stripe_payment_intent_id = $1) AS "exists!""#, |
| 1022 |
payment_intent_id, |
| 1023 |
) |
| 1024 |
.fetch_one(executor) |
| 1025 |
.await?; |
| 1026 |
|
| 1027 |
Ok(exists) |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
pub async fn transaction_exists_for_checkout_session<'e>( |
| 1035 |
executor: impl sqlx::PgExecutor<'e>, |
| 1036 |
checkout_session_id: &str, |
| 1037 |
) -> Result<bool> { |
| 1038 |
let exists = sqlx::query_scalar!( |
| 1039 |
r#"SELECT EXISTS(SELECT 1 FROM transactions WHERE stripe_checkout_session_id = $1) AS "exists!""#, |
| 1040 |
checkout_session_id, |
| 1041 |
) |
| 1042 |
.fetch_one(executor) |
| 1043 |
.await?; |
| 1044 |
|
| 1045 |
Ok(exists) |
| 1046 |
} |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
#[tracing::instrument(skip_all)] |
| 1052 |
pub async fn revoke_child_transactions<'e>( |
| 1053 |
executor: impl sqlx::PgExecutor<'e>, |
| 1054 |
parent_transaction_id: TransactionId, |
| 1055 |
) -> Result<Vec<ItemId>> { |
| 1056 |
let item_ids = sqlx::query_scalar!( |
| 1057 |
r#" |
| 1058 |
UPDATE transactions |
| 1059 |
SET status = 'refunded' |
| 1060 |
WHERE parent_transaction_id = $1 AND status = 'completed' |
| 1061 |
RETURNING item_id AS "item_id: ItemId" |
| 1062 |
"#, |
| 1063 |
parent_transaction_id as TransactionId, |
| 1064 |
) |
| 1065 |
.fetch_all(executor) |
| 1066 |
.await?; |
| 1067 |
|
| 1068 |
Ok(item_ids.into_iter().flatten().collect()) |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
#[tracing::instrument(skip_all)] |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
pub async fn get_seller_transactions_for_export_page( |
| 1085 |
pool: &PgPool, |
| 1086 |
seller_id: UserId, |
| 1087 |
limit: i64, |
| 1088 |
offset: i64, |
| 1089 |
) -> Result<Vec<DbTransactionExportRow>> { |
| 1090 |
let rows = sqlx::query_as!( |
| 1091 |
DbTransactionExportRow, |
| 1092 |
r#" |
| 1093 |
SELECT |
| 1094 |
t.created_at AS "created_at: chrono::DateTime<chrono::Utc>", |
| 1095 |
t.item_id AS "item_id: ItemId", |
| 1096 |
t.item_title, |
| 1097 |
t.amount_cents AS "amount_cents: Cents", |
| 1098 |
t.status AS "status: crate::db::TransactionStatus", |
| 1099 |
CASE WHEN t.share_contact AND NOT EXISTS ( |
| 1100 |
SELECT 1 FROM contact_revocations cr |
| 1101 |
WHERE cr.buyer_id = t.buyer_id AND cr.seller_id = t.seller_id |
| 1102 |
) THEN u.email ELSE NULL END as buyer_email |
| 1103 |
FROM transactions t |
| 1104 |
LEFT JOIN users u ON u.id = t.buyer_id |
| 1105 |
WHERE t.seller_id = $1 |
| 1106 |
ORDER BY t.created_at DESC, t.id DESC |
| 1107 |
LIMIT $2 OFFSET $3 |
| 1108 |
"#, |
| 1109 |
seller_id as UserId, |
| 1110 |
limit, |
| 1111 |
offset, |
| 1112 |
) |
| 1113 |
.fetch_all(pool) |
| 1114 |
.await?; |
| 1115 |
|
| 1116 |
Ok(rows) |
| 1117 |
} |
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
pub async fn get_seller_transactions_for_export( |
| 1124 |
pool: &PgPool, |
| 1125 |
seller_id: UserId, |
| 1126 |
) -> Result<Vec<DbTransactionExportRow>> { |
| 1127 |
|
| 1128 |
const PAGE: i64 = 5_000; |
| 1129 |
|
| 1130 |
const EXPORT_ACCUMULATE_CAP: usize = 1_000_000; |
| 1131 |
|
| 1132 |
let mut all = Vec::new(); |
| 1133 |
let mut offset = 0i64; |
| 1134 |
loop { |
| 1135 |
let page = get_seller_transactions_for_export_page(pool, seller_id, PAGE, offset).await?; |
| 1136 |
let n = page.len(); |
| 1137 |
all.extend(page); |
| 1138 |
offset += n as i64; |
| 1139 |
if (n as i64) < PAGE || all.len() >= EXPORT_ACCUMULATE_CAP { |
| 1140 |
break; |
| 1141 |
} |
| 1142 |
} |
| 1143 |
Ok(all) |
| 1144 |
} |
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
#[tracing::instrument(skip_all)] |
| 1151 |
pub async fn create_subscription_pending_transaction( |
| 1152 |
pool: &PgPool, |
| 1153 |
buyer_id: UserId, |
| 1154 |
seller_id: UserId, |
| 1155 |
project_id: ProjectId, |
| 1156 |
stripe_checkout_session_id: &str, |
| 1157 |
promo_code_id: PromoCodeId, |
| 1158 |
) -> Result<()> { |
| 1159 |
sqlx::query!( |
| 1160 |
r#" |
| 1161 |
INSERT INTO transactions (buyer_id, seller_id, project_id, amount_cents, platform_fee_cents, |
| 1162 |
stripe_checkout_session_id, item_title, seller_username, share_contact, promo_code_id) |
| 1163 |
VALUES ($1, $2, $3, 0, 0, $4, 'subscription-promo-hold', '', false, $5) |
| 1164 |
"#, |
| 1165 |
buyer_id as UserId, |
| 1166 |
seller_id as UserId, |
| 1167 |
project_id as ProjectId, |
| 1168 |
stripe_checkout_session_id, |
| 1169 |
promo_code_id as PromoCodeId, |
| 1170 |
) |
| 1171 |
.execute(pool) |
| 1172 |
.await?; |
| 1173 |
|
| 1174 |
Ok(()) |
| 1175 |
} |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
#[tracing::instrument(skip_all)] |
| 1180 |
pub async fn delete_subscription_pending_transaction<'e>( |
| 1181 |
executor: impl sqlx::PgExecutor<'e>, |
| 1182 |
stripe_checkout_session_id: &str, |
| 1183 |
) -> Result<()> { |
| 1184 |
sqlx::query!( |
| 1185 |
"DELETE FROM transactions WHERE stripe_checkout_session_id = $1 AND status = 'pending'", |
| 1186 |
stripe_checkout_session_id, |
| 1187 |
) |
| 1188 |
.execute(executor) |
| 1189 |
.await?; |
| 1190 |
|
| 1191 |
Ok(()) |
| 1192 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
#[tracing::instrument(skip_all)] |
| 1201 |
pub async fn cleanup_stale_pending( |
| 1202 |
pool: &PgPool, |
| 1203 |
older_than: chrono::Duration, |
| 1204 |
) -> Result<Vec<Option<crate::db::PromoCodeId>>> { |
| 1205 |
let cutoff = chrono::Utc::now() - older_than; |
| 1206 |
|
| 1207 |
let rows: Vec<(Option<crate::db::PromoCodeId>,)> = sqlx::query_as( |
| 1208 |
r" |
| 1209 |
DELETE FROM transactions |
| 1210 |
WHERE status = 'pending' |
| 1211 |
AND created_at < $1 |
| 1212 |
RETURNING promo_code_id |
| 1213 |
", |
| 1214 |
) |
| 1215 |
.bind(cutoff) |
| 1216 |
.fetch_all(pool) |
| 1217 |
.await?; |
| 1218 |
|
| 1219 |
Ok(rows.into_iter().map(|(id,)| id).collect()) |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
#[tracing::instrument(skip_all)] |
| 1227 |
pub async fn pending_subset( |
| 1228 |
pool: &PgPool, |
| 1229 |
buyer_id: UserId, |
| 1230 |
item_ids: &[ItemId], |
| 1231 |
) -> Result<std::collections::HashSet<ItemId>> { |
| 1232 |
if item_ids.is_empty() { |
| 1233 |
return Ok(std::collections::HashSet::new()); |
| 1234 |
} |
| 1235 |
let rows = sqlx::query_scalar!( |
| 1236 |
r#"SELECT DISTINCT item_id AS "item_id!: ItemId" FROM transactions |
| 1237 |
WHERE buyer_id = $1 AND status = 'pending' AND item_id = ANY($2)"#, |
| 1238 |
buyer_id as UserId, |
| 1239 |
item_ids as &[ItemId], |
| 1240 |
) |
| 1241 |
.fetch_all(pool) |
| 1242 |
.await?; |
| 1243 |
Ok(rows.into_iter().collect()) |
| 1244 |
} |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
#[tracing::instrument(skip_all)] |
| 1249 |
pub async fn get_pending_item_purchase( |
| 1250 |
pool: &PgPool, |
| 1251 |
buyer_id: UserId, |
| 1252 |
item_id: ItemId, |
| 1253 |
) -> Result<Option<(TransactionId, chrono::DateTime<chrono::Utc>)>> { |
| 1254 |
let row = sqlx::query!( |
| 1255 |
r#" |
| 1256 |
SELECT id AS "id: TransactionId", created_at AS "created_at: chrono::DateTime<chrono::Utc>" |
| 1257 |
FROM transactions |
| 1258 |
WHERE buyer_id = $1 AND item_id = $2 AND status = 'pending' |
| 1259 |
LIMIT 1 |
| 1260 |
"#, |
| 1261 |
buyer_id as UserId, |
| 1262 |
item_id as ItemId, |
| 1263 |
) |
| 1264 |
.fetch_optional(pool) |
| 1265 |
.await?; |
| 1266 |
|
| 1267 |
Ok(row.map(|r| (r.id, r.created_at))) |
| 1268 |
} |
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
#[tracing::instrument(skip_all)] |
| 1274 |
pub async fn delete_pending_item_purchase( |
| 1275 |
pool: &PgPool, |
| 1276 |
buyer_id: UserId, |
| 1277 |
item_id: ItemId, |
| 1278 |
) -> Result<Option<crate::db::PromoCodeId>> { |
| 1279 |
let row: Option<Option<crate::db::PromoCodeId>> = sqlx::query_scalar!( |
| 1280 |
r#" |
| 1281 |
DELETE FROM transactions |
| 1282 |
WHERE buyer_id = $1 AND item_id = $2 AND status = 'pending' |
| 1283 |
RETURNING promo_code_id AS "promo_code_id: crate::db::PromoCodeId" |
| 1284 |
"#, |
| 1285 |
buyer_id as UserId, |
| 1286 |
item_id as ItemId, |
| 1287 |
) |
| 1288 |
.fetch_optional(pool) |
| 1289 |
.await?; |
| 1290 |
|
| 1291 |
Ok(row.flatten()) |
| 1292 |
} |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
#[allow(clippy::too_many_arguments)] |
| 1298 |
#[tracing::instrument(skip_all)] |
| 1299 |
pub async fn create_free_guest_transaction( |
| 1300 |
pool: &PgPool, |
| 1301 |
buyer_id: Option<UserId>, |
| 1302 |
seller_id: UserId, |
| 1303 |
item_id: ItemId, |
| 1304 |
checkout_session_id: &str, |
| 1305 |
item_title: &str, |
| 1306 |
seller_username: &str, |
| 1307 |
guest_email: &str, |
| 1308 |
claim_token: Option<ClaimToken>, |
| 1309 |
download_token: DownloadToken, |
| 1310 |
) -> std::result::Result<u64, sqlx::Error> { |
| 1311 |
let result = sqlx::query!( |
| 1312 |
r#" |
| 1313 |
INSERT INTO transactions ( |
| 1314 |
buyer_id, seller_id, item_id, amount_cents, platform_fee_cents, |
| 1315 |
stripe_checkout_session_id, status, completed_at, |
| 1316 |
item_title, seller_username, share_contact, |
| 1317 |
guest_email, claim_token, download_token |
| 1318 |
) |
| 1319 |
VALUES ($1, $2, $3, 0, 0, $4, 'completed', NOW(), $5, $6, false, $7, $8, $9) |
| 1320 |
ON CONFLICT (guest_email, item_id) WHERE status = 'completed' AND guest_email IS NOT NULL DO NOTHING |
| 1321 |
"#, |
| 1322 |
buyer_id as Option<UserId>, |
| 1323 |
seller_id as UserId, |
| 1324 |
item_id as ItemId, |
| 1325 |
checkout_session_id, |
| 1326 |
item_title, |
| 1327 |
seller_username, |
| 1328 |
guest_email, |
| 1329 |
claim_token as Option<ClaimToken>, |
| 1330 |
download_token as DownloadToken, |
| 1331 |
) |
| 1332 |
.execute(pool) |
| 1333 |
.await?; |
| 1334 |
|
| 1335 |
Ok(result.rows_affected()) |
| 1336 |
} |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
#[tracing::instrument(skip_all)] |
| 1347 |
pub async fn claim_free_project( |
| 1348 |
pool: &PgPool, |
| 1349 |
buyer_id: UserId, |
| 1350 |
seller_id: UserId, |
| 1351 |
project_id: ProjectId, |
| 1352 |
item_title: &str, |
| 1353 |
seller_username: &str, |
| 1354 |
share_contact: bool, |
| 1355 |
) -> Result<bool> { |
| 1356 |
let result = sqlx::query!( |
| 1357 |
r#" |
| 1358 |
INSERT INTO transactions (buyer_id, seller_id, project_id, amount_cents, platform_fee_cents, |
| 1359 |
status, completed_at, item_title, seller_username, share_contact) |
| 1360 |
VALUES ($1, $2, $3, 0, 0, 'completed', NOW(), $4, $5, $6) |
| 1361 |
ON CONFLICT (buyer_id, project_id) WHERE status = 'completed' AND project_id IS NOT NULL DO NOTHING |
| 1362 |
"#, |
| 1363 |
buyer_id as UserId, |
| 1364 |
seller_id as UserId, |
| 1365 |
project_id as ProjectId, |
| 1366 |
item_title, |
| 1367 |
seller_username, |
| 1368 |
share_contact, |
| 1369 |
) |
| 1370 |
.execute(pool) |
| 1371 |
.await?; |
| 1372 |
|
| 1373 |
Ok(result.rows_affected() > 0) |
| 1374 |
} |
| 1375 |
|