reasonkit-web 0.1.7

High-performance MCP server for browser automation, web capture, and content extraction. Rust-powered CDP client for AI agents.
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
//! Stripe Event Types
//!
//! Strongly-typed representations of Stripe webhook events for SaaS subscriptions.

use std::str::FromStr;

use serde::{Deserialize, Serialize};

use crate::stripe::error::{StripeWebhookError, StripeWebhookResult};

/// Stripe event types we handle
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StripeEventType {
    // Customer events
    #[serde(rename = "customer.created")]
    CustomerCreated,

    // Subscription events
    #[serde(rename = "customer.subscription.created")]
    SubscriptionCreated,
    #[serde(rename = "customer.subscription.updated")]
    SubscriptionUpdated,
    #[serde(rename = "customer.subscription.deleted")]
    SubscriptionDeleted,

    // Invoice events
    #[serde(rename = "invoice.payment_succeeded")]
    InvoicePaymentSucceeded,
    #[serde(rename = "invoice.payment_failed")]
    InvoicePaymentFailed,

    // Catch-all for events we don't explicitly handle
    #[serde(other)]
    Unknown,
}

impl FromStr for StripeEventType {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "customer.created" => Self::CustomerCreated,
            "customer.subscription.created" => Self::SubscriptionCreated,
            "customer.subscription.updated" => Self::SubscriptionUpdated,
            "customer.subscription.deleted" => Self::SubscriptionDeleted,
            "invoice.payment_succeeded" => Self::InvoicePaymentSucceeded,
            "invoice.payment_failed" => Self::InvoicePaymentFailed,
            _ => Self::Unknown,
        })
    }
}

impl StripeEventType {
    /// Get the string representation
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::CustomerCreated => "customer.created",
            Self::SubscriptionCreated => "customer.subscription.created",
            Self::SubscriptionUpdated => "customer.subscription.updated",
            Self::SubscriptionDeleted => "customer.subscription.deleted",
            Self::InvoicePaymentSucceeded => "invoice.payment_succeeded",
            Self::InvoicePaymentFailed => "invoice.payment_failed",
            Self::Unknown => "unknown",
        }
    }

    /// Check if this is a known event type
    pub fn is_known(&self) -> bool {
        !matches!(self, Self::Unknown)
    }
}

/// Generic Stripe event envelope
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StripeEvent {
    /// Unique identifier for the event
    pub id: String,

    /// Type of event
    #[serde(rename = "type")]
    pub event_type: String,

    /// Time of event creation (Unix timestamp)
    pub created: i64,

    /// API version used to render data
    #[serde(default)]
    pub api_version: Option<String>,

    /// Whether this is a live mode event
    pub livemode: bool,

    /// Number of times Stripe has attempted to deliver
    #[serde(default)]
    pub pending_webhooks: u32,

    /// Object containing event data
    pub data: EventData,

    /// Request that caused the event (if applicable)
    #[serde(default)]
    pub request: Option<EventRequest>,
}

impl StripeEvent {
    /// Parse from raw JSON bytes
    pub fn from_bytes(bytes: &[u8]) -> StripeWebhookResult<Self> {
        serde_json::from_slice(bytes).map_err(|e| StripeWebhookError::InvalidPayload(e.to_string()))
    }

    /// Get the typed event type
    pub fn typed_event_type(&self) -> StripeEventType {
        // Infallible error type means this can never fail
        StripeEventType::from_str(&self.event_type).unwrap()
    }

    /// Extract subscription from event data
    pub fn as_subscription(&self) -> StripeWebhookResult<SubscriptionEvent> {
        match self.typed_event_type() {
            StripeEventType::SubscriptionCreated
            | StripeEventType::SubscriptionUpdated
            | StripeEventType::SubscriptionDeleted => {
                let subscription: Subscription =
                    serde_json::from_value(self.data.object.clone())
                        .map_err(|e| StripeWebhookError::InvalidPayload(e.to_string()))?;

                Ok(SubscriptionEvent {
                    event_id: self.id.clone(),
                    event_type: self.typed_event_type(),
                    subscription,
                    previous_attributes: self.data.previous_attributes.clone(),
                })
            }
            _ => Err(StripeWebhookError::InvalidPayload(format!(
                "Event {} is not a subscription event",
                self.event_type
            ))),
        }
    }

    /// Extract invoice from event data
    pub fn as_invoice(&self) -> StripeWebhookResult<InvoiceEvent> {
        match self.typed_event_type() {
            StripeEventType::InvoicePaymentSucceeded | StripeEventType::InvoicePaymentFailed => {
                let invoice: Invoice = serde_json::from_value(self.data.object.clone())
                    .map_err(|e| StripeWebhookError::InvalidPayload(e.to_string()))?;

                Ok(InvoiceEvent {
                    event_id: self.id.clone(),
                    event_type: self.typed_event_type(),
                    invoice,
                })
            }
            _ => Err(StripeWebhookError::InvalidPayload(format!(
                "Event {} is not an invoice event",
                self.event_type
            ))),
        }
    }

