zeptoclaw 0.5.5

Ultra-lightweight personal AI assistant
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
//! Stripe payment integration tool with production hardening.
//!
//! Provides a `StripeTool` for interacting with the Stripe API. Features:
//!
//! - **Idempotency keys**: All `create_payment` calls send a unique `Idempotency-Key`
//!   header to prevent duplicate charges on network retries.
//! - **Webhook signature verification**: Validates Stripe-Signature header using
//!   HMAC-SHA256 with timing-safe comparison and timestamp tolerance (5 minutes).
//! - **Rate limit handling**: Automatically sleeps for the `Retry-After` duration
//!   (capped at 30s) and retries once on HTTP 429 responses.
//!
//! ## Supported actions
//!
//! - `create_payment` — Create a PaymentIntent
//! - `get_payment` — Get a PaymentIntent by ID
//! - `list_payments` — List recent PaymentIntents
//! - `create_customer` — Create a Customer
//! - `get_customer` — Get a Customer by ID
//! - `list_customers` — List recent Customers
//! - `create_refund` — Refund a charge or PaymentIntent
//! - `get_balance` — Retrieve current account balance
//! - `verify_webhook` — Verify a Stripe webhook signature (HMAC-SHA256 + timestamp)

use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};

use async_trait::async_trait;
use reqwest::Client;
use serde_json::{json, Value};
use sha2::Digest;

use crate::error::{Result, ZeptoError};

use super::{Tool, ToolContext, ToolOutput};

/// Monotonically-increasing counter for idempotency key disambiguation.
static IDEM_KEY_COUNTER: AtomicU64 = AtomicU64::new(0);

const STRIPE_API_BASE: &str = "https://api.stripe.com/v1";

// ---------------------------------------------------------------------------
// HMAC-SHA256 implementation using the sha2 crate
// ---------------------------------------------------------------------------

/// Block size for SHA-256 in bytes.
const SHA256_BLOCK_SIZE: usize = 64;
/// Output size for SHA-256 in bytes.
const SHA256_OUTPUT_SIZE: usize = 32;

/// Compute HMAC-SHA256(key, message) as a lowercase hex string.
///
/// Implements RFC 2104 HMAC using the sha2 crate's SHA-256 implementation.
/// No external hmac crate required.
fn hmac_sha256_hex(key: &[u8], message: &[u8]) -> String {
    // Normalise key: hash if longer than block size, pad to block size otherwise.
    let mut k = [0u8; SHA256_BLOCK_SIZE];
    if key.len() > SHA256_BLOCK_SIZE {
        let hashed = sha2::Sha256::digest(key);
        k[..SHA256_OUTPUT_SIZE].copy_from_slice(&hashed);
    } else {
        k[..key.len()].copy_from_slice(key);
    }

    // Derive ipad and opad masked keys.
    let mut k_ipad = [0u8; SHA256_BLOCK_SIZE];
    let mut k_opad = [0u8; SHA256_BLOCK_SIZE];
    for i in 0..SHA256_BLOCK_SIZE {
        k_ipad[i] = k[i] ^ 0x36;
        k_opad[i] = k[i] ^ 0x5c;
    }

    // Inner hash: SHA256(k_ipad || message)
    let mut inner = sha2::Sha256::new();
    inner.update(k_ipad);
    inner.update(message);
    let inner_result = inner.finalize();

    // Outer hash: SHA256(k_opad || inner_hash)
    let mut outer = sha2::Sha256::new();
    outer.update(k_opad);
    outer.update(inner_result);
    let mac = outer.finalize();

    // Encode as lowercase hex.
    hex::encode(mac)
}

/// Constant-time byte-slice comparison to resist timing attacks.
fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }
    let mut diff = 0u8;
    for (x, y) in a.iter().zip(b.iter()) {
        diff |= x ^ y;
    }
    diff == 0
}

// ---------------------------------------------------------------------------
// Idempotency key
// ---------------------------------------------------------------------------

/// Generate a unique idempotency key using nanosecond timestamp + process ID +
/// a monotonically-increasing counter.
///
/// The counter ensures keys are unique even when multiple keys are generated
/// within the same nanosecond (e.g. in unit tests). No external uuid crate required.
fn generate_idempotency_key() -> String {
    let ts = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    let pid = std::process::id();
    let seq = IDEM_KEY_COUNTER.fetch_add(1, Ordering::Relaxed);
    format!("zc_{:x}_{:x}_{:x}", ts, pid, seq)
}

// ---------------------------------------------------------------------------
// StripeTool
// ---------------------------------------------------------------------------

/// Tool for Stripe payment API operations with production hardening.
pub struct StripeTool {
    /// Stripe secret key (sk_live_... or sk_test_...).
    secret_key: String,
    /// Default ISO 4217 currency code (e.g., "usd", "myr", "sgd").
    default_currency: String,
    /// Optional webhook signing secret for signature verification.
    webhook_secret: Option<String>,
    /// Reqwest HTTP client.
    client: Client,
}

