-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment-api.php
More file actions
1916 lines (1716 loc) · 97.8 KB
/
payment-api.php
File metadata and controls
1916 lines (1716 loc) · 97.8 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
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
<?php
// Proxy for Amwal API
session_start();
// Security Configuration
define('MAX_REQUESTS_PER_HOUR', 100);
define('ALLOWED_ORIGINS', ['amwal-tech.github.io', 'localhost', '127.0.0.1']);
define('AMWAL_API_BASE', 'https://backend.sa.amwal.tech');
// Handle API proxy requests
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['proxy'])) {
header('Content-Type: application/json');
// CORS Security - Only allow specific origins
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
$allowedOrigin = null;
foreach (ALLOWED_ORIGINS as $allowed) {
if (strpos($origin, $allowed) !== false) {
$allowedOrigin = $origin;
break;
}
}
if ($allowedOrigin) {
header("Access-Control-Allow-Origin: $allowedOrigin");
}
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Max-Age: 86400');
// Handle preflight
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
exit(0);
}
// Rate limiting (simple session-based)
$sessionKey = 'api_requests_' . date('Y-m-d-H');
if (!isset($_SESSION[$sessionKey])) {
$_SESSION[$sessionKey] = 0;
}
if ($_SESSION[$sessionKey] >= MAX_REQUESTS_PER_HOUR) {
http_response_code(429);
echo json_encode(['error' => 'Rate limit exceeded. Try again later.']);
exit;
}
$_SESSION[$sessionKey]++;
// Parse and validate input
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || !is_array($input)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid JSON input']);
exit;
}
// Required fields validation
$required = ['apiKey', 'storeId', 'endpoint'];
foreach ($required as $field) {
if (empty($input[$field]) || !is_string($input[$field])) {
http_response_code(400);
echo json_encode(['error' => "Missing or invalid: $field"]);
exit;
}
}
// Sanitize and validate inputs
$apiKey = trim(htmlspecialchars($input['apiKey'], ENT_QUOTES, 'UTF-8'));
$storeId = trim(htmlspecialchars($input['storeId'], ENT_QUOTES, 'UTF-8'));
$endpoint = trim(htmlspecialchars($input['endpoint'], ENT_QUOTES, 'UTF-8'));
$method = in_array($input['method'] ?? 'POST', ['GET', 'POST', 'PUT', 'DELETE'])
? $input['method'] : 'POST';
// Validate UUID format for security
if (!preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$/i', $apiKey) ||
!preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}$/i', $storeId)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid API key or Store ID format']);
exit;
}
// Validate endpoint starts with /
if (!str_starts_with($endpoint, '/')) {
http_response_code(400);
echo json_encode(['error' => 'Invalid endpoint format']);
exit;
}
// Build request headers securely
$headers = [
'Authorization: ' . $apiKey,
'Accept: application/json',
'Content-Type: application/json',
'User-Agent: AmwalProxy/1.2'
];
// Optional headers (with validation)
if (!empty($input['originHeader']) && is_string($input['originHeader'])) {
$originHeader = trim(htmlspecialchars($input['originHeader'], ENT_QUOTES, 'UTF-8'));
if (preg_match('/^[a-zA-Z0-9.-]+$/', $originHeader)) {
$headers[] = 'Origin: https://' . $originHeader;
}
}
if (!empty($input['amwalKey']) && is_string($input['amwalKey'])) {
$amwalKey = trim(htmlspecialchars($input['amwalKey'], ENT_QUOTES, 'UTF-8'));
if (preg_match('/^(prod|sandbox)-amwal-[a-f0-9-]+$/i', $amwalKey)) {
$headers[] = 'X-Amwal-Key: ' . $amwalKey;
}
}
// Setup cURL with security best practices
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => AMWAL_API_BASE . $endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_TIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_MAXREDIRS => 0,
CURLOPT_USERAGENT => 'AmwalProxy/1.2'
]);
// Add request body if provided
if (!empty($input['data'])) {
if (!is_array($input['data']) && !is_object($input['data'])) {
http_response_code(400);
echo json_encode(['error' => 'Invalid data format']);
curl_close($ch);
exit;
}
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($input['data']));
}
// Execute request
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
// Handle cURL errors securely
if ($curlError || $response === false) {
error_log("Amwal API Proxy Error: $curlError");
http_response_code(502);
echo json_encode(['error' => 'Service temporarily unavailable']);
exit;
}
// Return response
http_response_code($httpCode);
// Validate response is JSON to prevent XSS
$decodedResponse = json_decode($response, true);
if (json_last_error() === JSON_ERROR_NONE) {
echo json_encode($decodedResponse);
} else {
echo $response;
}
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Amwal Payment API Demo - v1.2.0</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data:;">
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(8px); }
to { opacity: 1; transform: translateY(0); }
}
.animate-spin {
animation: spin 1s linear infinite;
}
.animate-fadeIn {
animation: fadeIn 0.4s ease-out;
}
.field-group {
border: 1px solid #e5e7eb;
border-radius: 8px;
padding: 16px;
margin-bottom: 16px;
background: #fafafa;
}
.field-group h4 {
margin: 0 0 12px 0;
font-weight: 600;
color: #374151;
}
.tooltip {
position: relative;
cursor: help;
}
.tooltip:hover::after {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
background: #1f2937;
color: white;
padding: 8px 12px;
border-radius: 6px;
font-size: 12px;
white-space: nowrap;
z-index: 1000;
margin-bottom: 5px;
}
.tooltip:hover::before {
content: '';
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
border: 5px solid transparent;
border-top-color: #1f2937;
margin-bottom: -5px;
}
</style>
</head>
<body class="bg-gray-50 min-h-screen font-sans antialiased">
<div class="max-w-6xl mx-auto p-6">
<!-- Header -->
<div class="text-center py-12 mb-8">
<h1 class="text-4xl font-light text-gray-900 mb-2">Amwal Payment API</h1>
<p class="text-gray-500 text-lg mb-2">Integration Demo v1.2.0</p>
<p class="text-sm text-gray-400 mb-6">Comprehensive API for creating and managing payment links</p>
<div class="flex justify-center gap-6 text-sm">
<a href="https://docs.amwal.tech"
target="_blank" rel="noopener noreferrer"
class="text-gray-600 hover:text-gray-900 underline transition-colors">
📖 Documentation
</a>
<a href="https://docs.amwal.tech/reference/getting-started-1#/"
target="_blank" rel="noopener noreferrer"
class="text-gray-600 hover:text-gray-900 underline transition-colors">
🔗 API Reference
</a>
<a href="https://merchant.sa.amwal.tech"
target="_blank" rel="noopener noreferrer"
class="text-gray-600 hover:text-gray-900 underline transition-colors">
🏪 Merchant Portal
</a>
</div>
</div>
<!-- API Configuration -->
<div class="bg-white rounded-lg border border-gray-200 p-6 mb-8">
<h3 class="text-lg font-medium text-gray-900 mb-4">🔐 API Configuration</h3>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
<div>
<label for="apiKey" class="block text-sm font-medium text-gray-700 mb-2">
API Key
<span class="tooltip text-gray-400" data-tooltip="Get your API key from Merchant Portal > Integrations">ⓘ</span>
</label>
<input type="text" id="apiKey" value="d9ccf8bc-ed63-44ad-a54c-9d8fee63df6b"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500"
onchange="updateAmwalKeyPreview()">
</div>
<div>
<label for="storeId" class="block text-sm font-medium text-gray-700 mb-2">
Store ID
<span class="tooltip text-gray-400" data-tooltip="Your unique store identifier from the Merchant Portal">ⓘ</span>
</label>
<input type="text" id="storeId" value="f24267bd-79f4-433f-a9f2-485c171301e4"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="originHeader" class="block text-sm font-medium text-gray-700 mb-2">
Origin Header
<span class="tooltip text-gray-400" data-tooltip="Origin header for API security validation">ⓘ</span>
</label>
<input type="text" id="originHeader" value="amwal-tech.github.io"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="amwalKey" class="block text-sm font-medium text-gray-700 mb-2">
X-Amwal-Key Header
<span class="tooltip text-gray-400" data-tooltip="Environment key for sandbox/production selection">ⓘ</span>
</label>
<input type="text" id="amwalKey" value="sandbox-amwal-b87482f2-55da-486f-9ec5-e2ceb47a0333"
placeholder="prod-amwal-{apiKey} or sandbox-amwal-{apiKey}"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
</div>
<div class="mt-4 flex gap-2">
<button type="button" onclick="setAmwalKey('prod')"
class="text-xs bg-green-100 text-green-700 px-3 py-1 rounded hover:bg-green-200 transition-colors">
🟢 Set Production Key
</button>
<button type="button" onclick="setAmwalKey('sandbox')"
class="text-xs bg-yellow-100 text-yellow-700 px-3 py-1 rounded hover:bg-yellow-200 transition-colors">
🟡 Set Sandbox Key
</button>
<button type="button" onclick="clearAmwalKey()"
class="text-xs bg-gray-100 text-gray-700 px-3 py-1 rounded hover:bg-gray-200 transition-colors">
🚫 Clear Header
</button>
</div>
<!-- Environment Info -->
<div id="environmentInfo" class="mt-4 p-3 bg-green-50 border border-green-200 rounded-lg">
<div class="flex items-center gap-2">
<span class="text-lg">🟢</span>
<span class="text-sm font-medium text-green-800">Production Environment</span>
</div>
<p class="text-xs text-green-700 mt-1">Using production mode. Real payments will be processed.</p>
</div>
</div>
<!-- Navigation -->
<div class="flex flex-wrap gap-1 mb-8 border-b border-gray-200">
<button class="tab px-4 py-3 text-sm font-medium border-b-2 border-gray-900 text-gray-900"
onclick="showTab('create')">💳 Create Payment</button>
<button class="tab px-4 py-3 text-sm font-medium text-gray-500 hover:text-gray-700 border-b-2 border-transparent"
onclick="showTab('details')">🔍 Payment Details</button>
<button class="tab px-4 py-3 text-sm font-medium text-gray-500 hover:text-gray-700 border-b-2 border-transparent"
onclick="showTab('list')">📋 List Payments</button>
<button class="tab px-4 py-3 text-sm font-medium text-gray-500 hover:text-gray-700 border-b-2 border-transparent"
onclick="showTab('status')">⚡ Check Status</button>
<button class="tab px-4 py-3 text-sm font-medium text-gray-500 hover:text-gray-700 border-b-2 border-transparent"
onclick="showTab('resolve')">🔧 Resolve Link</button>
<button class="tab px-4 py-3 text-sm font-medium text-gray-500 hover:text-gray-700 border-b-2 border-transparent"
onclick="showTab('refund')">💸 Process Refund</button>
</div>
<!-- Create Payment Tab -->
<div id="create" class="tab-content animate-fadeIn">
<div class="bg-white rounded-lg border border-gray-200 p-6">
<h2 class="text-xl font-medium text-gray-900 mb-6">💳 Create New Payment Link</h2>
<form id="createPaymentForm" class="space-y-6">
<!-- Basic Payment Information -->
<div class="field-group">
<h4>💰 Payment Information</h4>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label for="amount" class="block text-sm font-medium text-gray-700 mb-1">
Amount (SAR) *
<span class="tooltip text-gray-400" data-tooltip="Payment amount in Saudi Riyals. Must be greater than 0.01">ⓘ</span>
</label>
<input type="number" id="amount" required step="0.01" min="0.01" placeholder="299.99"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="singleUse" class="block text-sm font-medium text-gray-700 mb-1">
Usage Type
<span class="tooltip text-gray-400" data-tooltip="Single-use: Link expires after one payment. Multi-use: Can be used multiple times">ⓘ</span>
</label>
<select id="singleUse" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
<option value="true">Single Use (recommended for invoices)</option>
<option value="false">Multi Use (for donations, subscriptions)</option>
</select>
</div>
</div>
<div>
<label for="title" class="block text-sm font-medium text-gray-700 mb-1">
Payment Title *
<span class="tooltip text-gray-400" data-tooltip="Descriptive title that customers will see">ⓘ</span>
</label>
<input type="text" id="title" required maxlength="200" placeholder="Invoice #INV-2024-001"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="description" class="block text-sm font-medium text-gray-700 mb-1">
Description
<span class="tooltip text-gray-400" data-tooltip="Detailed description of what the payment is for">ⓘ</span>
</label>
<textarea id="description" rows="3" maxlength="500" placeholder="Annual premium subscription with advanced features"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500"></textarea>
</div>
<div>
<label for="selectedDate" class="block text-sm font-medium text-gray-700 mb-1">
Expiry Date
<span class="tooltip text-gray-400" data-tooltip="Optional: After this date, the payment link becomes invalid">ⓘ</span>
</label>
<input type="datetime-local" id="selectedDate"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
</div>
<!-- Customer Information -->
<div class="field-group">
<h4>👤 Customer Information</h4>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label for="client_first_name" class="block text-sm font-medium text-gray-700 mb-1">
First Name
<span class="tooltip text-gray-400" data-tooltip="Customer's first name - personalizes the payment experience">ⓘ</span>
</label>
<input type="text" id="client_first_name" maxlength="50" placeholder="Ahmed"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="client_last_name" class="block text-sm font-medium text-gray-700 mb-1">
Last Name
<span class="tooltip text-gray-400" data-tooltip="Customer's last name">ⓘ</span>
</label>
<input type="text" id="client_last_name" maxlength="50" placeholder="Al-Rashid"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label for="client_email" class="block text-sm font-medium text-gray-700 mb-1">
Email Address
<span class="tooltip text-gray-400" data-tooltip="Customer's email for receipts and notifications">ⓘ</span>
</label>
<input type="email" id="client_email" placeholder="customer@example.com"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="client_phone_number" class="block text-sm font-medium text-gray-700 mb-1">
Phone Number
<span class="tooltip text-gray-400" data-tooltip="International format with country code (e.g., +966501234567)">ⓘ</span>
</label>
<input type="tel" id="client_phone_number" placeholder="+966501234567" pattern="^\+[1-9]\d{1,14}$"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
</div>
</div>
<!-- Address Information -->
<div class="field-group">
<h4>📍 Address Information</h4>
<div class="mb-4">
<label class="flex items-center">
<input type="checkbox" id="address_required"
class="w-4 h-4 text-gray-600 border-gray-300 focus:ring-gray-500 focus:ring-2">
<span class="ml-2 text-sm text-gray-700">
Require address during payment
<span class="tooltip text-gray-400" data-tooltip="Customer must provide address information during payment">ⓘ</span>
</span>
</label>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label for="client_street1" class="block text-sm font-medium text-gray-700 mb-1">
Street Address 1
<span class="tooltip text-gray-400" data-tooltip="Primary street address">ⓘ</span>
</label>
<input type="text" id="client_street1" maxlength="100" placeholder="123 King Fahd Road"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="client_street2" class="block text-sm font-medium text-gray-700 mb-1">
Street Address 2
<span class="tooltip text-gray-400" data-tooltip="Apartment, suite, etc. (optional)">ⓘ</span>
</label>
<input type="text" id="client_street2" maxlength="100" placeholder="Apartment 4B"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<div>
<label for="client_city" class="block text-sm font-medium text-gray-700 mb-1">
City
<span class="tooltip text-gray-400" data-tooltip="Customer's city">ⓘ</span>
</label>
<input type="text" id="client_city" maxlength="50" placeholder="Riyadh"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="client_state" class="block text-sm font-medium text-gray-700 mb-1">
State/Province
<span class="tooltip text-gray-400" data-tooltip="State or province">ⓘ</span>
</label>
<input type="text" id="client_state" maxlength="50" placeholder="Riyadh Province"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="client_country" class="block text-sm font-medium text-gray-700 mb-1">
Country
<span class="tooltip text-gray-400" data-tooltip="2-letter country code (ISO 3166-1)">ⓘ</span>
</label>
<select id="client_country" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
<option value="">Select Country</option>
<option value="SA" selected>SA - Saudi Arabia</option>
<option value="AE">AE - UAE</option>
<option value="EG">EG - Egypt</option>
<option value="US">US - United States</option>
<option value="GB">GB - United Kingdom</option>
</select>
</div>
<div>
<label for="client_postcode" class="block text-sm font-medium text-gray-700 mb-1">
Postal Code
<span class="tooltip text-gray-400" data-tooltip="ZIP or postal code">ⓘ</span>
</label>
<input type="text" id="client_postcode" maxlength="20" placeholder="12345"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
</div>
</div>
<!-- Payment Options -->
<div class="field-group">
<h4>⚙️ Payment Options</h4>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label for="language" class="block text-sm font-medium text-gray-700 mb-1">
Payment Page Language
<span class="tooltip text-gray-400" data-tooltip="Language for the payment page interface">ⓘ</span>
</label>
<select id="language" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
<option value="ar">العربية (Arabic)</option>
<option value="en" selected>English</option>
</select>
</div>
<div>
<label for="sms_language" class="block text-sm font-medium text-gray-700 mb-1">
SMS Language
<span class="tooltip text-gray-400" data-tooltip="Language for SMS notifications (independent of page language)">ⓘ</span>
</label>
<select id="sms_language" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
<option value="ar" selected>العربية (Arabic)</option>
<option value="en">English</option>
</select>
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="flex items-center">
<input type="checkbox" id="send_sms"
class="w-4 h-4 text-gray-600 border-gray-300 focus:ring-gray-500 focus:ring-2">
<span class="ml-2 text-sm text-gray-700">
Send SMS notifications
<span class="tooltip text-gray-400" data-tooltip="Send SMS to customer about payment status">ⓘ</span>
</span>
</label>
</div>
<div>
<label class="flex items-center">
<input type="checkbox" id="passkey_enabled"
class="w-4 h-4 text-gray-600 border-gray-300 focus:ring-gray-500 focus:ring-2">
<span class="ml-2 text-sm text-gray-700">
Enable passkey authentication
<span class="tooltip text-gray-400" data-tooltip="Allow biometric/security key authentication">ⓘ</span>
</span>
</label>
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="flex items-center">
<input type="checkbox" id="only_show_bank_installments"
class="w-4 h-4 text-gray-600 border-gray-300 focus:ring-gray-500 focus:ring-2">
<span class="ml-2 text-sm text-gray-700">
Only show bank installments
<span class="tooltip text-gray-400" data-tooltip="Show only installment payment options">ⓘ</span>
</span>
</label>
</div>
<div>
<label class="flex items-center">
<input type="checkbox" id="only_show_pay_in_full"
class="w-4 h-4 text-gray-600 border-gray-300 focus:ring-gray-500 focus:ring-2">
<span class="ml-2 text-sm text-gray-700">
Only show full payment
<span class="tooltip text-gray-400" data-tooltip="Show only full payment options (no installments)">ⓘ</span>
</span>
</label>
</div>
</div>
</div>
<!-- Integration Settings -->
<div class="field-group">
<h4>🔧 Integration Settings</h4>
<div>
<label for="callback_url" class="block text-sm font-medium text-gray-700 mb-1">
Webhook URL
<span class="tooltip text-gray-400" data-tooltip="Your HTTPS endpoint to receive payment status updates">ⓘ</span>
</label>
<input type="url" id="callback_url" placeholder="https://yourstore.com/webhook/payment-complete"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="branch" class="block text-sm font-medium text-gray-700 mb-1">
Branch Identifier
<span class="tooltip text-gray-400" data-tooltip="Optional branch ID for multi-location businesses">ⓘ</span>
</label>
<input type="text" id="branch" maxlength="50" placeholder="RIYADH-001"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="metadata" class="block text-sm font-medium text-gray-700 mb-1">
Metadata (JSON)
<span class="tooltip text-gray-400" data-tooltip="Custom data associated with this payment (returned in webhooks)">ⓘ</span>
</label>
<textarea id="metadata" rows="3"
placeholder='{"order_id": "ORD-2024-001", "customer_id": "CUST-12345"}'
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500 font-mono text-sm"></textarea>
</div>
</div>
<div class="pt-4 border-t border-gray-200">
<button type="submit" id="createBtn"
class="bg-gray-900 text-white px-8 py-3 rounded-md hover:bg-gray-800 disabled:opacity-50 disabled:cursor-not-allowed transition-colors">
<span id="createLoader" class="inline-block w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin mr-2" style="display: none;"></span>
Create Payment Link
</button>
<button type="button" onclick="fillExampleData()"
class="ml-3 bg-gray-100 text-gray-700 px-6 py-3 rounded-md hover:bg-gray-200 transition-colors">
Fill Example Data
</button>
</div>
</form>
<div id="createResult"></div>
</div>
</div>
<!-- Payment Details Tab -->
<div id="details" class="tab-content hidden">
<div class="bg-white rounded-lg border border-gray-200 p-6">
<h2 class="text-xl font-medium text-gray-900 mb-6">🔍 Payment Link Details</h2>
<div class="mb-4">
<label for="paymentIdDetails" class="block text-sm font-medium text-gray-700 mb-1">Payment Link ID</label>
<input type="text" id="paymentIdDetails" placeholder="Enter payment link ID (UUID)"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
<div>
<label for="detailsPage" class="block text-sm font-medium text-gray-700 mb-1">
Page
<span class="tooltip text-gray-400" data-tooltip="Page number for transaction pagination">ⓘ</span>
</label>
<input type="number" id="detailsPage" value="1" min="1"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="detailsPageSize" class="block text-sm font-medium text-gray-700 mb-1">
Page Size
<span class="tooltip text-gray-400" data-tooltip="Number of transactions per page">ⓘ</span>
</label>
<select id="detailsPageSize" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
<option value="5">5</option>
<option value="10" selected>10</option>
<option value="25">25</option>
</select>
</div>
</div>
<button onclick="getPaymentDetails()" id="detailsBtn"
class="bg-gray-900 text-white px-6 py-2 rounded-md hover:bg-gray-800 disabled:opacity-50 disabled:cursor-not-allowed transition-colors">
<span id="detailsLoader" class="inline-block w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin mr-2" style="display: none;"></span>
Get Details
</button>
<div id="detailsResult"></div>
</div>
</div>
<!-- List Payments Tab -->
<div id="list" class="tab-content hidden">
<div class="bg-white rounded-lg border border-gray-200 p-6">
<h2 class="text-xl font-medium text-gray-900 mb-6">📋 Payment Links List</h2>
<div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-4">
<div>
<label for="search" class="block text-sm font-medium text-gray-700 mb-1">
Search
<span class="tooltip text-gray-400" data-tooltip="Search in title, description, customer name, and email">ⓘ</span>
</label>
<input type="text" id="search" maxlength="100" placeholder="Search payments..."
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="page" class="block text-sm font-medium text-gray-700 mb-1">Page</label>
<input type="number" id="page" value="1" min="1"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="pageSize" class="block text-sm font-medium text-gray-700 mb-1">Page Size</label>
<select id="pageSize" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
<option value="5">5</option>
<option value="10" selected>10</option>
<option value="20">20</option>
<option value="50">50</option>
</select>
</div>
</div>
<div class="grid grid-cols-2 gap-4 mb-6">
<div>
<label for="sortBy" class="block text-sm font-medium text-gray-700 mb-1">Sort By</label>
<select id="sortBy" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
<option value="created_at" selected>Created Date</option>
<option value="amount">Amount</option>
<option value="title">Title</option>
<option value="status">Status</option>
</select>
</div>
<div>
<label for="sortOrder" class="block text-sm font-medium text-gray-700 mb-1">Sort Order</label>
<select id="sortOrder" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
<option value="desc" selected>Newest First</option>
<option value="asc">Oldest First</option>
</select>
</div>
</div>
<button onclick="listPayments()" id="listBtn"
class="bg-gray-900 text-white px-6 py-2 rounded-md hover:bg-gray-800 disabled:opacity-50 disabled:cursor-not-allowed transition-colors">
<span id="listLoader" class="inline-block w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin mr-2" style="display: none;"></span>
Load Payments
</button>
<div id="listResult"></div>
</div>
</div>
<!-- Check Status Tab -->
<div id="status" class="tab-content hidden">
<div class="bg-white rounded-lg border border-gray-200 p-6">
<h2 class="text-xl font-medium text-gray-900 mb-6">⚡ Payment Status Check</h2>
<div class="mb-4">
<label for="paymentIdStatus" class="block text-sm font-medium text-gray-700 mb-1">Payment Link ID</label>
<input type="text" id="paymentIdStatus" placeholder="Enter payment link ID"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<button onclick="checkPaymentStatus()" id="statusBtn"
class="bg-gray-900 text-white px-6 py-2 rounded-md hover:bg-gray-800 disabled:opacity-50 disabled:cursor-not-allowed transition-colors">
<span id="statusLoader" class="inline-block w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin mr-2" style="display: none;"></span>
Check Status
</button>
<div id="statusResult"></div>
</div>
</div>
<!-- Resolve Payment Link Tab -->
<div id="resolve" class="tab-content hidden">
<div class="bg-white rounded-lg border border-gray-200 p-6">
<h2 class="text-xl font-medium text-gray-900 mb-6">🔧 Resolve Payment Link</h2>
<div class="mb-4 p-4 bg-yellow-50 border border-yellow-200 rounded-lg">
<h4 class="font-medium text-yellow-800 mb-2">⚠️ About the Resolve Endpoint</h4>
<p class="text-sm text-yellow-700">
This endpoint is primarily for internal use by the Amwal payment system when a customer clicks on a payment link.
It requires proper Origin/Referer headers for security validation. Use this for testing payment link validity or building custom payment interfaces.
</p>
</div>
<div class="mb-4">
<label for="paymentIdResolve" class="block text-sm font-medium text-gray-700 mb-1">
Payment Link ID
<span class="tooltip text-gray-400" data-tooltip="The payment link ID to resolve">ⓘ</span>
</label>
<input type="text" id="paymentIdResolve" placeholder="Enter payment link ID"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<button onclick="resolvePaymentLink()" id="resolveBtn"
class="bg-gray-900 text-white px-6 py-2 rounded-md hover:bg-gray-800 disabled:opacity-50 disabled:cursor-not-allowed transition-colors">
<span id="resolveLoader" class="inline-block w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin mr-2" style="display: none;"></span>
Resolve Payment Link
</button>
<div id="resolveResult"></div>
</div>
</div>
<!-- Process Refund Tab -->
<div id="refund" class="tab-content hidden">
<div class="bg-white rounded-lg border border-gray-200 p-6">
<h2 class="text-xl font-medium text-gray-900 mb-6">💸 Process Transaction Refund</h2>
<div class="mb-4 p-4 bg-orange-50 border border-orange-200 rounded-lg">
<h4 class="font-medium text-orange-800 mb-2">⚠️ Important Refund Information</h4>
<ul class="text-sm text-orange-700 space-y-1">
<li>• Refunds can only be processed on completed transactions</li>
<li>• Partial refunds are supported - enter amount less than original transaction</li>
<li>• Full refunds should match the original transaction amount</li>
<li>• Refund processing time varies by payment method</li>
<li>• You'll receive a refund reference number for tracking</li>
</ul>
</div>
<form id="refundForm" class="space-y-4">
<div>
<label for="transactionIdRefund" class="block text-sm font-medium text-gray-700 mb-1">
Transaction ID *
<span class="tooltip text-gray-400" data-tooltip="The transaction ID from a completed payment that you want to refund">ⓘ</span>
</label>
<input type="text" id="transactionIdRefund" required placeholder="Enter transaction ID (UUID)"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div>
<label for="refundAmount" class="block text-sm font-medium text-gray-700 mb-1">
Refund Amount *
<span class="tooltip text-gray-400" data-tooltip="Amount to refund in SAR. Can be partial or full amount of original transaction">ⓘ</span>
</label>
<input type="number" id="refundAmount" required step="0.01" min="0.01" placeholder="299.99"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-1 focus:ring-gray-500 focus:border-gray-500">
</div>
<div class="pt-4 border-t border-gray-200">
<button type="submit" id="refundBtn"
class="bg-red-600 text-white px-8 py-3 rounded-md hover:bg-red-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors">
<span id="refundLoader" class="inline-block w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin mr-2" style="display: none;"></span>
Process Refund
</button>
<button type="button" onclick="fillRefundExample()"
class="ml-3 bg-gray-100 text-gray-700 px-6 py-3 rounded-md hover:bg-gray-200 transition-colors">
Fill Example
</button>
</div>
</form>
<div id="refundResult"></div>
</div>
</div>
</div>
<script>
// PHP proxy URL (same file with ?proxy parameter)
const PROXY_URL = '<?php echo $_SERVER['PHP_SELF']; ?>?proxy=1';
// X-Amwal-Key management
function setAmwalKey(type) {
const apiKey = document.getElementById('apiKey').value;
const amwalKeyField = document.getElementById('amwalKey');
if (!apiKey) {
alert('Please enter an API Key first');
return;
}
if (type === 'prod') {
amwalKeyField.value = `prod-amwal-5563d261-8545-418b-b122-8767a2e59931`;
} else if (type === 'sandbox') {
amwalKeyField.value = `sandbox-amwal-b87482f2-55da-486f-9ec5-e2ceb47a0333`;
}
updateAmwalKeyStatus();
}
function clearAmwalKey() {
document.getElementById('amwalKey').value = '';
updateAmwalKeyStatus();
}
function updateAmwalKeyPreview() {
updateAmwalKeyStatus();
}
function updateAmwalKeyStatus() {
const amwalKey = document.getElementById('amwalKey').value;
const environmentInfo = document.getElementById('environmentInfo');
if (!amwalKey) {
environmentInfo.className = 'mt-4 p-3 bg-gray-50 border border-gray-200 rounded-lg';
environmentInfo.innerHTML = `
<div class="flex items-center gap-2">
<span class="text-lg">🚫</span>
<span class="text-sm font-medium text-gray-800">No Environment Header</span>
</div>
<p class="text-xs text-gray-700 mt-1">X-Amwal-Key header will not be sent with requests. May use default production behavior.</p>
`;
} else if (amwalKey.includes('sandbox')) {
environmentInfo.className = 'mt-4 p-3 bg-yellow-50 border border-yellow-200 rounded-lg';
environmentInfo.innerHTML = `
<div class="flex items-center gap-2">
<span class="text-lg">🟡</span>
<span class="text-sm font-medium text-yellow-800">Sandbox Environment</span>
</div>
<p class="text-xs text-yellow-700 mt-1">Using sandbox mode. No real payments will be processed. Perfect for testing.</p>
`;
} else if (amwalKey.includes('prod')) {
environmentInfo.className = 'mt-4 p-3 bg-green-50 border border-green-200 rounded-lg';
environmentInfo.innerHTML = `
<div class="flex items-center gap-2">
<span class="text-lg">🟢</span>
<span class="text-sm font-medium text-green-800">Production Environment</span>
</div>
<p class="text-xs text-green-700 mt-1">Using production mode. Real payments will be processed.</p>
`;
} else {
environmentInfo.className = 'mt-4 p-3 bg-blue-50 border border-blue-200 rounded-lg';
environmentInfo.innerHTML = `
<div class="flex items-center gap-2">
<span class="text-lg">🔧</span>
<span class="text-sm font-medium text-blue-800">Custom Environment Key</span>
</div>
<p class="text-xs text-blue-700 mt-1">Using custom X-Amwal-Key value: <code class="bg-blue-100 px-1 rounded text-xs">${amwalKey}</code></p>
`;
}
}
// Tab management
function showTab(tabName) {
// Hide all tab contents
document.querySelectorAll('.tab-content').forEach(content => {
content.classList.add('hidden');
content.classList.remove('animate-fadeIn');
});
// Remove active class from all tabs
document.querySelectorAll('.tab').forEach(tab => {
tab.classList.remove('border-gray-900', 'text-gray-900');
tab.classList.add('border-transparent', 'text-gray-500', 'hover:text-gray-700');
});
// Show selected tab content
const selectedTab = document.getElementById(tabName);
selectedTab.classList.remove('hidden');
selectedTab.classList.add('animate-fadeIn');
// Add active class to clicked tab
event.target.classList.remove('border-transparent', 'text-gray-500', 'hover:text-gray-700');
event.target.classList.add('border-gray-900', 'text-gray-900');
}
// API Helper function that uses PHP proxy with enhanced error handling
async function makeAPICall(endpoint, method = 'POST', data = null) {
const apiKey = document.getElementById('apiKey').value;
const storeId = document.getElementById('storeId').value;
const originHeader = document.getElementById('originHeader').value;
const amwalKey = document.getElementById('amwalKey').value.trim();
if (!apiKey || !storeId) {
throw new Error('Please configure API Key and Store ID');
}
// Prepare data for PHP proxy
const proxyData = {
apiKey,
storeId,
originHeader,
amwalKey,
endpoint,
method,
data
};
try {
const response = await fetch(PROXY_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(proxyData)
});
const result = await response.json();
if (response.status === 429) {
throw new Error('Rate limit exceeded (100 requests/hour). Please wait before making more requests.');
}
if (response.status === 400) {
throw new Error(result.error || 'Bad request - please check your input data');
}
if (response.status === 502) {
throw new Error('Service temporarily unavailable. Please try again later.');
}
if (!response.ok) {
// Enhanced error message extraction
let errorMessage = '';
if (result.detail) {
errorMessage = result.detail;
} else if (result.error) {
errorMessage = result.error;
} else if (result.message) {
errorMessage = result.message;
} else if (typeof result === 'string') {
errorMessage = result;
} else {
errorMessage = `HTTP ${response.status}: ${response.statusText}`;
}
// Create an error with the full response data for debugging
const error = new Error(errorMessage);
error.response = result;
error.status = response.status;
throw error;
}
return result;
} catch (fetchError) {
if (fetchError.name === 'TypeError') {
throw new Error('Network error - please check your internet connection');
}
throw fetchError;
}
}
// Generate curl command (for display purposes)
function generateCurlCommand(endpoint, method = 'POST', data = null) {
const apiKey = document.getElementById('apiKey').value;