pyjs-extras 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,653 @@
1
+ """Server-rendered operator interface for the local Stripe simulator."""
2
+
3
+ from dataclasses import dataclass
4
+ from functools import cache
5
+
6
+ from pyjs import js
7
+ from pyjs.transpiler import bundle
8
+ from pyjs.web.dom import Element
9
+ from pyjs.web.domx import button, div, form, h1, input, p, span, tag, tw
10
+ from pyjs.web.ssr import raw_html, render_document
11
+
12
+
13
+ @js
14
+ @dataclass(frozen=True)
15
+ class SubscriptionView:
16
+ id: str
17
+ customer_email: str
18
+ status: str
19
+ period_end: str
20
+ cancel_at_period_end: bool
21
+ payment_method: str
22
+ renewal_behavior: str
23
+ invoice_id: str = ""
24
+ invoice_status: str = ""
25
+ invoice_attempts: int = 0
26
+
27
+
28
+ @js
29
+ @dataclass(frozen=True)
30
+ class EventView:
31
+ id: str
32
+ type: str
33
+ created: str
34
+ delivery: str
35
+ attempts: int
36
+ payload: str
37
+ can_resend: bool
38
+ last_response: str
39
+ attempt_history: tuple[str, ...] = ()
40
+
41
+
42
+ @js
43
+ @dataclass(frozen=True)
44
+ class InvoiceView:
45
+ id: str
46
+ subscription_id: str
47
+ status: str
48
+ billing_reason: str
49
+ attempt_count: int
50
+ next_attempt: str = ""
51
+ error_code: str = ""
52
+
53
+
54
+ @js
55
+ @dataclass(frozen=True)
56
+ class DashboardView:
57
+ clock: str
58
+ api_base: str
59
+ webhook_url: str
60
+ retry_policy: str
61
+ customer_count: int
62
+ subscription_count: int
63
+ invoice_count: int
64
+ delivery_mode: str
65
+ pending_count: int
66
+ subscriptions: tuple[SubscriptionView, ...]
67
+ invoices: tuple[InvoiceView, ...]
68
+ events: tuple[EventView, ...]
69
+
70
+
71
+ @js
72
+ def status_badge(status: str) -> Element:
73
+ if status in ("active", "paid", "delivered"):
74
+ return span(
75
+ tw(
76
+ "inline-flex rounded-full bg-emerald-100 px-2.5 py-1 text-xs "
77
+ "font-semibold text-emerald-800"
78
+ ),
79
+ status.replace("_", " "),
80
+ )
81
+ if status in ("past_due", "open"):
82
+ return span(
83
+ tw(
84
+ "inline-flex rounded-full bg-amber-100 px-2.5 py-1 text-xs "
85
+ "font-semibold text-amber-800"
86
+ ),
87
+ status.replace("_", " "),
88
+ )
89
+ if status in ("canceled", "failed", "unpaid", "uncollectible"):
90
+ return span(
91
+ tw(
92
+ "inline-flex rounded-full bg-red-100 px-2.5 py-1 text-xs "
93
+ "font-semibold text-red-800"
94
+ ),
95
+ status.replace("_", " "),
96
+ )
97
+ return span(
98
+ tw(
99
+ "inline-flex rounded-full bg-slate-100 px-2.5 py-1 text-xs "
100
+ "font-semibold text-slate-700"
101
+ ),
102
+ status.replace("_", " "),
103
+ )
104
+
105
+
106
+ @js
107
+ def metric(label: str, value: str) -> Element:
108
+ return div(
109
+ tw("rounded-xl border border-slate-200 bg-white p-4 shadow-sm"),
110
+ p(tw("text-xs font-medium uppercase tracking-wide text-slate-500"), label),
111
+ p(tw("mt-1 text-2xl font-semibold text-slate-950"), value),
112
+ )
113
+
114
+
115
+ @js
116
+ def behavior_option(value: str, label: str, selected: str) -> Element:
117
+ attributes = {"value": value}
118
+ if value == selected:
119
+ attributes["selected"] = ""
120
+ return tag("option", attributes, label)
121
+
122
+
123
+ @js
124
+ def subscription_card(subscription: SubscriptionView) -> Element:
125
+ cancellation = "Renews automatically"
126
+ if subscription.cancel_at_period_end:
127
+ cancellation = "Cancellation scheduled"
128
+ invoice: Element = p(
129
+ tw("text-sm text-slate-500"),
130
+ "No open invoice",
131
+ )
132
+ retry: Element = div()
133
+ renew: Element = form(
134
+ {
135
+ "action": "/_simulator/subscriptions/" + subscription.id + "/renew",
136
+ "method": "post",
137
+ },
138
+ button(
139
+ tw(
140
+ "rounded-lg bg-indigo-600 px-4 py-2.5 text-sm font-semibold "
141
+ "text-white hover:bg-indigo-500"
142
+ ),
143
+ {"type": "submit"},
144
+ "Run renewal now",
145
+ ),
146
+ )
147
+ if subscription.invoice_id:
148
+ invoice = div(
149
+ tw("flex flex-wrap items-center gap-2 text-sm text-slate-600"),
150
+ span("Latest invoice " + subscription.invoice_id),
151
+ status_badge(subscription.invoice_status),
152
+ span("Attempts " + str(subscription.invoice_attempts)),
153
+ )
154
+ if subscription.invoice_status == "open":
155
+ renew = div()
156
+ retry = form(
157
+ {
158
+ "action": ("/_simulator/subscriptions/" + subscription.id + "/retry"),
159
+ "method": "post",
160
+ },
161
+ button(
162
+ tw(
163
+ "rounded-lg border border-slate-300 bg-white px-3 py-2 "
164
+ "text-sm font-semibold text-slate-700 hover:bg-slate-50"
165
+ ),
166
+ {"type": "submit"},
167
+ "Retry open invoice",
168
+ ),
169
+ )
170
+ return tag(
171
+ "article",
172
+ tw("rounded-2xl border border-slate-200 bg-white p-5 shadow-sm"),
173
+ {"data-subscription-id": subscription.id},
174
+ div(
175
+ tw("flex flex-wrap items-start justify-between gap-3"),
176
+ div(
177
+ p(
178
+ tw("text-sm font-semibold text-slate-950"),
179
+ subscription.customer_email or "Customer without email",
180
+ ),
181
+ p(tw("mt-1 font-mono text-xs text-slate-500"), subscription.id),
182
+ ),
183
+ status_badge(subscription.status),
184
+ ),
185
+ div(
186
+ tw("mt-4 grid gap-1 text-sm text-slate-600 sm:grid-cols-2"),
187
+ p("Period ends " + subscription.period_end),
188
+ p(subscription.payment_method),
189
+ p(cancellation),
190
+ ),
191
+ div(tw("mt-4"), invoice),
192
+ form(
193
+ tw("mt-5 grid gap-3 sm:grid-cols-[1fr_auto]"),
194
+ {
195
+ "action": ("/_simulator/subscriptions/" + subscription.id + "/behavior"),
196
+ "method": "post",
197
+ },
198
+ tag(
199
+ "label",
200
+ tw("grid gap-1 text-sm font-medium text-slate-700"),
201
+ "Next payment outcome",
202
+ tag(
203
+ "select",
204
+ tw(
205
+ "h-10 rounded-lg border border-slate-300 bg-white px-3 "
206
+ "text-sm text-slate-950"
207
+ ),
208
+ {"name": "behavior"},
209
+ behavior_option(
210
+ "success", "Successful payment", subscription.renewal_behavior
211
+ ),
212
+ behavior_option(
213
+ "soft_decline",
214
+ "Soft decline — retryable",
215
+ subscription.renewal_behavior,
216
+ ),
217
+ behavior_option(
218
+ "hard_decline",
219
+ "Hard decline — no retry",
220
+ subscription.renewal_behavior,
221
+ ),
222
+ behavior_option(
223
+ "authentication_required",
224
+ "Customer authentication required",
225
+ subscription.renewal_behavior,
226
+ ),
227
+ ),
228
+ ),
229
+ button(
230
+ tw(
231
+ "self-end rounded-lg bg-slate-900 px-4 py-2.5 text-sm "
232
+ "font-semibold text-white hover:bg-slate-700"
233
+ ),
234
+ {"type": "submit"},
235
+ "Save outcome",
236
+ ),
237
+ ),
238
+ div(
239
+ tw("mt-3 flex flex-wrap gap-3"),
240
+ renew,
241
+ retry,
242
+ ),
243
+ form(
244
+ tw("mt-4 flex flex-wrap items-end gap-3 border-t border-slate-100 pt-4"),
245
+ {
246
+ "action": ("/_simulator/subscriptions/" + subscription.id + "/payment"),
247
+ "method": "post",
248
+ },
249
+ tag(
250
+ "label",
251
+ tw("grid gap-1 text-sm font-medium text-slate-700"),
252
+ "Replace payment method",
253
+ tag(
254
+ "select",
255
+ tw(
256
+ "h-10 rounded-lg border border-slate-300 bg-white px-3 "
257
+ "text-sm text-slate-950"
258
+ ),
259
+ {"name": "card_number"},
260
+ tag("option", {"value": "4242424242424242"}, "Visa •••• 4242"),
261
+ tag(
262
+ "option",
263
+ {"value": "4000000000000341"},
264
+ "Retryable Visa •••• 0341",
265
+ ),
266
+ tag(
267
+ "option",
268
+ {"value": "4000000000000002"},
269
+ "Declined Visa •••• 0002",
270
+ ),
271
+ ),
272
+ ),
273
+ button(
274
+ tw(
275
+ "rounded-lg border border-slate-300 bg-white px-3 py-2 "
276
+ "text-sm font-semibold text-slate-700 hover:bg-slate-50"
277
+ ),
278
+ {"type": "submit"},
279
+ "Replace and retry",
280
+ ),
281
+ ),
282
+ )
283
+
284
+
285
+ @js
286
+ def invoice_row(invoice: InvoiceView) -> Element:
287
+ next_attempt: Element = div()
288
+ if invoice.next_attempt:
289
+ next_attempt = p(
290
+ tw("mt-1 text-xs text-slate-500"),
291
+ "Next payment attempt " + invoice.next_attempt,
292
+ )
293
+ error: Element = div()
294
+ if invoice.error_code:
295
+ error = p(
296
+ tw("mt-1 text-xs font-medium text-red-700"),
297
+ "Last error: " + invoice.error_code,
298
+ )
299
+ return tag(
300
+ "li",
301
+ tw("border-b border-slate-200 py-4 last:border-0"),
302
+ div(
303
+ tw("flex flex-wrap items-start justify-between gap-3"),
304
+ div(
305
+ p(tw("font-mono text-sm font-semibold text-slate-900"), invoice.id),
306
+ p(
307
+ tw("mt-1 text-xs text-slate-500"),
308
+ invoice.billing_reason.replace("_", " ")
309
+ + " · "
310
+ + invoice.subscription_id,
311
+ ),
312
+ ),
313
+ status_badge(invoice.status),
314
+ ),
315
+ p(
316
+ tw("mt-2 text-xs text-slate-500"),
317
+ "Payment attempts: " + str(invoice.attempt_count),
318
+ ),
319
+ next_attempt,
320
+ error,
321
+ )
322
+
323
+
324
+ @js
325
+ def event_row(event: EventView) -> Element:
326
+ resend: Element = div()
327
+ if event.can_resend:
328
+ resend_buttons: list[Element] = []
329
+ for action, label in (
330
+ ("resend", "Send duplicate"),
331
+ ("invalid", "Send bad signature"),
332
+ ("stale", "Send stale signature"),
333
+ ):
334
+ resend_buttons.append(
335
+ form(
336
+ {
337
+ "action": "/_simulator/events/" + event.id + "/" + action,
338
+ "method": "post",
339
+ },
340
+ button(
341
+ tw(
342
+ "rounded-lg border border-slate-300 bg-white px-3 "
343
+ "py-2 text-xs font-semibold text-slate-700 "
344
+ "hover:bg-slate-50"
345
+ ),
346
+ {"type": "submit"},
347
+ label,
348
+ ),
349
+ )
350
+ )
351
+ resend = div(tw("mt-3 flex flex-wrap gap-2"), *resend_buttons)
352
+ history: list[Element] = []
353
+ for attempt in event.attempt_history:
354
+ history.append(tag("li", tw("mt-1 text-xs text-slate-500"), attempt))
355
+ attempt_details: Element = div()
356
+ if len(history) > 0:
357
+ attempt_details = tag(
358
+ "details",
359
+ tw("mt-3"),
360
+ tag(
361
+ "summary",
362
+ tw("cursor-pointer text-xs font-medium text-slate-700"),
363
+ "Delivery history",
364
+ ),
365
+ tag("ol", *history),
366
+ )
367
+ delivery_detail: Element = div()
368
+ if event.last_response:
369
+ delivery_detail = p(
370
+ tw("mt-1 text-xs text-slate-500"),
371
+ "Last response: " + event.last_response,
372
+ )
373
+ return tag(
374
+ "li",
375
+ tw("border-b border-slate-200 py-4 last:border-0"),
376
+ {"data-event-id": event.id},
377
+ div(
378
+ tw("flex flex-wrap items-start justify-between gap-3"),
379
+ div(
380
+ p(tw("font-mono text-sm font-semibold text-slate-900"), event.type),
381
+ p(
382
+ tw("mt-1 text-xs text-slate-500"),
383
+ event.created + " · " + event.id,
384
+ ),
385
+ ),
386
+ status_badge(event.delivery),
387
+ ),
388
+ p(
389
+ tw("mt-2 text-xs text-slate-500"),
390
+ "Webhook delivery attempts: " + str(event.attempts),
391
+ ),
392
+ delivery_detail,
393
+ attempt_details,
394
+ tag(
395
+ "details",
396
+ tw("mt-3 text-sm"),
397
+ tag("summary", tw("cursor-pointer font-medium text-indigo-700"), "Payload"),
398
+ tag(
399
+ "pre",
400
+ tw(
401
+ "mt-2 max-h-72 overflow-auto rounded-lg bg-slate-950 p-3 "
402
+ "text-xs text-slate-100"
403
+ ),
404
+ event.payload,
405
+ ),
406
+ ),
407
+ resend,
408
+ )
409
+
410
+
411
+ @js
412
+ def dashboard(view: DashboardView) -> Element:
413
+ subscriptions: list[Element] = []
414
+ for subscription in view.subscriptions:
415
+ subscriptions.append(subscription_card(subscription))
416
+ if len(subscriptions) == 0:
417
+ subscriptions.append(
418
+ p(
419
+ tw(
420
+ "rounded-2xl border border-dashed border-slate-300 bg-white "
421
+ "p-8 text-center text-sm text-slate-500"
422
+ ),
423
+ "Complete a Checkout session to create a subscription.",
424
+ )
425
+ )
426
+ events: list[Element] = []
427
+ for event in view.events:
428
+ events.append(event_row(event))
429
+ if len(events) == 0:
430
+ events.append(p(tw("py-6 text-sm text-slate-500"), "No events yet."))
431
+ invoices: list[Element] = []
432
+ for invoice in view.invoices:
433
+ invoices.append(invoice_row(invoice))
434
+ if len(invoices) == 0:
435
+ invoices.append(p(tw("py-6 text-sm text-slate-500"), "No invoices yet."))
436
+ webhook = view.webhook_url or "Not configured"
437
+ return tag(
438
+ "main",
439
+ tw("mx-auto w-full max-w-6xl p-4 sm:p-8"),
440
+ tag(
441
+ "header",
442
+ tw("flex flex-wrap items-end justify-between gap-4"),
443
+ div(
444
+ p(
445
+ tw("text-sm font-semibold uppercase tracking-widest text-indigo-600"),
446
+ "PyJS Extras",
447
+ ),
448
+ h1(
449
+ tw("mt-1 text-3xl font-bold tracking-tight text-slate-950"),
450
+ "Local Stripe simulator",
451
+ ),
452
+ p(
453
+ tw("mt-2 max-w-2xl text-sm text-slate-600"),
454
+ "Drive subscription renewals and inspect the webhooks received "
455
+ "by your application.",
456
+ ),
457
+ ),
458
+ tag(
459
+ "details",
460
+ tw("text-right"),
461
+ tag(
462
+ "summary",
463
+ tw("cursor-pointer text-sm font-medium text-red-700"),
464
+ "Reset simulator…",
465
+ ),
466
+ form(
467
+ tw("mt-2"),
468
+ {"action": "/_simulator/reset", "method": "post"},
469
+ button(
470
+ tw(
471
+ "rounded-lg border border-red-200 bg-white px-4 "
472
+ "py-2.5 text-sm font-semibold text-red-700 "
473
+ "hover:bg-red-50"
474
+ ),
475
+ {"type": "submit"},
476
+ "Delete all simulator data",
477
+ ),
478
+ ),
479
+ ),
480
+ ),
481
+ div(
482
+ tw("mt-8 grid gap-4 sm:grid-cols-2 lg:grid-cols-4"),
483
+ metric("Customers", str(view.customer_count)),
484
+ metric("Subscriptions", str(view.subscription_count)),
485
+ metric("Invoices", str(view.invoice_count)),
486
+ metric("Simulator time", view.clock),
487
+ ),
488
+ div(
489
+ tw("mt-6 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm"),
490
+ p(tw("text-sm text-slate-600"), "API base: " + view.api_base),
491
+ p(tw("mt-1 text-sm text-slate-600"), "Webhook: " + webhook),
492
+ p(
493
+ tw("mt-1 text-sm text-slate-600"),
494
+ "Retry policy: " + view.retry_policy,
495
+ ),
496
+ form(
497
+ tw("mt-4 flex flex-wrap items-end gap-3"),
498
+ {"action": "/_simulator/delivery/mode", "method": "post"},
499
+ tag(
500
+ "label",
501
+ tw("grid gap-1 text-sm font-medium text-slate-700"),
502
+ "Webhook delivery",
503
+ tag(
504
+ "select",
505
+ tw(
506
+ "h-10 rounded-lg border border-slate-300 bg-white "
507
+ "px-3 text-sm text-slate-950"
508
+ ),
509
+ {"name": "mode"},
510
+ behavior_option("automatic", "Automatic", view.delivery_mode),
511
+ behavior_option("manual", "Manual", view.delivery_mode),
512
+ behavior_option("paused", "Paused", view.delivery_mode),
513
+ ),
514
+ ),
515
+ button(
516
+ tw(
517
+ "rounded-lg border border-slate-300 bg-white px-4 py-2.5 "
518
+ "text-sm font-semibold text-slate-700 hover:bg-slate-50"
519
+ ),
520
+ {"type": "submit"},
521
+ "Set mode",
522
+ ),
523
+ span(
524
+ tw("self-center text-xs text-slate-500"),
525
+ str(view.pending_count) + " pending or failed",
526
+ ),
527
+ ),
528
+ div(
529
+ tw("mt-3 flex flex-wrap gap-2"),
530
+ form(
531
+ {
532
+ "action": "/_simulator/delivery/pending/oldest",
533
+ "method": "post",
534
+ },
535
+ button(
536
+ tw(
537
+ "rounded-lg border border-slate-300 px-3 py-2 text-xs "
538
+ "font-semibold text-slate-700 hover:bg-slate-50"
539
+ ),
540
+ {"type": "submit"},
541
+ "Deliver oldest first",
542
+ ),
543
+ ),
544
+ form(
545
+ {
546
+ "action": "/_simulator/delivery/pending/newest",
547
+ "method": "post",
548
+ },
549
+ button(
550
+ tw(
551
+ "rounded-lg border border-slate-300 px-3 py-2 text-xs "
552
+ "font-semibold text-slate-700 hover:bg-slate-50"
553
+ ),
554
+ {"type": "submit"},
555
+ "Deliver newest first",
556
+ ),
557
+ ),
558
+ ),
559
+ form(
560
+ tw("mt-4 flex flex-wrap items-end gap-3"),
561
+ {"action": "/_simulator/clock/advance", "method": "post"},
562
+ tag(
563
+ "label",
564
+ tw("grid gap-1 text-sm font-medium text-slate-700"),
565
+ "Advance time by days",
566
+ input(
567
+ tw("h-10 w-28 rounded-lg border border-slate-300 px-3"),
568
+ {
569
+ "max": "366",
570
+ "min": "1",
571
+ "name": "days",
572
+ "type": "number",
573
+ "value": "30",
574
+ },
575
+ ),
576
+ ),
577
+ button(
578
+ tw(
579
+ "rounded-lg bg-indigo-600 px-4 py-2.5 text-sm font-semibold "
580
+ "text-white hover:bg-indigo-500"
581
+ ),
582
+ {"type": "submit"},
583
+ "Advance clock",
584
+ ),
585
+ ),
586
+ ),
587
+ tag(
588
+ "section",
589
+ tw("mt-10"),
590
+ tag("h2", tw("text-xl font-semibold text-slate-950"), "Subscriptions"),
591
+ div(tw("mt-4 grid gap-4"), *subscriptions),
592
+ ),
593
+ tag(
594
+ "section",
595
+ tw("mt-10 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm"),
596
+ tag("h2", tw("text-xl font-semibold text-slate-950"), "Invoices"),
597
+ tag("ol", tw("mt-2"), *invoices),
598
+ ),
599
+ tag(
600
+ "section",
601
+ tw("mt-10 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm"),
602
+ tag("h2", tw("text-xl font-semibold text-slate-950"), "Event deliveries"),
603
+ tag("ol", tw("mt-2"), *events),
604
+ ),
605
+ )
606
+
607
+
608
+ def main():
609
+ return dashboard(
610
+ DashboardView(
611
+ clock="Now",
612
+ api_base="http://127.0.0.1:12111/v1",
613
+ webhook_url="",
614
+ retry_policy="3 payment attempts",
615
+ customer_count=0,
616
+ subscription_count=0,
617
+ invoice_count=0,
618
+ delivery_mode="automatic",
619
+ pending_count=0,
620
+ subscriptions=(),
621
+ invoices=(),
622
+ events=(),
623
+ )
624
+ )
625
+
626
+
627
+ @cache
628
+ def stylesheet() -> str:
629
+ _, stylesheet = bundle(main, include_main=False)
630
+ return stylesheet
631
+
632
+
633
+ def document(view: DashboardView) -> str:
634
+ return render_document(
635
+ tag(
636
+ "html",
637
+ {"lang": "en"},
638
+ tag(
639
+ "head",
640
+ tag("meta", {"charset": "utf-8"}),
641
+ tag(
642
+ "meta",
643
+ {
644
+ "content": "width=device-width, initial-scale=1",
645
+ "name": "viewport",
646
+ },
647
+ ),
648
+ tag("title", "Local Stripe simulator"),
649
+ tag("style", raw_html(stylesheet())),
650
+ ),
651
+ tag("body", tw("min-h-screen bg-slate-50 text-slate-950"), dashboard(view)),
652
+ )
653
+ )