impl StripeTool {
    /// Create a new StripeTool from explicit credentials.
    pub fn new(secret_key: &str, default_currency: &str) -> Self {
        Self {
            secret_key: secret_key.to_string(),
            default_currency: default_currency.to_string(),
            webhook_secret: None,
            client: Client::new(),
        }
    }

    /// Create with a webhook signing secret for `verify_webhook` support.
    pub fn with_webhook_secret(mut self, webhook_secret: &str) -> Self {
        self.webhook_secret = Some(webhook_secret.to_string());
        self
    }

    /// Create from the global ZeptoClaw configuration.
    ///
    /// Returns an error if `stripe.secret_key` is not configured.
    pub fn from_config() -> Result<Self> {
        let config = crate::config::Config::get();
        let stripe_cfg = &config.stripe;

        let secret_key = stripe_cfg
            .secret_key
            .as_deref()
            .filter(|k| !k.is_empty())
            .ok_or_else(|| {
                ZeptoError::Tool(
                    "stripe.secret_key not configured; set it in config.json or \
                     ZEPTOCLAW_STRIPE_SECRET_KEY"
                        .into(),
                )
            })?;

        Ok(Self {
            secret_key: secret_key.to_string(),
            default_currency: stripe_cfg.default_currency.clone(),
            webhook_secret: stripe_cfg.webhook_secret.clone(),
            client: Client::new(),
        })
    }

    // -----------------------------------------------------------------------
    // HTTP helper with rate-limit retry
    // -----------------------------------------------------------------------

    /// Execute a Stripe API request, retrying once on HTTP 429 (rate limited).
    ///
    /// On 429, sleeps for the `Retry-After` header value (capped at 30s) before
    /// retrying. API errors (4xx/5xx other than 429) are propagated as
    /// `ZeptoError::Tool`.
    async fn stripe_request_with_retry(
        &self,
        method: reqwest::Method,
        url: &str,
        form: Option<Vec<(&str, String)>>,
        idempotency_key: Option<&str>,
    ) -> Result<Value> {
        let resp = self
            .execute_request(method.clone(), url, form.clone(), idempotency_key)
            .await?;

        if resp.status() == 429 {
            // Rate limited — honour Retry-After (capped at 30 seconds).
            let retry_after = resp
                .headers()
                .get("retry-after")
                .and_then(|v| v.to_str().ok())
                .and_then(|s| s.parse::<u64>().ok())
                .unwrap_or(1)
                .min(30);

            tracing::warn!(
                retry_after_secs = retry_after,
                "Stripe rate limit hit (429); backing off"
            );
            tokio::time::sleep(std::time::Duration::from_secs(retry_after)).await;

            // Retry once.
            let resp2 = self
                .execute_request(method, url, form, idempotency_key)
                .await?;
            return self.parse_stripe_response(resp2).await;
        }

        self.parse_stripe_response(resp).await
    }

    /// Build and send a single HTTP request to Stripe.
    async fn execute_request(
        &self,
        method: reqwest::Method,
        url: &str,
        form: Option<Vec<(&str, String)>>,
        idempotency_key: Option<&str>,
    ) -> Result<reqwest::Response> {
        let mut builder = self
            .client
            .request(method, url)
            .basic_auth(&self.secret_key, None::<&str>);

        if let Some(key) = idempotency_key {
            builder = builder.header("Idempotency-Key", key);
        }

        if let Some(params) = form {
            builder = builder.form(&params);
        }

        builder
            .send()
            .await
            .map_err(|e| ZeptoError::Tool(format!("Stripe HTTP error: {}", e)))
    }

    /// Parse a Stripe HTTP response into a JSON `Value`.
    ///
    /// On non-2xx responses, extracts and surfaces the Stripe error message.
    async fn parse_stripe_response(&self, resp: reqwest::Response) -> Result<Value> {
        let status = resp.status();
        let body: Value = resp
            .json()
            .await
            .map_err(|e| ZeptoError::Tool(format!("Failed to parse Stripe response: {}", e)))?;

        if !status.is_success() {
            let error_msg = body
                .get("error")
                .and_then(|e| e.get("message"))
                .and_then(|m| m.as_str())
                .unwrap_or("Unknown Stripe error");
            return Err(ZeptoError::Tool(format!(
                "Stripe API error ({}): {}",
                status, error_msg
            )));
        }

        Ok(body)
    }

    // -----------------------------------------------------------------------
    // Action implementations
    // -----------------------------------------------------------------------