    /// Extract customer from event data
    pub fn as_customer(&self) -> StripeWebhookResult<CustomerEvent> {
        match self.typed_event_type() {
            StripeEventType::CustomerCreated => {
                let customer: Customer = serde_json::from_value(self.data.object.clone())
                    .map_err(|e| StripeWebhookError::InvalidPayload(e.to_string()))?;

                Ok(CustomerEvent {
                    event_id: self.id.clone(),
                    event_type: self.typed_event_type(),
                    customer,
                })
            }
            _ => Err(StripeWebhookError::InvalidPayload(format!(
                "Event {} is not a customer event",
                self.event_type
            ))),
        }
    }
}

/// Event data container
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventData {
    /// The actual event object (subscription, invoice, customer, etc.)
    pub object: serde_json::Value,

    /// Previous values for updated fields (only in *.updated events)
    #[serde(default)]
    pub previous_attributes: Option<serde_json::Value>,
}

/// Request that triggered the event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventRequest {
    /// Request ID
    pub id: Option<String>,
    /// Idempotency key used in the request
    pub idempotency_key: Option<String>,
}

// =============================================================================
// Subscription Types
// =============================================================================

/// Subscription event with typed data
#[derive(Debug, Clone)]
pub struct SubscriptionEvent {
    /// The event ID
    pub event_id: String,
    /// Type of subscription event
    pub event_type: StripeEventType,
    /// The subscription object
    pub subscription: Subscription,
    /// Previous values (for updates)
    pub previous_attributes: Option<serde_json::Value>,
}

/// Stripe subscription object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Subscription {
    /// Subscription ID (sub_...)
    pub id: String,
    /// Customer ID (cus_...)
    pub customer: String,
    /// Subscription status
    pub status: SubscriptionStatus,
    /// Current billing period start (Unix timestamp)
    pub current_period_start: i64,
    /// Current billing period end (Unix timestamp)
    pub current_period_end: i64,
    /// Whether subscription will cancel at period end
    #[serde(default)]
    pub cancel_at_period_end: bool,
    /// When the subscription was canceled (if applicable)
    pub canceled_at: Option<i64>,
    /// When the subscription ended (if applicable)
    pub ended_at: Option<i64>,
    /// Trial end date (if applicable)
    pub trial_end: Option<i64>,
    /// Subscription items (plans/prices)
    pub items: SubscriptionItems,
    /// Metadata attached to the subscription
    #[serde(default)]
    pub metadata: serde_json::Value,
    /// Whether this is a live mode subscription
    pub livemode: bool,
}

/// Subscription status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SubscriptionStatus {
    Active,
    PastDue,
    Unpaid,
    Canceled,
    Incomplete,
    IncompleteExpired,
    Trialing,
    Paused,
    #[serde(other)]
    Unknown,
}

impl SubscriptionStatus {
    /// Check if subscription is in a "good" state
    pub fn is_active(&self) -> bool {
        matches!(self, Self::Active | Self::Trialing)
    }

    /// Check if subscription requires payment attention
    pub fn requires_payment_action(&self) -> bool {
        matches!(self, Self::PastDue | Self::Unpaid | Self::Incomplete)
    }
}

/// Subscription items container
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubscriptionItems {
    /// List of subscription items
    pub data: Vec<SubscriptionItem>,
}

/// Individual subscription item
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubscriptionItem {
    /// Item ID
    pub id: String,
    /// Price object
    pub price: Price,
    /// Quantity
    #[serde(default = "default_quantity")]
    pub quantity: u32,
}

fn default_quantity() -> u32 {
    1
}

/// Price object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Price {
    /// Price ID
    pub id: String,
    /// Product ID
    pub product: String,
    /// Unit amount in cents
    pub unit_amount: Option<i64>,
    /// Currency
    pub currency: String,
    /// Recurring information
    pub recurring: Option<Recurring>,
}

/// Recurring price details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Recurring {
    /// Billing interval (day, week, month, year)
    pub interval: String,
    /// Number of intervals
    pub interval_count: u32,
}

// =============================================================================
// Invoice Types
// =============================================================================

/// Invoice event with typed data
#[derive(Debug, Clone)]
pub struct InvoiceEvent {
    /// The event ID
    pub event_id: String,
    /// Type of invoice event
    pub event_type: StripeEventType,
    /// The invoice object
    pub invoice: Invoice,
}

