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