    async fn create_payment(&self, args: &Value) -> Result<String> {
        let amount = args
            .get("amount")
            .and_then(Value::as_i64)
            .ok_or_else(|| ZeptoError::Tool("Missing 'amount' parameter (integer cents)".into()))?;

        if amount <= 0 {
            return Err(ZeptoError::Tool(
                "'amount' must be a positive integer (smallest currency unit)".into(),
            ));
        }

        let currency = args
            .get("currency")
            .and_then(Value::as_str)
            .unwrap_or(&self.default_currency)
            .to_ascii_lowercase();

        let description = args
            .get("description")
            .and_then(Value::as_str)
            .unwrap_or("")
            .to_string();

        let url = format!("{}/payment_intents", STRIPE_API_BASE);
        let mut form: Vec<(&str, String)> =
            vec![("amount", amount.to_string()), ("currency", currency)];
        if !description.is_empty() {
            form.push(("description", description));
        }

        // Idempotency key prevents duplicate charges on network retries.
        let idem_key = generate_idempotency_key();

        let data = self
            .stripe_request_with_retry(reqwest::Method::POST, &url, Some(form), Some(&idem_key))
            .await?;

        let id = data.get("id").and_then(Value::as_str).unwrap_or("unknown");
        let status = data
            .get("status")
            .and_then(Value::as_str)
            .unwrap_or("unknown");
        let client_secret = data
            .get("client_secret")
            .and_then(Value::as_str)
            .unwrap_or("(none)");

        Ok(format!(
            "PaymentIntent created. id={} status={} client_secret={} idempotency_key={}",
            id, status, client_secret, idem_key
        ))
    }

    async fn get_payment(&self, args: &Value) -> Result<String> {
        let id = args
            .get("payment_id")
            .and_then(Value::as_str)
            .ok_or_else(|| ZeptoError::Tool("Missing 'payment_id' parameter".into()))?;

        let url = format!("{}/payment_intents/{}", STRIPE_API_BASE, id);
        let data = self
            .stripe_request_with_retry(reqwest::Method::GET, &url, None, None)
            .await?;

        Ok(serde_json::to_string_pretty(&data)
            .unwrap_or_else(|_| "Failed to format response".to_string()))
    }

    async fn list_payments(&self, args: &Value) -> Result<String> {
        let limit = args
            .get("limit")
            .and_then(Value::as_u64)
            .unwrap_or(10)
            .min(100);

        let url = format!("{}/payment_intents?limit={}", STRIPE_API_BASE, limit);
        let data = self
            .stripe_request_with_retry(reqwest::Method::GET, &url, None, None)
            .await?;

        let items = data
            .get("data")
            .and_then(Value::as_array)
            .map(|arr| arr.len())
            .unwrap_or(0);

        Ok(format!(
            "Found {} payment intent(s):\n{}",
            items,
            serde_json::to_string_pretty(&data).unwrap_or_default()
        ))
    }

    async fn create_customer(&self, args: &Value) -> Result<String> {
        let email = args
            .get("email")
            .and_then(Value::as_str)
            .ok_or_else(|| ZeptoError::Tool("Missing 'email' parameter".into()))?;

        let name = args
            .get("name")
            .and_then(Value::as_str)
            .unwrap_or("")
            .to_string();

        let url = format!("{}/customers", STRIPE_API_BASE);
        let mut form: Vec<(&str, String)> = vec![("email", email.to_string())];
        if !name.is_empty() {
            form.push(("name", name));
        }

        let idem_key = generate_idempotency_key();
        let data = self
            .stripe_request_with_retry(reqwest::Method::POST, &url, Some(form), Some(&idem_key))
            .await?;

        let id = data.get("id").and_then(Value::as_str).unwrap_or("unknown");

        Ok(format!(
            "Customer created. id={} email={} idempotency_key={}",
            id, email, idem_key
        ))
    }

    async fn get_customer(&self, args: &Value) -> Result<String> {
        let id = args
            .get("customer_id")
            .and_then(Value::as_str)
            .ok_or_else(|| ZeptoError::Tool("Missing 'customer_id' parameter".into()))?;

        let url = format!("{}/customers/{}", STRIPE_API_BASE, id);
        let data = self
            .stripe_request_with_retry(reqwest::Method::GET, &url, None, None)
            .await?;

        Ok(serde_json::to_string_pretty(&data)
            .unwrap_or_else(|_| "Failed to format response".to_string()))
    }

    async fn list_customers(&self, args: &Value) -> Result<String> {
        let limit = args
            .get("limit")
            .and_then(Value::as_u64)
            .unwrap_or(10)
            .min(100);

        let url = format!("{}/customers?limit={}", STRIPE_API_BASE, limit);
        let data = self
            .stripe_request_with_retry(reqwest::Method::GET, &url, None, None)
            .await?;

        let items = data
            .get("data")
            .and_then(Value::as_array)
            .map(|arr| arr.len())
            .unwrap_or(0);

        Ok(format!(
            "Found {} customer(s):\n{}",
            items,
            serde_json::to_string_pretty(&data).unwrap_or_default()
        ))
    }

    async fn create_refund(&self, args: &Value) -> Result<String> {
        let payment_intent_id = args
            .get("payment_intent_id")
            .and_then(Value::as_str)
            .ok_or_else(|| ZeptoError::Tool("Missing 'payment_intent_id' parameter".into()))?;

        let url = format!("{}/refunds", STRIPE_API_BASE);
        let mut form: Vec<(&str, String)> = vec![("payment_intent", payment_intent_id.to_string())];

        if let Some(amount) = args.get("amount").and_then(Value::as_i64) {
            if amount > 0 {
                form.push(("amount", amount.to_string()));
            }
        }

        let idem_key = generate_idempotency_key();
        let data = self
            .stripe_request_with_retry(reqwest::Method::POST, &url, Some(form), Some(&idem_key))
            .await?;

        let id = data.get("id").and_then(Value::as_str).unwrap_or("unknown");
        let status = data
            .get("status")
            .and_then(Value::as_str)
            .unwrap_or("unknown");

        Ok(format!(
            "Refund created. id={} status={} idempotency_key={}",
            id, status, idem_key
        ))
    }

