paykit 0.1.0

Payment provider abstraction with a Mollie implementation
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
//! Mollie payment provider.
//!
//! [`MollieProvider`] implements [`PaymentProvider`] against Mollie's
//! [Payments API](https://docs.mollie.com/reference/v2/payments-api/overview).
//!
//! # Security
//!
//! Mollie does not sign webhook notifications: the only trustworthy content
//! in one is the payment id. Everything else — including the amount — must
//! be re-fetched from the API and checked against a value you already know.
//! [`PaymentProvider::fetch_verified`] does exactly that and is the
//! recommended way to handle a webhook. See its documentation for what it
//! does and does not prove.
//!
//! The API key given to [`MollieProvider::new`] / [`MollieProvider::builder`]
//! is attached as a bearer token to every request this provider sends. To
//! keep that from leaking:
//!
//! - [`MollieProvider`] and [`MollieProviderBuilder`] do not derive `Debug`,
//!   `Display`, or `Serialize`. [`MollieProvider`]'s hand-written `Debug`
//!   redacts the key.
//! - The key is stored as a [`reqwest::header::HeaderValue`] marked
//!   [`set_sensitive`](reqwest::header::HeaderValue::set_sensitive), which
//!   keeps it out of HPACK's dynamic table on the wire and out of `http`'s
//!   own `Debug` output.
//! - [`MollieProviderBuilder::base_url`] only accepts `https://` origins
//!   with no userinfo, query string, or fragment, since the key is attached
//!   unconditionally to whatever origin the provider is pointed at. Tests
//!   that need a plain-`http` mock server use
//!   [`MollieProviderBuilder::insecure_base_url_for_testing`] instead.
//! - An internally-built client never follows redirects, so a redirect
//!   response can't be used to forward the `Authorization` header to a
//!   different host. A client supplied via
//!   [`MollieProviderBuilder::client`] / [`MollieProvider::with_client`]
//!   keeps whatever redirect policy the caller configured — that's the
//!   caller's responsibility.

mod types;

use std::collections::BTreeMap;
use std::fmt;
use std::time::Duration;

use async_trait::async_trait;
use reqwest::header::{HeaderValue, AUTHORIZATION, RETRY_AFTER};
use reqwest::Method;
use serde::de::DeserializeOwned;

use crate::error::Error;
use crate::money::{Currency, Money};
use crate::payment::{CreatePayment, Payment, PaymentStatus, Refund, RefundRequest, RefundStatus};
use crate::provider::PaymentProvider;
use types::{
    CreatePaymentBody, CreateRefundBody, MollieAmount, MollieErrorBody, MolliePayment, MollieRefund,
};

pub mod webhook;

/// Mollie's production API base URL.
const DEFAULT_BASE_URL: &str = "https://api.mollie.com/v2";

/// Cap on how many bytes of a single provider response this client will
/// buffer in memory, for both success and error responses. An unbounded
/// read from a wedged or hostile endpoint could otherwise OOM the process;
/// no legitimate Mollie response comes close to this size.
const MAX_RESPONSE_BYTES: usize = 1024 * 1024;

/// Default total-request timeout for an internally-built client.
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10);

/// Default connect timeout for an internally-built client.
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(5);

/// Default idle-connection pool size per host for an internally-built
/// client.
const DEFAULT_POOL_MAX_IDLE_PER_HOST: usize = 4;

/// Metadata key paykit uses to round-trip [`CreatePayment`]'s reference
/// through Mollie's free-form `metadata` object (Mollie has no first-class
/// reference field). [`build_metadata`] rejects a caller-supplied metadata
/// entry under this key outright, rather than silently merging over — or
/// being overwritten by — it.
const REFERENCE_METADATA_KEY: &str = "paykit_reference";

/// Mollie payment provider.
///
/// Construct with [`MollieProvider::new`] (internally-built HTTP client),
/// [`MollieProvider::with_client`] (caller-supplied, preferred when the
/// application already owns a [`reqwest::Client`]), or
/// [`MollieProvider::builder`] for finer control.
///
/// Cheap to clone: every field is either a plain `String` or, like
/// [`reqwest::Client`], internally reference-counted.
///
/// Implements [`PaymentProvider`]. See the [module docs](self) for the
/// security properties this type upholds around its API key.
#[derive(Clone)]
pub struct MollieProvider {
    base_url: String,
    client: reqwest::Client,
    auth_header: HeaderValue,
}

// Hand-written rather than derived so the API key is redacted rather than
// printed. `auth_header` itself would already redact via `http`'s own
// `Debug` (it is marked sensitive), but it is left out entirely here rather
// than relying on that as the only line of defense.
impl fmt::Debug for MollieProvider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MollieProvider")
            .field("base_url", &self.base_url)
            .field("api_key", &"***")
            .finish_non_exhaustive()
    }
}

impl MollieProvider {
    /// Starts building a provider with non-default configuration. See
    /// [`MollieProviderBuilder`].
    #[must_use]
    pub fn builder(api_key: impl Into<String>) -> MollieProviderBuilder {
        MollieProviderBuilder::new(api_key.into())
    }

    /// Creates a provider with an internally-built HTTP client, using this
    /// crate's opinionated defaults: a 5 second connect timeout, a 10 second
    /// total request timeout, 4 idle connections pooled per host, and no
    /// redirect following. Use [`MollieProvider::builder`] to override any
    /// of these, or [`MollieProvider::with_client`] to supply your own
    /// client outright.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidRequest`] if `api_key` contains bytes that
    /// are not valid in an HTTP header value, or if the internal HTTP client
    /// could not be built. Never panics.
    pub fn new(api_key: impl Into<String>) -> Result<Self, Error> {
        Self::builder(api_key).build()
    }

