stateset-db 1.23.2

Database implementations for StateSet iCommerce
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#![cfg(feature = "sqlite")]
//! Regression tests for payment refund correctness against the live SQLite
//! engine.
//!
//! These cover two verified defects:
//!
//! 1. **Over-refund / invalid-status refund.** `create_refund` previously
//!    inserted any requested amount with no bounds check and no payment-status
//!    check, so a `Pending`/`Failed` payment could be refunded and a payment
//!    could be refunded for more than its remaining balance.
//! 2. **Money arithmetic on TEXT columns.** `complete_refund` did
//!    `amount_refunded + ?` and `>= amount` directly in SQL on TEXT columns,
//!    which SQLite coerces to IEEE-754 floats (e.g. `'0.10' + '0.20'` yields
//!    `0.30000000000000004`). The arithmetic is now done in Rust with
//!    `rust_decimal::Decimal` and written back as exact TEXT.

use rust_decimal_macros::dec;
use stateset_core::{
    CommerceError, CreatePayment, CreateRefund, PaymentMethodType, PaymentRepository,
};
use stateset_db::SqliteDatabase;

fn db() -> SqliteDatabase {
    SqliteDatabase::in_memory().expect("create in-memory sqlite db")
}

/// Create a payment and advance it to `Completed` (the only state from which a
/// payment normally becomes refundable).
fn completed_payment(db: &SqliteDatabase, amount: rust_decimal::Decimal) -> stateset_core::Payment {
    let payment = db
        .payments()
        .create(CreatePayment {
            payment_method: PaymentMethodType::CreditCard,
            amount,
            ..Default::default()
        })
        .expect("create payment");
    db.payments().mark_completed(payment.id).expect("mark payment completed")
}

#[test]
fn refund_amount_must_be_positive() {
    let db = db();
    let payment = completed_payment(&db, dec!(100.00));

    let err = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(0.00)),
            ..Default::default()
        })
        .expect_err("zero-amount refund must be rejected");
    assert!(matches!(err, CommerceError::ValidationError(_)), "got {err:?}");

    let err = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(-5.00)),
            ..Default::default()
        })
        .expect_err("negative-amount refund must be rejected");
    assert!(matches!(err, CommerceError::ValidationError(_)), "got {err:?}");
}

#[test]
fn refund_exceeding_remaining_is_rejected() {
    let db = db();
    let payment = completed_payment(&db, dec!(100.00));

    let err = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(150.00)),
            ..Default::default()
        })
        .expect_err("refund larger than payment must be rejected");
    assert!(matches!(err, CommerceError::ValidationError(_)), "got {err:?}");

    // No refund row should have been persisted.
    assert!(db.payments().get_refunds(payment.id).expect("list refunds").is_empty());
    // And the payment's refunded amount stays at zero.
    let reloaded = db.payments().get(payment.id).expect("get").expect("payment present");
    assert_eq!(reloaded.amount_refunded, dec!(0));
}

#[test]
fn over_refund_across_two_refunds_is_rejected() {
    let db = db();
    let payment = completed_payment(&db, dec!(100.00));

    // First partial refund of 60 succeeds and completes.
    let r1 = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(60.00)),
            ..Default::default()
        })
        .expect("first refund");
    db.payments().complete_refund(r1.id).expect("complete first refund");

    // A second refund of 60 would push total refunded to 120 > 100 and must be
    // rejected: only 40 remains refundable.
    let err = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(60.00)),
            ..Default::default()
        })
        .expect_err("second refund exceeding remaining must be rejected");
    assert!(matches!(err, CommerceError::ValidationError(_)), "got {err:?}");
}

#[test]
fn refunding_pending_payment_is_rejected() {
    let db = db();
    // Freshly created payment is `Pending` (not captured/completed).
    let payment = db
        .payments()
        .create(CreatePayment {
            payment_method: PaymentMethodType::CreditCard,
            amount: dec!(100.00),
            ..Default::default()
        })
        .expect("create payment");

    let err = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(10.00)),
            ..Default::default()
        })
        .expect_err("refunding a pending payment must be rejected");
    assert!(matches!(err, CommerceError::ValidationError(_)), "got {err:?}");
}

#[test]
fn refunding_failed_payment_is_rejected() {
    let db = db();
    let payment = db
        .payments()
        .create(CreatePayment {
            payment_method: PaymentMethodType::CreditCard,
            amount: dec!(100.00),
            ..Default::default()
        })
        .expect("create payment");
    db.payments().mark_failed(payment.id, "card declined", Some("declined")).expect("mark failed");

    let err = db
        .payments()
        .create_refund(CreateRefund { payment_id: payment.id, amount: None, ..Default::default() })
        .expect_err("refunding a failed payment must be rejected");
    assert!(matches!(err, CommerceError::ValidationError(_)), "got {err:?}");
}