    async fn get_balance(&self) -> Result<String> {
        let url = format!("{}/balance", STRIPE_API_BASE);
        let data = self
            .stripe_request_with_retry(reqwest::Method::GET, &url, None, None)
            .await?;

        Ok(serde_json::to_string_pretty(&data)
            .unwrap_or_else(|_| "Failed to format response".to_string()))
    }

    /// Verify a Stripe webhook signature.
    ///
    /// Stripe sends a `Stripe-Signature` header of the form:
    /// `t=<unix_ts>,v1=<hmac_hex>`
    ///
    /// This method:
    /// 1. Parses `t=` (timestamp) and `v1=` (HMAC-SHA256 hex) from the header.
    /// 2. Rejects events whose timestamp is more than 5 minutes old or in the future
    ///    to prevent replay attacks.
    /// 3. Computes `HMAC-SHA256(webhook_secret, "<timestamp>.<payload>")` and
    ///    performs a **constant-time** comparison with the received signature.
    async fn verify_webhook(&self, args: &Value) -> Result<String> {
        let payload = args
            .get("payload")
            .and_then(Value::as_str)
            .ok_or_else(|| ZeptoError::Tool("Missing 'payload' parameter".into()))?;

        let signature = args
            .get("signature")
            .and_then(Value::as_str)
            .ok_or_else(|| ZeptoError::Tool("Missing 'signature' parameter".into()))?;

        let webhook_secret = self
            .webhook_secret
            .as_deref()
            .filter(|s| !s.is_empty())
            .ok_or_else(|| {
                ZeptoError::Tool(
                    "stripe.webhook_secret not configured; set it in config.json or \
                     ZEPTOCLAW_STRIPE_WEBHOOK_SECRET"
                        .into(),
                )
            })?;

        // Parse Stripe-Signature header: t=<ts>,v1=<hex>
        let mut timestamp_str: Option<&str> = None;
        let mut sig_v1: Option<&str> = None;
        for part in signature.split(',') {
            let part = part.trim();
            if let Some(val) = part.strip_prefix("t=") {
                timestamp_str = Some(val);
            } else if let Some(val) = part.strip_prefix("v1=") {
                sig_v1 = Some(val);
            }
        }

        let ts_str = timestamp_str.ok_or_else(|| {
            ZeptoError::Tool("Invalid Stripe-Signature header: missing t= timestamp field".into())
        })?;

        let received_sig = sig_v1.ok_or_else(|| {
            ZeptoError::Tool("Invalid Stripe-Signature header: missing v1= signature field".into())
        })?;

        // Validate timestamp (reject events older than 5 minutes or 60s in future).
        let ts_secs: u64 = ts_str
            .parse()
            .map_err(|_| ZeptoError::Tool("Invalid timestamp in Stripe-Signature header".into()))?;

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let tolerance_secs: u64 = 300; // 5 minutes
        if now.abs_diff(ts_secs) > tolerance_secs {
            return Err(ZeptoError::Tool(format!(
                "Webhook timestamp rejected: {}s delta exceeds {}s tolerance (replay attack or clock skew)",
                now.abs_diff(ts_secs),
                tolerance_secs
            )));
        }

        // Compute expected HMAC-SHA256.
        // Stripe signed_payload = "<timestamp>.<body>"
        let signed_payload = format!("{}.{}", ts_str, payload);
        let expected_sig = hmac_sha256_hex(webhook_secret.as_bytes(), signed_payload.as_bytes());

        // Constant-time comparison to prevent timing attacks.
        if !constant_time_eq(received_sig.as_bytes(), expected_sig.as_bytes()) {
            return Err(ZeptoError::Tool(
                "Webhook signature verification failed: HMAC mismatch".into(),
            ));
        }

        Ok(format!(
            "Webhook signature verified. timestamp={} ({}s ago)",
            ts_str,
            now.saturating_sub(ts_secs)
        ))
    }
}

// ---------------------------------------------------------------------------
// Tool trait implementation
// ---------------------------------------------------------------------------

#[async_trait]
impl Tool for StripeTool {
    fn name(&self) -> &str {
        "stripe"
    }

    fn description(&self) -> &str {
        "Interact with the Stripe payment API. Supports creating and retrieving \
         PaymentIntents, Customers, Refunds, and balance. Also verifies Stripe \
         webhook signatures (HMAC-SHA256). Requires stripe.secret_key in config."
    }

    fn compact_description(&self) -> &str {
        "Stripe payments: create/get payment, customer, refund, balance, verify webhook"
    }