    /// Creates a provider that sends requests through a caller-supplied
    /// [`reqwest::Client`].
    ///
    /// **Preferred** over [`MollieProvider::new`] in an application that
    /// already owns a `Client`: sharing one keeps connection pooling and TLS
    /// session resumption working across payment creation, webhook
    /// verification, and any reconciliation sweep, instead of each paying
    /// for its own connection setup. The supplied client's redirect policy
    /// is the caller's responsibility — see the [module docs](self).
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidRequest`] if `api_key` contains bytes that
    /// are not valid in an HTTP header value. Never panics.
    pub fn with_client(api_key: impl Into<String>, client: reqwest::Client) -> Result<Self, Error> {
        Self::builder(api_key).client(client).build()
    }

    fn request(&self, method: Method, path: &str) -> reqwest::RequestBuilder {
        let url = format!("{}{path}", self.base_url);
        self.client
            .request(method, url)
            .header(AUTHORIZATION, self.auth_header.clone())
    }

    async fn send(&self, builder: reqwest::RequestBuilder) -> Result<reqwest::Response, Error> {
        builder.send().await.map_err(map_transport_error)
    }

    /// Sends `builder` and decodes a successful (2xx) JSON response into
    /// `T`, or maps a non-2xx response into the appropriate [`Error`]
    /// variant.
    async fn send_json<T: DeserializeOwned>(
        &self,
        builder: reqwest::RequestBuilder,
    ) -> Result<T, Error> {
        let response = self.send(builder).await?;
        if response.status().is_success() {
            decode_json(response).await
        } else {
            Err(build_error_response(response).await)
        }
    }
}

#[async_trait]
impl PaymentProvider for MollieProvider {
    async fn create_payment(&self, req: CreatePayment) -> Result<Payment, Error> {
        req.validate()?;

        let amount = req.amount();
        let body = CreatePaymentBody {
            amount: MollieAmount::from(amount),
            description: req.description().to_string(),
            redirect_url: req.redirect_url().to_string(),
            webhook_url: req.webhook_url().map(str::to_string),
            metadata: build_metadata(req.reference(), req.metadata())?,
        };

        // `req.idempotency_key()` is always populated (materialized by
        // `CreatePayment::new`), and — critically — the *same* value on
        // every call for a given `req`. Generating a fresh key here on
        // every call would defeat the point of an idempotency key: a caller
        // retrying after a timeout (which `Error::is_retriable` invites)
        // would create a second, live payment instead of Mollie recognizing
        // the retry and returning the first one.
        let builder = self
            .request(Method::POST, "/payments")
            .header("Idempotency-Key", req.idempotency_key())
            .json(&body);

        let wire: MolliePayment = self.send_json(builder).await?;
        let payment = payment_from_wire(wire)?;
        // Mirrors the id check in `get_payment`/`cancel_payment`: never
        // trust an echoed value without checking it against what was asked
        // for. A caller that persists this returned amount and later feeds
        // it to `fetch_verified` would otherwise be comparing provider data
        // against provider data.
        if payment.amount != amount {
            return Err(Error::decode(format!(
                "created a payment for {amount} but the provider echoed back {}",
                payment.amount
            )));
        }
        Ok(payment)
    }

    async fn get_payment(&self, id: &str) -> Result<Payment, Error> {
        let id = validate_payment_id(id)?;
        let path = format!("/payments/{}", encode_path_segment(id));
        let wire: MolliePayment = self.send_json(self.request(Method::GET, &path)).await?;
        let payment = payment_from_wire(wire)?;
        if payment.id != id {
            return Err(Error::decode(format!(
                "requested payment {id:?} but the provider returned payment {:?}",
                payment.id
            )));
        }
        Ok(payment)
    }

    async fn cancel_payment(&self, id: &str) -> Result<Payment, Error> {
        let id = validate_payment_id(id)?;
        let path = format!("/payments/{}", encode_path_segment(id));
        // Mollie returns the updated payment resource on a successful
        // DELETE, not a bare 204 — decode and hand it back rather than
        // discarding it.
        let wire: MolliePayment = self.send_json(self.request(Method::DELETE, &path)).await?;
        let payment = payment_from_wire(wire)?;
        if payment.id != id {
            return Err(Error::decode(format!(
                "requested to cancel payment {id:?} but the provider returned payment {:?}",
                payment.id
            )));
        }
        Ok(payment)
    }

    async fn refund(&self, id: &str, req: RefundRequest) -> Result<Refund, Error> {
        let id = validate_payment_id(id)?;
        let path = format!("/payments/{}/refunds", encode_path_segment(id));
        // `amount` is always sent: Mollie's refund endpoint documents it as
        // required and returns a 422 without it, so the previous
        // "omit for a full refund" behavior always failed in production.
        // `RefundRequest::amount` is required at construction time, so
        // there is no `Option` to omit here in the first place.
        let body = CreateRefundBody {
            amount: MollieAmount::from(req.amount()),
            description: req.description().map(str::to_string),
        };

        // `RefundRequest::new` generates an idempotency key eagerly, exactly
        // like `CreatePayment::new` (see its doc comment for why), so it is
        // always present here and attached unconditionally — mirroring
        // `create_payment` above. Omitting the header on a retry, as this
        // used to do whenever the caller had not called
        // `with_idempotency_key` themselves, would make Mollie treat the
        // retry as a brand new refund: following this crate's own
        // `Error::is_retriable` protocol (retry on timeout/5xx) would then
        // issue a second, duplicate refund — a mistake that loses money just
        // as directly as a duplicate charge.
        let builder = self
            .request(Method::POST, &path)
            .header("Idempotency-Key", req.idempotency_key())
            .json(&body);

        let wire: MollieRefund = self.send_json(builder).await?;
        refund_from_wire(wire, id)
    }
}

/// Builder for [`MollieProvider`]. Construct via [`MollieProvider::builder`].
///
/// Does not implement `Debug`: it holds the same API key `MollieProvider`
/// does, before that key has been wrapped in a redacting `HeaderValue`.
pub struct MollieProviderBuilder {
    api_key: String,
    client: Option<reqwest::Client>,
    base_url: Result<String, Error>,
    timeout: Duration,
    connect_timeout: Duration,
}

impl MollieProviderBuilder {
    fn new(api_key: String) -> Self {
        Self {
            api_key,
            client: None,
            base_url: Ok(DEFAULT_BASE_URL.to_string()),
            timeout: DEFAULT_TIMEOUT,
            connect_timeout: DEFAULT_CONNECT_TIMEOUT,
        }
    }

    /// Supplies a pre-built [`reqwest::Client`] instead of letting this
    /// builder construct one. See [`MollieProvider::with_client`] for why
    /// this is generally preferred.
    ///
    /// [`MollieProviderBuilder::timeout`], [`MollieProviderBuilder::connect_timeout`],
    /// and the internal no-redirect policy only apply to a client this
    /// builder constructs itself; once a client is supplied here, they have
    /// no effect and the supplied client's own configuration — including its
    /// redirect policy — is used as-is.
    #[must_use]
    pub fn client(mut self, client: reqwest::Client) -> Self {
        self.client = Some(client);
        self
    }

    /// Sets the API base URL. Must be an `https://` URL with no userinfo
    /// (`user:pass@host`), query string, or fragment — the API key this
    /// provider holds is attached to every request sent to it, so accepting
    /// an insecure or attacker-chosen origin here would hand that key over.
    /// A single trailing slash, if present, is stripped, so
    /// `https://api.mollie.com/v2` and `https://api.mollie.com/v2/` behave
    /// identically. Use
    /// [`MollieProviderBuilder::insecure_base_url_for_testing`] to point at
    /// a plain-`http` mock server in tests.
    ///
    /// Rejection is deferred to [`MollieProviderBuilder::build`], which
    /// returns [`Error::InvalidRequest`] if `url` was rejected here — this
    /// method's signature has no `Result` to return one from directly.
    #[must_use]
    pub fn base_url(mut self, url: impl Into<String>) -> Self {
        let url = url.into();
        self.base_url = if url.starts_with("https://") {
            normalize_base_url(url)
        } else {
            Err(Error::InvalidRequest(format!(
                "base_url must be an https:// URL, got {url:?}"
            )))
        };
        self
    }

    /// Sets the API base URL without requiring `https://`.
    ///
    /// **Never use this in production.** It exists only so tests can point
    /// the provider at a local plain-`http` mock server. This provider
    /// attaches its bearer token to every request unconditionally; using
    /// this for a real base URL hands that token to whatever is listening
    /// there in plaintext.
    ///
    /// Hidden from generated documentation, and **not covered by this
    /// crate's semver guarantees**: its signature or behavior may change in
    /// a patch release.
    #[doc(hidden)]
    #[must_use]
    pub fn insecure_base_url_for_testing(mut self, url: impl Into<String>) -> Self {
        self.base_url = normalize_base_url(url.into());
        self
    }

