-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx402.html
More file actions
805 lines (698 loc) · 41.9 KB
/
x402.html
File metadata and controls
805 lines (698 loc) · 41.9 KB
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
<!DOCTYPE html>
<html lang="en" class="light">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ZPayNow Playground</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Roboto+Mono:wght@400;500;600&display=swap" rel="stylesheet">
<script src="wallet-connector.js"></script>
<script>
tailwind.config = {
darkMode: 'class',
theme: {
extend: {
colors: {
primary: '#0ea5a4',
accent: '#14b8a6',
},
fontFamily: {
sans: ['Inter', 'sans-serif'],
mono: ['Roboto Mono', 'monospace'],
},
}
}
}
</script>
<style>
* {
font-family: 'Inter', sans-serif;
}
.font-mono {
font-family: 'Roboto Mono', monospace;
}
.glassmorphism {
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
.transition-smooth {
transition: all 0.2s ease-in-out;
}
/* Custom scrollbar */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: transparent;
}
::-webkit-scrollbar-thumb {
background: #cbd5e1;
border-radius: 4px;
}
.dark ::-webkit-scrollbar-thumb {
background: #475569;
}
.nav-item {
transition: all 0.2s ease-in-out;
}
.nav-item:hover {
transform: translateX(4px);
}
.card-hover {
transition: all 0.2s ease-in-out;
}
.card-hover:hover {
transform: translateY(-2px);
box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1);
}
</style>
</head>
<body class="bg-slate-50 dark:bg-slate-900 transition-smooth">
<!-- Sidebar -->
<aside id="sidebar" class="fixed left-0 top-0 h-screen w-60 bg-white dark:bg-slate-800 border-r border-slate-200 dark:border-slate-700 z-40 transition-transform duration-300">
<div class="flex flex-col h-full">
<!-- Logo -->
<div class="p-6 border-b border-slate-200 dark:border-slate-700">
<div class="flex items-center gap-3">
<div class="w-10 h-10 bg-gradient-to-br from-primary to-accent rounded-lg flex items-center justify-center">
<svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/>
</svg>
</div>
<div>
<h1 class="text-lg font-bold text-slate-800 dark:text-white">ZPayNow</h1>
<p class="text-xs text-slate-500 dark:text-slate-400">Playground</p>
</div>
</div>
</div>
<!-- Navigation -->
<nav class="flex-1 p-4 space-y-2">
<a href="/" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-600 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700 rounded-lg font-medium">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z"/>
</svg>
<span>Payment</span>
</a>
<a href="/x402" class="nav-item flex items-center gap-3 px-4 py-3 bg-primary/10 text-primary rounded-lg font-medium">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>
</svg>
<span>x402</span>
</a>
<a href="/8004" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-600 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700 rounded-lg font-medium">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 24 24">
<path d="M11.944 17.97L4.58 13.62 11.943 24l7.37-10.38-7.372 4.35h.003zM12.056 0L4.69 12.223l7.365 4.354 7.365-4.35L12.056 0z"/>
</svg>
<span>EIP-8004</span>
</a>
<a href="https://faucet.zpaynow.com" target="_blank" rel="noopener noreferrer" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-600 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700 rounded-lg font-medium">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
<span>USDS</span>
<svg class="w-4 h-4 ml-auto opacity-50" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"/>
</svg>
</a>
<a href="https://github.com/zpaynow/playground" target="_blank" rel="noopener noreferrer" class="nav-item flex items-center gap-3 px-4 py-3 text-slate-600 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700 rounded-lg font-medium">
<svg class="w-5 h-5" fill="currentColor" viewBox="0 0 24 24">
<path fill-rule="evenodd" d="M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.202 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.943.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0022 12.017C22 6.484 17.522 2 12 2z" clip-rule="evenodd"/>
</svg>
<span>Code</span>
<svg class="w-4 h-4 ml-auto opacity-50" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"/>
</svg>
</a>
</nav>
<!-- Footer -->
<div class="p-4 border-t border-slate-200 dark:border-slate-700">
<p class="text-xs text-slate-500 dark:text-slate-400 text-center">
v1.0.0 Beta
</p>
</div>
</div>
</aside>
<!-- Main Content -->
<div class="ml-60 transition-all duration-300" id="main-content">
<!-- Top Bar -->
<header class="fixed top-0 right-0 left-60 h-16 bg-white/80 dark:bg-slate-800/80 glassmorphism border-b border-slate-200 dark:border-slate-700 z-30 transition-all duration-300" id="topbar">
<div class="h-full px-6 flex items-center justify-between">
<!-- Mobile Menu Button -->
<button id="mobile-menu-btn" class="lg:hidden p-2 text-slate-600 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700 rounded-lg">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"/>
</svg>
</button>
<div class="flex-1"></div>
<!-- Right Side Controls -->
<div class="flex items-center gap-4">
<!-- Wallet Status -->
<div id="wallet-status" class="hidden items-center gap-2 px-4 py-2 bg-green-50 dark:bg-green-900/20 text-green-600 dark:text-green-400 rounded-lg border border-green-200 dark:border-green-800">
<div class="w-2 h-2 bg-green-500 rounded-full"></div>
<span id="wallet-address" class="text-sm font-medium">0x...0000</span>
</div>
<!-- Connect Wallet Button -->
<button id="connect-wallet-btn" onclick="connectWallet()" class="px-5 py-2 bg-gradient-to-r from-primary to-accent text-white rounded-lg font-medium hover:shadow-lg transition-smooth">
Connect Wallet
</button>
<!-- Theme Toggle -->
<button id="theme-toggle" class="p-2 text-slate-600 dark:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700 rounded-lg transition-smooth">
<svg class="w-6 h-6 hidden dark:block" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"/>
</svg>
<svg class="w-6 h-6 dark:hidden" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"/>
</svg>
</button>
</div>
</div>
</header>
<!-- Content Area -->
<main class="pt-16 min-h-screen">
<div class="p-6">
<!-- Two Column Layout -->
<div class="grid grid-cols-1 xl:grid-cols-2 gap-6">
<!-- Left: Products and Email Form -->
<div class="space-y-6">
<!-- Products -->
<div class="grid grid-cols-2 gap-4">
<!-- Product 1: Buy me a coffee -->
<div id="product-1" class="bg-white dark:bg-slate-800 rounded-xl shadow-sm border-2 border-slate-200 dark:border-slate-700 overflow-hidden card-hover cursor-pointer transition-all" onclick="selectProduct(1, 'Buy me a coffee', 2.00)">
<div class="p-4">
<div class="flex justify-center mb-3">
<div class="w-16 h-16 bg-gradient-to-br from-amber-400 to-orange-500 rounded-full flex items-center justify-center">
<svg class="w-9 h-9 text-white" fill="currentColor" viewBox="0 0 24 24">
<path d="M2,21H20V19H2M20,8H18V5H20M20,3H4V13A4,4 0 0,0 8,17H14A4,4 0 0,0 18,13V10H20A2,2 0 0,0 22,8V5C22,3.89 21.1,3 20,3Z"/>
</svg>
</div>
</div>
<div class="text-center mb-3">
<h3 class="text-base font-bold text-slate-800 dark:text-white mb-1">Buy me a coffee</h3>
<p class="text-slate-600 dark:text-slate-400 text-xs mb-2">Support this open source project</p>
<p class="text-xl font-bold text-primary">$2.00</p>
</div>
<div class="flex justify-center">
<div class="w-6 h-6 rounded-full border-2 border-slate-300 dark:border-slate-600 flex items-center justify-center product-indicator">
<div class="w-3 h-3 bg-primary rounded-full hidden"></div>
</div>
</div>
</div>
</div>
<!-- Product 2: Premium Member -->
<div id="product-2" class="bg-white dark:bg-slate-800 rounded-xl shadow-sm border-2 border-slate-200 dark:border-slate-700 overflow-hidden card-hover cursor-pointer transition-all" onclick="selectProduct(2, 'Become a Premium Member', 10.00)">
<div class="p-4">
<div class="flex justify-center mb-3">
<div class="w-16 h-16 bg-gradient-to-br from-yellow-400 to-amber-500 rounded-full flex items-center justify-center">
<svg class="w-9 h-9 text-white" fill="currentColor" viewBox="0 0 24 24">
<path d="M12,17.27L18.18,21L16.54,13.97L22,9.24L14.81,8.62L12,2L9.19,8.62L2,9.24L7.45,13.97L5.82,21L12,17.27Z"/>
</svg>
</div>
</div>
<div class="text-center mb-3">
<h3 class="text-base font-bold text-slate-800 dark:text-white mb-1">Premium Member</h3>
<p class="text-slate-600 dark:text-slate-400 text-xs mb-2">Get 24/7 support & exclusive features</p>
<p class="text-xl font-bold text-primary">$10.00</p>
</div>
<div class="flex justify-center">
<div class="w-6 h-6 rounded-full border-2 border-slate-300 dark:border-slate-600 flex items-center justify-center product-indicator">
<div class="w-3 h-3 bg-primary rounded-full hidden"></div>
</div>
</div>
</div>
</div>
</div>
<!-- Email Form -->
<div class="bg-white dark:bg-slate-800 rounded-xl shadow-sm border border-slate-200 dark:border-slate-700 overflow-hidden">
<div class="px-6 py-4 border-b border-slate-200 dark:border-slate-700">
<h2 class="text-lg font-bold text-slate-800 dark:text-white">Your Information</h2>
<p class="text-sm text-slate-500 dark:text-slate-400 mt-1">Select a product and enter your email</p>
</div>
<div class="p-6">
<form id="x402-form" class="space-y-4">
<div id="selected-product-display" class="bg-gradient-to-r from-primary/10 to-accent/10 rounded-lg p-4 mb-4 border border-primary/20 hidden">
<p class="text-xs text-slate-600 dark:text-slate-400 mb-1">Selected Product</p>
<div class="flex items-center justify-between">
<p id="selected-product-name" class="text-base font-bold text-slate-800 dark:text-white"></p>
<p id="selected-product-price" class="text-xl font-bold text-primary"></p>
</div>
</div>
<div>
<label class="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">Email Address</label>
<input type="email" id="email" required placeholder="your@email.com" class="w-full px-4 py-3 bg-slate-50 dark:bg-slate-900 text-slate-700 dark:text-slate-200 rounded-lg border border-slate-300 dark:border-slate-600 font-sans text-sm focus:outline-none focus:ring-2 focus:ring-primary transition-smooth">
</div>
<button type="submit" id="submit-btn" disabled class="w-full px-5 py-3 bg-gradient-to-r from-primary to-accent text-white rounded-lg font-semibold hover:shadow-lg transition-smooth disabled:opacity-50 disabled:cursor-not-allowed">
Get Payment Options
</button>
</form>
</div>
</div>
<!-- Additional Info Card -->
<div class="bg-gradient-to-br from-primary/10 to-accent/10 rounded-xl p-6 border border-primary/20">
<h3 class="font-semibold text-slate-800 dark:text-white mb-2">Protocol Info</h3>
<p class="text-sm text-slate-600 dark:text-slate-300">
x402 enables HTTP payment-required status codes with crypto micropayments. Perfect for paywalled APIs and metered services.
</p>
</div>
</div>
<!-- Right Column: Payment Options -->
<div class="space-y-6">
<div class="bg-white dark:bg-slate-800 rounded-xl shadow-sm border border-slate-200 dark:border-slate-700 overflow-hidden card-hover">
<!-- Header -->
<div class="px-6 py-4 border-b border-slate-200 dark:border-slate-700">
<h2 class="text-lg font-bold text-slate-800 dark:text-white">Payment Options</h2>
<p class="text-sm text-slate-500 dark:text-slate-400 mt-1">Select a payment method</p>
</div>
<!-- Payment Tabs -->
<div id="payment-tabs-section" class="hidden">
<div class="px-6 pt-4">
<p class="text-xs text-slate-500 dark:text-slate-400 mb-2 font-medium">Available Networks</p>
<div id="payment-tabs" class="flex gap-2 flex-wrap pb-4 border-b border-slate-200 dark:border-slate-700">
<!-- Tabs will be populated dynamically -->
</div>
</div>
<!-- JSON Code Display -->
<div class="p-6">
<div class="flex items-center justify-between mb-2">
<label class="block text-sm font-medium text-slate-700 dark:text-slate-300">
Payment Request
</label>
<button onclick="copyPaymentCode()" class="text-xs font-medium text-primary hover:text-accent transition-smooth flex items-center gap-1">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/>
</svg>
Copy
</button>
</div>
<div class="bg-slate-900 dark:bg-black rounded-lg p-4 font-mono text-sm overflow-x-auto max-h-96 overflow-y-auto">
<pre id="payment-code" class="text-slate-300">Select a product and enter your email to see payment options</pre>
</div>
</div>
<!-- Sign Button -->
<div class="px-6 pb-6">
<button id="sign-btn" class="w-full px-5 py-3 bg-gradient-to-r from-primary to-accent text-white rounded-lg font-semibold hover:shadow-lg transition-smooth flex items-center justify-center gap-2">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z"/>
</svg>
Sign & Send
</button>
<!-- Transaction Result -->
<div id="transaction-result" class="mt-4 hidden">
<div class="bg-green-50 dark:bg-green-900/20 rounded-lg p-4 border border-green-200 dark:border-green-800">
<p class="text-sm text-green-600 dark:text-green-400 mb-1 font-medium">Payment Successful!</p>
<p class="text-xs text-green-600 dark:text-green-400 mb-2">Transaction submitted on-chain</p>
<div class="bg-white dark:bg-slate-800 rounded p-2">
<p class="text-xs text-slate-500 dark:text-slate-400 mb-1">Transaction Hash:</p>
<p id="transaction-hash" class="text-xs font-mono text-slate-800 dark:text-white break-all"></p>
</div>
</div>
</div>
</div>
</div>
<!-- Empty State -->
<div id="payment-empty-state" class="p-6 text-center py-16">
<svg class="w-20 h-20 mx-auto text-slate-300 dark:text-slate-600 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z"/>
</svg>
<p class="text-slate-500 dark:text-slate-400 text-sm mb-2 font-medium">
No Payment Options Yet
</p>
<p class="text-slate-400 dark:text-slate-500 text-xs">
Select a product and enter your email to see available payment options
</p>
</div>
</div>
</div>
</div>
</div>
</main>
</div>
<!-- Mobile Sidebar Overlay -->
<div id="sidebar-overlay" class="fixed inset-0 bg-black/50 z-30 lg:hidden hidden"></div>
<script>
// Backend API Configuration
const API_BASE_URL = '';
// Store selected product and payment options
let selectedProduct = {
id: null,
name: '',
price: 0
};
let paymentOptions = null;
let selectedPaymentIndex = 0;
// Wallet connector callbacks
walletConnector.onConnect((address, provider) => {
console.log('Wallet connected:', address);
});
// Product selection
function selectProduct(id, name, price) {
selectedProduct = { id, name, price };
// Update visual selection indicators
document.querySelectorAll('[id^="product-"]').forEach(el => {
el.classList.remove('border-primary', 'dark:border-accent', 'shadow-lg');
el.classList.add('border-slate-200', 'dark:border-slate-700');
const indicator = el.querySelector('.product-indicator div');
indicator.classList.add('hidden');
});
// Highlight selected product
const selectedEl = document.getElementById(`product-${id}`);
selectedEl.classList.remove('border-slate-200', 'dark:border-slate-700');
selectedEl.classList.add('border-primary', 'dark:border-accent', 'shadow-lg');
const indicator = selectedEl.querySelector('.product-indicator div');
indicator.classList.remove('hidden');
// Update selected product display in form
document.getElementById('selected-product-name').textContent = name;
document.getElementById('selected-product-price').textContent = '$' + price.toFixed(2);
document.getElementById('selected-product-display').classList.remove('hidden');
// Enable submit button
document.getElementById('submit-btn').disabled = false;
}
// Handle form submission
document.getElementById('x402-form').addEventListener('submit', async function(e) {
e.preventDefault();
const email = document.getElementById('email').value;
if (!email || !selectedProduct.id) {
alert('Please select a product and enter your email');
return;
}
// Disable submit button during processing
const submitBtn = document.getElementById('submit-btn');
const originalText = submitBtn.textContent;
submitBtn.disabled = true;
submitBtn.textContent = 'Processing...';
// Call backend API
try {
const apiUrl = `${API_BASE_URL}/x402/requirements`;
console.log('Sending request to:', apiUrl);
console.log('Request payload:', {
product: selectedProduct.id,
email: email
});
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
product: selectedProduct.id,
email: email
})
});
console.log('Response status:', response.status);
console.log('Response ok:', response.ok);
// Try to get response data even if status is not ok
const data = await response.json();
console.log('Response data:', data);
if (!response.ok) {
throw new Error(`Server returned ${response.status}: ${JSON.stringify(data)}`);
}
paymentOptions = data;
// Display payment options
displayPaymentOptions(data);
} catch (error) {
console.error('Error getting payment options:', error);
alert('Failed to get payment options. Error: ' + error.message);
} finally {
// Re-enable submit button
submitBtn.disabled = false;
submitBtn.textContent = originalText;
}
});
// Display payment options
function displayPaymentOptions(data) {
console.log('Displaying payment options:', data);
if (!data.accepts || data.accepts.length === 0) {
alert('No payment options available');
return;
}
// Hide empty state, show tabs section
document.getElementById('payment-empty-state').classList.add('hidden');
document.getElementById('payment-tabs-section').classList.remove('hidden');
// Create payment tabs
const tabsContainer = document.getElementById('payment-tabs');
tabsContainer.innerHTML = '';
data.accepts.forEach((payment, index) => {
const tabButton = document.createElement('button');
tabButton.className = `px-4 py-2 rounded-lg font-medium text-sm transition-smooth ${
index === 0
? 'bg-primary text-white'
: 'bg-slate-100 dark:bg-slate-700 text-slate-700 dark:text-slate-200 hover:bg-slate-200 dark:hover:bg-slate-600'
}`;
tabButton.textContent = payment.network;
tabButton.onclick = () => selectPayment(index);
tabButton.id = `payment-tab-${index}`;
tabsContainer.appendChild(tabButton);
});
// Display first payment by default
selectPayment(0);
}
// Select a payment and display its JSON
function selectPayment(paymentIndex) {
selectedPaymentIndex = paymentIndex;
const payment = paymentOptions.accepts[paymentIndex];
console.log('Selected payment:', payment);
console.log('Payment extra field:', payment.extra);
// Update tab styles
paymentOptions.accepts.forEach((_, index) => {
const tab = document.getElementById(`payment-tab-${index}`);
if (tab) {
if (index === paymentIndex) {
tab.className = 'px-4 py-2 rounded-lg font-medium text-sm transition-smooth bg-primary text-white';
} else {
tab.className = 'px-4 py-2 rounded-lg font-medium text-sm transition-smooth bg-slate-100 dark:bg-slate-700 text-slate-700 dark:text-slate-200 hover:bg-slate-200 dark:hover:bg-slate-600';
}
}
});
// Display payment JSON
const codeEl = document.getElementById('payment-code');
codeEl.textContent = JSON.stringify(payment, null, 2);
}
// Copy payment code to clipboard
function copyPaymentCode() {
const code = document.getElementById('payment-code').textContent;
navigator.clipboard.writeText(code).then(() => {
console.log('Payment code copied to clipboard');
});
}
// Sign button handler
document.getElementById('sign-btn').addEventListener('click', async function() {
// Prevent duplicate clicks if already processing
if (this.disabled) {
return;
}
// Check wallet connection
if (!walletConnector.isConnected()) {
alert('Please connect your wallet first!');
connectWallet();
return;
}
const userAddress = walletConnector.getAddress();
const ethereum = walletConnector.getProvider();
if (!paymentOptions || selectedPaymentIndex === null) {
alert('Please select a payment option first');
return;
}
const selectedPayment = paymentOptions.accepts[selectedPaymentIndex];
console.log('Signing payment:', selectedPayment);
// Prepare UI: disable button and show waiting state
const signBtn = document.getElementById('sign-btn');
const originalBtnHtml = signBtn.innerHTML;
const originalBtnClasses = signBtn.className;
signBtn.disabled = true;
signBtn.className = originalBtnClasses + ' opacity-50 cursor-not-allowed';
signBtn.innerHTML = '<svg class="w-5 h-5 animate-spin" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"></path></svg><span class="ml-2">Submitting...</span>';
try {
// Parse payment details
const tokenAddress = selectedPayment.asset;
const recipientAddress = selectedPayment.payTo;
const value = selectedPayment.maxAmountRequired;
const tokenName = selectedPayment.extra?.name || 'USDC';
const tokenVersion = selectedPayment.extra?.version || '2';
const chainIdDecimal = selectedPayment.extra?.chainId || 1;
console.log('Chain ID from payment requirements:', chainIdDecimal);
// Check current wallet chain ID
const currentChainId = await ethereum.request({ method: 'eth_chainId' });
const currentChainIdDecimal = parseInt(currentChainId, 16);
console.log('Current wallet chain ID:', currentChainIdDecimal);
// If wallet is on different chain, request to switch
if (currentChainIdDecimal !== chainIdDecimal) {
console.log('Switching to chain ID:', chainIdDecimal);
const chainIdHex = '0x' + chainIdDecimal.toString(16);
try {
await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: chainIdHex }],
});
console.log('Successfully switched to chain:', chainIdDecimal);
} catch (switchError) {
// This error code indicates that the chain has not been added to MetaMask
if (switchError.code === 4902) {
alert(`Please add chain ${chainIdDecimal} to your wallet first. Chain ID: ${chainIdHex}`);
return;
}
throw switchError;
}
}
// Create EIP-712 typed data
const nonce = '0x' + Array.from({length: 64}, () => Math.floor(Math.random() * 16).toString(16)).join('');
const validBefore = Math.floor(Date.now() / 1000) + 3600; // Valid for 1 hour
const data = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
TransferWithAuthorization: [
{ name: "from", type: "address" },
{ name: "to", type: "address" },
{ name: "value", type: "uint256" },
{ name: "validAfter", type: "uint256" },
{ name: "validBefore", type: "uint256" },
{ name: "nonce", type: "bytes32" },
],
},
domain: {
name: tokenName,
version: tokenVersion,
chainId: chainIdDecimal,
verifyingContract: tokenAddress,
},
primaryType: "TransferWithAuthorization",
message: {
from: userAddress,
to: recipientAddress,
value: value,
validAfter: 0,
validBefore: validBefore,
nonce: nonce,
},
};
console.log('=== SIGNING DETAILS ===');
console.log('Domain:', JSON.stringify(data.domain, null, 2));
console.log('Message:', JSON.stringify(data.message, null, 2));
console.log('Token Address:', tokenAddress);
console.log('Token Name:', tokenName);
console.log('Token Version:', tokenVersion);
console.log('Chain ID:', chainIdDecimal);
console.log('Full typed data:', JSON.stringify(data, null, 2));
// Request signature
const signature = await ethereum.request({
method: "eth_signTypedData_v4",
params: [userAddress, JSON.stringify(data)],
});
console.log('Signature:', signature);
// Create payment payload
const paymentPayload = {
paymentPayload: {
x402Version: 1,
scheme: selectedPayment.scheme,
network: selectedPayment.network,
payload: {
signature: signature,
authorization: {
from: userAddress,
to: recipientAddress,
value: value,
validAfter: "0",
validBefore: validBefore.toString(),
nonce: nonce,
}
}
},
paymentRequirements: selectedPayment
};
console.log('Payment payload:', paymentPayload);
// Send to backend
await submitPayment(paymentPayload);
} catch (error) {
console.error('Error signing payment:', error);
alert('Failed to sign payment: ' + error.message);
} finally {
// Restore button state
signBtn.disabled = false;
signBtn.className = originalBtnClasses;
signBtn.innerHTML = originalBtnHtml;
}
});
// Submit payment to backend
async function submitPayment(paymentPayload) {
try {
const apiUrl = `${API_BASE_URL}/x402/payments`;
console.log('Submitting payment to:', apiUrl);
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(paymentPayload)
});
console.log('Response status:', response.status);
const data = await response.json();
console.log('Response data:', data);
if (!response.ok) {
throw new Error(`Server returned ${response.status}: ${JSON.stringify(data)}`);
}
// Check for errorReason in response
if (data.errorReason) {
alert('Payment failed: ' + data.errorReason);
return;
}
// Show success message and transaction result
const transactionHash = data.transaction || 'N/A';
// Update transaction result component
document.getElementById('transaction-hash').textContent = transactionHash;
document.getElementById('transaction-result').classList.remove('hidden');
alert('Payment submitted successfully! Transaction hash: ' + transactionHash);
} catch (error) {
console.error('Error submitting payment:', error);
alert('Failed to submit payment: ' + error.message);
}
}
// Theme Toggle
const themeToggle = document.getElementById('theme-toggle');
const html = document.documentElement;
themeToggle.addEventListener('click', () => {
if (html.classList.contains('dark')) {
html.classList.remove('dark');
html.classList.add('light');
} else {
html.classList.remove('light');
html.classList.add('dark');
}
});
// Mobile Menu Toggle
const mobileMenuBtn = document.getElementById('mobile-menu-btn');
const sidebar = document.getElementById('sidebar');
const sidebarOverlay = document.getElementById('sidebar-overlay');
const mainContent = document.getElementById('main-content');
const topbar = document.getElementById('topbar');
mobileMenuBtn.addEventListener('click', () => {
sidebar.classList.toggle('-translate-x-full');
sidebarOverlay.classList.toggle('hidden');
});
sidebarOverlay.addEventListener('click', () => {
sidebar.classList.add('-translate-x-full');
sidebarOverlay.classList.add('hidden');
});
// Responsive Sidebar
function handleResize() {
if (window.innerWidth < 1024) {
sidebar.classList.add('-translate-x-full');
mainContent.classList.remove('ml-60');
topbar.classList.remove('left-60');
} else {
sidebar.classList.remove('-translate-x-full');
mainContent.classList.add('ml-60');
topbar.classList.add('left-60');
sidebarOverlay.classList.add('hidden');
}
}
window.addEventListener('resize', handleResize);
handleResize();
</script>
</body>
</html>