    fn parameters(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "description": "The Stripe operation to perform.",
                    "enum": [
                        "create_payment",
                        "get_payment",
                        "list_payments",
                        "create_customer",
                        "get_customer",
                        "list_customers",
                        "create_refund",
                        "get_balance",
                        "verify_webhook"
                    ]
                },
                "amount": {
                    "type": "integer",
                    "description": "Amount in smallest currency unit (e.g. cents for USD). Required for create_payment."
                },
                "currency": {
                    "type": "string",
                    "description": "ISO 4217 currency code (e.g. 'usd', 'myr'). Defaults to config default_currency."
                },
                "description": {
                    "type": "string",
                    "description": "Optional description for create_payment."
                },
                "payment_id": {
                    "type": "string",
                    "description": "PaymentIntent ID (pi_...). Required for get_payment."
                },
                "payment_intent_id": {
                    "type": "string",
                    "description": "PaymentIntent ID to refund. Required for create_refund."
                },
                "customer_id": {
                    "type": "string",
                    "description": "Customer ID (cus_...). Required for get_customer."
                },
                "email": {
                    "type": "string",
                    "description": "Customer email address. Required for create_customer."
                },
                "name": {
                    "type": "string",
                    "description": "Customer name. Optional for create_customer."
                },
                "limit": {
                    "type": "integer",
                    "description": "Max results for list operations (1-100, default 10)."
                },
                "payload": {
                    "type": "string",
                    "description": "Raw webhook request body. Required for verify_webhook."
                },
                "signature": {
                    "type": "string",
                    "description": "Stripe-Signature header value. Required for verify_webhook."
                }
            },
            "required": ["action"]
        })
    }

    async fn execute(&self, args: Value, _ctx: &ToolContext) -> Result<ToolOutput> {
        let action = args
            .get("action")
            .and_then(Value::as_str)
            .ok_or_else(|| ZeptoError::Tool("Missing 'action' parameter".into()))?;

        match action {
            "create_payment" => self.create_payment(&args).await,
            "get_payment" => self.get_payment(&args).await,
            "list_payments" => self.list_payments(&args).await,
            "create_customer" => self.create_customer(&args).await,
            "get_customer" => self.get_customer(&args).await,
            "list_customers" => self.list_customers(&args).await,
            "create_refund" => self.create_refund(&args).await,
            "get_balance" => self.get_balance().await,
            "verify_webhook" => self.verify_webhook(&args).await,
            other => Err(ZeptoError::Tool(format!(
                "Unknown stripe action '{}'. Valid actions: create_payment, get_payment, \
                 list_payments, create_customer, get_customer, list_customers, \
                 create_refund, get_balance, verify_webhook",
                other
            ))),
        }
        .map(ToolOutput::llm_only)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // -----------------------------------------------------------------------
    // Config & constructor tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_stripe_config_defaults() {
        let cfg = crate::config::StripeConfig::default();
        assert!(cfg.secret_key.is_none());
        assert_eq!(cfg.default_currency, "usd");
        assert!(cfg.webhook_secret.is_none());
    }

    #[test]
    fn test_stripe_config_deserialize() {
        let json = r#"{
            "secret_key": "sk_test_abc",
            "default_currency": "myr",
            "webhook_secret": "whsec_xyz"
        }"#;
        let cfg: crate::config::StripeConfig = serde_json::from_str(json).unwrap();
        assert_eq!(cfg.secret_key.as_deref(), Some("sk_test_abc"));
        assert_eq!(cfg.default_currency, "myr");
        assert_eq!(cfg.webhook_secret.as_deref(), Some("whsec_xyz"));
    }

    #[test]
    fn test_stripe_config_deserialize_minimal() {
        // Only secret_key, rest defaults.
        let json = r#"{"secret_key": "sk_test_xyz"}"#;
        let cfg: crate::config::StripeConfig = serde_json::from_str(json).unwrap();
        assert_eq!(cfg.secret_key.as_deref(), Some("sk_test_xyz"));
        assert_eq!(cfg.default_currency, "usd");
        assert!(cfg.webhook_secret.is_none());
    }

    #[test]
    fn test_stripe_config_webhook_secret_present() {
        let cfg = crate::config::StripeConfig {
            secret_key: Some("sk_test_key".to_string()),
            default_currency: "sgd".to_string(),
            webhook_secret: Some("whsec_secret".to_string()),
        };
        assert!(cfg.webhook_secret.is_some());
    }

    #[test]
    fn test_config_stripe_field_in_top_level() {
        // Verify stripe field is recognized at the top level of Config.
        let json = r#"{"stripe": {"secret_key": "sk_test_abc", "default_currency": "myr"}}"#;
        let config: crate::config::Config = serde_json::from_str(json).unwrap();
        assert_eq!(config.stripe.secret_key.as_deref(), Some("sk_test_abc"));
        assert_eq!(config.stripe.default_currency, "myr");
    }

    #[test]
    fn test_stripe_tool_constructor() {
        let tool = StripeTool::new("sk_test_abc", "usd");
        assert_eq!(tool.name(), "stripe");
        assert_eq!(tool.secret_key, "sk_test_abc");
        assert_eq!(tool.default_currency, "usd");
        assert!(tool.webhook_secret.is_none());
    }

    #[test]
    fn test_stripe_tool_with_webhook_secret() {
        let tool = StripeTool::new("sk_test_abc", "usd").with_webhook_secret("whsec_test_secret");
        assert_eq!(tool.webhook_secret.as_deref(), Some("whsec_test_secret"));
    }

    #[test]
    fn test_stripe_tool_metadata() {
        let tool = StripeTool::new("sk_test_abc", "usd");
        assert_eq!(tool.name(), "stripe");
        assert!(!tool.description().is_empty());
        assert!(!tool.compact_description().is_empty());
        // Compact description should be shorter than full description.
        assert!(
            tool.compact_description().len() < tool.description().len(),
            "compact_description should be shorter than description"
        );
    }

    #[test]
    fn test_stripe_tool_parameters_schema() {
        let tool = StripeTool::new("sk_test_abc", "usd");
        let params = tool.parameters();
        assert_eq!(params["type"], "object");
        assert!(params["properties"]["action"].is_object());
        assert_eq!(params["required"], json!(["action"]));
        // Verify all actions are listed in the enum.
        let actions = params["properties"]["action"]["enum"].as_array().unwrap();
        let action_strs: Vec<&str> = actions.iter().map(|v| v.as_str().unwrap()).collect();
        assert!(action_strs.contains(&"create_payment"));
        assert!(action_strs.contains(&"verify_webhook"));
        assert!(action_strs.contains(&"get_balance"));
    }

    // -----------------------------------------------------------------------
    // Idempotency key tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_idempotency_key_format() {
        let key = generate_idempotency_key();
        assert!(
            key.starts_with("zc_"),
            "key should start with 'zc_': {}",
            key
        );
        // Should have three hex components after the 'zc' prefix: ts, pid, seq.
        let parts: Vec<&str> = key.splitn(4, '_').collect();
        assert_eq!(
            parts.len(),
            4,
            "key should have format zc_<ts>_<pid>_<seq>: {}",
            key
        );
        // All three components should be valid hex strings.
        assert!(
            u128::from_str_radix(parts[1], 16).is_ok(),
            "timestamp part should be hex: {}",
            parts[1]
        );
        assert!(
            u32::from_str_radix(parts[2], 16).is_ok(),
            "pid part should be hex: {}",
            parts[2]
        );
        assert!(
            u64::from_str_radix(parts[3], 16).is_ok(),
            "sequence part should be hex: {}",
            parts[3]
        );
    }

    #[test]
    fn test_idempotency_key_uniqueness() {
        // Generate multiple keys and verify they're unique.
        let mut keys: Vec<String> = (0..20).map(|_| generate_idempotency_key()).collect();
        keys.sort();
        keys.dedup();
        // Allow for up to 1 collision given nanosecond resolution, but all should be unique.
        assert!(
            keys.len() >= 19,
            "expected at least 19/20 unique keys, got {}",
            keys.len()
        );
    }

    #[test]
    fn test_idempotency_key_not_empty() {
        let key = generate_idempotency_key();
        assert!(!key.is_empty());
        assert!(key.len() > 5);
    }

    // -----------------------------------------------------------------------
    // HMAC-SHA256 tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_hmac_sha256_known_vector() {
        // RFC 2202 Test Case 1:
        // Key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b (20 bytes)
        // Data = "Hi There"
        // Expected HMAC-SHA256 = b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7
        let key = [0x0bu8; 20];
        let data = b"Hi There";
        let result = hmac_sha256_hex(&key, data);
        assert_eq!(
            result, "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7",
            "HMAC-SHA256 RFC 2202 test vector 1 failed"
        );
    }

    #[test]
    fn test_hmac_sha256_key_larger_than_block() {
        // Key longer than 64 bytes should be hashed first.
        let key = vec![0xaau8; 131]; // 131 bytes > 64-byte block
        let data = b"Test With Truncation";
        // Just verify it doesn't panic and returns 64-char hex.
        let result = hmac_sha256_hex(&key, data);
        assert_eq!(result.len(), 64);
    }

    #[test]
    fn test_hmac_sha256_different_inputs_produce_different_outputs() {
        let key = b"test-key";
        let h1 = hmac_sha256_hex(key, b"message1");
        let h2 = hmac_sha256_hex(key, b"message2");
        assert_ne!(h1, h2);
    }

    #[test]
    fn test_hmac_sha256_same_inputs_deterministic() {
        let key = b"secret";
        let msg = b"payload";
        let h1 = hmac_sha256_hex(key, msg);
        let h2 = hmac_sha256_hex(key, msg);
        assert_eq!(h1, h2);
    }

    #[test]
    fn test_constant_time_eq_equal() {
        assert!(constant_time_eq(b"hello", b"hello"));
    }

    #[test]
    fn test_constant_time_eq_different() {
        assert!(!constant_time_eq(b"hello", b"world"));
    }

    #[test]
    fn test_constant_time_eq_different_lengths() {
        assert!(!constant_time_eq(b"hello", b"hell"));
    }

    #[test]
    fn test_constant_time_eq_empty() {
        assert!(constant_time_eq(b"", b""));
    }

    // -----------------------------------------------------------------------
    // Webhook verification tests
    // -----------------------------------------------------------------------

    /// Build a valid Stripe-Signature header for testing.
    fn make_stripe_signature(secret: &str, payload: &str, ts: u64) -> String {
        let signed_payload = format!("{}.{}", ts, payload);
        let sig = hmac_sha256_hex(secret.as_bytes(), signed_payload.as_bytes());
        format!("t={},v1={}", ts, sig)
    }

    #[tokio::test]
    async fn test_verify_webhook_valid() {
        let secret = "whsec_test_secret";
        let payload = r#"{"id":"evt_123","type":"payment_intent.succeeded"}"#;
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let sig = make_stripe_signature(secret, payload, now);

        let tool = StripeTool::new("sk_test_abc", "usd").with_webhook_secret(secret);
        let args = json!({"action": "verify_webhook", "payload": payload, "signature": sig});

        let result = tool.verify_webhook(&args).await;
        assert!(result.is_ok(), "Expected success but got: {:?}", result);
        let msg = result.unwrap();
        assert!(msg.contains("verified"), "Expected 'verified' in: {}", msg);
    }

    #[tokio::test]
    async fn test_verify_webhook_missing_payload() {
        let tool = StripeTool::new("sk_test_abc", "usd").with_webhook_secret("whsec_x");
        let args = json!({"action": "verify_webhook", "signature": "t=123,v1=abc"});
        let result = tool.verify_webhook(&args).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("payload"));
    }

    #[tokio::test]
    async fn test_verify_webhook_missing_signature() {
        let tool = StripeTool::new("sk_test_abc", "usd").with_webhook_secret("whsec_x");
        let args = json!({"action": "verify_webhook", "payload": "body"});
        let result = tool.verify_webhook(&args).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("signature"));
    }

    #[tokio::test]
    async fn test_verify_webhook_no_secret_configured() {
        // Tool without webhook_secret.
        let tool = StripeTool::new("sk_test_abc", "usd");
        let args = json!({
            "action": "verify_webhook",
            "payload": "body",
            "signature": "t=123,v1=abc"
        });
        let result = tool.verify_webhook(&args).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("webhook_secret") || err.contains("not configured"),
            "Expected webhook_secret error, got: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_verify_webhook_missing_timestamp_field() {
        let tool = StripeTool::new("sk_test_abc", "usd").with_webhook_secret("whsec_x");
        let args = json!({
            "action": "verify_webhook",
            "payload": "body",
            "signature": "v1=abc123"  // no t= field
        });
        let result = tool.verify_webhook(&args).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("t=") || err.contains("timestamp"),
            "Expected timestamp error, got: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_verify_webhook_missing_v1_field() {
        let tool = StripeTool::new("sk_test_abc", "usd").with_webhook_secret("whsec_x");
        let args = json!({
            "action": "verify_webhook",
            "payload": "body",
            "signature": "t=1234567890"  // no v1= field
        });
        let result = tool.verify_webhook(&args).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("v1=") || err.contains("signature"),
            "Expected v1 signature error, got: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_verify_webhook_expired_timestamp() {
        let tool = StripeTool::new("sk_test_abc", "usd").with_webhook_secret("whsec_x");
        // Timestamp 10 minutes in the past (exceeds 5-minute tolerance).
        let old_ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs()
            .saturating_sub(601);

        let args = json!({
            "action": "verify_webhook",
            "payload": "body",
            "signature": format!("t={},v1=abc123", old_ts)
        });
        let result = tool.verify_webhook(&args).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("timestamp") || err.contains("tolerance") || err.contains("delta"),
            "Expected timestamp expiry error, got: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_verify_webhook_future_timestamp() {
        let tool = StripeTool::new("sk_test_abc", "usd").with_webhook_secret("whsec_x");
        // Timestamp 10 minutes in the future.
        let future_ts = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs()
            + 601;

        let args = json!({
            "action": "verify_webhook",
            "payload": "body",
            "signature": format!("t={},v1=abc123", future_ts)
        });
        let result = tool.verify_webhook(&args).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("timestamp") || err.contains("tolerance") || err.contains("delta"),
            "Expected future timestamp error, got: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_verify_webhook_wrong_signature() {
        let tool = StripeTool::new("sk_test_abc", "usd").with_webhook_secret("whsec_test_secret");
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let args = json!({
            "action": "verify_webhook",
            "payload": "real payload",
            "signature": format!("t={},v1={}", now, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef")
        });
        let result = tool.verify_webhook(&args).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("mismatch") || err.contains("failed") || err.contains("HMAC"),
            "Expected HMAC mismatch error, got: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_verify_webhook_invalid_timestamp_not_numeric() {
        let tool = StripeTool::new("sk_test_abc", "usd").with_webhook_secret("whsec_x");
        let args = json!({
            "action": "verify_webhook",
            "payload": "body",
            "signature": "t=notanumber,v1=abc123"
        });
        let result = tool.verify_webhook(&args).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("timestamp") || err.contains("Invalid"),
            "Expected timestamp parse error, got: {}",
            err
        );
    }

    // -----------------------------------------------------------------------
    // Missing action / unknown action tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_unknown_action_returns_error() {
        let tool = StripeTool::new("sk_test_abc", "usd");
        let ctx = ToolContext::new();
        let result = tool
            .execute(json!({"action": "fly_to_the_moon"}), &ctx)
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("fly_to_the_moon"));
    }

    #[tokio::test]
    async fn test_missing_action_returns_error() {
        let tool = StripeTool::new("sk_test_abc", "usd");
        let ctx = ToolContext::new();
        let result = tool.execute(json!({}), &ctx).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("action"));
    }

    // -----------------------------------------------------------------------
    // create_payment validation tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_create_payment_missing_amount_returns_error() {
        let tool = StripeTool::new("sk_test_abc", "usd");
        let ctx = ToolContext::new();
        let result = tool
            .execute(json!({"action": "create_payment", "currency": "usd"}), &ctx)
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("amount"));
    }

    #[tokio::test]
    async fn test_create_payment_zero_amount_returns_error() {
        let tool = StripeTool::new("sk_test_abc", "usd");
        let ctx = ToolContext::new();
        let result = tool
            .execute(
                json!({"action": "create_payment", "amount": 0, "currency": "usd"}),
                &ctx,
            )
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("positive"));
    }

    #[tokio::test]
    async fn test_create_payment_negative_amount_returns_error() {
        let tool = StripeTool::new("sk_test_abc", "usd");
        let ctx = ToolContext::new();
        let result = tool
            .execute(json!({"action": "create_payment", "amount": -100}), &ctx)
            .await;
        assert!(result.is_err());
    }

    // -----------------------------------------------------------------------
    // get_payment validation tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_get_payment_missing_id_returns_error() {
        let tool = StripeTool::new("sk_test_abc", "usd");
        let ctx = ToolContext::new();
        let result = tool.execute(json!({"action": "get_payment"}), &ctx).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("payment_id"));
    }

    // -----------------------------------------------------------------------
    // create_customer validation tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_create_customer_missing_email_returns_error() {
        let tool = StripeTool::new("sk_test_abc", "usd");
        let ctx = ToolContext::new();
        let result = tool
            .execute(json!({"action": "create_customer"}), &ctx)
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("email"));
    }

    // -----------------------------------------------------------------------
    // create_refund validation tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_create_refund_missing_payment_intent_id_returns_error() {
        let tool = StripeTool::new("sk_test_abc", "usd");
        let ctx = ToolContext::new();
        let result = tool.execute(json!({"action": "create_refund"}), &ctx).await;
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("payment_intent_id"));
    }

    // -----------------------------------------------------------------------
    // Env override tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_stripe_env_overrides() {
        // Set env vars and load config via Config::load_from_path (uses a
        // nonexistent path so it starts from defaults, then applies env vars).
        std::env::set_var("ZEPTOCLAW_STRIPE_SECRET_KEY", "sk_test_env_key");
        std::env::set_var("ZEPTOCLAW_STRIPE_DEFAULT_CURRENCY", "SGD");
        std::env::set_var("ZEPTOCLAW_STRIPE_WEBHOOK_SECRET", "whsec_env_secret");

        let path = std::path::PathBuf::from("/nonexistent/stripe_test_config.json");
        let config = crate::config::Config::load_from_path(&path).unwrap();

        assert_eq!(config.stripe.secret_key.as_deref(), Some("sk_test_env_key"));
        assert_eq!(config.stripe.default_currency, "sgd"); // env var is lowercased
        assert_eq!(
            config.stripe.webhook_secret.as_deref(),
            Some("whsec_env_secret")
        );

        std::env::remove_var("ZEPTOCLAW_STRIPE_SECRET_KEY");
        std::env::remove_var("ZEPTOCLAW_STRIPE_DEFAULT_CURRENCY");
        std::env::remove_var("ZEPTOCLAW_STRIPE_WEBHOOK_SECRET");
    }

    #[test]
    fn test_stripe_config_in_config_json() {
        // Verify stripe fields round-trip through JSON serialization.
        let mut config = crate::config::Config::default();
        config.stripe = crate::config::StripeConfig {
            secret_key: Some("sk_test_round_trip".to_string()),
            default_currency: "eur".to_string(),
            webhook_secret: Some("whsec_round_trip".to_string()),
        };

        let json_str = serde_json::to_string(&config).unwrap();
        let loaded: crate::config::Config = serde_json::from_str(&json_str).unwrap();

        assert_eq!(
            loaded.stripe.secret_key.as_deref(),
            Some("sk_test_round_trip")
        );
        assert_eq!(loaded.stripe.default_currency, "eur");
        assert_eq!(
            loaded.stripe.webhook_secret.as_deref(),
            Some("whsec_round_trip")
        );
    }
}