    /// Sets the total-request timeout for an internally-built client
    /// (default 10 seconds). No effect if [`MollieProviderBuilder::client`]
    /// was called.
    #[must_use]
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Sets the connect timeout for an internally-built client (default 5
    /// seconds). No effect if [`MollieProviderBuilder::client`] was called.
    #[must_use]
    pub fn connect_timeout(mut self, timeout: Duration) -> Self {
        self.connect_timeout = timeout;
        self
    }

    /// Builds the provider.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidRequest`] if `base_url` was rejected (see
    /// [`MollieProviderBuilder::base_url`]), if the API key contains bytes
    /// that are not valid in an HTTP header value, or if building an
    /// internal HTTP client failed. This never panics, unlike constructing a
    /// [`reqwest::Client`] with `.expect(..)`.
    pub fn build(self) -> Result<MollieProvider, Error> {
        let base_url = self.base_url?;
        let auth_header = build_auth_header(&self.api_key)?;
        let client = match self.client {
            Some(client) => client,
            None => reqwest::Client::builder()
                .connect_timeout(self.connect_timeout)
                .timeout(self.timeout)
                .pool_max_idle_per_host(DEFAULT_POOL_MAX_IDLE_PER_HOST)
                .redirect(reqwest::redirect::Policy::none())
                .build()
                .map_err(|err| {
                    Error::InvalidRequest(format!("failed to build internal HTTP client: {err}"))
                })?,
        };
        Ok(MollieProvider {
            base_url,
            client,
            auth_header,
        })
    }
}

/// Builds the `Authorization: Bearer <key>` header value, marked sensitive
/// so it is redacted from `http`'s own `Debug` output and excluded from
/// HPACK's dynamic table.
fn build_auth_header(api_key: &str) -> Result<HeaderValue, Error> {
    let mut value = HeaderValue::from_str(&format!("Bearer {api_key}")).map_err(|_| {
        Error::InvalidRequest(
            "api_key contains bytes that are not valid in an HTTP header value".to_string(),
        )
    })?;
    value.set_sensitive(true);
    Ok(value)
}

/// Rejects a `base_url` containing userinfo (`user:pass@host`), a query
/// string, or a fragment, and strips a single trailing slash so
/// `https://api.mollie.com/v2/` and `https://api.mollie.com/v2` produce
/// identical request paths instead of a double slash.
///
/// Userinfo is the sharpest edge this closes:
/// `https://api.mollie.com@evil.example/v2` has `api.mollie.com` as
/// *userinfo*, not the host — the request, and this provider's bearer
/// token, actually go to `evil.example`. A query string or fragment
/// appended here would land partway through every path this provider
/// builds (`{base_url}/payments/{id}`), corrupting it silently rather than
/// failing loudly.
///
/// This is a hand-rolled, minimal check rather than a dependency on the
/// `url` crate: `url` is only present transitively, via `reqwest`, not as a
/// direct dependency of this crate.
fn normalize_base_url(url: String) -> Result<String, Error> {
    if url.contains('?') || url.contains('#') {
        return Err(Error::InvalidRequest(format!(
            "base_url must not contain a query string or fragment, got {url:?}"
        )));
    }

    // The authority is everything between "scheme://" and the next '/' (or
    // the rest of the string, if there is no path). Userinfo, if present,
    // is a "user:pass@" prefix on it.
    let after_scheme = url.split_once("://").map_or(url.as_str(), |(_, rest)| rest);
    let authority = after_scheme.split('/').next().unwrap_or(after_scheme);
    if authority.contains('@') {
        return Err(Error::InvalidRequest(format!(
            "base_url must not contain userinfo (user:pass@host), got {url:?}"
        )));
    }

    Ok(url.strip_suffix('/').map(str::to_string).unwrap_or(url))
}

/// Rejects an empty or all-whitespace payment id before it is interpolated
/// into a request path.
///
/// Mollie's `GET /v2/payments/{id}` degrades to the **list** endpoint,
/// `GET /v2/payments/`, when `{id}` is empty — silently returning a
/// different resource, with the live API key still attached, instead of the
/// 404 a missing single payment should produce.
fn validate_payment_id(id: &str) -> Result<&str, Error> {
    if id.trim().is_empty() {
        return Err(Error::InvalidRequest(
            "payment id must not be empty".to_string(),
        ));
    }
    Ok(id)
}

/// Percent-encodes a path segment (RFC 3986 unreserved characters pass
/// through unchanged, everything else becomes `%XX`).
///
/// Defensive: `id` is a caller-supplied string (from a webhook payload, a
/// query parameter, ...) that is otherwise interpolated directly into a
/// request path. Without this, a crafted id could inject extra path
/// segments or query parameters into the request this provider sends with
/// its bearer token attached.
fn encode_path_segment(input: &str) -> String {
    use std::fmt::Write as _;

    let mut out = String::with_capacity(input.len());
    for byte in input.bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
                out.push(byte as char);
            }
            _ => {
                // `write!` to a `String` never fails.
                let _ = write!(out, "%{byte:02X}");
            }
        }
    }
    out
}

/// Builds the `metadata` object sent to Mollie by merging a request's
/// caller-supplied metadata with its reference, nested under
/// [`REFERENCE_METADATA_KEY`] — Mollie has no first-class reference field.
/// Returns `Ok(None)` if there is nothing to send.
///
/// # Errors
///
/// Returns [`Error::InvalidRequest`] if `metadata` already uses
/// [`REFERENCE_METADATA_KEY`] as one of its own keys. The two used to be
/// merged flatly rather than actually nested as documented, silently
/// letting the reference clobber a caller value under the same key (or
/// vice versa, depending on iteration order); rejecting the collision
/// up front is the only option that can't silently lose data in either
/// direction.
fn build_metadata(
    reference: Option<&str>,
    metadata: &BTreeMap<String, String>,
) -> Result<Option<serde_json::Value>, Error> {
    if metadata.contains_key(REFERENCE_METADATA_KEY) {
        return Err(Error::InvalidRequest(format!(
            "metadata key {REFERENCE_METADATA_KEY:?} is reserved for CreatePayment::reference"
        )));
    }
    if reference.is_none() && metadata.is_empty() {
        return Ok(None);
    }
    let mut map = serde_json::Map::with_capacity(metadata.len() + 1);
    for (key, value) in metadata {
        map.insert(key.clone(), serde_json::Value::String(value.clone()));
    }
    if let Some(reference) = reference {
        map.insert(
            REFERENCE_METADATA_KEY.to_string(),
            serde_json::Value::String(reference.to_string()),
        );
    }
    Ok(Some(serde_json::Value::Object(map)))
}

/// Inverse of [`build_metadata`]: splits a provider-returned `metadata`
/// value back into a reference and the caller's own metadata map. Never
/// fails — a non-object value or a non-string entry is treated as absent
/// metadata rather than an error, since this is provider-controlled data
/// that must not be able to break decoding of an otherwise-valid payment.
fn parse_metadata(value: Option<serde_json::Value>) -> (Option<String>, BTreeMap<String, String>) {
    let mut metadata = BTreeMap::new();
    let mut reference = None;

    if let Some(serde_json::Value::Object(map)) = value {
        for (key, value) in map {
            let value = match value {
                serde_json::Value::String(s) => s,
                other => other.to_string(),
            };
            if key == REFERENCE_METADATA_KEY {
                reference = Some(value);
            } else {
                metadata.insert(key, value);
            }
        }
    }

    (reference, metadata)
}

fn money_from_wire(amount: &MollieAmount) -> Result<Money, Error> {
    let currency = Currency::new(&amount.currency).map_err(|err| {
        Error::decode(format!(
            "invalid currency {:?} in provider response: {err}",
            amount.currency
        ))
    })?;
    Money::parse_decimal(&amount.value, currency).map_err(|err| {
        Error::decode(format!(
            "invalid amount {:?} in provider response: {err}",
            amount.value
        ))
    })
}

/// Returns `true` if `url` is an absolute `http://` or `https://` URL.
///
/// Mirrors the check `CreatePayment::validate` applies to this crate's own
/// *outbound* `redirect_url`/`webhook_url`, applied here to Mollie's
/// *inbound* `_links.checkout.href` — see the safety note on
/// [`payment_from_wire`] for why the asymmetry matters. A plain prefix
/// check, not a full parse: this crate has no URL-parsing dependency, and a
/// scheme check is enough to reject the realistic hostile case (a
/// `javascript:`, `data:`, or similar non-`http(s)` scheme).
fn is_absolute_http_url(url: &str) -> bool {
    url.starts_with("http://") || url.starts_with("https://")
}

/// Converts a wire payment into a [`Payment`].
///
/// # Security: `checkout_url` scheme
///
/// `_links.checkout.href` is provider-supplied data, copied verbatim into
/// [`Payment::checkout_url`] — which this crate's own README tells
/// integrators to redirect their customer to. A hostile or compromised
/// provider response naming a `javascript:` (or other non-`http(s)`) URL
/// there would otherwise round-trip straight into that redirect unchecked.
/// Rejected here with [`Error::Decode`] before it ever reaches a caller,
/// the same way [`CreatePayment::validate`](crate::payment::CreatePayment::validate)
/// already validates this crate's outbound URLs.
fn payment_from_wire(wire: MolliePayment) -> Result<Payment, Error> {
    let amount = money_from_wire(&wire.amount)?;
    let status = payment_status(&wire.status);
    let checkout_url = wire
        .links
        .and_then(|links| links.checkout)
        .map(|link| link.href);
    // Absent data maps to `None`, never a fabricated placeholder value.
    let (reference, metadata) = parse_metadata(wire.metadata);

    let mut payment = Payment::new(wire.id, status, amount).with_metadata_map(metadata);
    if let Some(url) = checkout_url {
        if !is_absolute_http_url(&url) {
            return Err(Error::decode(format!(
                "provider returned a checkout URL with a non-http(s) scheme: {url:?}"
            )));
        }
        payment = payment.with_checkout_url(url);
    }
    if let Some(reference) = reference {
        payment = payment.with_reference(reference);
    }
    Ok(payment)
}

/// Converts a wire refund into a [`Refund`], falling back to
/// `requested_payment_id` when Mollie's response omits `paymentId` (see
/// [`types::MollieRefund::payment_id`]), and rejecting a response that
/// names a *different* payment outright rather than silently trusting it.
fn refund_from_wire(wire: MollieRefund, requested_payment_id: &str) -> Result<Refund, Error> {
    let amount = money_from_wire(&wire.amount)?;
    let payment_id = match wire.payment_id {
        Some(payment_id) if payment_id == requested_payment_id => payment_id,
        Some(payment_id) => {
            return Err(Error::decode(format!(
                "refunded payment {requested_payment_id:?} but the provider returned a refund \
                 for payment {payment_id:?}"
            )));
        }
        None => requested_payment_id.to_string(),
    };
    Ok(Refund::new(
        wire.id,
        payment_id,
        amount,
        refund_status(&wire.status),
    ))
}