/// Stripe invoice object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Invoice {
    /// Invoice ID (in_...)
    pub id: String,
    /// Customer ID
    pub customer: String,
    /// Associated subscription ID (if any)
    pub subscription: Option<String>,
    /// Invoice status
    pub status: InvoiceStatus,
    /// Total amount in cents
    pub amount_due: i64,
    /// Amount paid in cents
    pub amount_paid: i64,
    /// Amount remaining in cents
    pub amount_remaining: i64,
    /// Currency
    pub currency: String,
    /// Billing reason
    pub billing_reason: Option<String>,
    /// Customer email at time of invoice
    pub customer_email: Option<String>,
    /// Hosted invoice URL
    pub hosted_invoice_url: Option<String>,
    /// Invoice PDF URL
    pub invoice_pdf: Option<String>,
    /// Payment intent ID (if payment attempted)
    pub payment_intent: Option<String>,
    /// When created (Unix timestamp)
    pub created: i64,
    /// Period start
    pub period_start: i64,
    /// Period end
    pub period_end: i64,
    /// Whether this is live mode
    pub livemode: bool,
}

/// Invoice status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InvoiceStatus {
    Draft,
    Open,
    Paid,
    Uncollectible,
    Void,
    #[serde(other)]
    Unknown,
}

// =============================================================================
// Customer Types
// =============================================================================

/// Customer event with typed data
#[derive(Debug, Clone)]
pub struct CustomerEvent {
    /// The event ID
    pub event_id: String,
    /// Type of customer event
    pub event_type: StripeEventType,
    /// The customer object
    pub customer: Customer,
}

/// Stripe customer object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Customer {
    /// Customer ID (cus_...)
    pub id: String,
    /// Customer email
    pub email: Option<String>,
    /// Customer name
    pub name: Option<String>,
    /// Customer description
    pub description: Option<String>,
    /// When created (Unix timestamp)
    pub created: i64,
    /// Metadata
    #[serde(default)]
    pub metadata: serde_json::Value,
    /// Whether this is live mode
    pub livemode: bool,
}

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

    #[test]
    fn test_event_type_parsing() {
        assert_eq!(
            StripeEventType::from_str("customer.subscription.created").unwrap(),
            StripeEventType::SubscriptionCreated
        );
        assert_eq!(
            StripeEventType::from_str("invoice.payment_succeeded").unwrap(),
            StripeEventType::InvoicePaymentSucceeded
        );
        assert_eq!(
            StripeEventType::from_str("unknown.event").unwrap(),
            StripeEventType::Unknown
        );
    }

    #[test]
    fn test_subscription_status() {
        assert!(SubscriptionStatus::Active.is_active());
        assert!(SubscriptionStatus::Trialing.is_active());
        assert!(!SubscriptionStatus::Canceled.is_active());

        assert!(SubscriptionStatus::PastDue.requires_payment_action());
        assert!(!SubscriptionStatus::Active.requires_payment_action());
    }

    #[test]
    fn test_parse_subscription_event() {
        let json = r#"{
            "id": "evt_1234567890",
            "type": "customer.subscription.created",
            "created": 1614556800,
            "livemode": false,
            "pending_webhooks": 1,
            "data": {
                "object": {
                    "id": "sub_1234567890",
                    "customer": "cus_1234567890",
                    "status": "active",
                    "current_period_start": 1614556800,
                    "current_period_end": 1617235200,
                    "cancel_at_period_end": false,
                    "items": {
                        "data": [{
                            "id": "si_1234567890",
                            "price": {
                                "id": "price_1234567890",
                                "product": "prod_1234567890",
                                "unit_amount": 2000,
                                "currency": "usd",
                                "recurring": {
                                    "interval": "month",
                                    "interval_count": 1
                                }
                            },
                            "quantity": 1
                        }]
                    },
                    "metadata": {},
                    "livemode": false
                }
            }
        }"#;

        let event = StripeEvent::from_bytes(json.as_bytes()).unwrap();
        assert_eq!(event.id, "evt_1234567890");
        assert_eq!(
            event.typed_event_type(),
            StripeEventType::SubscriptionCreated
        );

        let sub_event = event.as_subscription().unwrap();
        assert_eq!(sub_event.subscription.id, "sub_1234567890");
        assert_eq!(sub_event.subscription.status, SubscriptionStatus::Active);
    }

    #[test]
    fn test_parse_invoice_event() {
        let json = r#"{
            "id": "evt_invoice_1234",
            "type": "invoice.payment_succeeded",
            "created": 1614556800,
            "livemode": false,
            "pending_webhooks": 1,
            "data": {
                "object": {
                    "id": "in_1234567890",
                    "customer": "cus_1234567890",
                    "subscription": "sub_1234567890",
                    "status": "paid",
                    "amount_due": 2000,
                    "amount_paid": 2000,
                    "amount_remaining": 0,
                    "currency": "usd",
                    "created": 1614556800,
                    "period_start": 1614556800,
                    "period_end": 1617235200,
                    "livemode": false
                }
            }
        }"#;

        let event = StripeEvent::from_bytes(json.as_bytes()).unwrap();
        let invoice_event = event.as_invoice().unwrap();

        assert_eq!(invoice_event.invoice.id, "in_1234567890");
        assert_eq!(invoice_event.invoice.status, InvoiceStatus::Paid);
        assert_eq!(invoice_event.invoice.amount_paid, 2000);
    }
}