| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use sqlx::PgPool; |
| 8 |
|
| 9 |
use super::validated_types::Cents; |
| 10 |
|
| 11 |
use crate::error::Result; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
pub async fn insert_pending_refund( |
| 17 |
pool: &PgPool, |
| 18 |
payment_intent_id: &str, |
| 19 |
amount: i64, |
| 20 |
amount_refunded: i64, |
| 21 |
) -> Result<()> { |
| 22 |
sqlx::query!( |
| 23 |
r#" |
| 24 |
INSERT INTO pending_refunds (payment_intent_id, amount, amount_refunded) |
| 25 |
VALUES ($1, $2, $3) |
| 26 |
ON CONFLICT (payment_intent_id) WHERE matched_at IS NULL DO NOTHING |
| 27 |
"#, |
| 28 |
payment_intent_id, |
| 29 |
amount, |
| 30 |
amount_refunded, |
| 31 |
) |
| 32 |
.execute(pool) |
| 33 |
.await?; |
| 34 |
|
| 35 |
Ok(()) |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
#[derive(Debug, sqlx::FromRow)] |
| 40 |
pub struct PendingRefund { |
| 41 |
pub id: uuid::Uuid, |
| 42 |
pub payment_intent_id: String, |
| 43 |
pub amount: Cents, |
| 44 |
pub amount_refunded: Cents, |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
pub async fn claim_pending_refund( |
| 55 |
pool: &PgPool, |
| 56 |
payment_intent_id: &str, |
| 57 |
) -> Result<Option<PendingRefund>> { |
| 58 |
let row = sqlx::query_as!( |
| 59 |
PendingRefund, |
| 60 |
r#" |
| 61 |
UPDATE pending_refunds |
| 62 |
SET matched_at = NOW() |
| 63 |
WHERE id = ( |
| 64 |
SELECT id FROM pending_refunds |
| 65 |
WHERE payment_intent_id = $1 AND matched_at IS NULL |
| 66 |
LIMIT 1 |
| 67 |
FOR UPDATE SKIP LOCKED |
| 68 |
) |
| 69 |
RETURNING id, payment_intent_id, amount AS "amount: Cents", amount_refunded AS "amount_refunded: Cents" |
| 70 |
"#, |
| 71 |
payment_intent_id, |
| 72 |
) |
| 73 |
.fetch_optional(pool) |
| 74 |
.await?; |
| 75 |
|
| 76 |
Ok(row) |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
pub async fn mark_refund_completed(pool: &PgPool, id: uuid::Uuid) -> Result<()> { |
| 87 |
sqlx::query!( |
| 88 |
"UPDATE pending_refunds SET completed_at = NOW() WHERE id = $1", |
| 89 |
id |
| 90 |
) |
| 91 |
.execute(pool) |
| 92 |
.await?; |
| 93 |
Ok(()) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
pub async fn unclaim_pending_refund(pool: &PgPool, id: uuid::Uuid) -> Result<()> { |
| 103 |
sqlx::query!( |
| 104 |
"UPDATE pending_refunds SET matched_at = NULL WHERE id = $1", |
| 105 |
id |
| 106 |
) |
| 107 |
.execute(pool) |
| 108 |
.await?; |
| 109 |
Ok(()) |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
#[derive(Debug, sqlx::FromRow)] |
| 114 |
pub struct StaleRefund { |
| 115 |
pub id: uuid::Uuid, |
| 116 |
pub payment_intent_id: String, |
| 117 |
pub amount: Cents, |
| 118 |
pub amount_refunded: Cents, |
| 119 |
pub created_at: chrono::DateTime<chrono::Utc>, |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
pub const STALE_REFUND_BATCH: i64 = 100; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
pub async fn get_stale_refunds(pool: &PgPool, age: chrono::Duration) -> Result<Vec<StaleRefund>> { |
| 135 |
let cutoff = chrono::Utc::now() - age; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
let rows = sqlx::query_as::<_, StaleRefund>( |
| 140 |
r" |
| 141 |
SELECT id, payment_intent_id, amount, amount_refunded, created_at |
| 142 |
FROM pending_refunds |
| 143 |
WHERE completed_at IS NULL |
| 144 |
AND escalated_at IS NULL |
| 145 |
AND created_at < $1 |
| 146 |
ORDER BY created_at |
| 147 |
LIMIT $2 |
| 148 |
", |
| 149 |
) |
| 150 |
.bind(cutoff) |
| 151 |
.bind(STALE_REFUND_BATCH) |
| 152 |
.fetch_all(pool) |
| 153 |
.await?; |
| 154 |
|
| 155 |
Ok(rows) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
pub async fn mark_escalated(pool: &PgPool, id: uuid::Uuid) -> Result<()> { |
| 160 |
sqlx::query!( |
| 161 |
"UPDATE pending_refunds SET escalated_at = NOW() WHERE id = $1", |
| 162 |
id |
| 163 |
) |
| 164 |
.execute(pool) |
| 165 |
.await?; |
| 166 |
Ok(()) |
| 167 |
} |
| 168 |
|