/// Maps Mollie's payment status string to [`PaymentStatus`]. Anything this
/// crate does not recognize is preserved verbatim in
/// [`PaymentStatus::Unknown`] rather than causing a decode failure or being
/// silently discarded.
fn payment_status(raw: &str) -> PaymentStatus {
    match raw {
        "open" => PaymentStatus::Open,
        "pending" => PaymentStatus::Pending,
        "authorized" => PaymentStatus::Authorized,
        "paid" => PaymentStatus::Paid,
        "failed" => PaymentStatus::Failed,
        // Mollie's documented value is the US spelling "canceled"; the UK
        // spelling is accepted defensively in case it is ever sent.
        "canceled" | "cancelled" => PaymentStatus::Cancelled,
        "expired" => PaymentStatus::Expired,
        other => PaymentStatus::Unknown(other.into()),
    }
}

/// Maps Mollie's refund status string to [`RefundStatus`]. See
/// [`payment_status`] for why unrecognized values are preserved rather than
/// rejected.
fn refund_status(raw: &str) -> RefundStatus {
    match raw {
        "queued" => RefundStatus::Queued,
        "pending" => RefundStatus::Pending,
        "processing" => RefundStatus::Processing,
        "refunded" => RefundStatus::Refunded,
        "failed" => RefundStatus::Failed,
        other => RefundStatus::Unknown(other.into()),
    }
}

/// Maps a transport-level [`reqwest::Error`] (one that happened before, or
/// while, reading a response — not an HTTP error status) to [`Error`].
///
/// Always strips the URL first: it can carry sensitive query material (e.g.
/// an id) into whatever logs a consumer's `{:?}` of the resulting `Error`
/// ends up in.
///
/// A *builder* error (an invalid header value, an unparseable URL, ...) is
/// mapped to [`Error::InvalidRequest`], not [`Error::Transport`]: it means
/// the request was never sendable in the first place, so it can never
/// succeed on retry — unlike a genuine transport failure. Checked before
/// [`reqwest::Error::is_timeout`] since a builder error is never a timeout.
fn map_transport_error(err: reqwest::Error) -> Error {
    let err = err.without_url();
    if err.is_builder() {
        Error::InvalidRequest(format!("failed to build the request: {err}"))
    } else if err.is_timeout() {
        Error::Timeout
    } else {
        Error::Transport(Box::new(err))
    }
}

/// Reads a response body without buffering more than [`MAX_RESPONSE_BYTES`].
/// Checked against `Content-Length` up front, and against the actual bytes
/// read as they arrive, since `Content-Length` can be absent or simply
/// wrong.
async fn read_bounded(mut response: reqwest::Response) -> Result<Vec<u8>, Error> {
    if let Some(len) = response.content_length() {
        if len > MAX_RESPONSE_BYTES as u64 {
            return Err(too_large_error(len));
        }
    }

    let mut body = Vec::new();
    while let Some(chunk) = response.chunk().await.map_err(map_transport_error)? {
        if body.len().saturating_add(chunk.len()) > MAX_RESPONSE_BYTES {
            return Err(too_large_error(
                (body.len().saturating_add(chunk.len())) as u64,
            ));
        }
        body.extend_from_slice(&chunk);
    }
    Ok(body)
}

fn too_large_error(observed_bytes: u64) -> Error {
    Error::decode(format!(
        "response body of at least {observed_bytes} bytes exceeds the \
         {MAX_RESPONSE_BYTES}-byte cap"
    ))
}

async fn decode_json<T: DeserializeOwned>(response: reqwest::Response) -> Result<T, Error> {
    let body = read_bounded(response).await?;
    serde_json::from_slice(&body).map_err(|err| {
        Error::decode(format!("failed to decode provider response as JSON: {err}"))
            .with_raw_body(String::from_utf8_lossy(&body).into_owned())
    })
}

/// Cap on how many characters of a provider-supplied `title`/`detail`
/// string end up in an [`Error::Api`]. Unlike `raw_body`, these two are
/// printed by [`Error`]'s `Display` impl, so an unbounded value here is a
/// direct (if minor) log-noise concern, not just a confidentiality one.
const MAX_ERROR_FIELD_LEN: usize = 200;

/// Strips control characters — including `\n`/`\r`, the classic
/// log-forging vector: a `title` containing one can otherwise inject a fake
/// extra line into anything that prints this error — and truncates to
/// [`MAX_ERROR_FIELD_LEN`] characters.
///
/// Applied to `title` and `detail` before they reach [`Error::Api`]: the
/// only two fields copied out of a provider's response body into something
/// `Error`'s `Display` prints. `raw_body` itself is already kept out of
/// `Display`/`Debug` by `Error` and needs no such treatment.
fn sanitize_error_field(input: &str) -> String {
    input
        .chars()
        .filter(|c| !c.is_control())
        .take(MAX_ERROR_FIELD_LEN)
        .collect()
}

/// Maps a non-2xx response to the corresponding [`Error`] variant so that
/// [`Error::is_retriable`] is meaningful. The response body is always read
/// (bounded by [`MAX_RESPONSE_BYTES`]) for every non-2xx status *except*
/// `429`: [`Error::RateLimited`] has no `raw_body` field to put it in, so a
/// `429` response body is never read at all — there is nothing to gain from
/// paying for a read whose result could not be attached to anything and
/// would just be dropped. For `401`/`403`/`404` the body *is* read and
/// captured: Mollie's body is usually the only thing that says *why* —
/// "test key against a live payment" versus an inactive profile for a
/// `403`, or a misconfigured `base_url` for a `404` whose status alone is
/// otherwise maximally misleading. That body is reachable via
/// [`Error::raw_body`], never via `Display`/`Debug`.
///
/// - `429` -> [`Error::rate_limited`], with `retry_after` parsed from the
///   `Retry-After` header when present (retriable). No body is read.
/// - `401`/`403` -> [`Error::unauthorized`], `404` -> [`Error::not_found`],
///   each carrying the body. These keep their own variants rather than
///   collapsing into [`Error::api`], so that a consumer matching on
///   `Error::Unauthorized` — the obvious way to detect a bad API key —
///   actually fires.
/// - everything else (`422` and other `4xx`, and `5xx`) -> [`Error::api`],
///   retriable only for `5xx`. When Mollie's typed error body decodes, its
///   `title`/`detail` are attached after [`sanitize_error_field`] strips
///   control characters and bounds their length. Both are kept out of
///   `Display`/`Debug` — see [`Error::title`]/[`Error::detail`].
async fn build_error_response(response: reqwest::Response) -> Error {
    let status = response.status();
    let retry_after = parse_retry_after(response.headers());

    // Handled before anything reads the body: a 429 response body has
    // nowhere to go (see the doc comment above), so it is left unread on
    // the wire rather than buffered and immediately discarded.
    if status.as_u16() == 429 {
        return Error::rate_limited(retry_after);
    }

    let raw_body = read_bounded(response)
        .await
        .ok()
        .map(|bytes| String::from_utf8_lossy(&bytes).into_owned());

    let dedicated = match status.as_u16() {
        401 | 403 => Some(Error::unauthorized()),
        404 => Some(Error::not_found()),
        _ => None,
    };
    if let Some(mut err) = dedicated {
        if let Some(raw_body) = raw_body {
            err = err.with_raw_body(raw_body);
        }
        return err;
    }

    // Only parsed once the response is known to need it: neither the `429`
    // nor the `401`/`403`/`404` paths above use the typed error body, so
    // deserializing it for them would be wasted work.
    let parsed: Option<MollieErrorBody> = raw_body
        .as_deref()
        .and_then(|body| serde_json::from_str(body).ok());

    let title = parsed
        .as_ref()
        .and_then(|body| body.title.as_deref())
        .map(sanitize_error_field)
        .unwrap_or_else(|| {
            status
                .canonical_reason()
                .unwrap_or("unknown provider error")
                .to_string()
        });

    let mut err = Error::api(status.as_u16(), title);
    if let Some(code) = parsed.as_ref().and_then(|body| body.field.clone()) {
        err = err.with_code(code);
    }
    if let Some(detail) = parsed.and_then(|body| body.detail) {
        err = err.with_detail(sanitize_error_field(&detail));
    }
    if let Some(raw_body) = raw_body {
        err = err.with_raw_body(raw_body);
    }
    err
}

/// Cap applied to a parsed `Retry-After` value. Mollie's real rate-limit
/// windows are seconds to low minutes; without a cap, a malformed or
/// hostile response (`Retry-After: 999999999999`) would otherwise produce a
/// multi-millennium [`Duration`] that a caller sleeping on it would never
/// wake up from.
const MAX_RETRY_AFTER: Duration = Duration::from_secs(300);

