| 1 |
|
| 2 |
|
| 3 |
use super::super::{ItemId, PgPool, Result, TransactionId}; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#[tracing::instrument(skip_all)] |
| 15 |
pub async fn claim_transaction_for_refund( |
| 16 |
pool: &PgPool, |
| 17 |
id: TransactionId, |
| 18 |
) -> Result<Option<TransactionId>> { |
| 19 |
let row = sqlx::query_scalar!( |
| 20 |
r#" |
| 21 |
UPDATE transactions |
| 22 |
SET status = 'refunding' |
| 23 |
WHERE id = $1 AND status = 'completed' |
| 24 |
RETURNING id AS "id: TransactionId" |
| 25 |
"#, |
| 26 |
id as TransactionId, |
| 27 |
) |
| 28 |
.fetch_optional(pool) |
| 29 |
.await?; |
| 30 |
|
| 31 |
Ok(row) |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#[tracing::instrument(skip_all)] |
| 38 |
pub async fn release_refund_claim(pool: &PgPool, id: TransactionId) -> Result<()> { |
| 39 |
sqlx::query!( |
| 40 |
r#" |
| 41 |
UPDATE transactions |
| 42 |
SET status = 'completed' |
| 43 |
WHERE id = $1 AND status = 'refunding' |
| 44 |
"#, |
| 45 |
id as TransactionId, |
| 46 |
) |
| 47 |
.execute(pool) |
| 48 |
.await?; |
| 49 |
|
| 50 |
Ok(()) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#[tracing::instrument(skip_all)] |
| 68 |
pub(crate) async fn refund_transaction_by_payment_intent<'e>( |
| 69 |
executor: impl sqlx::PgExecutor<'e>, |
| 70 |
payment_intent_id: &str, |
| 71 |
) -> Result<Vec<(crate::db::TransactionId, Option<ItemId>)>> { |
| 72 |
|
| 73 |
|
| 74 |
let rows = sqlx::query!( |
| 75 |
r#" |
| 76 |
UPDATE transactions |
| 77 |
SET status = 'refunded' |
| 78 |
WHERE stripe_payment_intent_id = $1 AND status IN ('completed', 'refunding') |
| 79 |
RETURNING id AS "id: crate::db::TransactionId", item_id AS "item_id: ItemId" |
| 80 |
"#, |
| 81 |
payment_intent_id, |
| 82 |
) |
| 83 |
.fetch_all(executor) |
| 84 |
.await?; |
| 85 |
|
| 86 |
Ok(rows.into_iter().map(|r| (r.id, r.item_id)).collect()) |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
#[tracing::instrument(skip_all)] |
| 98 |
pub(crate) async fn refund_transaction_by_id<'e>( |
| 99 |
executor: impl sqlx::PgExecutor<'e>, |
| 100 |
id: TransactionId, |
| 101 |
) -> Result<Option<(crate::db::TransactionId, Option<ItemId>)>> { |
| 102 |
let row = sqlx::query!( |
| 103 |
r#" |
| 104 |
UPDATE transactions |
| 105 |
SET status = 'refunded' |
| 106 |
WHERE id = $1 AND status IN ('completed', 'refunding') |
| 107 |
RETURNING id AS "id: crate::db::TransactionId", item_id AS "item_id: ItemId" |
| 108 |
"#, |
| 109 |
id as TransactionId, |
| 110 |
) |
| 111 |
.fetch_optional(executor) |
| 112 |
.await?; |
| 113 |
|
| 114 |
Ok(row.map(|r| (r.id, r.item_id))) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
pub async fn transaction_exists_for_payment_intent<'e>( |
| 121 |
executor: impl sqlx::PgExecutor<'e>, |
| 122 |
payment_intent_id: &str, |
| 123 |
) -> Result<bool> { |
| 124 |
let exists = sqlx::query_scalar!( |
| 125 |
r#"SELECT EXISTS(SELECT 1 FROM transactions WHERE stripe_payment_intent_id = $1) AS "exists!""#, |
| 126 |
payment_intent_id, |
| 127 |
) |
| 128 |
.fetch_one(executor) |
| 129 |
.await?; |
| 130 |
|
| 131 |
Ok(exists) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
pub async fn transaction_exists_for_checkout_session<'e>( |
| 139 |
executor: impl sqlx::PgExecutor<'e>, |
| 140 |
checkout_session_id: &str, |
| 141 |
) -> Result<bool> { |
| 142 |
let exists = sqlx::query_scalar!( |
| 143 |
r#"SELECT EXISTS(SELECT 1 FROM transactions WHERE stripe_checkout_session_id = $1) AS "exists!""#, |
| 144 |
checkout_session_id, |
| 145 |
) |
| 146 |
.fetch_one(executor) |
| 147 |
.await?; |
| 148 |
|
| 149 |
Ok(exists) |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
#[tracing::instrument(skip_all)] |
| 156 |
pub async fn revoke_child_transactions<'e>( |
| 157 |
executor: impl sqlx::PgExecutor<'e>, |
| 158 |
parent_transaction_id: TransactionId, |
| 159 |
) -> Result<Vec<ItemId>> { |
| 160 |
let item_ids = sqlx::query_scalar!( |
| 161 |
r#" |
| 162 |
UPDATE transactions |
| 163 |
SET status = 'refunded' |
| 164 |
WHERE parent_transaction_id = $1 AND status = 'completed' |
| 165 |
RETURNING item_id AS "item_id: ItemId" |
| 166 |
"#, |
| 167 |
parent_transaction_id as TransactionId, |
| 168 |
) |
| 169 |
.fetch_all(executor) |
| 170 |
.await?; |
| 171 |
|
| 172 |
Ok(item_ids.into_iter().flatten().collect()) |
| 173 |
} |
| 174 |
|