#[test]
fn exact_full_refund_succeeds_and_flips_to_refunded() {
    let db = db();
    let payment = completed_payment(&db, dec!(100.00));

    // Omitting the amount means "full remaining refund".
    let refund = db
        .payments()
        .create_refund(CreateRefund { payment_id: payment.id, amount: None, ..Default::default() })
        .expect("full refund");
    assert_eq!(refund.amount, dec!(100.00));

    db.payments().complete_refund(refund.id).expect("complete full refund");

    let reloaded = db.payments().get(payment.id).expect("get").expect("payment present");
    assert_eq!(reloaded.amount_refunded, dec!(100.00));
    assert_eq!(reloaded.status, stateset_core::PaymentTransactionStatus::Refunded);
}

#[test]
fn two_partial_refunds_sum_to_exact_decimal_and_flip_to_refunded() {
    let db = db();
    // 0.10 + 0.20 == 0.30 exactly, but '0.10' + '0.20' in SQLite float math is
    // 0.30000000000000004. This is the core money-precision regression.
    let payment = completed_payment(&db, dec!(0.30));

    let r1 = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(0.10)),
            ..Default::default()
        })
        .expect("first partial refund");
    db.payments().complete_refund(r1.id).expect("complete first partial refund");

    // After the first partial refund the payment is partially refunded.
    let mid = db.payments().get(payment.id).expect("get").expect("payment present");
    assert_eq!(mid.amount_refunded, dec!(0.10));
    assert_eq!(mid.status, stateset_core::PaymentTransactionStatus::PartiallyRefunded);

    let r2 = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(0.20)),
            ..Default::default()
        })
        .expect("second partial refund");
    db.payments().complete_refund(r2.id).expect("complete second partial refund");

    let reloaded = db.payments().get(payment.id).expect("get").expect("payment present");
    // Exact decimal: must be 0.30, never 0.30000000000000004.
    assert_eq!(reloaded.amount_refunded, dec!(0.30));
    assert_eq!(
        reloaded.amount_refunded.to_string(),
        "0.30",
        "amount_refunded must be exact decimal text, not a float-coerced value"
    );
    // And the status must flip to fully refunded.
    assert_eq!(reloaded.status, stateset_core::PaymentTransactionStatus::Refunded);
}

/// Atomicity / in-flight-reservation regression.
///
/// `create_refund` now reads the payment, validates the over-refund guard, and
/// inserts the refund inside a single `IMMEDIATE` transaction, and the guard
/// counts *in-flight* (`Pending`/`Processing`) refunds against the remaining
/// balance — not just the already-committed `amount_refunded`.
///
/// Previously the over-refund check only saw `amount_refunded`, which is only
/// updated on `complete_refund`. So two refunds could be *created* (both
/// `Pending`) that together exceeded the payment, and completing both would
/// over-refund the payment. This test pins the new behavior: a second pending
/// refund that would exceed the balance is rejected at creation time even
/// though the first refund has NOT been completed yet.
#[test]
fn second_pending_refund_exceeding_remaining_is_rejected_before_completion() {
    let db = db();
    let payment = completed_payment(&db, dec!(100.00));

    // First refund of 60 is created but deliberately left Pending (not completed),
    // so `amount_refunded` is still 0 on the payment row.
    let r1 = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(60.00)),
            ..Default::default()
        })
        .expect("first pending refund");

    // The committed balance is untouched: amount_refunded is still 0.
    let mid = db.payments().get(payment.id).expect("get").expect("payment present");
    assert_eq!(mid.amount_refunded, dec!(0));

    // A second refund of 60 would, once both complete, push the total to 120 >
    // 100. Because the first 60 is reserved as an in-flight refund, the guard
    // must reject this at creation time — without it, both pending refunds would
    // persist and later over-refund the payment.
    let err = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(60.00)),
            ..Default::default()
        })
        .expect_err("second pending refund exceeding remaining must be rejected");
    assert!(matches!(err, CommerceError::ValidationError(_)), "got {err:?}");

    // Only the first refund should exist.
    let refunds = db.payments().get_refunds(payment.id).expect("list refunds");
    assert_eq!(refunds.len(), 1, "over-refunding second pending refund must not persist");
    assert_eq!(refunds[0].id, r1.id);
}