/// Parses a `Retry-After` header as a whole number of delta-seconds, which
/// is the form Mollie's rate-limit responses use, clamped to
/// [`MAX_RETRY_AFTER`]. The HTTP-date form is not handled — a `None` here
/// just means the caller falls back to its own retry/backoff policy.
fn parse_retry_after(headers: &reqwest::header::HeaderMap) -> Option<Duration> {
    headers
        .get(RETRY_AFTER)
        .and_then(|value| value.to_str().ok())
        .and_then(|value| value.trim().parse::<u64>().ok())
        .map(|secs| Duration::from_secs(secs).min(MAX_RETRY_AFTER))
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use wiremock::matchers::{body_partial_json, method, path};
    use wiremock::{Match, Mock, MockServer, Request, ResponseTemplate};

    use super::*;
    use crate::payment::RefundStatus;

    fn test_provider(server: &MockServer) -> MollieProvider {
        MollieProvider::builder("test_key")
            .insecure_base_url_for_testing(server.uri())
            .build()
            .unwrap()
    }

    fn eur(minor: i64) -> Money {
        Money::from_minor(minor, Currency::EUR)
    }

    fn mollie_payment_json(
        id: &str,
        status: &str,
        value: &str,
        currency: &str,
    ) -> serde_json::Value {
        serde_json::json!({
            "id": id,
            "status": status,
            "amount": {"currency": currency, "value": value},
            "_links": {"checkout": {"href": "https://mollie.example/pay/1"}},
        })
    }

    // --- construction: no panics, redaction, clone ---

    #[test]
    fn new_never_panics_and_defaults_to_the_mollie_api() {
        let provider = MollieProvider::new("test_key").unwrap();
        assert!(format!("{provider:?}").contains("api.mollie.com"));
    }

    #[test]
    fn provider_is_cheaply_cloneable() {
        let provider = MollieProvider::new("test_key").unwrap();
        let cloned = provider.clone();
        assert_eq!(format!("{provider:?}"), format!("{cloned:?}"));
    }

    #[test]
    fn base_url_rejects_non_https_scheme() {
        let err = MollieProvider::builder("key")
            .base_url("http://api.mollie.com/v2")
            .build()
            .unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[test]
    fn base_url_accepts_https_scheme() {
        let provider = MollieProvider::builder("key")
            .base_url("https://api.mollie.com/v2")
            .build();
        assert!(provider.is_ok());
    }

    #[test]
    fn base_url_rejects_userinfo() {
        let err = MollieProvider::builder("key")
            .base_url("https://api.mollie.com@evil.example/v2")
            .build()
            .unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[test]
    fn base_url_rejects_query_string() {
        let err = MollieProvider::builder("key")
            .base_url("https://api.mollie.com/v2?x=1")
            .build()
            .unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[test]
    fn base_url_rejects_fragment() {
        let err = MollieProvider::builder("key")
            .base_url("https://api.mollie.com/v2#frag")
            .build()
            .unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[tokio::test]
    async fn base_url_trailing_slash_does_not_double_up_the_path() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(mollie_payment_json("tr_1", "open", "1.00", "EUR")),
            )
            .mount(&server)
            .await;

        let provider = MollieProvider::builder("test_key")
            .insecure_base_url_for_testing(format!("{}/", server.uri()))
            .build()
            .unwrap();
        let payment = provider.get_payment("tr_1").await.unwrap();
        assert_eq!(payment.id, "tr_1");
    }

    #[test]
    fn build_never_panics_on_an_api_key_with_invalid_header_bytes() {
        let err = MollieProvider::new("bad\nkey").unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[test]
    fn debug_never_contains_the_api_key() {
        let provider = MollieProvider::new("sk_live_super_secret_key").unwrap();
        let debug = format!("{provider:?}");
        assert!(!debug.contains("sk_live_super_secret_key"));
    }

    #[tokio::test]
    async fn api_key_never_appears_in_an_error_from_an_unreachable_host() {
        let provider = MollieProvider::builder("sk_live_super_secret_key")
            .insecure_base_url_for_testing("http://127.0.0.1:1")
            .build()
            .unwrap();
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(!format!("{err:?}").contains("sk_live_super_secret_key"));
        assert!(!format!("{err}").contains("sk_live_super_secret_key"));
    }

    // --- redirects ---

    #[tokio::test]
    async fn redirects_are_not_followed() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(
                ResponseTemplate::new(302)
                    .insert_header("Location", "http://example.invalid/elsewhere"),
            )
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(matches!(err, Error::Api { status: 302, .. }));
    }

    // --- bounded reads ---

    #[tokio::test]
    async fn oversized_response_body_is_rejected() {
        let server = MockServer::start().await;
        let huge = "x".repeat(2 * MAX_RESPONSE_BYTES);
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(200).set_body_string(huge))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(matches!(err, Error::Decode { .. }));
    }

    // --- empty/blank ids ---

    #[tokio::test]
    async fn get_payment_rejects_an_empty_id_without_sending_a_request() {
        let server = MockServer::start().await;
        // No mock mounted: if a request were sent despite the empty id,
        // wiremock's default 404 would surface as `Error::Api` instead of
        // `Error::InvalidRequest`.
        let provider = test_provider(&server);
        let err = provider.get_payment("").await.unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[tokio::test]
    async fn get_payment_rejects_a_blank_id() {
        let server = MockServer::start().await;
        let provider = test_provider(&server);
        let err = provider.get_payment("   ").await.unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[tokio::test]
    async fn cancel_payment_rejects_an_empty_id() {
        let server = MockServer::start().await;
        let provider = test_provider(&server);
        let err = provider.cancel_payment("").await.unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[tokio::test]
    async fn refund_rejects_an_empty_id() {
        let server = MockServer::start().await;
        let provider = test_provider(&server);
        let err = provider
            .refund("", RefundRequest::new(eur(500)))
            .await
            .unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    // --- get_payment ---

    #[tokio::test]
    async fn get_payment_maps_status_amount_checkout_url_reference_and_metadata() {
        let server = MockServer::start().await;
        let body = serde_json::json!({
            "id": "tr_1",
            "status": "paid",
            "amount": {"currency": "EUR", "value": "12.34"},
            "metadata": {"paykit_reference": "order-42", "channel": "web"},
            "_links": {"checkout": {"href": "https://mollie.example/pay/tr_1"}},
        });
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(body))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let payment = provider.get_payment("tr_1").await.unwrap();

        assert_eq!(payment.id, "tr_1");
        assert_eq!(payment.status, PaymentStatus::Paid);
        assert_eq!(payment.amount, eur(1234));
        assert_eq!(
            payment.checkout_url.as_deref(),
            Some("https://mollie.example/pay/tr_1")
        );
        assert_eq!(payment.reference.as_deref(), Some("order-42"));
        assert_eq!(
            payment.metadata.get("channel").map(String::as_str),
            Some("web")
        );
        assert!(!payment.metadata.contains_key("paykit_reference"));
    }

    #[tokio::test]
    async fn get_payment_rejects_a_checkout_url_with_a_non_http_scheme() {
        let server = MockServer::start().await;
        let body = serde_json::json!({
            "id": "tr_1",
            "status": "open",
            "amount": {"currency": "EUR", "value": "1.00"},
            "_links": {"checkout": {"href": "javascript:alert(1)"}},
        });
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(body))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(matches!(err, Error::Decode { .. }));
    }

    #[tokio::test]
    async fn get_payment_accepts_a_plain_http_checkout_url() {
        let server = MockServer::start().await;
        let body = serde_json::json!({
            "id": "tr_1",
            "status": "open",
            "amount": {"currency": "EUR", "value": "1.00"},
            "_links": {"checkout": {"href": "http://mollie.example/pay/tr_1"}},
        });
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(body))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let payment = provider.get_payment("tr_1").await.unwrap();
        assert_eq!(
            payment.checkout_url.as_deref(),
            Some("http://mollie.example/pay/tr_1")
        );
    }

    #[tokio::test]
    async fn absent_metadata_and_links_map_to_none_never_fabricated() {
        let server = MockServer::start().await;
        let body = serde_json::json!({
            "id": "tr_1",
            "status": "open",
            "amount": {"currency": "EUR", "value": "1.00"},
        });
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(body))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let payment = provider.get_payment("tr_1").await.unwrap();
        assert_eq!(payment.reference, None);
        assert_eq!(payment.checkout_url, None);
        assert!(payment.metadata.is_empty());
    }

    #[tokio::test]
    async fn get_payment_rejects_a_mismatched_id_in_the_response() {
        let server = MockServer::start().await;
        let body = serde_json::json!({
            "id": "tr_other",
            "status": "open",
            "amount": {"currency": "EUR", "value": "1.00"},
        });
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(body))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(matches!(err, Error::Decode { .. }));
    }

    #[tokio::test]
    async fn unknown_payment_status_is_preserved_verbatim() {
        let server = MockServer::start().await;
        let body = serde_json::json!({
            "id": "tr_1",
            "status": "a_future_status_mollie_invented",
            "amount": {"currency": "EUR", "value": "1.00"},
        });
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(body))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let payment = provider.get_payment("tr_1").await.unwrap();
        match payment.status {
            PaymentStatus::Unknown(raw) => assert_eq!(&*raw, "a_future_status_mollie_invented"),
            other => panic!("expected Unknown, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn payment_id_is_percent_encoded_in_the_request_path() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/weird%20id%2Fwith%20slash"))
            .respond_with(
                ResponseTemplate::new(200).set_body_json(mollie_payment_json(
                    "weird id/with slash",
                    "open",
                    "1.00",
                    "EUR",
                )),
            )
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let payment = provider.get_payment("weird id/with slash").await.unwrap();
        assert_eq!(payment.id, "weird id/with slash");
    }

    // --- error mapping ---

    #[tokio::test]
    async fn unauthorized_maps_to_the_unauthorized_variant_and_captures_the_body() {
        let server = MockServer::start().await;
        let body = serde_json::json!({
            "title": "Unauthorized",
            "detail": "test key used for a live payment",
        });
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(401).set_body_json(&body))
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(matches!(err, Error::Unauthorized { .. }));
        assert!(!err.is_retriable());
        assert!(err.raw_body().unwrap().contains("live payment"));
    }

    #[tokio::test]
    async fn forbidden_also_maps_to_the_unauthorized_variant() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(403))
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(matches!(err, Error::Unauthorized { .. }));
        assert!(!err.is_retriable());
    }

    #[tokio::test]
    async fn not_found_maps_to_the_not_found_variant_and_captures_the_body() {
        let server = MockServer::start().await;
        let body = serde_json::json!({"title": "Not Found", "detail": "No payment exists with token tr_1."});
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(404).set_body_json(&body))
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(matches!(err, Error::NotFound { .. }));
        assert!(!err.is_retriable());
        assert!(err.raw_body().unwrap().contains("No payment exists"));
    }

    #[tokio::test]
    async fn unprocessable_entity_maps_to_api_with_field_as_code_and_is_not_retriable() {
        let server = MockServer::start().await;
        let body = serde_json::json!({
            "status": 422,
            "title": "Unprocessable Entity",
            "detail": "amount is required",
            "field": "amount",
        });
        Mock::given(method("POST"))
            .and(path("/payments"))
            .respond_with(ResponseTemplate::new(422).set_body_json(body))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let req = CreatePayment::new(eur(1000), "order", "https://shop.example/return");
        let err = provider.create_payment(req).await.unwrap_err();
        match &err {
            Error::Api {
                status,
                code,
                title,
                detail,
                ..
            } => {
                assert_eq!(*status, 422);
                assert_eq!(code.as_deref(), Some("amount"));
                assert_eq!(title, "Unprocessable Entity");
                assert_eq!(detail.as_deref(), Some("amount is required"));
            }
            other => panic!("expected Api, got {other:?}"),
        }
        assert!(!err.is_retriable());
    }

    #[tokio::test]
    async fn error_title_and_detail_are_sanitized_of_control_characters() {
        let server = MockServer::start().await;
        let body = serde_json::json!({
            "title": "Bad\r\nRequest: forged log line",
            "detail": "some\ndetail\twith control chars",
        });
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(400).set_body_json(&body))
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        match &err {
            Error::Api { title, detail, .. } => {
                assert!(!title.contains('\n') && !title.contains('\r'));
                let detail = detail.as_deref().unwrap_or_default();
                assert!(!detail.contains('\n') && !detail.contains('\t'));
            }
            other => panic!("expected Api, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn rate_limited_parses_retry_after_seconds_and_is_retriable() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(429).insert_header("Retry-After", "30"))
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        match &err {
            Error::RateLimited { retry_after, .. } => {
                assert_eq!(*retry_after, Some(Duration::from_secs(30)));
            }
            other => panic!("expected RateLimited, got {other:?}"),
        }
        assert!(err.is_retriable());
    }

    #[tokio::test]
    async fn rate_limited_clamps_an_absurd_retry_after_value() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(429).insert_header("Retry-After", "999999999999"))
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        match &err {
            Error::RateLimited { retry_after, .. } => {
                assert_eq!(*retry_after, Some(MAX_RETRY_AFTER));
            }
            other => panic!("expected RateLimited, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn rate_limited_never_captures_a_body_even_when_the_provider_sends_one() {
        // `Error::RateLimited` has no `raw_body` field, so a 429 response
        // body must never be read at all, not merely dropped after reading.
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(
                ResponseTemplate::new(429)
                    .insert_header("Retry-After", "5")
                    .set_body_json(serde_json::json!({"title": "Rate limit exceeded"})),
            )
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(matches!(err, Error::RateLimited { .. }));
        assert_eq!(err.raw_body(), None);
    }

    #[tokio::test]
    async fn rate_limited_does_not_choke_on_an_oversized_body() {
        // Proof that the body genuinely goes unread for a 429: an oversized
        // body would fail with `Error::Decode` (see
        // `oversized_response_body_is_rejected`) for any status this crate
        // actually reads the body for.
        let server = MockServer::start().await;
        let huge = "x".repeat(2 * MAX_RESPONSE_BYTES);
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(
                ResponseTemplate::new(429)
                    .insert_header("Retry-After", "5")
                    .set_body_string(huge),
            )
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(matches!(err, Error::RateLimited { .. }));
    }

    #[tokio::test]
    async fn server_error_maps_to_api_and_is_retriable() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(503))
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(matches!(err, Error::Api { status: 503, .. }));
        assert!(err.is_retriable());
    }

    #[tokio::test]
    async fn api_error_raw_body_is_reachable_but_never_in_display() {
        let server = MockServer::start().await;
        let body = serde_json::json!({
            "title": "Bad Request",
            "detail": "contains a card_number that must never be logged by default",
        });
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(ResponseTemplate::new(400).set_body_json(&body))
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider.get_payment("tr_1").await.unwrap_err();
        assert!(err.raw_body().unwrap().contains("card_number"));
        assert!(!format!("{err}").contains("card_number"));
    }

    // --- create_payment ---

    #[tokio::test]
    async fn create_payment_validates_before_sending_anything() {
        // No mock mounted: if the request were sent despite failing
        // validation, wiremock's default 404 response would surface as
        // `Error::Api` instead of `Error::InvalidRequest`.
        let server = MockServer::start().await;
        let provider = test_provider(&server);
        let req = CreatePayment::new(eur(1000), "order", "/relative-not-absolute");
        let err = provider.create_payment(req).await.unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[tokio::test]
    async fn create_payment_sends_amount_redirect_url_and_metadata_with_reference_nested() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/payments"))
            .and(body_partial_json(serde_json::json!({
                "amount": {"currency": "EUR", "value": "10.00"},
                "redirectUrl": "https://shop.example/return",
                "metadata": {"channel": "web", "paykit_reference": "order-42"},
            })))
            .respond_with(
                ResponseTemplate::new(201)
                    .set_body_json(mollie_payment_json("tr_1", "open", "10.00", "EUR")),
            )
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let req = CreatePayment::new(eur(1000), "order #1", "https://shop.example/return")
            .with_reference("order-42")
            .with_metadata_entry("channel", "web");
        let payment = provider.create_payment(req).await.unwrap();
        assert_eq!(payment.status, PaymentStatus::Open);
    }

    #[tokio::test]
    async fn create_payment_rejects_a_reserved_metadata_key() {
        let server = MockServer::start().await;
        let provider = test_provider(&server);
        let req = CreatePayment::new(eur(1000), "order", "https://shop.example/return")
            .with_metadata_entry(REFERENCE_METADATA_KEY, "user-supplied");
        let err = provider.create_payment(req).await.unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[tokio::test]
    async fn create_payment_rejects_an_amount_mismatch_echoed_by_the_provider() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/payments"))
            .respond_with(
                ResponseTemplate::new(201)
                    // Provider echoes back a different amount than requested.
                    .set_body_json(mollie_payment_json("tr_1", "open", "5.00", "EUR")),
            )
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let req = CreatePayment::new(eur(1000), "order", "https://shop.example/return");
        let err = provider.create_payment(req).await.unwrap_err();
        assert!(matches!(err, Error::Decode { .. }));
    }

    #[tokio::test]
    async fn create_payment_encodes_zero_decimal_currencies_without_dividing_by_100() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/payments"))
            .and(body_partial_json(
                serde_json::json!({"amount": {"currency": "JPY", "value": "1000"}}),
            ))
            .respond_with(
                ResponseTemplate::new(201)
                    .set_body_json(mollie_payment_json("tr_jpy", "open", "1000", "JPY")),
            )
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let req = CreatePayment::new(
            Money::from_minor(1000, Currency::JPY),
            "order",
            "https://shop.example/return",
        );
        let payment = provider.create_payment(req).await.unwrap();
        assert_eq!(payment.amount, Money::from_minor(1000, Currency::JPY));
    }

    /// Captures the value of a header on every matching request, always
    /// matching so the mock never fails to respond.
    struct CaptureHeader {
        name: &'static str,
        seen: Arc<Mutex<Vec<String>>>,
    }

    impl Match for CaptureHeader {
        fn matches(&self, request: &Request) -> bool {
            if let Some(value) = request.headers.get(self.name).and_then(|v| v.to_str().ok()) {
                self.seen.lock().unwrap().push(value.to_string());
            }
            true
        }
    }

    #[tokio::test]
    async fn create_payment_reuses_the_requests_idempotency_key_on_retry() {
        let server = MockServer::start().await;
        let seen = Arc::new(Mutex::new(Vec::new()));
        Mock::given(method("POST"))
            .and(path("/payments"))
            .and(CaptureHeader {
                name: "idempotency-key",
                seen: Arc::clone(&seen),
            })
            .respond_with(
                ResponseTemplate::new(201)
                    .set_body_json(mollie_payment_json("tr_1", "open", "10.00", "EUR")),
            )
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let req = CreatePayment::new(eur(1000), "order", "https://shop.example/return");
        // Simulates a caller retrying the exact same request after, say, a
        // timeout — the entire point of an idempotency key. Generating a
        // fresh one per call here (rather than reading it from `req`) would
        // make Mollie treat the retry as a brand new payment.
        provider.create_payment(req.clone()).await.unwrap();
        provider.create_payment(req.clone()).await.unwrap();

        let seen = seen.lock().unwrap();
        assert_eq!(seen.len(), 2);
        assert_eq!(
            seen[0], seen[1],
            "a retried request must reuse the same idempotency key, not a fresh one"
        );
    }

    #[tokio::test]
    async fn create_payment_uses_the_caller_supplied_idempotency_key_when_present() {
        let server = MockServer::start().await;
        let seen = Arc::new(Mutex::new(Vec::new()));
        Mock::given(method("POST"))
            .and(path("/payments"))
            .and(CaptureHeader {
                name: "idempotency-key",
                seen: Arc::clone(&seen),
            })
            .respond_with(
                ResponseTemplate::new(201)
                    .set_body_json(mollie_payment_json("tr_1", "open", "10.00", "EUR")),
            )
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let req = CreatePayment::new(eur(1000), "order", "https://shop.example/return")
            .with_idempotency_key("caller-chosen-key");
        provider.create_payment(req).await.unwrap();

        assert_eq!(seen.lock().unwrap().as_slice(), ["caller-chosen-key"]);
    }

    // --- cancel_payment ---

    #[tokio::test]
    async fn cancel_payment_returns_the_decoded_payment_not_a_bare_ack() {
        let server = MockServer::start().await;
        Mock::given(method("DELETE"))
            .and(path("/payments/tr_1"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(mollie_payment_json("tr_1", "canceled", "5.00", "EUR")),
            )
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let payment = provider.cancel_payment("tr_1").await.unwrap();
        assert_eq!(payment.status, PaymentStatus::Cancelled);
        assert_eq!(payment.amount, eur(500));
    }

    #[tokio::test]
    async fn cancel_payment_rejects_a_mismatched_id_in_the_response() {
        let server = MockServer::start().await;
        Mock::given(method("DELETE"))
            .and(path("/payments/tr_1"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(mollie_payment_json("tr_other", "canceled", "5.00", "EUR")),
            )
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider.cancel_payment("tr_1").await.unwrap_err();
        assert!(matches!(err, Error::Decode { .. }));
    }

    // --- refund ---

    #[tokio::test]
    async fn refund_always_sends_amount_and_returns_the_decoded_refund() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/payments/tr_1/refunds"))
            .and(body_partial_json(
                serde_json::json!({"amount": {"currency": "EUR", "value": "5.00"}}),
            ))
            .respond_with(ResponseTemplate::new(201).set_body_json(serde_json::json!({
                "id": "re_1",
                "paymentId": "tr_1",
                "amount": {"currency": "EUR", "value": "5.00"},
                "status": "pending",
            })))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let refund = provider
            .refund("tr_1", RefundRequest::new(eur(500)))
            .await
            .unwrap();
        assert_eq!(refund.id, "re_1");
        assert_eq!(refund.payment_id, "tr_1");
        assert_eq!(refund.amount, eur(500));
        assert_eq!(refund.status, RefundStatus::Pending);
    }

    #[tokio::test]
    async fn refund_falls_back_to_the_requested_payment_id_when_the_response_omits_it() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/payments/tr_1/refunds"))
            .respond_with(ResponseTemplate::new(201).set_body_json(serde_json::json!({
                "id": "re_1",
                "amount": {"currency": "EUR", "value": "5.00"},
                "status": "queued",
            })))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let refund = provider
            .refund("tr_1", RefundRequest::new(eur(500)))
            .await
            .unwrap();
        assert_eq!(refund.payment_id, "tr_1");
    }

    #[tokio::test]
    async fn refund_rejects_a_mismatched_payment_id_in_the_response() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/payments/tr_1/refunds"))
            .respond_with(ResponseTemplate::new(201).set_body_json(serde_json::json!({
                "id": "re_1",
                "paymentId": "tr_other",
                "amount": {"currency": "EUR", "value": "5.00"},
                "status": "queued",
            })))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        let err = provider
            .refund("tr_1", RefundRequest::new(eur(500)))
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Decode { .. }));
    }

    fn mollie_refund_response() -> serde_json::Value {
        serde_json::json!({
            "id": "re_1",
            "paymentId": "tr_1",
            "amount": {"currency": "EUR", "value": "5.00"},
            "status": "pending",
        })
    }

    #[tokio::test]
    async fn refund_always_sends_the_idempotency_key_header() {
        let server = MockServer::start().await;
        let seen = Arc::new(Mutex::new(Vec::new()));
        Mock::given(method("POST"))
            .and(path("/payments/tr_1/refunds"))
            .and(CaptureHeader {
                name: "idempotency-key",
                seen: Arc::clone(&seen),
            })
            .respond_with(ResponseTemplate::new(201).set_body_json(mollie_refund_response()))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        // No `with_idempotency_key` call: the header must still be sent,
        // since `RefundRequest::new` generates one eagerly.
        provider
            .refund("tr_1", RefundRequest::new(eur(500)))
            .await
            .unwrap();

        assert_eq!(
            seen.lock().unwrap().len(),
            1,
            "expected exactly one request, with the Idempotency-Key header present"
        );
    }

    #[tokio::test]
    async fn refund_reuses_the_same_requests_idempotency_key_across_retries() {
        let server = MockServer::start().await;
        let seen = Arc::new(Mutex::new(Vec::new()));
        Mock::given(method("POST"))
            .and(path("/payments/tr_1/refunds"))
            .and(CaptureHeader {
                name: "idempotency-key",
                seen: Arc::clone(&seen),
            })
            .respond_with(ResponseTemplate::new(201).set_body_json(mollie_refund_response()))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        // Simulates a caller retrying the exact same `RefundRequest` value
        // after, say, `Error::Timeout` — the protocol `Error::is_retriable`
        // invites. This must not create a second, duplicate refund.
        let req = RefundRequest::new(eur(500));
        provider.refund("tr_1", req.clone()).await.unwrap();
        provider.refund("tr_1", req).await.unwrap();

        let seen = seen.lock().unwrap();
        assert_eq!(seen.len(), 2);
        assert_eq!(
            seen[0], seen[1],
            "retrying the same RefundRequest value must reuse the same idempotency key"
        );
    }

    #[tokio::test]
    async fn refund_uses_different_idempotency_keys_for_separately_constructed_requests() {
        let server = MockServer::start().await;
        let seen = Arc::new(Mutex::new(Vec::new()));
        Mock::given(method("POST"))
            .and(path("/payments/tr_1/refunds"))
            .and(CaptureHeader {
                name: "idempotency-key",
                seen: Arc::clone(&seen),
            })
            .respond_with(ResponseTemplate::new(201).set_body_json(mollie_refund_response()))
            .mount(&server)
            .await;

        let provider = test_provider(&server);
        provider
            .refund("tr_1", RefundRequest::new(eur(500)))
            .await
            .unwrap();
        provider
            .refund("tr_1", RefundRequest::new(eur(500)))
            .await
            .unwrap();

        let seen = seen.lock().unwrap();
        assert_eq!(seen.len(), 2);
        assert_ne!(
            seen[0], seen[1],
            "two separately constructed RefundRequests must not share an idempotency key"
        );
    }

    // --- fetch_verified / VerifiedPayment (default trait behavior, exercised via the
    // real Mollie wire mapping) ---

    #[tokio::test]
    async fn fetch_verified_returns_verified_payment_on_matching_amount_and_paid_status() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(mollie_payment_json("tr_1", "paid", "10.00", "EUR")),
            )
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let verified = provider.fetch_verified("tr_1", eur(1000)).await.unwrap();
        assert_eq!(verified.payment().status, PaymentStatus::Paid);
        let payment = verified.into_payment();
        assert_eq!(payment.amount, eur(1000));
    }

    #[tokio::test]
    async fn fetch_verified_rejects_amount_mismatch() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(mollie_payment_json("tr_1", "paid", "9.99", "EUR")),
            )
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider
            .fetch_verified("tr_1", eur(1000))
            .await
            .unwrap_err();
        match err {
            Error::AmountMismatch { expected, actual } => {
                assert_eq!(expected, eur(1000));
                assert_eq!(actual, eur(999));
            }
            other => panic!("expected AmountMismatch, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn fetch_verified_rejects_currency_mismatch_even_with_equal_minor_units() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(mollie_payment_json("tr_1", "paid", "10.00", "USD")),
            )
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider
            .fetch_verified("tr_1", eur(1000))
            .await
            .unwrap_err();
        assert!(matches!(err, Error::AmountMismatch { .. }));
    }

    #[tokio::test]
    async fn fetch_verified_rejects_a_matching_amount_that_is_not_paid() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/payments/tr_1"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(mollie_payment_json("tr_1", "open", "10.00", "EUR")),
            )
            .mount(&server)
            .await;
        let provider = test_provider(&server);
        let err = provider
            .fetch_verified("tr_1", eur(1000))
            .await
            .unwrap_err();
        assert!(matches!(err, Error::NotPaid { .. }));
    }

    // --- pure helper functions ---

    #[test]
    fn encode_path_segment_leaves_unreserved_chars_untouched() {
        assert_eq!(encode_path_segment("tr_ABC123-._~"), "tr_ABC123-._~");
    }

    #[test]
    fn encode_path_segment_escapes_everything_else() {
        assert_eq!(encode_path_segment("a b/c"), "a%20b%2Fc");
    }

    #[test]
    fn is_absolute_http_url_accepts_http_and_https() {
        assert!(is_absolute_http_url("http://mollie.example/pay/1"));
        assert!(is_absolute_http_url("https://mollie.example/pay/1"));
    }

    #[test]
    fn is_absolute_http_url_rejects_other_schemes_and_relative_values() {
        assert!(!is_absolute_http_url("javascript:alert(1)"));
        assert!(!is_absolute_http_url(
            "data:text/html,<script>alert(1)</script>"
        ));
        assert!(!is_absolute_http_url("/relative/path"));
        assert!(!is_absolute_http_url(""));
    }

    #[test]
    fn map_transport_error_maps_a_builder_error_to_invalid_request_not_transport() {
        let err = reqwest::Client::new()
            .get("not a valid url")
            .build()
            .unwrap_err();
        assert!(err.is_builder());
        let mapped = map_transport_error(err);
        assert!(matches!(mapped, Error::InvalidRequest(_)));
        assert!(!mapped.is_retriable());
    }

    #[test]
    fn build_and_parse_metadata_round_trips_reference_and_user_metadata() {
        let mut metadata = BTreeMap::new();
        metadata.insert("channel".to_string(), "web".to_string());
        let value = build_metadata(Some("order-42"), &metadata)
            .unwrap()
            .unwrap();
        let (reference, parsed) = parse_metadata(Some(value));
        assert_eq!(reference.as_deref(), Some("order-42"));
        assert_eq!(parsed.get("channel").map(String::as_str), Some("web"));
        assert!(!parsed.contains_key(REFERENCE_METADATA_KEY));
    }

    #[test]
    fn build_metadata_returns_none_when_there_is_nothing_to_send() {
        assert_eq!(build_metadata(None, &BTreeMap::new()).unwrap(), None);
    }

    #[test]
    fn build_metadata_rejects_a_caller_supplied_reserved_key() {
        let mut metadata = BTreeMap::new();
        metadata.insert(REFERENCE_METADATA_KEY.to_string(), "user-value".to_string());
        let err = build_metadata(Some("order-42"), &metadata).unwrap_err();
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[test]
    fn parse_metadata_of_none_is_empty() {
        let (reference, metadata) = parse_metadata(None);
        assert_eq!(reference, None);
        assert!(metadata.is_empty());
    }

    #[test]
    fn money_from_wire_rejects_invalid_currency() {
        let err = money_from_wire(&MollieAmount {
            currency: "EU".into(),
            value: "1.00".into(),
        })
        .unwrap_err();
        assert!(matches!(err, Error::Decode { .. }));
    }

    #[test]
    fn money_from_wire_rejects_invalid_amount() {
        let err = money_from_wire(&MollieAmount {
            currency: "EUR".into(),
            value: "abc".into(),
        })
        .unwrap_err();
        assert!(matches!(err, Error::Decode { .. }));
    }

    #[test]
    fn payment_status_preserves_unrecognized_values() {
        match payment_status("a_future_status") {
            PaymentStatus::Unknown(raw) => assert_eq!(&*raw, "a_future_status"),
            other => panic!("expected Unknown, got {other:?}"),
        }
    }

    #[test]
    fn payment_status_accepts_both_cancelled_spellings() {
        assert_eq!(payment_status("canceled"), PaymentStatus::Cancelled);
        assert_eq!(payment_status("cancelled"), PaymentStatus::Cancelled);
    }

    #[test]
    fn refund_status_preserves_unrecognized_values() {
        match refund_status("a_future_status") {
            RefundStatus::Unknown(raw) => assert_eq!(&*raw, "a_future_status"),
            other => panic!("expected Unknown, got {other:?}"),
        }
    }

    #[test]
    fn sanitize_error_field_strips_control_characters() {
        assert_eq!(sanitize_error_field("line1\r\nline2\ttab"), "line1line2tab");
    }

    #[test]
    fn sanitize_error_field_truncates_long_input() {
        let long = "a".repeat(500);
        assert_eq!(
            sanitize_error_field(&long).chars().count(),
            MAX_ERROR_FIELD_LEN
        );
    }

    #[test]
    fn parse_retry_after_reads_delta_seconds() {
        let mut headers = reqwest::header::HeaderMap::new();
        headers.insert(RETRY_AFTER, HeaderValue::from_static("120"));
        assert_eq!(parse_retry_after(&headers), Some(Duration::from_secs(120)));
    }

    #[test]
    fn parse_retry_after_clamps_to_the_maximum() {
        let mut headers = reqwest::header::HeaderMap::new();
        headers.insert(RETRY_AFTER, HeaderValue::from_static("999999999999"));
        assert_eq!(parse_retry_after(&headers), Some(MAX_RETRY_AFTER));
    }

    #[test]
    fn parse_retry_after_ignores_non_numeric_values() {
        let mut headers = reqwest::header::HeaderMap::new();
        headers.insert(
            RETRY_AFTER,
            HeaderValue::from_static("Wed, 21 Oct 2015 07:28:00 GMT"),
        );
        assert_eq!(parse_retry_after(&headers), None);
    }

    #[test]
    fn parse_retry_after_is_none_when_absent() {
        let headers = reqwest::header::HeaderMap::new();
        assert_eq!(parse_retry_after(&headers), None);
    }
}