@zahlen/checkout 0.1.0

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.
package/index.esm.js ADDED
@@ -0,0 +1,958 @@
1
+ /******************************************************************************
2
+ Copyright (c) Microsoft Corporation.
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
+ ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ function _instanceof(left, right) {
15
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
16
+ return !!right[Symbol.hasInstance](left);
17
+ } else {
18
+ return left instanceof right;
19
+ }
20
+ }
21
+ function __awaiter(thisArg, _arguments, P, generator) {
22
+ function adopt(value) {
23
+ return _instanceof(value, P) ? value : new P(function(resolve) {
24
+ resolve(value);
25
+ });
26
+ }
27
+ return new (P || (P = Promise))(function(resolve, reject) {
28
+ function fulfilled(value) {
29
+ try {
30
+ step(generator.next(value));
31
+ } catch (e) {
32
+ reject(e);
33
+ }
34
+ }
35
+ function rejected(value) {
36
+ try {
37
+ step(generator["throw"](value));
38
+ } catch (e) {
39
+ reject(e);
40
+ }
41
+ }
42
+ function step(result) {
43
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
44
+ }
45
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
46
+ });
47
+ }
48
+ typeof SuppressedError === "function" ? SuppressedError : function _SuppressedError(error, suppressed, message) {
49
+ var e = new Error(message);
50
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
51
+ };
52
+
53
+ /**
54
+ * Card validation utilities for Zahlen Checkout
55
+ */ const CARD_BRANDS = [
56
+ {
57
+ name: 'Visa',
58
+ code: 'visa',
59
+ pattern: /^4/,
60
+ lengths: [
61
+ 13,
62
+ 16,
63
+ 19
64
+ ],
65
+ cvvLength: 3
66
+ },
67
+ {
68
+ name: 'Mastercard',
69
+ code: 'mastercard',
70
+ pattern: /^(5[1-5]|2[2-7])/,
71
+ lengths: [
72
+ 16
73
+ ],
74
+ cvvLength: 3
75
+ },
76
+ {
77
+ name: 'Verve',
78
+ code: 'verve',
79
+ pattern: /^(506[0-9]|507[0-9]|6500)/,
80
+ lengths: [
81
+ 16,
82
+ 18,
83
+ 19
84
+ ],
85
+ cvvLength: 3
86
+ },
87
+ {
88
+ name: 'American Express',
89
+ code: 'amex',
90
+ pattern: /^3[47]/,
91
+ lengths: [
92
+ 15
93
+ ],
94
+ cvvLength: 4
95
+ },
96
+ {
97
+ name: 'Discover',
98
+ code: 'discover',
99
+ pattern: /^(6011|65|64[4-9])/,
100
+ lengths: [
101
+ 16,
102
+ 19
103
+ ],
104
+ cvvLength: 3
105
+ }
106
+ ];
107
+ /**
108
+ * Detect card brand from card number
109
+ */ function detectCardBrand(cardNumber) {
110
+ const cleaned = cardNumber.replace(/\s/g, '');
111
+ for (const brand of CARD_BRANDS){
112
+ if (brand.pattern.test(cleaned)) {
113
+ return brand;
114
+ }
115
+ }
116
+ return null;
117
+ }
118
+ /**
119
+ * Luhn algorithm for card validation
120
+ */ function luhnCheck(cardNumber) {
121
+ const cleaned = cardNumber.replace(/\s/g, '');
122
+ if (!/^\d+$/.test(cleaned)) return false;
123
+ let sum = 0;
124
+ let isEven = false;
125
+ for(let i = cleaned.length - 1; i >= 0; i--){
126
+ let digit = parseInt(cleaned[i], 10);
127
+ if (isEven) {
128
+ digit *= 2;
129
+ if (digit > 9) {
130
+ digit -= 9;
131
+ }
132
+ }
133
+ sum += digit;
134
+ isEven = !isEven;
135
+ }
136
+ return sum % 10 === 0;
137
+ }
138
+ /**
139
+ * Validate card number
140
+ */ function validateCardNumber(cardNumber) {
141
+ const cleaned = cardNumber.replace(/\s/g, '');
142
+ if (!cleaned) {
143
+ return {
144
+ valid: false,
145
+ error: 'Card number is required'
146
+ };
147
+ }
148
+ if (!/^\d+$/.test(cleaned)) {
149
+ return {
150
+ valid: false,
151
+ error: 'Invalid card number'
152
+ };
153
+ }
154
+ const brand = detectCardBrand(cleaned);
155
+ if (!brand) {
156
+ return {
157
+ valid: false,
158
+ error: 'Unsupported card type'
159
+ };
160
+ }
161
+ if (!brand.lengths.includes(cleaned.length)) {
162
+ return {
163
+ valid: false,
164
+ error: 'Invalid card number length'
165
+ };
166
+ }
167
+ if (!luhnCheck(cleaned)) {
168
+ return {
169
+ valid: false,
170
+ error: 'Invalid card number'
171
+ };
172
+ }
173
+ return {
174
+ valid: true
175
+ };
176
+ }
177
+ /**
178
+ * Validate expiry date
179
+ */ function validateExpiry(expiry) {
180
+ const cleaned = expiry.replace(/\s/g, '');
181
+ if (!cleaned) {
182
+ return {
183
+ valid: false,
184
+ error: 'Expiry date is required'
185
+ };
186
+ }
187
+ const match = cleaned.match(/^(\d{2})\/(\d{2})$/);
188
+ if (!match) {
189
+ return {
190
+ valid: false,
191
+ error: 'Invalid format (MM/YY)'
192
+ };
193
+ }
194
+ const month = parseInt(match[1], 10);
195
+ const year = parseInt(match[2], 10) + 2000;
196
+ if (month < 1 || month > 12) {
197
+ return {
198
+ valid: false,
199
+ error: 'Invalid month'
200
+ };
201
+ }
202
+ const now = new Date();
203
+ const currentYear = now.getFullYear();
204
+ const currentMonth = now.getMonth() + 1;
205
+ if (year < currentYear || year === currentYear && month < currentMonth) {
206
+ return {
207
+ valid: false,
208
+ error: 'Card has expired'
209
+ };
210
+ }
211
+ if (year > currentYear + 20) {
212
+ return {
213
+ valid: false,
214
+ error: 'Invalid expiry year'
215
+ };
216
+ }
217
+ return {
218
+ valid: true
219
+ };
220
+ }
221
+ /**
222
+ * Validate CVV
223
+ */ function validateCVV(cvv, cardNumber) {
224
+ const cleaned = cvv.replace(/\s/g, '');
225
+ if (!cleaned) {
226
+ return {
227
+ valid: false,
228
+ error: 'CVV is required'
229
+ };
230
+ }
231
+ if (!/^\d+$/.test(cleaned)) {
232
+ return {
233
+ valid: false,
234
+ error: 'Invalid CVV'
235
+ };
236
+ }
237
+ const brand = cardNumber ? detectCardBrand(cardNumber) : null;
238
+ const expectedLength = (brand === null || brand === void 0 ? void 0 : brand.cvvLength) || 3;
239
+ if (cleaned.length !== expectedLength && cleaned.length !== 3 && cleaned.length !== 4) {
240
+ return {
241
+ valid: false,
242
+ error: `CVV must be ${expectedLength} digits`
243
+ };
244
+ }
245
+ return {
246
+ valid: true
247
+ };
248
+ }
249
+
250
+ /**
251
+ * Input formatting utilities for Zahlen Checkout
252
+ */ /**
253
+ * Format card number with spaces every 4 digits
254
+ */ function formatCardNumber(value) {
255
+ const cleaned = value.replace(/\D/g, '');
256
+ const groups = cleaned.match(/.{1,4}/g) || [];
257
+ return groups.join(' ').slice(0, 23); // Max: 19 digits + 4 spaces
258
+ }
259
+ /**
260
+ * Format expiry date as MM/YY
261
+ */ function formatExpiry(value) {
262
+ const cleaned = value.replace(/\D/g, '');
263
+ if (cleaned.length === 0) return '';
264
+ if (cleaned.length === 1) {
265
+ // If first digit is greater than 1, prefix with 0
266
+ return parseInt(cleaned) > 1 ? `0${cleaned}` : cleaned;
267
+ }
268
+ if (cleaned.length === 2) {
269
+ const month = parseInt(cleaned);
270
+ if (month > 12) return '12';
271
+ if (month === 0) return '01';
272
+ return cleaned;
273
+ }
274
+ const month = cleaned.slice(0, 2);
275
+ const year = cleaned.slice(2, 4);
276
+ return `${month}/${year}`;
277
+ }
278
+ /**
279
+ * Format CVV (numbers only, max 4 digits)
280
+ */ function formatCVV(value) {
281
+ return value.replace(/\D/g, '').slice(0, 4);
282
+ }
283
+ /**
284
+ * Format currency amount
285
+ */ function formatAmount(amount, currency) {
286
+ const formatter = new Intl.NumberFormat('en-US', {
287
+ style: 'currency',
288
+ currency: currency,
289
+ minimumFractionDigits: 2
290
+ });
291
+ // Convert from smallest unit (cents/kobo) to main unit
292
+ return formatter.format(amount / 100);
293
+ }
294
+ /**
295
+ * Get currency symbol
296
+ */ function getCurrencySymbol(currency) {
297
+ const symbols = {
298
+ NGN: '₦',
299
+ USD: '$',
300
+ EUR: '€',
301
+ GBP: '£',
302
+ GHS: '₵',
303
+ KES: 'KSh',
304
+ ZAR: 'R'
305
+ };
306
+ return symbols[currency.toUpperCase()] || currency;
307
+ }
308
+
309
+ // SVG Icons
310
+ const ICONS = {
311
+ close: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
312
+ <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
313
+ </svg>`,
314
+ lock: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
315
+ <path stroke-linecap="round" stroke-linejoin="round" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
316
+ </svg>`,
317
+ check: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="3">
318
+ <path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
319
+ </svg>`,
320
+ error: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 20" stroke="currentColor" stroke-width="2">
321
+ <path stroke-linecap="round" stroke-linejoin="round" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" />
322
+ </svg>`
323
+ };
324
+ // Card brand logos (simple text for now)
325
+ const CARD_BRAND_DISPLAY = {
326
+ visa: 'VISA',
327
+ mastercard: 'MC',
328
+ amex: 'AMEX',
329
+ verve: 'VERVE',
330
+ discover: 'DISC'
331
+ };
332
+ class ZahlenModal {
333
+ /**
334
+ * Open the checkout modal
335
+ */ open() {
336
+ this.injectStyles();
337
+ this.render();
338
+ this.attachEventListeners();
339
+ // Trigger open animation
340
+ requestAnimationFrame(()=>{
341
+ var _a;
342
+ (_a = this.overlay) === null || _a === void 0 ? void 0 : _a.classList.add('zahlen-visible');
343
+ });
344
+ }
345
+ /**
346
+ * Close the checkout modal
347
+ */ close() {
348
+ var _a;
349
+ (_a = this.overlay) === null || _a === void 0 ? void 0 : _a.classList.remove('zahlen-visible');
350
+ setTimeout(()=>{
351
+ var _a, _b, _c;
352
+ (_a = this.container) === null || _a === void 0 ? void 0 : _a.remove();
353
+ this.container = null;
354
+ this.overlay = null;
355
+ (_c = (_b = this.options).onClose) === null || _c === void 0 ? void 0 : _c.call(_b);
356
+ }, 300);
357
+ }
358
+ /**
359
+ * Inject styles if not already present
360
+ */ injectStyles() {
361
+ if (document.getElementById('zahlen-checkout-styles')) return;
362
+ const style = document.createElement('style');
363
+ style.id = 'zahlen-checkout-styles';
364
+ style.textContent = this.getStyles();
365
+ document.head.appendChild(style);
366
+ }
367
+ /**
368
+ * Render the modal HTML
369
+ */ render() {
370
+ this.container = document.createElement('div');
371
+ this.container.className = 'zahlen-checkout';
372
+ this.container.innerHTML = this.getModalHTML();
373
+ document.body.appendChild(this.container);
374
+ this.overlay = this.container.querySelector('.zahlen-overlay');
375
+ }
376
+ /**
377
+ * Get modal HTML
378
+ */ getModalHTML() {
379
+ const formattedAmount = formatAmount(this.options.amount, this.options.currency);
380
+ return `
381
+ <div class="zahlen-overlay">
382
+ <div class="zahlen-modal" role="dialog" aria-modal="true" aria-labelledby="zahlen-title">
383
+ <div class="zahlen-header">
384
+ <div class="zahlen-header-left">
385
+ <div class="zahlen-logo">Z</div>
386
+ <div>
387
+ <div class="zahlen-amount" id="zahlen-title">Pay ${formattedAmount}</div>
388
+ ${this.options.description ? `<div style="font-size: 0.875rem; color: var(--zahlen-text-muted);">${this.options.description}</div>` : ''}
389
+ </div>
390
+ </div>
391
+ <button class="zahlen-close" aria-label="Close" data-action="close">
392
+ ${ICONS.close}
393
+ </button>
394
+ </div>
395
+
396
+ <div class="zahlen-body" id="zahlen-form-container">
397
+ <form id="zahlen-payment-form" novalidate>
398
+ <div class="zahlen-form-group">
399
+ <label class="zahlen-label">
400
+ <span class="zahlen-label-icon">💳</span>
401
+ Card Number
402
+ </label>
403
+ <div class="zahlen-card-input-wrapper">
404
+ <input
405
+ type="text"
406
+ class="zahlen-input"
407
+ id="zahlen-card-number"
408
+ name="cardNumber"
409
+ placeholder="1234 5678 9012 3456"
410
+ autocomplete="cc-number"
411
+ inputmode="numeric"
412
+ />
413
+ <div class="zahlen-card-brand" id="zahlen-card-brand"></div>
414
+ </div>
415
+ <div class="zahlen-error-message" id="zahlen-card-error" style="display: none;"></div>
416
+ </div>
417
+
418
+ <div class="zahlen-row">
419
+ <div class="zahlen-form-group">
420
+ <label class="zahlen-label">
421
+ <span class="zahlen-label-icon">📅</span>
422
+ Expiry
423
+ </label>
424
+ <input
425
+ type="text"
426
+ class="zahlen-input"
427
+ id="zahlen-expiry"
428
+ name="expiry"
429
+ placeholder="MM/YY"
430
+ autocomplete="cc-exp"
431
+ inputmode="numeric"
432
+ maxlength="5"
433
+ />
434
+ <div class="zahlen-error-message" id="zahlen-expiry-error" style="display: none;"></div>
435
+ </div>
436
+
437
+ <div class="zahlen-form-group">
438
+ <label class="zahlen-label">
439
+ <span class="zahlen-label-icon">🔒</span>
440
+ CVV
441
+ </label>
442
+ <input
443
+ type="text"
444
+ class="zahlen-input"
445
+ id="zahlen-cvv"
446
+ name="cvv"
447
+ placeholder="•••"
448
+ autocomplete="cc-csc"
449
+ inputmode="numeric"
450
+ maxlength="4"
451
+ />
452
+ <div class="zahlen-error-message" id="zahlen-cvv-error" style="display: none;"></div>
453
+ </div>
454
+ </div>
455
+
456
+ <button type="submit" class="zahlen-submit-btn" id="zahlen-submit">
457
+ <span>✨ Pay Now ${formattedAmount}</span>
458
+ </button>
459
+ </form>
460
+ </div>
461
+
462
+ <div class="zahlen-footer">
463
+ <div class="zahlen-powered-by">
464
+ ${ICONS.lock}
465
+ <span>Secured by <strong>Zahlen</strong></span>
466
+ </div>
467
+ </div>
468
+ </div>
469
+ </div>
470
+ `;
471
+ }
472
+ /**
473
+ * Render success view
474
+ */ renderSuccessView() {
475
+ var _a;
476
+ const formContainer = (_a = this.container) === null || _a === void 0 ? void 0 : _a.querySelector('#zahlen-form-container');
477
+ if (!formContainer) return;
478
+ const formattedAmount = formatAmount(this.options.amount, this.options.currency);
479
+ formContainer.innerHTML = `
480
+ <div class="zahlen-success-view">
481
+ <div class="zahlen-success-icon">
482
+ ${ICONS.check}
483
+ </div>
484
+ <h2 class="zahlen-success-title">Payment Successful!</h2>
485
+ <p class="zahlen-success-message">Your payment of ${formattedAmount} has been processed.</p>
486
+ <button class="zahlen-success-btn" data-action="close">Done</button>
487
+ </div>
488
+ `;
489
+ // Re-attach close listener
490
+ const closeBtn = formContainer.querySelector('[data-action="close"]');
491
+ closeBtn === null || closeBtn === void 0 ? void 0 : closeBtn.addEventListener('click', ()=>this.close());
492
+ }
493
+ /**
494
+ * Attach event listeners
495
+ */ attachEventListeners() {
496
+ var _a, _b, _c, _d, _e, _f;
497
+ // Close button
498
+ const closeBtn = (_a = this.container) === null || _a === void 0 ? void 0 : _a.querySelector('[data-action="close"]');
499
+ closeBtn === null || closeBtn === void 0 ? void 0 : closeBtn.addEventListener('click', ()=>this.close());
500
+ // Close on overlay click
501
+ (_b = this.overlay) === null || _b === void 0 ? void 0 : _b.addEventListener('click', (e)=>{
502
+ if (e.target === this.overlay) {
503
+ this.close();
504
+ }
505
+ });
506
+ // Close on escape key
507
+ document.addEventListener('keydown', this.handleEscape);
508
+ // Card number input
509
+ const cardInput = (_c = this.container) === null || _c === void 0 ? void 0 : _c.querySelector('#zahlen-card-number');
510
+ cardInput === null || cardInput === void 0 ? void 0 : cardInput.addEventListener('input', (e)=>this.handleCardInput(e));
511
+ cardInput === null || cardInput === void 0 ? void 0 : cardInput.addEventListener('blur', ()=>this.validateField('cardNumber'));
512
+ // Expiry input
513
+ const expiryInput = (_d = this.container) === null || _d === void 0 ? void 0 : _d.querySelector('#zahlen-expiry');
514
+ expiryInput === null || expiryInput === void 0 ? void 0 : expiryInput.addEventListener('input', (e)=>this.handleExpiryInput(e));
515
+ expiryInput === null || expiryInput === void 0 ? void 0 : expiryInput.addEventListener('blur', ()=>this.validateField('expiry'));
516
+ // CVV input
517
+ const cvvInput = (_e = this.container) === null || _e === void 0 ? void 0 : _e.querySelector('#zahlen-cvv');
518
+ cvvInput === null || cvvInput === void 0 ? void 0 : cvvInput.addEventListener('input', (e)=>this.handleCVVInput(e));
519
+ cvvInput === null || cvvInput === void 0 ? void 0 : cvvInput.addEventListener('blur', ()=>this.validateField('cvv'));
520
+ // Form submission
521
+ const form = (_f = this.container) === null || _f === void 0 ? void 0 : _f.querySelector('#zahlen-payment-form');
522
+ form === null || form === void 0 ? void 0 : form.addEventListener('submit', (e)=>this.handleSubmit(e));
523
+ }
524
+ /**
525
+ * Handle card number input
526
+ */ handleCardInput(e) {
527
+ var _a;
528
+ const input = e.target;
529
+ const formatted = formatCardNumber(input.value);
530
+ input.value = formatted;
531
+ this.state.cardNumber = formatted.replace(/\s/g, '');
532
+ // Update card brand
533
+ const brandEl = (_a = this.container) === null || _a === void 0 ? void 0 : _a.querySelector('#zahlen-card-brand');
534
+ const brand = detectCardBrand(this.state.cardNumber);
535
+ if (brandEl) {
536
+ brandEl.textContent = brand ? CARD_BRAND_DISPLAY[brand.code] || '' : '';
537
+ brandEl.style.opacity = brand ? '1' : '0';
538
+ }
539
+ // Clear error on input
540
+ this.clearError('cardNumber');
541
+ }
542
+ /**
543
+ * Handle expiry input
544
+ */ handleExpiryInput(e) {
545
+ const input = e.target;
546
+ const formatted = formatExpiry(input.value);
547
+ input.value = formatted;
548
+ this.state.expiry = formatted;
549
+ this.clearError('expiry');
550
+ }
551
+ /**
552
+ * Handle CVV input
553
+ */ handleCVVInput(e) {
554
+ const input = e.target;
555
+ const formatted = formatCVV(input.value);
556
+ input.value = formatted;
557
+ this.state.cvv = formatted;
558
+ this.clearError('cvv');
559
+ }
560
+ /**
561
+ * Validate a specific field
562
+ */ validateField(field) {
563
+ let result;
564
+ switch(field){
565
+ case 'cardNumber':
566
+ result = validateCardNumber(this.state.cardNumber);
567
+ break;
568
+ case 'expiry':
569
+ result = validateExpiry(this.state.expiry);
570
+ break;
571
+ case 'cvv':
572
+ result = validateCVV(this.state.cvv, this.state.cardNumber);
573
+ break;
574
+ default:
575
+ return true;
576
+ }
577
+ if (!result.valid) {
578
+ this.showError(field, result.error || 'Invalid');
579
+ return false;
580
+ }
581
+ this.clearError(field);
582
+ return true;
583
+ }
584
+ /**
585
+ * Show error for a field
586
+ */ showError(field, message) {
587
+ var _a, _b;
588
+ const errorEl = (_a = this.container) === null || _a === void 0 ? void 0 : _a.querySelector(`#zahlen-${field === 'cardNumber' ? 'card' : field}-error`);
589
+ const inputEl = (_b = this.container) === null || _b === void 0 ? void 0 : _b.querySelector(`#zahlen-${field === 'cardNumber' ? 'card-number' : field}`);
590
+ if (errorEl) {
591
+ errorEl.innerHTML = `${ICONS.error} ${message}`;
592
+ errorEl.style.display = 'flex';
593
+ }
594
+ inputEl === null || inputEl === void 0 ? void 0 : inputEl.classList.add('zahlen-error');
595
+ }
596
+ /**
597
+ * Clear error for a field
598
+ */ clearError(field) {
599
+ var _a, _b;
600
+ const errorEl = (_a = this.container) === null || _a === void 0 ? void 0 : _a.querySelector(`#zahlen-${field === 'cardNumber' ? 'card' : field}-error`);
601
+ const inputEl = (_b = this.container) === null || _b === void 0 ? void 0 : _b.querySelector(`#zahlen-${field === 'cardNumber' ? 'card-number' : field}`);
602
+ if (errorEl) {
603
+ errorEl.style.display = 'none';
604
+ }
605
+ inputEl === null || inputEl === void 0 ? void 0 : inputEl.classList.remove('zahlen-error');
606
+ }
607
+ /**
608
+ * Handle form submission
609
+ */ handleSubmit(e) {
610
+ return __awaiter(this, void 0, void 0, function*() {
611
+ e.preventDefault();
612
+ // Validate all fields
613
+ const isCardValid = this.validateField('cardNumber');
614
+ const isExpiryValid = this.validateField('expiry');
615
+ const isCVVValid = this.validateField('cvv');
616
+ if (!isCardValid || !isExpiryValid || !isCVVValid) {
617
+ return;
618
+ }
619
+ // Show loading state
620
+ this.setProcessing(true);
621
+ try {
622
+ // Simulate API call (replace with actual API integration)
623
+ const result = yield this.processPayment();
624
+ // Show success
625
+ this.state.isSuccess = true;
626
+ this.renderSuccessView();
627
+ // Callback
628
+ this.options.onSuccess(result);
629
+ } catch (error) {
630
+ this.setProcessing(false);
631
+ const paymentError = error;
632
+ this.options.onError(paymentError);
633
+ // Show general error
634
+ this.showError('cardNumber', paymentError.message || 'Payment failed. Please try again.');
635
+ }
636
+ });
637
+ }
638
+ /**
639
+ * Set processing state
640
+ */ setProcessing(isProcessing) {
641
+ var _a;
642
+ this.state.isProcessing = isProcessing;
643
+ const submitBtn = (_a = this.container) === null || _a === void 0 ? void 0 : _a.querySelector('#zahlen-submit');
644
+ if (submitBtn) {
645
+ submitBtn.disabled = isProcessing;
646
+ submitBtn.innerHTML = isProcessing ? '<div class="zahlen-spinner"></div><span>Processing...</span>' : `<span>✨ Pay Now ${formatAmount(this.options.amount, this.options.currency)}</span>`;
647
+ }
648
+ }
649
+ /**
650
+ * Process payment (placeholder - integrate with your backend)
651
+ */ processPayment() {
652
+ return __awaiter(this, void 0, void 0, function*() {
653
+ // Simulate network delay
654
+ yield new Promise((resolve)=>setTimeout(resolve, 2000));
655
+ // In production, send to your API endpoint
656
+ // const response = await fetch('/api/payments', {
657
+ // method: 'POST',
658
+ // body: JSON.stringify({
659
+ // cardNumber: this.state.cardNumber,
660
+ // expiry: this.state.expiry,
661
+ // cvv: this.state.cvv,
662
+ // amount: this.options.amount,
663
+ // currency: this.options.currency,
664
+ // }),
665
+ // });
666
+ // Simulated success response
667
+ return {
668
+ id: `pay_${Date.now()}`,
669
+ status: 'success',
670
+ amount: this.options.amount,
671
+ currency: this.options.currency,
672
+ timestamp: new Date().toISOString()
673
+ };
674
+ });
675
+ }
676
+ /**
677
+ * Get base styles
678
+ */ getStyles() {
679
+ // Return embedded CSS (in production, this would be imported)
680
+ return `
681
+ /* Zahlen Checkout Styles - Embedded */
682
+ :root {
683
+ /* Premium Color Palette - Violet/Indigo */
684
+ --zahlen-primary: #8B5CF6;
685
+ --zahlen-primary-hover: #7C3AED;
686
+ --zahlen-secondary: #6366F1;
687
+ --zahlen-accent: #C4B5FD;
688
+ --zahlen-glow: rgba(139, 92, 246, 0.35);
689
+ --zahlen-success: #10B981;
690
+ --zahlen-success-bg: rgba(16, 185, 129, 0.1);
691
+ --zahlen-error: #EF4444;
692
+
693
+ /* Dark Mode (Default) */
694
+ --zahlen-bg: #0C0C1D;
695
+ --zahlen-surface: rgba(255, 255, 255, 0.04);
696
+ --zahlen-surface-hover: rgba(255, 255, 255, 0.08);
697
+ --zahlen-border: rgba(255, 255, 255, 0.08);
698
+ --zahlen-text: #FFFFFF;
699
+ --zahlen-text-muted: #B4B4C7;
700
+ --zahlen-text-subtle: #6B7280;
701
+ --zahlen-input-bg: rgba(255, 255, 255, 0.06);
702
+
703
+ /* Effects */
704
+ --zahlen-backdrop-blur: blur(24px);
705
+ --zahlen-border-radius: 12px;
706
+ --zahlen-border-radius-lg: 16px;
707
+ --zahlen-border-radius-xl: 24px;
708
+ --zahlen-glow-shadow: 0 0 60px var(--zahlen-glow);
709
+ --zahlen-shadow: 0 25px 60px -15px rgba(0, 0, 0, 0.6);
710
+ --zahlen-transition-fast: 150ms ease;
711
+ --zahlen-transition: 300ms cubic-bezier(0.4, 0, 0.2, 1);
712
+ }
713
+
714
+ /* Light Mode Theme - using html.zahlen-light for higher specificity */
715
+ html.zahlen-light {
716
+ --zahlen-primary: #7C3AED;
717
+ --zahlen-primary-hover: #6D28D9;
718
+ --zahlen-secondary: #4F46E5;
719
+ --zahlen-glow: rgba(124, 58, 237, 0.2);
720
+
721
+ --zahlen-bg: #FFFFFF;
722
+ --zahlen-surface: #F8FAFC;
723
+ --zahlen-surface-hover: #F1F5F9;
724
+ --zahlen-border: #E2E8F0;
725
+ --zahlen-text: #0F172A;
726
+ --zahlen-text-muted: #475569;
727
+ --zahlen-text-subtle: #94A3B8;
728
+ --zahlen-input-bg: #F8FAFC;
729
+
730
+ --zahlen-glow-shadow: 0 0 40px rgba(124, 58, 237, 0.12);
731
+ --zahlen-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.15);
732
+ }
733
+
734
+ .zahlen-checkout * { box-sizing: border-box; margin: 0; padding: 0; }
735
+ .zahlen-checkout { font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; font-size: 1rem; color: var(--zahlen-text); line-height: 1.5; -webkit-font-smoothing: antialiased; }
736
+
737
+ .zahlen-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.5); backdrop-filter: var(--zahlen-backdrop-blur); -webkit-backdrop-filter: var(--zahlen-backdrop-blur); display: flex; align-items: center; justify-content: center; z-index: 999999; opacity: 0; visibility: hidden; transition: opacity var(--zahlen-transition), visibility var(--zahlen-transition); }
738
+ html.zahlen-light .zahlen-overlay { background: rgba(15, 23, 42, 0.4); }
739
+ .zahlen-overlay.zahlen-visible { opacity: 1; visibility: visible; }
740
+
741
+ .zahlen-modal { background: var(--zahlen-bg); border: 1px solid var(--zahlen-border); border-radius: var(--zahlen-border-radius-xl); width: 100%; max-width: 420px; max-height: 90vh; overflow: hidden; box-shadow: var(--zahlen-shadow), var(--zahlen-glow-shadow); transform: scale(0.95) translateY(20px); opacity: 0; transition: transform var(--zahlen-transition), opacity var(--zahlen-transition); }
742
+ .zahlen-overlay.zahlen-visible .zahlen-modal { transform: scale(1) translateY(0); opacity: 1; }
743
+
744
+ .zahlen-header { display: flex; align-items: center; justify-content: space-between; padding: 20px 24px; border-bottom: 1px solid var(--zahlen-border); background: var(--zahlen-surface); }
745
+ .zahlen-header-left { display: flex; align-items: center; gap: 12px; }
746
+ .zahlen-logo { width: 36px; height: 36px; border-radius: var(--zahlen-border-radius); background: linear-gradient(135deg, var(--zahlen-primary), var(--zahlen-secondary)); display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 1.25rem; color: white; box-shadow: 0 4px 12px rgba(139, 92, 246, 0.3); }
747
+ .zahlen-amount { font-size: 1.25rem; font-weight: 600; }
748
+ .zahlen-close { width: 36px; height: 36px; border: none; background: var(--zahlen-surface-hover); border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center; color: var(--zahlen-text-muted); transition: all var(--zahlen-transition-fast); }
749
+ .zahlen-close:hover { background: var(--zahlen-border); color: var(--zahlen-text); transform: rotate(90deg); }
750
+ .zahlen-close svg { width: 18px; height: 18px; }
751
+
752
+ .zahlen-body { padding: 24px; background: var(--zahlen-bg); }
753
+ .zahlen-form-group { margin-bottom: 20px; }
754
+ .zahlen-label { display: flex; align-items: center; gap: 8px; font-size: 0.875rem; font-weight: 500; color: var(--zahlen-text-muted); margin-bottom: 8px; }
755
+ .zahlen-label-icon { font-size: 1rem; }
756
+
757
+ .zahlen-input { width: 100%; padding: 14px 16px; background: var(--zahlen-input-bg); border: 1.5px solid var(--zahlen-border); border-radius: var(--zahlen-border-radius); color: var(--zahlen-text); font-size: 1rem; font-family: inherit; outline: none; transition: border-color var(--zahlen-transition-fast), box-shadow var(--zahlen-transition-fast), background var(--zahlen-transition-fast); }
758
+ .zahlen-input::placeholder { color: var(--zahlen-text-subtle); }
759
+ .zahlen-input:hover { border-color: var(--zahlen-text-subtle); }
760
+ .zahlen-input:focus { border-color: var(--zahlen-primary); box-shadow: 0 0 0 4px var(--zahlen-glow); background: var(--zahlen-bg); }
761
+ .zahlen-input.zahlen-error { border-color: var(--zahlen-error); box-shadow: 0 0 0 4px rgba(239, 68, 68, 0.15); }
762
+
763
+ .zahlen-card-input-wrapper { position: relative; }
764
+ .zahlen-card-brand { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); padding: 4px 8px; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, var(--zahlen-primary), var(--zahlen-secondary)); border-radius: 6px; font-size: 0.7rem; font-weight: 700; color: white; opacity: 0; transition: opacity var(--zahlen-transition-fast); letter-spacing: 0.5px; }
765
+ .zahlen-card-input-wrapper .zahlen-input { padding-right: 70px; }
766
+
767
+ .zahlen-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
768
+
769
+ .zahlen-error-message { display: flex; align-items: center; gap: 6px; font-size: 0.813rem; color: var(--zahlen-error); margin-top: 8px; animation: zahlen-shake 0.4s ease; }
770
+ .zahlen-error-message svg { width: 16px; height: 16px; flex-shrink: 0; }
771
+ @keyframes zahlen-shake { 0%, 100% { transform: translateX(0); } 20%, 60% { transform: translateX(-4px); } 40%, 80% { transform: translateX(4px); } }
772
+
773
+ .zahlen-submit-btn { width: 100%; padding: 16px 24px; background: linear-gradient(135deg, var(--zahlen-primary), var(--zahlen-secondary)); border: none; border-radius: var(--zahlen-border-radius); color: white; font-size: 1.063rem; font-weight: 600; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 10px; transition: all var(--zahlen-transition-fast); position: relative; overflow: hidden; box-shadow: 0 4px 15px rgba(139, 92, 246, 0.35); }
774
+ .zahlen-submit-btn:hover:not(:disabled) { transform: translateY(-2px); box-shadow: 0 8px 25px rgba(139, 92, 246, 0.45); }
775
+ .zahlen-submit-btn:active:not(:disabled) { transform: translateY(0); }
776
+ .zahlen-submit-btn:disabled { opacity: 0.6; cursor: not-allowed; box-shadow: none; }
777
+
778
+ .zahlen-spinner { width: 20px; height: 20px; border: 2.5px solid rgba(255, 255, 255, 0.3); border-top-color: white; border-radius: 50%; animation: zahlen-spin 0.8s linear infinite; }
779
+ @keyframes zahlen-spin { to { transform: rotate(360deg); } }
780
+
781
+ .zahlen-footer { text-align: center; padding: 16px 24px 20px; border-top: 1px solid var(--zahlen-border); background: var(--zahlen-surface); }
782
+ .zahlen-powered-by { font-size: 0.75rem; color: var(--zahlen-text-subtle); display: flex; align-items: center; justify-content: center; gap: 6px; }
783
+ .zahlen-powered-by svg { width: 14px; height: 14px; color: var(--zahlen-primary); }
784
+ .zahlen-powered-by strong { color: var(--zahlen-primary); font-weight: 600; }
785
+
786
+ .zahlen-success-view { text-align: center; padding: 40px 24px; }
787
+ .zahlen-success-icon { width: 80px; height: 80px; margin: 0 auto 24px; background: linear-gradient(135deg, var(--zahlen-success), #059669); border-radius: 50%; display: flex; align-items: center; justify-content: center; animation: zahlen-success-pop 0.5s ease; box-shadow: 0 8px 25px rgba(16, 185, 129, 0.35); }
788
+ @keyframes zahlen-success-pop { 0% { transform: scale(0); opacity: 0; } 50% { transform: scale(1.1); } 100% { transform: scale(1); opacity: 1; } }
789
+ .zahlen-success-icon svg { width: 40px; height: 40px; color: white; }
790
+ .zahlen-success-title { font-size: 1.5rem; font-weight: 700; margin-bottom: 8px; color: var(--zahlen-text); }
791
+ .zahlen-success-message { font-size: 1rem; color: var(--zahlen-text-muted); margin-bottom: 24px; }
792
+ .zahlen-success-btn { padding: 12px 32px; background: var(--zahlen-surface); border: 1.5px solid var(--zahlen-border); border-radius: var(--zahlen-border-radius); color: var(--zahlen-text); font-size: 1rem; font-weight: 500; cursor: pointer; transition: all var(--zahlen-transition-fast); }
793
+ .zahlen-success-btn:hover { background: var(--zahlen-surface-hover); border-color: var(--zahlen-primary); color: var(--zahlen-primary); }
794
+
795
+ @media (max-width: 480px) { .zahlen-modal { max-width: 100%; max-height: 100%; border-radius: 0; height: 100%; } .zahlen-body { padding: 20px; } }
796
+ `;
797
+ }
798
+ constructor(options){
799
+ this.container = null;
800
+ this.overlay = null;
801
+ this.handleEscape = (e)=>{
802
+ if (e.key === 'Escape') {
803
+ this.close();
804
+ document.removeEventListener('keydown', this.handleEscape);
805
+ }
806
+ };
807
+ this.options = options;
808
+ this.state = {
809
+ cardNumber: '',
810
+ expiry: '',
811
+ cvv: '',
812
+ errors: {},
813
+ isProcessing: false,
814
+ isSuccess: false
815
+ };
816
+ }
817
+ }
818
+
819
+ let ZahlenSDK = class ZahlenSDK {
820
+ /**
821
+ * Initialize Zahlen with your configuration
822
+ * @param config - Configuration object with API key and optional settings
823
+ */ init(config) {
824
+ if (!config.apiKey) {
825
+ console.error('[Zahlen] API key is required');
826
+ return;
827
+ }
828
+ this.config = Object.assign({
829
+ theme: 'dark',
830
+ locale: 'en-US'
831
+ }, config);
832
+ this.initialized = true;
833
+ // Apply theme
834
+ if (config.theme === 'auto') {
835
+ this.applyAutoTheme();
836
+ } else if (config.theme === 'light') {
837
+ document.documentElement.classList.add('zahlen-light');
838
+ }
839
+ // Apply custom styles
840
+ if (config.customStyles) {
841
+ this.applyCustomStyles(config.customStyles);
842
+ }
843
+ console.log('[Zahlen] Initialized successfully');
844
+ }
845
+ /**
846
+ * Open the checkout modal
847
+ * @param options - Checkout options including amount, currency, and callbacks
848
+ */ checkout(options) {
849
+ if (!this.initialized) {
850
+ console.error('[Zahlen] Not initialized. Call Zahlen.init() first.');
851
+ options.onError({
852
+ code: 'NOT_INITIALIZED',
853
+ message: 'Zahlen SDK not initialized. Call Zahlen.init() first.',
854
+ recoverable: false
855
+ });
856
+ return;
857
+ }
858
+ if (!options.amount || options.amount <= 0) {
859
+ console.error('[Zahlen] Invalid amount');
860
+ options.onError({
861
+ code: 'INVALID_AMOUNT',
862
+ message: 'Payment amount must be greater than 0',
863
+ recoverable: false
864
+ });
865
+ return;
866
+ }
867
+ if (!options.currency) {
868
+ console.error('[Zahlen] Currency is required');
869
+ options.onError({
870
+ code: 'INVALID_CURRENCY',
871
+ message: 'Currency code is required',
872
+ recoverable: false
873
+ });
874
+ return;
875
+ }
876
+ // Close any existing modal
877
+ this.closeModal();
878
+ // Create and open new modal
879
+ this.currentModal = new ZahlenModal(Object.assign(Object.assign({}, options), {
880
+ onClose: ()=>{
881
+ var _a;
882
+ this.currentModal = null;
883
+ (_a = options.onClose) === null || _a === void 0 ? void 0 : _a.call(options);
884
+ }
885
+ }));
886
+ this.currentModal.open();
887
+ }
888
+ /**
889
+ * Close the current checkout modal
890
+ */ closeModal() {
891
+ if (this.currentModal) {
892
+ this.currentModal.close();
893
+ this.currentModal = null;
894
+ }
895
+ }
896
+ /**
897
+ * Set the theme
898
+ * @param theme - 'dark', 'light', or 'auto'
899
+ */ setTheme(theme) {
900
+ document.documentElement.classList.remove('zahlen-light');
901
+ if (theme === 'light') {
902
+ document.documentElement.classList.add('zahlen-light');
903
+ } else if (theme === 'auto') {
904
+ this.applyAutoTheme();
905
+ }
906
+ if (this.config) {
907
+ this.config.theme = theme;
908
+ }
909
+ }
910
+ /**
911
+ * Apply system theme preference
912
+ */ applyAutoTheme() {
913
+ const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
914
+ if (!prefersDark) {
915
+ document.documentElement.classList.add('zahlen-light');
916
+ }
917
+ // Listen for theme changes
918
+ window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e)=>{
919
+ var _a;
920
+ if (((_a = this.config) === null || _a === void 0 ? void 0 : _a.theme) === 'auto') {
921
+ document.documentElement.classList.toggle('zahlen-light', !e.matches);
922
+ }
923
+ });
924
+ }
925
+ /**
926
+ * Apply custom CSS variables
927
+ */ applyCustomStyles(styles) {
928
+ const root = document.documentElement;
929
+ for (const [key, value] of Object.entries(styles)){
930
+ if (value) {
931
+ root.style.setProperty(key, value);
932
+ }
933
+ }
934
+ }
935
+ /**
936
+ * Get the SDK version
937
+ */ get version() {
938
+ return '0.1.0';
939
+ }
940
+ /**
941
+ * Check if SDK is initialized
942
+ */ get isInitialized() {
943
+ return this.initialized;
944
+ }
945
+ constructor(){
946
+ this.config = null;
947
+ this.currentModal = null;
948
+ this.initialized = false;
949
+ }
950
+ };
951
+ // Create singleton instance
952
+ const Zahlen = new ZahlenSDK();
953
+ // Make available on window for script tag usage
954
+ if (typeof window !== 'undefined') {
955
+ window.Zahlen = Zahlen;
956
+ }
957
+
958
+ export { Zahlen, ZahlenSDK, Zahlen as default, detectCardBrand, formatAmount, formatCVV, formatCardNumber, formatExpiry, getCurrencySymbol, luhnCheck, validateCVV, validateCardNumber, validateExpiry };