/// A second pending refund within the remaining balance is allowed, and the
/// reservation only consumes what each in-flight refund actually requests.
#[test]
fn second_pending_refund_within_remaining_is_allowed() {
    let db = db();
    let payment = completed_payment(&db, dec!(100.00));

    // 40 + 50 = 90 <= 100: both fit even though neither is completed yet.
    db.payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(40.00)),
            ..Default::default()
        })
        .expect("first pending refund");

    db.payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(50.00)),
            ..Default::default()
        })
        .expect("second pending refund within remaining");

    // A third refund of 20 would push the reserved total to 110 > 100 and is
    // rejected, confirming the reservation accumulates across in-flight refunds.
    let err = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(20.00)),
            ..Default::default()
        })
        .expect_err("third refund exceeding reserved remaining must be rejected");
    assert!(matches!(err, CommerceError::ValidationError(_)), "got {err:?}");

    let refunds = db.payments().get_refunds(payment.id).expect("list refunds");
    assert_eq!(refunds.len(), 2, "only the two fitting refunds should persist");
}

/// A failed (terminal) refund releases its reservation, so the remaining
/// balance becomes available to a new refund again.
#[test]
fn failed_refund_releases_its_reservation() {
    let db = db();
    let payment = completed_payment(&db, dec!(100.00));

    // Reserve the whole balance with a pending refund...
    let r1 = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(100.00)),
            ..Default::default()
        })
        .expect("full pending refund");

    // ...while it is pending, no further refund can be created.
    let err = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(10.00)),
            ..Default::default()
        })
        .expect_err("balance fully reserved by pending refund");
    assert!(matches!(err, CommerceError::ValidationError(_)), "got {err:?}");

    // Fail the first refund: its reservation is released.
    db.payments().fail_refund(r1.id, "processor declined").expect("fail refund");

    // Now the full balance is refundable again.
    let r2 = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(100.00)),
            ..Default::default()
        })
        .expect("refund after failed reservation released");
    db.payments().complete_refund(r2.id).expect("complete refund");

    let reloaded = db.payments().get(payment.id).expect("get").expect("payment present");
    assert_eq!(reloaded.amount_refunded, dec!(100.00));
    assert_eq!(reloaded.status, stateset_core::PaymentTransactionStatus::Refunded);
}

/// Completing the same refund twice (e.g. a duplicated payment-processor webhook
/// or a retry) must be idempotent: the refund amount is folded into the
/// payment's `amount_refunded` exactly once, never doubled.
#[test]
fn complete_refund_is_idempotent() {
    let db = db();
    let payment = completed_payment(&db, dec!(100.00));

    let refund = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(50.00)),
            ..Default::default()
        })
        .expect("create refund");

    db.payments().complete_refund(refund.id).expect("first completion");
    let once = db.payments().get(payment.id).expect("get").expect("present");
    assert_eq!(once.amount_refunded, dec!(50.00));
    assert_eq!(once.status, stateset_core::PaymentTransactionStatus::PartiallyRefunded);

    // Second completion must be a no-op, not a double-count.
    db.payments().complete_refund(refund.id).expect("second completion is idempotent");
    let twice = db.payments().get(payment.id).expect("get").expect("present");
    assert_eq!(
        twice.amount_refunded,
        dec!(50.00),
        "duplicate complete_refund must NOT double-count into amount_refunded"
    );
    assert_eq!(
        twice.status,
        stateset_core::PaymentTransactionStatus::PartiallyRefunded,
        "payment must not flip to fully Refunded on a duplicate completion"
    );
}

/// A failed (terminal) refund cannot be completed, and completing it must not
/// fold its amount into the payment.
#[test]
fn complete_refund_rejects_failed_refund() {
    let db = db();
    let payment = completed_payment(&db, dec!(100.00));

    let refund = db
        .payments()
        .create_refund(CreateRefund {
            payment_id: payment.id,
            amount: Some(dec!(40.00)),
            ..Default::default()
        })
        .expect("create refund");
    db.payments().fail_refund(refund.id, "processor declined").expect("fail refund");

    let err =
        db.payments().complete_refund(refund.id).expect_err("a failed refund cannot be completed");
    assert!(matches!(err, CommerceError::ValidationError(_)), "got {err:?}");

    let reloaded = db.payments().get(payment.id).expect("get").expect("present");
    assert_eq!(
        reloaded.amount_refunded,
        dec!(0.00),
        "a failed refund must not fold its amount into the payment"
    );
}