@funnelfox/billing 0.1.1 → 0.2.1

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.
@@ -6,1518 +6,1254 @@
6
6
  * @license MIT
7
7
  */
8
8
  (function (global, factory) {
9
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
10
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
11
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FunnelfoxSDK = {}));
9
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
10
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
11
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.FunnelfoxSDK = {}));
12
12
  })(this, (function (exports) { 'use strict';
13
13
 
14
- /**
15
- * @fileoverview Custom error classes for Funnefox SDK
16
- */
17
-
18
- /**
19
- * Base error class for all Funnefox SDK errors
20
- */
21
- class FunnefoxSDKError extends Error {
22
- /**
23
- * @param {string} message - Error message
24
- * @param {string} [code='SDK_ERROR'] - Error code
25
- * @param {*} [details=null] - Additional error details
26
- */
27
- constructor(message, code = 'SDK_ERROR', details = null) {
28
- super(message);
29
- this.name = 'FunnefoxSDKError';
30
- this.code = code;
31
- this.details = details;
32
- if (Error.captureStackTrace) {
33
- Error.captureStackTrace(this, FunnefoxSDKError);
34
- }
35
- }
36
- }
37
-
38
- /**
39
- * Error thrown when input validation fails
40
- */
41
- class ValidationError extends FunnefoxSDKError {
42
- /**
43
- * @param {string} field - Field that failed validation
44
- * @param {string} message - Validation error message
45
- * @param {*} [value] - Invalid value that caused the error
46
- */
47
- constructor(field, message, value = null) {
48
- super(`Invalid ${field}: ${message}`, 'VALIDATION_ERROR');
49
- this.name = 'ValidationError';
50
- this.field = field;
51
- this.value = value;
52
- }
53
- }
54
-
55
- /**
56
- * Error thrown when API calls fail
57
- */
58
- class APIError extends FunnefoxSDKError {
59
- /**
60
- * @param {string} message - API error message
61
- * @param {number} [statusCode] - HTTP status code
62
- * @param {Object} [options] - Additional error details
63
- * @param {string} [options.errorCode] - API error code (e.g., 'double_purchase')
64
- * @param {string} [options.errorType] - API error type (e.g., 'api_exception')
65
- * @param {string} [options.requestId] - Request ID for tracking
66
- * @param {*} [options.response] - Full API response data
67
- */
68
- constructor(message, statusCode = null, options = {}) {
69
- super(message, options.errorCode || 'API_ERROR');
70
- this.name = 'APIError';
71
- this.statusCode = statusCode;
72
- this.errorCode = options.errorCode || null;
73
- this.errorType = options.errorType || null;
74
- this.requestId = options.requestId || null;
75
- this.response = options.response || null;
76
- }
77
- }
78
-
79
- /**
80
- * Error thrown when Primer SDK integration fails
81
- */
82
- class PrimerError extends FunnefoxSDKError {
83
- /**
84
- * @param {string} message - Primer error message
85
- * @param {*} [primerError] - Original Primer error object
86
- */
87
- constructor(message, primerError = null) {
88
- super(message, 'PRIMER_ERROR');
89
- this.name = 'PrimerError';
90
- this.primerError = primerError;
91
- }
92
- }
93
-
94
- /**
95
- * Error thrown when checkout operations fail
96
- */
97
- class CheckoutError extends FunnefoxSDKError {
98
- /**
99
- * @param {string} message - Checkout error message
100
- * @param {string} [phase] - Checkout phase where error occurred
101
- */
102
- constructor(message, phase = null) {
103
- super(message, 'CHECKOUT_ERROR');
104
- this.name = 'CheckoutError';
105
- this.phase = phase;
106
- }
107
- }
108
-
109
- /**
110
- * Error thrown when SDK configuration is invalid
111
- */
112
- class ConfigurationError extends FunnefoxSDKError {
113
- /**
114
- * @param {string} message - Configuration error message
115
- */
116
- constructor(message) {
117
- super(message, 'CONFIGURATION_ERROR');
118
- this.name = 'ConfigurationError';
119
- }
120
- }
121
-
122
- /**
123
- * Error thrown when network requests fail
124
- */
125
- class NetworkError extends FunnefoxSDKError {
126
- /**
127
- * @param {string} message - Network error message
128
- * @param {*} [originalError] - Original network error
129
- */
130
- constructor(message, originalError = null) {
131
- super(message, 'NETWORK_ERROR');
132
- this.name = 'NetworkError';
133
- this.originalError = originalError;
134
- }
135
- }
136
-
137
- /**
138
- * @fileoverview Constants for Funnefox SDK
139
- */
140
-
141
- /**
142
- * SDK version
143
- */
144
- const SDK_VERSION = '0.1.1';
145
-
146
- /**
147
- * Default configuration values
148
- */
149
- const DEFAULTS = {
150
- BASE_URL: 'https://billing.funnelfox.com',
151
- REGION: 'default',
152
- SANDBOX: false,
153
- REQUEST_TIMEOUT: 30000,
154
- RETRY_ATTEMPTS: 3,
155
- RETRY_BASE_DELAY: 1000
156
- };
157
-
158
- /**
159
- * Checkout states
160
- */
161
- const CHECKOUT_STATES = {
162
- INITIALIZING: 'initializing',
163
- READY: 'ready',
164
- PROCESSING: 'processing',
165
- ACTION_REQUIRED: 'action_required',
166
- UPDATING: 'updating',
167
- COMPLETED: 'completed',
168
- ERROR: 'error',
169
- DESTROYED: 'destroyed'
170
- };
171
-
172
- /**
173
- * Event names
174
- */
175
- const EVENTS = {
176
- SUCCESS: 'success',
177
- ERROR: 'error',
178
- STATUS_CHANGE: 'status-change',
179
- DESTROY: 'destroy'
180
- };
181
-
182
- /**
183
- * Error codes
184
- */
185
- const ERROR_CODES = {
186
- SDK_ERROR: 'SDK_ERROR',
187
- VALIDATION_ERROR: 'VALIDATION_ERROR',
188
- API_ERROR: 'API_ERROR',
189
- PRIMER_ERROR: 'PRIMER_ERROR',
190
- CHECKOUT_ERROR: 'CHECKOUT_ERROR',
191
- CONFIGURATION_ERROR: 'CONFIGURATION_ERROR',
192
- NETWORK_ERROR: 'NETWORK_ERROR'
193
- };
194
-
195
- /**
196
- * @fileoverview Lightweight event emitter for Funnefox SDK
197
- */
198
-
199
- /**
200
- * Simple event emitter for checkout instances
201
- */
202
- class EventEmitter {
203
- constructor() {
204
- this._events = new Map();
205
- }
206
-
207
- /**
208
- * Add an event listener
209
- * @param {string} eventName - Name of the event
210
- * @param {function} handler - Event handler function
211
- * @returns {EventEmitter} Returns this for chaining
212
- */
213
- on(eventName, handler) {
214
- if (typeof handler !== 'function') {
215
- throw new Error('Event handler must be a function');
216
- }
217
- if (!this._events.has(eventName)) {
218
- this._events.set(eventName, []);
219
- }
220
- this._events.get(eventName).push(handler);
221
- return this;
222
- }
223
-
224
- /**
225
- * Add a one-time event listener
226
- * @param {string} eventName - Name of the event
227
- * @param {function} handler - Event handler function
228
- * @returns {EventEmitter} Returns this for chaining
229
- */
230
- once(eventName, handler) {
231
- if (typeof handler !== 'function') {
232
- throw new Error('Event handler must be a function');
233
- }
234
- const onceWrapper = (...args) => {
235
- this.off(eventName, onceWrapper);
236
- handler.apply(this, args);
237
- };
238
- return this.on(eventName, onceWrapper);
239
- }
240
-
241
- /**
242
- * Remove an event listener
243
- * @param {string} eventName - Name of the event
244
- * @param {function} [handler] - Specific handler to remove. If not provided, removes all handlers for the event
245
- * @returns {EventEmitter} Returns this for chaining
246
- */
247
- off(eventName, handler = null) {
248
- if (!this._events.has(eventName)) {
249
- return this;
250
- }
251
- if (handler === null) {
252
- this._events.delete(eventName);
253
- return this;
254
- }
255
- const handlers = this._events.get(eventName);
256
- const index = handlers.indexOf(handler);
257
- if (index !== -1) {
258
- handlers.splice(index, 1);
259
- if (handlers.length === 0) {
260
- this._events.delete(eventName);
261
- }
262
- }
263
- return this;
264
- }
265
-
266
14
  /**
267
- * Emit an event to all registered handlers
268
- * @param {string} eventName - Name of the event to emit
269
- * @param {...*} args - Arguments to pass to event handlers
270
- * @returns {boolean} Returns true if event had listeners, false otherwise
15
+ * @fileoverview Lightweight event emitter for Funnefox SDK
271
16
  */
272
- emit(eventName, ...args) {
273
- if (!this._events.has(eventName)) {
274
- return false;
275
- }
276
- const handlers = this._events.get(eventName).slice();
277
- for (const handler of handlers) {
278
- try {
279
- handler.apply(this, args);
280
- } catch (error) {
281
- // Don't let handler errors break the emission chain
282
- // eslint-disable-next-line no-console
283
- console.warn(`Error in event handler for "${eventName}":`, error);
284
- }
285
- }
286
- return true;
287
- }
288
-
289
- /**
290
- * Get the number of listeners for an event
291
- * @param {string} eventName - Name of the event
292
- * @returns {number} Number of listeners
293
- */
294
- listenerCount(eventName) {
295
- return this._events.has(eventName) ? this._events.get(eventName).length : 0;
296
- }
297
-
298
- /**
299
- * Get all event names that have listeners
300
- * @returns {string[]} Array of event names
301
- */
302
- eventNames() {
303
- return Array.from(this._events.keys());
304
- }
305
-
306
- /**
307
- * Remove all listeners for all events
308
- * @returns {EventEmitter} Returns this for chaining
309
- */
310
- removeAllListeners() {
311
- this._events.clear();
312
- return this;
17
+ class EventEmitter {
18
+ constructor() {
19
+ this._events = new Map();
20
+ }
21
+ on(eventName, handler) {
22
+ if (typeof handler !== 'function') {
23
+ throw new Error('Event handler must be a function');
24
+ }
25
+ if (!this._events.has(eventName)) {
26
+ this._events.set(eventName, []);
27
+ }
28
+ this._events.get(eventName).push(handler);
29
+ return this;
30
+ }
31
+ once(eventName, handler) {
32
+ if (typeof handler !== 'function') {
33
+ throw new Error('Event handler must be a function');
34
+ }
35
+ const onceWrapper = (...args) => {
36
+ this.off(eventName, onceWrapper);
37
+ handler.apply(this, args);
38
+ };
39
+ return this.on(eventName, onceWrapper);
40
+ }
41
+ off(eventName, handler = null) {
42
+ if (!this._events.has(eventName)) {
43
+ return this;
44
+ }
45
+ if (handler === null) {
46
+ this._events.delete(eventName);
47
+ return this;
48
+ }
49
+ const handlers = this._events.get(eventName);
50
+ const index = handlers.indexOf(handler);
51
+ if (index !== -1) {
52
+ handlers.splice(index, 1);
53
+ if (handlers.length === 0) {
54
+ this._events.delete(eventName);
55
+ }
56
+ }
57
+ return this;
58
+ }
59
+ emit(eventName, ...args) {
60
+ if (!this._events.has(eventName)) {
61
+ return false;
62
+ }
63
+ const handlers = this._events.get(eventName).slice();
64
+ for (const handler of handlers) {
65
+ try {
66
+ handler.apply(this, args);
67
+ }
68
+ catch (error) {
69
+ // eslint-disable-next-line no-console
70
+ console.warn(`Error in event handler for "${eventName}":`, error);
71
+ }
72
+ }
73
+ return true;
74
+ }
75
+ listenerCount(eventName) {
76
+ return this._events.has(eventName)
77
+ ? this._events.get(eventName).length
78
+ : 0;
79
+ }
80
+ eventNames() {
81
+ return Array.from(this._events.keys());
82
+ }
83
+ removeAllListeners() {
84
+ this._events.clear();
85
+ return this;
86
+ }
87
+ listeners(eventName) {
88
+ return this._events.has(eventName)
89
+ ? this._events.get(eventName).slice()
90
+ : [];
91
+ }
313
92
  }
314
93
 
315
94
  /**
316
- * Get all listeners for an event
317
- * @param {string} eventName - Name of the event
318
- * @returns {function[]} Array of handler functions
95
+ * @fileoverview Custom error classes for Funnefox SDK
319
96
  */
320
- listeners(eventName) {
321
- return this._events.has(eventName) ? this._events.get(eventName).slice() : [];
322
- }
323
- }
324
-
325
- /**
326
- * @fileoverview Helper utilities for Funnefox SDK
327
- */
328
-
329
-
330
- /**
331
- * Merges multiple objects into a new object
332
- * @param {...Object} objects - Objects to merge
333
- * @returns {Object} Merged object
334
- */
335
- function merge(...objects) {
336
- const result = {};
337
- for (const obj of objects) {
338
- if (obj && typeof obj === 'object') {
339
- for (const key in obj) {
340
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
341
- if (typeof obj[key] === 'object' && !Array.isArray(obj[key]) && obj[key] !== null) {
342
- result[key] = merge(result[key] || {}, obj[key]);
343
- } else {
344
- result[key] = obj[key];
97
+ class FunnefoxSDKError extends Error {
98
+ constructor(message, code = ERROR_CODES.SDK_ERROR, details = null) {
99
+ super(message);
100
+ this.name = 'FunnefoxSDKError';
101
+ this.code = code;
102
+ this.details = details;
103
+ if (Error.captureStackTrace) {
104
+ Error.captureStackTrace(this, FunnefoxSDKError);
345
105
  }
346
- }
347
106
  }
348
- }
349
- }
350
- return result;
351
- }
352
-
353
- /**
354
- * Generates a unique identifier
355
- * @param {string} [prefix=''] - Optional prefix for the ID
356
- * @returns {string} Unique identifier
357
- */
358
- function generateId(prefix = '') {
359
- const timestamp = Date.now().toString(36);
360
- const random = Math.random().toString(36).substr(2, 5);
361
- return `${prefix}${timestamp}_${random}`;
362
- }
363
-
364
- /**
365
- * Waits for a specified amount of time
366
- * @param {number} ms - Milliseconds to wait
367
- * @returns {Promise} Promise that resolves after the specified time
368
- */
369
- function sleep(ms) {
370
- return new Promise(resolve => setTimeout(resolve, ms));
371
- }
372
-
373
- /**
374
- * Retries a function with exponential backoff
375
- * @param {function} fn - Function to retry
376
- * @param {number} [maxAttempts=3] - Maximum number of attempts
377
- * @param {number} [baseDelay=1000] - Base delay in milliseconds
378
- * @returns {Promise} Promise that resolves with the function result
379
- */
380
- async function retry(fn, maxAttempts = 3, baseDelay = 1000) {
381
- let lastError;
382
- for (let attempt = 1; attempt <= maxAttempts; attempt++) {
383
- try {
384
- return await fn();
385
- } catch (error) {
386
- lastError = error;
387
- if (attempt === maxAttempts) {
388
- throw lastError;
389
- }
390
- const delay = baseDelay * Math.pow(2, attempt - 1);
391
- await sleep(delay);
392
- }
393
107
  }
394
- throw lastError;
395
- }
396
-
397
- /**
398
- * Creates a promise that rejects after a timeout
399
- * @param {Promise} promise - Promise to wrap
400
- * @param {number} timeoutMs - Timeout in milliseconds
401
- * @param {string} [message='Operation timed out'] - Timeout error message
402
- * @returns {Promise} Promise that rejects if timeout is reached
403
- */
404
- function withTimeout(promise, timeoutMs, message = 'Operation timed out') {
405
- const timeoutPromise = new Promise((_, reject) => {
406
- setTimeout(() => reject(new Error(message)), timeoutMs);
407
- });
408
- return Promise.race([promise, timeoutPromise]);
409
- }
410
-
411
- /**
412
- * @fileoverview Primer SDK integration wrapper
413
- *
414
- * @typedef {import('@primer-io/checkout-web').IPrimerHeadlessUniversalCheckout} PrimerCheckout
415
- * @typedef {import('@primer-io/checkout-web').ITokenizationHandler} PrimerTokenizationHandler
416
- * @typedef {import('@primer-io/checkout-web').IResumeHandler} PrimerResumeHandler
417
- * @typedef {import('@primer-io/checkout-web').PaymentMethodTokenData} PaymentMethodTokenData
418
- * @typedef {import('@primer-io/checkout-web').ResumeTokenData} ResumeTokenData
419
- * @typedef {import('@primer-io/checkout-web').UniversalCheckoutOptions} UniversalCheckoutOptions
420
- */
421
-
422
-
423
- /**
424
- * Wrapper class for Primer SDK integration
425
- */
426
- class PrimerWrapper {
427
- constructor() {
428
- this.currentCheckout = null;
429
- this.isInitialized = false;
430
- }
431
-
432
- /**
433
- * Checks if Primer SDK is available
434
- * @returns {boolean} True if Primer is available
435
- */
436
- isPrimerAvailable() {
437
- return typeof window !== 'undefined' && window.Primer && typeof window.Primer.showUniversalCheckout === 'function';
108
+ class ValidationError extends FunnefoxSDKError {
109
+ constructor(field, message, value = null) {
110
+ super(`Invalid ${field}: ${message}`, ERROR_CODES.VALIDATION_ERROR);
111
+ this.name = 'ValidationError';
112
+ this.field = field;
113
+ this.value = value;
114
+ }
438
115
  }
439
-
440
- /**
441
- * Ensures Primer SDK is available
442
- * @throws {PrimerError} If Primer SDK is not available
443
- */
444
- ensurePrimerAvailable() {
445
- if (!this.isPrimerAvailable()) {
446
- throw new PrimerError('Primer SDK not found. Please include the Primer SDK script before initializing FunnefoxSDK.');
447
- }
116
+ class APIError extends FunnefoxSDKError {
117
+ constructor(message, statusCode = null, options = {}) {
118
+ super(message, options.errorCode || ERROR_CODES.API_ERROR);
119
+ this.name = 'APIError';
120
+ this.statusCode = statusCode;
121
+ this.errorCode = options.errorCode || null;
122
+ this.errorType = options.errorType || null;
123
+ this.requestId = options.requestId || null;
124
+ this.response = options.response || null;
125
+ }
448
126
  }
449
-
450
- /**
451
- * Creates a universal checkout instance
452
- * @param {string} clientToken - Client token from your backend
453
- * @param {Partial<UniversalCheckoutOptions> & {onTokenizeSuccess: function, onResumeSuccess: function}} options - Checkout options
454
- * @returns {Promise<PrimerCheckout>} Primer checkout instance
455
- */
456
- async showUniversalCheckout(clientToken, options) {
457
- this.ensurePrimerAvailable();
458
-
459
- // Extract SDK-managed handlers
460
- const {
461
- onTokenizeSuccess,
462
- onResumeSuccess,
463
- container,
464
- ...userPrimerOptions
465
- } = options;
466
-
467
- // Merge defaults with user's Primer options
468
- /** @type {UniversalCheckoutOptions} */
469
- const primerOptions = merge({
470
- clientToken,
471
- container,
472
- paymentHandling: 'MANUAL',
473
- apiVersion: '2.4',
474
- paypal: {
475
- buttonColor: 'blue',
476
- paymentFlow: 'PREFER_VAULT'
477
- }
478
- }, userPrimerOptions);
479
-
480
- // Add the required event handlers (must override any user-provided ones)
481
- primerOptions.onTokenizeSuccess = this._wrapTokenizeHandler(onTokenizeSuccess);
482
- primerOptions.onResumeSuccess = this._wrapResumeHandler(onResumeSuccess);
483
- try {
484
- const checkout = await window.Primer.showUniversalCheckout(clientToken, primerOptions);
485
- this.currentCheckout = checkout;
486
- this.isInitialized = true;
487
- return checkout;
488
- } catch (error) {
489
- throw new PrimerError('Failed to initialize Primer checkout', error);
490
- }
127
+ class PrimerError extends FunnefoxSDKError {
128
+ constructor(message, primerError = null) {
129
+ super(message, ERROR_CODES.PRIMER_ERROR);
130
+ this.name = 'PrimerError';
131
+ this.primerError = primerError;
132
+ }
491
133
  }
492
-
493
- /**
494
- * Wraps the tokenize success handler with error handling
495
- * @private
496
- * @param {function(PaymentMethodTokenData, PrimerTokenizationHandler): Promise<void>} handler
497
- * @returns {function(PaymentMethodTokenData, PrimerTokenizationHandler): Promise<void>}
498
- */
499
- _wrapTokenizeHandler(handler) {
500
- return async (paymentMethodTokenData, primerHandler) => {
501
- try {
502
- await handler(paymentMethodTokenData, primerHandler);
503
- } catch (error) {
504
- console.error('Error in tokenize handler:', error);
505
- primerHandler.handleFailure('Payment processing failed. Please try again.');
506
- }
507
- };
134
+ class CheckoutError extends FunnefoxSDKError {
135
+ constructor(message, phase = null) {
136
+ super(message, ERROR_CODES.CHECKOUT_ERROR);
137
+ this.name = 'CheckoutError';
138
+ this.phase = phase;
139
+ }
508
140
  }
509
-
510
- /**
511
- * Wraps the resume success handler with error handling
512
- * @private
513
- * @param {function(ResumeTokenData, PrimerResumeHandler): Promise<void>} handler
514
- * @returns {function(ResumeTokenData, PrimerResumeHandler): Promise<void>}
515
- */
516
- _wrapResumeHandler(handler) {
517
- return async (resumeTokenData, primerHandler) => {
518
- try {
519
- await handler(resumeTokenData, primerHandler);
520
- } catch (error) {
521
- console.error('Error in resume handler:', error);
522
- primerHandler.handleFailure('Payment processing failed. Please try again.');
523
- }
524
- };
141
+ class ConfigurationError extends FunnefoxSDKError {
142
+ constructor(message) {
143
+ super(message, ERROR_CODES.CONFIGURATION_ERROR);
144
+ this.name = 'ConfigurationError';
145
+ }
525
146
  }
526
-
527
- /**
528
- * Updates the client token for an existing checkout
529
- * @param {string} newClientToken - New client token
530
- * @returns {Promise<void>}
531
- */
532
- async updateClientToken(newClientToken) {
533
- if (!this.currentCheckout) {
534
- throw new PrimerError('No active checkout to update');
535
- }
536
- try {
537
- // Primer SDK doesn't have a direct update method, so we need to destroy and recreate
538
- // This is handled at the checkout level
539
- throw new PrimerError('Client token updates require checkout recreation');
540
- } catch (error) {
541
- throw new PrimerError('Failed to update client token', error);
542
- }
147
+ class NetworkError extends FunnefoxSDKError {
148
+ constructor(message, originalError = null) {
149
+ super(message, ERROR_CODES.NETWORK_ERROR);
150
+ this.name = 'NetworkError';
151
+ this.originalError = originalError;
152
+ }
543
153
  }
544
154
 
545
155
  /**
546
- * Destroys the current checkout instance
547
- * @returns {Promise<void>}
156
+ * @fileoverview Helper utilities for Funnefox SDK
548
157
  */
549
- async destroy() {
550
- if (this.currentCheckout) {
551
- try {
552
- // Primer checkout cleanup - this depends on Primer SDK version
553
- if (typeof this.currentCheckout.destroy === 'function') {
554
- await this.currentCheckout.destroy();
555
- } else {
556
- // Fallback - clear the container
557
- const container = document.querySelector(this.currentCheckout.container);
558
- if (container) {
559
- container.innerHTML = '';
158
+ function merge(...objects) {
159
+ const result = {};
160
+ for (const obj of objects) {
161
+ if (obj && typeof obj === 'object') {
162
+ for (const key in obj) {
163
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
164
+ if (typeof obj[key] === 'object' &&
165
+ !Array.isArray(obj[key]) &&
166
+ obj[key] !== null) {
167
+ result[key] = merge(result[key] || {}, obj[key]);
168
+ }
169
+ else {
170
+ result[key] = obj[key];
171
+ }
172
+ }
173
+ }
560
174
  }
561
- }
562
- } catch (error) {
563
- console.warn('Error destroying Primer checkout:', error);
564
- } finally {
565
- this.currentCheckout = null;
566
- this.isInitialized = false;
567
- }
568
- }
569
- }
570
-
571
- /**
572
- * Creates standardized Primer handler helpers
573
- * @param {Object} handlers - Handler functions
574
- * @param {function} handlers.onSuccess - Success callback
575
- * @param {function} handlers.onError - Error callback
576
- * @param {function} handlers.onActionRequired - Action required callback
577
- * @returns {Object} Primer-compatible handler functions
578
- */
579
- createHandlers(handlers) {
580
- return {
581
- /**
582
- * Handle successful tokenization
583
- */
584
- handleSuccess: () => {
585
- if (handlers.onSuccess) {
586
- handlers.onSuccess();
587
- }
588
- },
589
- /**
590
- * Handle payment failure
591
- */
592
- handleFailure: message => {
593
- if (handlers.onError) {
594
- handlers.onError(new Error(message));
595
- }
596
- },
597
- /**
598
- * Continue with new client token (for 3DS flows)
599
- */
600
- continueWithNewClientToken: newClientToken => {
601
- if (handlers.onActionRequired) {
602
- handlers.onActionRequired(newClientToken);
603
- }
604
- }
605
- };
606
- }
607
-
608
- /**
609
- * Gets the current checkout state
610
- * @returns {PrimerCheckout|null} Current checkout instance or null
611
- */
612
- getCurrentCheckout() {
613
- return this.currentCheckout;
614
- }
615
-
616
- /**
617
- * Checks if checkout is currently active
618
- * @returns {boolean} True if checkout is active
619
- */
620
- isActive() {
621
- return this.isInitialized && this.currentCheckout !== null;
622
- }
623
-
624
- /**
625
- * Validates that a container element exists and is suitable for Primer
626
- * @param {string} selector - DOM selector
627
- * @returns {Element} The container element
628
- * @throws {PrimerError} If container is invalid
629
- */
630
- validateContainer(selector) {
631
- const element = document.querySelector(selector);
632
- if (!element) {
633
- throw new PrimerError(`Checkout container not found: ${selector}`);
634
- }
635
-
636
- // Check if container is suitable (visible, not already in use, etc.)
637
- const computedStyle = window.getComputedStyle(element);
638
- if (computedStyle.display === 'none') {
639
- console.warn('Checkout container is hidden, this may cause display issues');
640
- }
641
- return element;
642
- }
643
- }
644
-
645
- var primerWrapper = /*#__PURE__*/Object.freeze({
646
- __proto__: null,
647
- default: PrimerWrapper
648
- });
649
-
650
- /**
651
- * @fileoverview Input validation utilities for Funnefox SDK
652
- */
653
-
654
-
655
- /**
656
- * Sanitizes a string input
657
- * @param {*} input - Input to sanitize
658
- * @returns {string} Sanitized string
659
- */
660
- function sanitizeString(input) {
661
- if (input === null || input === undefined) {
662
- return '';
175
+ }
176
+ return result;
663
177
  }
664
- return String(input).trim();
665
- }
666
-
667
- /**
668
- * Validates that a value is a non-empty string
669
- * @param {*} value - Value to validate
670
- * @param {string} fieldName - Name of the field for error reporting
671
- * @returns {boolean} True if valid
672
- * @throws {ValidationError} If validation fails
673
- */
674
- function requireString(value, fieldName) {
675
- const sanitized = sanitizeString(value);
676
- if (sanitized.length === 0) {
677
- throw new ValidationError(fieldName, 'must be a non-empty string', value);
178
+ function generateId(prefix = '') {
179
+ const timestamp = Date.now().toString(36);
180
+ const random = Math.random().toString(36).substr(2, 5);
181
+ return `${prefix}${timestamp}_${random}`;
678
182
  }
679
- return true;
680
- }
681
-
682
- /**
683
- * Validates that a container element exists in the DOM
684
- * @param {string} selector - DOM selector
685
- * @returns {Element} The found element
686
- * @throws {ValidationError} If element not found
687
- */
688
- function validateContainer(selector) {
689
- requireString(selector, 'container');
690
- const element = document.querySelector(selector);
691
- if (!element) {
692
- throw new ValidationError('container', `element not found: ${selector}`, selector);
183
+ function sleep(ms) {
184
+ return new Promise(resolve => setTimeout(resolve, ms));
693
185
  }
694
- return element;
695
- }
696
-
697
- /**
698
- * @fileoverview API client for Funnefox backend integration
699
- */
700
-
701
-
702
- /**
703
- * HTTP client for Funnefox API requests
704
- */
705
- class APIClient {
706
- /**
707
- * @param {Object} config - API client configuration
708
- * @param {string} config.baseUrl - Base URL for API calls
709
- * @param {string} config.orgId - Organization identifier
710
- * @param {number} [config.timeout=30000] - Request timeout in milliseconds
711
- * @param {number} [config.retryAttempts=3] - Number of retry attempts
712
- */
713
- constructor(config) {
714
- this.baseUrl = config.baseUrl.replace(/\/$/, '');
715
- this.orgId = config.orgId;
716
- this.timeout = config.timeout || 30000;
717
- this.retryAttempts = config.retryAttempts || 3;
718
- }
719
-
720
- /**
721
- * Makes an HTTP request with error handling and retries
722
- * @param {string} endpoint - API endpoint
723
- * @param {Object} [options={}] - Request options
724
- * @returns {Promise<Object>} API response data
725
- * @throws {APIError|NetworkError} If request fails
726
- */
727
- async request(endpoint, options = {}) {
728
- const url = `${this.baseUrl}/${this.orgId}${endpoint}`;
729
- const requestOptions = {
730
- method: 'GET',
731
- headers: {
732
- 'Content-Type': 'application/json',
733
- ...options.headers
734
- },
735
- ...options
736
- };
737
- try {
738
- return await retry(async () => {
739
- return await withTimeout(this._makeRequest(url, requestOptions), this.timeout, 'Request timed out');
740
- }, this.retryAttempts);
741
- } catch (error) {
742
- if (error.name === 'APIError') {
743
- throw error;
744
- }
745
- throw new NetworkError('Network request failed', error);
746
- }
186
+ async function retry(fn, maxAttempts = 3, baseDelay = 1000) {
187
+ let lastError;
188
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
189
+ try {
190
+ return await fn();
191
+ }
192
+ catch (error) {
193
+ lastError = error;
194
+ if (attempt === maxAttempts)
195
+ throw lastError;
196
+ const delay = baseDelay * Math.pow(2, attempt - 1);
197
+ await sleep(delay);
198
+ }
199
+ }
200
+ throw lastError;
747
201
  }
748
-
749
- /**
750
- * Internal method to make the actual HTTP request
751
- * @private
752
- */
753
- async _makeRequest(url, options) {
754
- let response;
755
- try {
756
- response = await fetch(url, options);
757
- } catch (error) {
758
- throw new NetworkError('Network request failed', error);
759
- }
760
- let data;
761
- try {
762
- data = await response.json();
763
- } catch (error) {
764
- throw new APIError('Invalid JSON response', response.status, {});
765
- }
766
- if (!response.ok) {
767
- const message = data.message || data.error || `HTTP ${response.status}`;
768
- throw new APIError(message, response.status, {
769
- response: data
202
+ function withTimeout(promise, timeoutMs, message = 'Operation timed out') {
203
+ const timeoutPromise = new Promise((_, reject) => {
204
+ setTimeout(() => reject(new Error(message)), timeoutMs);
770
205
  });
771
- }
772
- return data;
206
+ return Promise.race([promise, timeoutPromise]);
773
207
  }
774
208
 
775
- /**
776
- * Creates a client session for checkout
777
- * @param {Object} params - Session parameters
778
- * @param {string} params.priceId - Price point identifier
779
- * @param {string} params.externalId - External user identifier
780
- * @param {string} params.email - Customer email
781
- * @param {Object} [params.clientMetadata] - Additional metadata
782
- * @param {string} [params.region='default'] - Checkout region
783
- * @param {string} [params.countryCode] - Optional ISO country code
784
- * @returns {Promise<Object>} Client session response
785
- */
786
- async createClientSession(params) {
787
- const payload = {
788
- region: params.region || 'default',
789
- integration_type: 'primer',
790
- pp_ident: params.priceId,
791
- external_id: params.externalId,
792
- email_address: params.email,
793
- client_metadata: params.clientMetadata || {}
794
- };
795
-
796
- // Add country_code if provided (nullable field per Swagger spec)
797
- if (params.countryCode !== undefined) {
798
- payload.country_code = params.countryCode;
799
- }
800
- return await this.request('/v1/checkout/create_client_session', {
801
- method: 'POST',
802
- body: JSON.stringify(payload)
803
- });
804
- }
209
+ var PaymentMethod;
210
+ (function (PaymentMethod) {
211
+ PaymentMethod["GOOGLE_PAY"] = "GOOGLE_PAY";
212
+ PaymentMethod["APPLE_PAY"] = "APPLE_PAY";
213
+ PaymentMethod["PAYPAL"] = "PAYPAL";
214
+ PaymentMethod["PAYMENT_CARD"] = "PAYMENT_CARD";
215
+ })(PaymentMethod || (PaymentMethod = {}));
805
216
 
806
217
  /**
807
- * Updates an existing client session
808
- * @param {Object} params - Update parameters
809
- * @param {string} params.orderId - Order identifier
810
- * @param {string} params.clientToken - Current client token
811
- * @param {string} params.priceId - New price identifier
812
- * @returns {Promise<Object>} Update response
218
+ * @fileoverview Constants for Funnefox SDK
813
219
  */
814
- async updateClientSession(params) {
815
- const payload = {
816
- order_id: params.orderId,
817
- client_token: params.clientToken,
818
- pp_ident: params.priceId
819
- };
820
- return await this.request('/v1/checkout/update_client_session', {
821
- method: 'POST',
822
- body: JSON.stringify(payload)
823
- });
824
- }
220
+ const SDK_VERSION = '0.2.1';
221
+ const DEFAULTS = {
222
+ BASE_URL: 'https://billing.funnelfox.com',
223
+ REGION: 'default',
224
+ SANDBOX: false,
225
+ REQUEST_TIMEOUT: 30000,
226
+ RETRY_ATTEMPTS: 3,
227
+ RETRY_BASE_DELAY: 1000,
228
+ };
229
+ const CHECKOUT_STATES = {
230
+ INITIALIZING: 'initializing',
231
+ READY: 'ready',
232
+ PROCESSING: 'processing',
233
+ ACTION_REQUIRED: 'action_required',
234
+ UPDATING: 'updating',
235
+ COMPLETED: 'completed',
236
+ ERROR: 'error',
237
+ DESTROYED: 'destroyed',
238
+ };
239
+ const EVENTS = {
240
+ SUCCESS: 'success',
241
+ ERROR: 'error',
242
+ STATUS_CHANGE: 'status-change',
243
+ DESTROY: 'destroy',
244
+ INPUT_ERROR: 'input-error',
245
+ LOADER_CHANGE: 'loader-change',
246
+ METHOD_RENDER: 'method-render',
247
+ };
248
+ const API_ENDPOINTS = {
249
+ CREATE_CLIENT_SESSION: '/v1/checkout/create_client_session',
250
+ UPDATE_CLIENT_SESSION: '/v1/checkout/update_client_session',
251
+ CREATE_PAYMENT: '/v1/checkout/create_payment',
252
+ RESUME_PAYMENT: '/v1/checkout/resume_payment',
253
+ };
254
+ const ERROR_CODES = {
255
+ SDK_ERROR: 'SDK_ERROR',
256
+ VALIDATION_ERROR: 'VALIDATION_ERROR',
257
+ API_ERROR: 'API_ERROR',
258
+ PRIMER_ERROR: 'PRIMER_ERROR',
259
+ CHECKOUT_ERROR: 'CHECKOUT_ERROR',
260
+ CONFIGURATION_ERROR: 'CONFIGURATION_ERROR',
261
+ NETWORK_ERROR: 'NETWORK_ERROR',
262
+ };
263
+ const ALLOWED_BUTTON_PAYMENT_METHODS = [
264
+ PaymentMethod.GOOGLE_PAY,
265
+ PaymentMethod.APPLE_PAY,
266
+ PaymentMethod.PAYPAL,
267
+ ];
268
+ const ALLOWED_CARD_PAYMENT_METHODS = [
269
+ PaymentMethod.PAYMENT_CARD,
270
+ ];
271
+ const ALLOWED_PAYMENT_METHODS = [
272
+ ...ALLOWED_BUTTON_PAYMENT_METHODS,
273
+ ...ALLOWED_CARD_PAYMENT_METHODS,
274
+ ];
275
+ const inputStyle = {
276
+ input: {
277
+ error: {
278
+ borderColor: 'rgb(227, 47, 65)',
279
+ },
280
+ base: {
281
+ borderWidth: '1px',
282
+ borderStyle: 'solid',
283
+ borderColor: 'rgb(0 0 0 / 10%)',
284
+ height: '36px',
285
+ paddingHorizontal: 10,
286
+ borderRadius: '6px',
287
+ },
288
+ },
289
+ };
290
+ ({
291
+ paddingLeft: inputStyle.input.base.paddingHorizontal + 'px',
292
+ paddingRight: inputStyle.input.base.paddingHorizontal + 'px'});
825
293
 
826
294
  /**
827
- * Creates a payment with tokenized payment method
828
- * @param {Object} params - Payment parameters
829
- * @param {string} params.orderId - Order identifier
830
- * @param {string} params.paymentMethodToken - Payment method token from Primer
831
- * @returns {Promise<Object>} Payment response
295
+ * @fileoverview Primer SDK integration wrapper
832
296
  */
833
- async createPayment(params) {
834
- const payload = {
835
- order_id: params.orderId,
836
- payment_method_token: params.paymentMethodToken
837
- };
838
- return await this.request('/v1/checkout/create_payment', {
839
- method: 'POST',
840
- body: JSON.stringify(payload)
841
- });
297
+ class PrimerWrapper {
298
+ constructor() {
299
+ this.isInitialized = false;
300
+ this.destroyCallbacks = [];
301
+ this.headless = null;
302
+ this.availableMethods = [];
303
+ }
304
+ isPrimerAvailable() {
305
+ return (typeof window !== 'undefined' &&
306
+ window.Primer &&
307
+ typeof window.Primer?.createHeadless === 'function');
308
+ }
309
+ ensurePrimerAvailable() {
310
+ if (!this.isPrimerAvailable()) {
311
+ throw new PrimerError('Primer SDK not found. Please include the Primer SDK script before initializing FunnefoxSDK.');
312
+ }
313
+ }
314
+ async createHeadlessCheckout(clientToken, options) {
315
+ if (this.headless) {
316
+ return this.headless;
317
+ }
318
+ this.ensurePrimerAvailable();
319
+ const primerOptions = merge({
320
+ paymentHandling: 'MANUAL',
321
+ apiVersion: '2.4',
322
+ paypal: {
323
+ buttonColor: 'blue',
324
+ paymentFlow: 'PREFER_VAULT',
325
+ },
326
+ }, options);
327
+ try {
328
+ const headless = await window.Primer.createHeadless(clientToken, primerOptions);
329
+ await headless.start();
330
+ this.headless = headless;
331
+ }
332
+ catch (error) {
333
+ throw new PrimerError('Failed to create Primer headless checkout', error);
334
+ }
335
+ }
336
+ initializeCardElements(selectors) {
337
+ const { cardNumber, expiryDate, cvv, cardholderName, button } = selectors;
338
+ return {
339
+ cardNumber: document.querySelector(cardNumber),
340
+ expiryDate: document.querySelector(expiryDate),
341
+ cvv: document.querySelector(cvv),
342
+ cardholderName: document.querySelector(cardholderName),
343
+ button: document.querySelector(button),
344
+ };
345
+ }
346
+ disableButtons(disabled) {
347
+ if (!this.paymentMethodsInterfaces)
348
+ return;
349
+ for (const method in this.paymentMethodsInterfaces) {
350
+ this.paymentMethodsInterfaces[method].setDisabled(disabled);
351
+ }
352
+ }
353
+ async renderCardCheckout({ onSubmit, cardSelectors, onInputChange, }) {
354
+ try {
355
+ const elements = this.initializeCardElements(cardSelectors);
356
+ const pmManager = await this.headless.createPaymentMethodManager('PAYMENT_CARD');
357
+ if (!pmManager) {
358
+ throw new Error('Payment method manager is not available');
359
+ }
360
+ const { cardNumberInput, expiryInput, cvvInput } = pmManager.createHostedInputs();
361
+ const validateForm = async () => {
362
+ if (!pmManager)
363
+ return false;
364
+ const { valid, validationErrors } = await pmManager.validate();
365
+ const cardHolderError = validationErrors.find(v => v.name === 'cardholderName');
366
+ dispatchError('cardholderName', cardHolderError?.message || null);
367
+ return valid;
368
+ };
369
+ const dispatchError = (inputName, error) => {
370
+ onInputChange(inputName, error);
371
+ };
372
+ const onHostedInputChange = (name) => (event) => {
373
+ const input = event;
374
+ if (input.submitted) {
375
+ dispatchError(name, input.error);
376
+ }
377
+ };
378
+ const cardHolderOnChange = async (e) => {
379
+ pmManager.setCardholderName(e.target.value);
380
+ dispatchError('cardholderName', null);
381
+ };
382
+ elements.cardholderName?.addEventListener('input', cardHolderOnChange);
383
+ cardNumberInput.addEventListener('change', onHostedInputChange('cardNumber'));
384
+ expiryInput.addEventListener('change', onHostedInputChange('expiryDate'));
385
+ cvvInput.addEventListener('change', onHostedInputChange('cvv'));
386
+ const onSubmitHandler = async () => {
387
+ if (!(await validateForm())) {
388
+ return;
389
+ }
390
+ try {
391
+ onSubmit(true);
392
+ await pmManager.submit();
393
+ }
394
+ catch (error) {
395
+ throw new PrimerError('Failed to submit payment', error);
396
+ }
397
+ finally {
398
+ onSubmit(false);
399
+ }
400
+ };
401
+ elements.button.addEventListener('click', onSubmitHandler);
402
+ await Promise.all([
403
+ cardNumberInput.render(elements.cardNumber, {
404
+ placeholder: '1234 1234 1234 1234',
405
+ ariaLabel: 'Card number',
406
+ style: inputStyle,
407
+ }),
408
+ expiryInput.render(elements.expiryDate, {
409
+ placeholder: 'MM/YY',
410
+ ariaLabel: 'Expiry date',
411
+ style: inputStyle,
412
+ }),
413
+ cvvInput.render(elements.cvv, {
414
+ placeholder: '123',
415
+ ariaLabel: 'CVV',
416
+ style: inputStyle,
417
+ }),
418
+ ]);
419
+ this.destroyCallbacks.push(() => {
420
+ pmManager.removeHostedInputs();
421
+ elements.cardholderName.removeEventListener('change', cardHolderOnChange);
422
+ });
423
+ return {
424
+ setDisabled: (disabled) => {
425
+ cardNumberInput.setDisabled(disabled);
426
+ expiryInput.setDisabled(disabled);
427
+ cvvInput.setDisabled(disabled);
428
+ elements.button.disabled = disabled;
429
+ },
430
+ };
431
+ }
432
+ catch (error) {
433
+ throw new PrimerError('Failed to initialize Primer checkout', error);
434
+ }
435
+ }
436
+ async renderButton(allowedPaymentMethod, { container, }) {
437
+ const containerEl = this.validateContainer(container);
438
+ let button;
439
+ this.ensurePrimerAvailable();
440
+ if (!this.headless) {
441
+ throw new PrimerError('Headless checkout not found');
442
+ }
443
+ try {
444
+ const pmManager = await this.headless.createPaymentMethodManager(allowedPaymentMethod);
445
+ if (!pmManager) {
446
+ throw new Error('Payment method manager is not available');
447
+ }
448
+ button = pmManager.createButton();
449
+ await button.render(containerEl, {});
450
+ this.destroyCallbacks.push(() => button.clean());
451
+ }
452
+ catch (error) {
453
+ throw new PrimerError('Failed to initialize Primer checkout', error);
454
+ }
455
+ }
456
+ async renderCheckout(clientToken, options) {
457
+ const { cardSelectors, paymentButtonSelectors, container, onTokenizeSuccess, onResumeSuccess, onSubmit, onInputChange, onMethodRender, ...restPrimerOptions } = options;
458
+ await this.createHeadlessCheckout(clientToken, {
459
+ ...restPrimerOptions,
460
+ onTokenizeSuccess: this.wrapTokenizeHandler(onTokenizeSuccess),
461
+ onResumeSuccess: this.wrapResumeHandler(onResumeSuccess),
462
+ onAvailablePaymentMethodsLoad: (items) => {
463
+ this.availableMethods = ALLOWED_PAYMENT_METHODS.filter(method => items.some((item) => item.type === method));
464
+ if (this.availableMethods.length === 0) {
465
+ throw new PrimerError('No allowed payment methods found');
466
+ }
467
+ },
468
+ onCheckoutFail: error => {
469
+ // eslint-disable-next-line no-console
470
+ console.error(error);
471
+ },
472
+ onTokenizeError: error => {
473
+ // eslint-disable-next-line no-console
474
+ console.error(error);
475
+ },
476
+ });
477
+ const methodOptions = {
478
+ cardSelectors,
479
+ container,
480
+ onSubmit,
481
+ onInputChange,
482
+ };
483
+ this.availableMethods.forEach(async (method) => {
484
+ if (method === PaymentMethod.PAYMENT_CARD) {
485
+ await this.renderCardCheckout(methodOptions);
486
+ onMethodRender(PaymentMethod.PAYMENT_CARD);
487
+ }
488
+ else {
489
+ const container = method === PaymentMethod.PAYPAL
490
+ ? paymentButtonSelectors.paypal
491
+ : method === PaymentMethod.GOOGLE_PAY
492
+ ? paymentButtonSelectors.googlePay
493
+ : paymentButtonSelectors.applePay;
494
+ await this.renderButton(method, { container });
495
+ onMethodRender(method);
496
+ }
497
+ });
498
+ this.isInitialized = true;
499
+ }
500
+ wrapTokenizeHandler(handler) {
501
+ return async (paymentMethodTokenData, primerHandler) => {
502
+ try {
503
+ await handler(paymentMethodTokenData, primerHandler);
504
+ }
505
+ catch (error) {
506
+ // eslint-disable-next-line no-console
507
+ console.error('Error in tokenize handler:', error);
508
+ primerHandler.handleFailure('Payment processing failed. Please try again.');
509
+ }
510
+ };
511
+ }
512
+ wrapResumeHandler(handler) {
513
+ return async (resumeTokenData, primerHandler) => {
514
+ try {
515
+ await handler(resumeTokenData, primerHandler);
516
+ }
517
+ catch (error) {
518
+ // eslint-disable-next-line no-console
519
+ console.error('Error in resume handler:', error);
520
+ primerHandler.handleFailure('Payment processing failed. Please try again.');
521
+ }
522
+ };
523
+ }
524
+ async destroy() {
525
+ if (this.destroyCallbacks) {
526
+ try {
527
+ Promise.all(this.destroyCallbacks.map(destroy => destroy()));
528
+ }
529
+ catch (error) {
530
+ // eslint-disable-next-line no-console
531
+ console.warn('Error destroying Primer checkout:', error);
532
+ }
533
+ }
534
+ this.destroyCallbacks = [];
535
+ this.isInitialized = false;
536
+ }
537
+ createHandlers(handlers) {
538
+ return {
539
+ handleSuccess: () => {
540
+ if (handlers.onSuccess)
541
+ handlers.onSuccess();
542
+ },
543
+ handleFailure: (message) => {
544
+ if (handlers.onError)
545
+ handlers.onError(new Error(message));
546
+ },
547
+ continueWithNewClientToken: (newClientToken) => {
548
+ if (handlers.onActionRequired)
549
+ handlers.onActionRequired(newClientToken);
550
+ },
551
+ };
552
+ }
553
+ getCurrentCheckout() {
554
+ return this.destroyCallbacks;
555
+ }
556
+ isActive() {
557
+ return this.isInitialized && this.destroyCallbacks.length;
558
+ }
559
+ validateContainer(selector) {
560
+ const element = document.querySelector(selector);
561
+ if (!element) {
562
+ throw new PrimerError(`Checkout container not found: ${selector}`);
563
+ }
564
+ const computedStyle = window.getComputedStyle(element);
565
+ if (computedStyle.display === 'none') {
566
+ // eslint-disable-next-line no-console
567
+ console.warn('Checkout container is hidden, this may cause display issues');
568
+ }
569
+ return element;
570
+ }
842
571
  }
843
572
 
844
573
  /**
845
- * Resumes a payment with 3DS or similar flows
846
- * @param {Object} params - Resume parameters
847
- * @param {string} params.orderId - Order identifier
848
- * @param {string} params.resumeToken - Resume token from Primer
849
- * @returns {Promise<Object>} Resume response
574
+ * @fileoverview Input validation utilities for Funnefox SDK
850
575
  */
851
- async resumePayment(params) {
852
- const payload = {
853
- order_id: params.orderId,
854
- resume_token: params.resumeToken
855
- };
856
- return await this.request('/v1/checkout/resume_payment', {
857
- method: 'POST',
858
- body: JSON.stringify(payload)
859
- });
576
+ function sanitizeString(input) {
577
+ if (input === null || input === undefined) {
578
+ return '';
579
+ }
580
+ return String(input).trim();
860
581
  }
861
-
862
- /**
863
- * One-click payment for returning customers with saved payment methods
864
- * @param {Object} params - One-click payment parameters
865
- * @param {string} params.externalId - External user identifier
866
- * @param {string} params.priceId - Price point identifier
867
- * @param {Object} [params.clientMetadata] - Additional metadata
868
- * @returns {Promise<Object>} Payment response
869
- */
870
- async oneClickPayment(params) {
871
- const payload = {
872
- external_id: params.externalId,
873
- pp_ident: params.priceId,
874
- client_metadata: params.clientMetadata || {}
875
- };
876
- return await this.request('/v1/checkout/one_click', {
877
- method: 'POST',
878
- body: JSON.stringify(payload)
879
- });
582
+ function requireString(value, fieldName) {
583
+ const sanitized = sanitizeString(value);
584
+ if (sanitized.length === 0) {
585
+ throw new ValidationError(fieldName, 'must be a non-empty string', value);
586
+ }
587
+ return true;
880
588
  }
881
589
 
882
590
  /**
883
- * Processes session creation response (create_client_session)
884
- * @param {Object} response - Raw API response
885
- * @returns {Object} Processed session data
886
- * @throws {APIError} If response indicates an error
591
+ * @fileoverview API client for Funnefox backend integration
887
592
  */
888
- processSessionResponse(response) {
889
- if (response.status === 'error') {
890
- // Extract error details from error array format
891
- const firstError = response.error?.[0];
892
- const message = firstError?.msg || 'Session creation failed';
893
- throw new APIError(message, null, {
894
- errorCode: firstError?.code,
895
- errorType: firstError?.type,
896
- requestId: response.req_id,
897
- response
898
- });
899
- }
900
- const data = response.data || response;
901
- return {
902
- type: 'session_created',
903
- orderId: data.order_id,
904
- clientToken: data.client_token
905
- };
593
+ class APIClient {
594
+ constructor(config) {
595
+ this.baseUrl = config.baseUrl.replace(/\/$/, '');
596
+ this.orgId = config.orgId;
597
+ this.timeout = config.timeout || 30000;
598
+ this.retryAttempts = config.retryAttempts || 3;
599
+ }
600
+ async request(endpoint, options = {}) {
601
+ const url = `${this.baseUrl}/${this.orgId}${endpoint}`;
602
+ const requestOptions = {
603
+ method: 'GET',
604
+ headers: {
605
+ 'Content-Type': 'application/json',
606
+ ...(options.headers || {}),
607
+ },
608
+ ...options,
609
+ };
610
+ try {
611
+ return await retry(async () => {
612
+ return await withTimeout(this._makeRequest(url, requestOptions), this.timeout, 'Request timed out');
613
+ }, this.retryAttempts);
614
+ }
615
+ catch (error) {
616
+ if (error.name === 'APIError') {
617
+ throw error;
618
+ }
619
+ throw new NetworkError('Network request failed', error);
620
+ }
621
+ }
622
+ async _makeRequest(url, options) {
623
+ let response;
624
+ try {
625
+ response = await fetch(url, options);
626
+ }
627
+ catch (error) {
628
+ throw new NetworkError('Network request failed', error);
629
+ }
630
+ let data;
631
+ try {
632
+ data = await response.json();
633
+ }
634
+ catch {
635
+ throw new APIError('Invalid JSON response', response.status, {});
636
+ }
637
+ if (!response.ok) {
638
+ const message = data.message || data.message || data.error || `HTTP ${response.status}`;
639
+ throw new APIError(message?.[0]?.msg || message, response.status, {
640
+ response: data,
641
+ });
642
+ }
643
+ return data;
644
+ }
645
+ async createClientSession(params) {
646
+ const payload = {
647
+ region: params.region || 'default',
648
+ integration_type: 'primer',
649
+ pp_ident: params.priceId,
650
+ external_id: params.externalId,
651
+ email_address: params.email,
652
+ client_metadata: params.clientMetadata || {},
653
+ };
654
+ if (params.countryCode !== undefined) {
655
+ payload.country_code = params.countryCode;
656
+ }
657
+ return await this.request(API_ENDPOINTS.CREATE_CLIENT_SESSION, {
658
+ method: 'POST',
659
+ body: JSON.stringify(payload),
660
+ });
661
+ }
662
+ async updateClientSession(params) {
663
+ const payload = {
664
+ order_id: params.orderId,
665
+ client_token: params.clientToken,
666
+ pp_ident: params.priceId,
667
+ };
668
+ return await this.request(API_ENDPOINTS.UPDATE_CLIENT_SESSION, {
669
+ method: 'POST',
670
+ body: JSON.stringify(payload),
671
+ });
672
+ }
673
+ async createPayment(params) {
674
+ const payload = {
675
+ order_id: params.orderId,
676
+ payment_method_token: params.paymentMethodToken,
677
+ };
678
+ return await this.request(API_ENDPOINTS.CREATE_PAYMENT, {
679
+ method: 'POST',
680
+ body: JSON.stringify(payload),
681
+ });
682
+ }
683
+ async resumePayment(params) {
684
+ const payload = {
685
+ order_id: params.orderId,
686
+ resume_token: params.resumeToken,
687
+ };
688
+ return await this.request(API_ENDPOINTS.RESUME_PAYMENT, {
689
+ method: 'POST',
690
+ body: JSON.stringify(payload),
691
+ });
692
+ }
693
+ processSessionResponse(response) {
694
+ if (response.status === 'error') {
695
+ const firstError = response.error?.[0];
696
+ const message = firstError?.msg || 'Session creation failed';
697
+ throw new APIError(message, null, {
698
+ errorCode: firstError?.code,
699
+ errorType: firstError?.type,
700
+ requestId: response.req_id,
701
+ response,
702
+ });
703
+ }
704
+ const data = response.data || response;
705
+ return {
706
+ type: 'session_created',
707
+ orderId: data.order_id,
708
+ clientToken: data.client_token,
709
+ };
710
+ }
711
+ processPaymentResponse(response) {
712
+ if (response.status === 'error') {
713
+ const firstError = response.error?.[0];
714
+ const message = firstError?.msg || 'Payment request failed';
715
+ throw new APIError(message, null, {
716
+ errorCode: firstError?.code,
717
+ errorType: firstError?.type,
718
+ requestId: response.req_id,
719
+ response,
720
+ });
721
+ }
722
+ const data = response.data || response;
723
+ if (data.action_required_token) {
724
+ return {
725
+ type: 'action_required',
726
+ orderId: data.order_id,
727
+ clientToken: data.action_required_token,
728
+ };
729
+ }
730
+ if (data.checkout_status) {
731
+ switch (data.checkout_status) {
732
+ case 'succeeded':
733
+ return {
734
+ type: 'success',
735
+ orderId: data.order_id,
736
+ status: 'succeeded',
737
+ transactionId: data.transaction_id,
738
+ };
739
+ case 'failed':
740
+ throw new APIError(data.failed_message_for_user || 'Payment failed', null, data);
741
+ case 'cancelled':
742
+ throw new APIError('Payment was cancelled by user', null, data);
743
+ case 'processing':
744
+ return {
745
+ type: 'processing',
746
+ orderId: data.order_id,
747
+ status: 'processing',
748
+ };
749
+ default:
750
+ throw new APIError(`Unhandled checkout status: ${data.checkout_status}`, null, data);
751
+ }
752
+ }
753
+ throw new APIError('Invalid payment response format', null, data);
754
+ }
755
+ processResponse(response) {
756
+ const data = response.data || response;
757
+ if (data.client_token && data.order_id && !data.checkout_status) {
758
+ return this.processSessionResponse(response);
759
+ }
760
+ if (data.checkout_status || data.action_required_token) {
761
+ return this.processPaymentResponse(response);
762
+ }
763
+ throw new APIError('Unknown response format', null, response);
764
+ }
906
765
  }
907
766
 
908
767
  /**
909
- * Processes payment/resume response (create_payment, resume_payment, one_click)
910
- * @param {Object} response - Raw API response
911
- * @returns {Object} Processed payment data
912
- * @throws {APIError} If response indicates an error
768
+ * @fileoverview Checkout instance manager for Funnefox SDK
913
769
  */
914
- processPaymentResponse(response) {
915
- if (response.status === 'error') {
916
- // Extract error details from error array format
917
- const firstError = response.error?.[0];
918
- const message = firstError?.msg || 'Payment request failed';
919
- throw new APIError(message, null, {
920
- errorCode: firstError?.code,
921
- errorType: firstError?.type,
922
- requestId: response.req_id,
923
- response
924
- });
925
- }
926
- const data = response.data || response;
927
-
928
- // Check for action required (3DS, etc.)
929
- if (data.action_required_token) {
930
- return {
931
- type: 'action_required',
932
- orderId: data.order_id,
933
- clientToken: data.action_required_token
934
- };
935
- }
936
-
937
- // Handle checkout status
938
- if (data.checkout_status) {
939
- switch (data.checkout_status) {
940
- case 'succeeded':
770
+ class CheckoutInstance extends EventEmitter {
771
+ constructor(config) {
772
+ super();
773
+ this.counter = 0;
774
+ this.handleInputChange = (inputName, error) => {
775
+ this.emit(EVENTS.INPUT_ERROR, { name: inputName, error });
776
+ };
777
+ this.handleMethodRender = (method) => {
778
+ this.emit(EVENTS.METHOD_RENDER, method);
779
+ };
780
+ this.handleSubmit = (isSubmitting) => {
781
+ this.onLoaderChangeWithRace(isSubmitting);
782
+ // Clear any previous errors
783
+ this.emit(EVENTS.ERROR);
784
+ this._setState(isSubmitting ? 'processing' : 'ready');
785
+ };
786
+ this.handleTokenizeSuccess = async (paymentMethodTokenData, primerHandler) => {
787
+ try {
788
+ this.onLoaderChangeWithRace(true);
789
+ this._setState('processing');
790
+ const paymentResponse = await this.apiClient.createPayment({
791
+ orderId: this.orderId,
792
+ paymentMethodToken: paymentMethodTokenData.token,
793
+ });
794
+ const result = this.apiClient.processPaymentResponse(paymentResponse);
795
+ await this._processPaymentResult(result, primerHandler);
796
+ }
797
+ catch (error) {
798
+ this._setState('error');
799
+ this.emit(EVENTS.ERROR, error);
800
+ primerHandler.handleFailure(error.message || 'Payment processing failed');
801
+ }
802
+ finally {
803
+ this.onLoaderChangeWithRace(false);
804
+ this._setState('ready');
805
+ }
806
+ };
807
+ this.handleResumeSuccess = async (resumeTokenData, primerHandler) => {
808
+ try {
809
+ this.onLoaderChangeWithRace(true);
810
+ this._setState('processing');
811
+ const resumeResponse = await this.apiClient.resumePayment({
812
+ orderId: this.orderId,
813
+ resumeToken: resumeTokenData.resumeToken,
814
+ });
815
+ const result = this.apiClient.processPaymentResponse(resumeResponse);
816
+ await this._processPaymentResult(result, primerHandler);
817
+ }
818
+ catch (error) {
819
+ this._setState('error');
820
+ this.emit(EVENTS.ERROR, error);
821
+ primerHandler.handleFailure(error.message || 'Payment processing failed');
822
+ }
823
+ finally {
824
+ this.onLoaderChangeWithRace(false);
825
+ this._setState('ready');
826
+ }
827
+ };
828
+ this.onLoaderChangeWithRace = (state) => {
829
+ const isLoading = !!(state ? ++this.counter : --this.counter);
830
+ this.emit(EVENTS.LOADER_CHANGE, isLoading);
831
+ };
832
+ this.id = generateId('checkout_');
833
+ this.orgId = config.orgId;
834
+ this.baseUrl = config.baseUrl;
835
+ this.region = config.region;
836
+ this.checkoutConfig = { ...config.checkoutConfig };
837
+ this.callbacks = {
838
+ onSuccess: this.checkoutConfig.onSuccess,
839
+ onError: this.checkoutConfig.onError,
840
+ onStatusChange: this.checkoutConfig.onStatusChange,
841
+ onDestroy: this.checkoutConfig.onDestroy,
842
+ };
843
+ delete this.checkoutConfig?.onSuccess;
844
+ delete this.checkoutConfig?.onError;
845
+ delete this.checkoutConfig?.onStatusChange;
846
+ delete this.checkoutConfig?.onDestroy;
847
+ this.state = 'initializing';
848
+ this.orderId = null;
849
+ this.clientToken = null;
850
+ this.primerWrapper = new PrimerWrapper();
851
+ this.isDestroyed = false;
852
+ this._setupCallbackBridges();
853
+ }
854
+ _setupCallbackBridges() {
855
+ if (this.callbacks.onSuccess) {
856
+ this.on(EVENTS.SUCCESS, this.callbacks.onSuccess);
857
+ }
858
+ if (this.callbacks.onError) {
859
+ this.on(EVENTS.ERROR, this.callbacks.onError);
860
+ }
861
+ if (this.callbacks.onStatusChange) {
862
+ this.on(EVENTS.STATUS_CHANGE, this.callbacks.onStatusChange);
863
+ }
864
+ if (this.callbacks.onDestroy) {
865
+ this.on(EVENTS.DESTROY, this.callbacks.onDestroy);
866
+ }
867
+ }
868
+ on(eventName, handler) {
869
+ return super.on(eventName, handler);
870
+ }
871
+ once(eventName, handler) {
872
+ return super.once(eventName, handler);
873
+ }
874
+ off(eventName, handler = null) {
875
+ return super.off(eventName, handler);
876
+ }
877
+ emit(eventName, ...args) {
878
+ return super.emit(eventName, ...args);
879
+ }
880
+ removeAllListeners() {
881
+ return super.removeAllListeners();
882
+ }
883
+ async initialize() {
884
+ try {
885
+ this._setState('initializing');
886
+ this.apiClient = new APIClient({
887
+ baseUrl: this.baseUrl || DEFAULTS.BASE_URL,
888
+ orgId: this.orgId,
889
+ timeout: DEFAULTS.REQUEST_TIMEOUT,
890
+ retryAttempts: DEFAULTS.RETRY_ATTEMPTS,
891
+ });
892
+ const sessionResponse = await this.apiClient.createClientSession({
893
+ priceId: this.checkoutConfig.priceId,
894
+ externalId: this.checkoutConfig.customer.externalId,
895
+ email: this.checkoutConfig.customer.email,
896
+ region: this.region || DEFAULTS.REGION,
897
+ clientMetadata: this.checkoutConfig.clientMetadata,
898
+ countryCode: this.checkoutConfig.customer.countryCode,
899
+ });
900
+ const sessionData = this.apiClient.processSessionResponse(sessionResponse);
901
+ this.orderId = sessionData.orderId;
902
+ this.clientToken = sessionData.clientToken;
903
+ await this._initializePrimerCheckout();
904
+ this._setState('ready');
905
+ return this;
906
+ }
907
+ catch (error) {
908
+ this._setState('error');
909
+ this.emit(EVENTS.ERROR, error);
910
+ throw error;
911
+ }
912
+ }
913
+ async _initializePrimerCheckout() {
914
+ const checkoutOptions = {
915
+ ...this.checkoutConfig,
916
+ onTokenizeSuccess: this.handleTokenizeSuccess,
917
+ onResumeSuccess: this.handleResumeSuccess,
918
+ onSubmit: this.handleSubmit,
919
+ onInputChange: this.handleInputChange,
920
+ onMethodRender: this.handleMethodRender,
921
+ };
922
+ if (!this.checkoutConfig.cardSelectors ||
923
+ !this.checkoutConfig.paymentButtonSelectors) {
924
+ const cardSelectors = await this.createCardElements(this.checkoutConfig.container);
925
+ const paymentButtonSelectors = {
926
+ paypal: '#paypalButton',
927
+ googlePay: '#googlePayButton',
928
+ applePay: '#applePayButton',
929
+ };
930
+ checkoutOptions.cardSelectors = cardSelectors;
931
+ checkoutOptions.paymentButtonSelectors = paymentButtonSelectors;
932
+ checkoutOptions.card = {
933
+ cardholderName: {
934
+ required: false,
935
+ },
936
+ };
937
+ checkoutOptions.applePay = {
938
+ buttonStyle: 'black',
939
+ };
940
+ checkoutOptions.paypal = {
941
+ buttonColor: 'gold',
942
+ buttonShape: 'pill',
943
+ };
944
+ checkoutOptions.googlePay = {
945
+ buttonColor: 'black',
946
+ };
947
+ }
948
+ await this.primerWrapper.renderCheckout(this.clientToken, checkoutOptions);
949
+ }
950
+ async _processPaymentResult(result, primerHandler) {
951
+ if (result.orderId) {
952
+ this.orderId = result.orderId;
953
+ }
954
+ switch (result.type) {
955
+ case 'success':
956
+ this._setState('completed');
957
+ this.emit(EVENTS.SUCCESS, {
958
+ orderId: result.orderId,
959
+ status: result.status,
960
+ transactionId: result.transactionId,
961
+ metadata: result.metadata,
962
+ });
963
+ primerHandler.handleSuccess();
964
+ break;
965
+ case 'action_required':
966
+ this._setState('action_required');
967
+ this.clientToken = result.clientToken;
968
+ primerHandler.continueWithNewClientToken(result.clientToken);
969
+ break;
970
+ case 'processing':
971
+ this._setState('processing');
972
+ setTimeout(() => {
973
+ primerHandler.handleFailure('Payment is still processing. Please check back later.');
974
+ }, 30000);
975
+ break;
976
+ default:
977
+ throw new CheckoutError(`Unknown payment result type: ${result.type}`);
978
+ }
979
+ }
980
+ async updatePrice(newPriceId) {
981
+ this._ensureNotDestroyed();
982
+ requireString(newPriceId, 'priceId');
983
+ if (this.state === 'processing') {
984
+ throw new CheckoutError('Cannot update price while payment is processing');
985
+ }
986
+ try {
987
+ this._setState('updating');
988
+ await this.apiClient.updateClientSession({
989
+ orderId: this.orderId,
990
+ clientToken: this.clientToken,
991
+ priceId: newPriceId,
992
+ });
993
+ this.checkoutConfig.priceId = newPriceId;
994
+ this._setState('ready');
995
+ this.emit(EVENTS.STATUS_CHANGE, 'price-updated');
996
+ }
997
+ catch (error) {
998
+ this._setState('error');
999
+ this.emit(EVENTS.ERROR, error);
1000
+ throw error;
1001
+ }
1002
+ }
1003
+ getStatus() {
941
1004
  return {
942
- type: 'success',
943
- orderId: data.order_id,
944
- status: 'succeeded',
945
- transactionId: data.transaction_id
1005
+ id: this.id,
1006
+ state: this.state,
1007
+ orderId: this.orderId,
1008
+ priceId: this.checkoutConfig.priceId,
1009
+ isDestroyed: this.isDestroyed,
1010
+ };
1011
+ }
1012
+ async destroy() {
1013
+ if (this.isDestroyed)
1014
+ return;
1015
+ try {
1016
+ await this.primerWrapper.destroy();
1017
+ this._setState('destroyed');
1018
+ this.orderId = null;
1019
+ this.clientToken = null;
1020
+ this.isDestroyed = true;
1021
+ this.emit(EVENTS.DESTROY);
1022
+ this.removeAllListeners();
1023
+ }
1024
+ catch (error) {
1025
+ // eslint-disable-next-line no-console
1026
+ console.warn('Error during checkout cleanup:', error);
1027
+ }
1028
+ }
1029
+ _setState(newState) {
1030
+ if (this.state !== newState) {
1031
+ const oldState = this.state;
1032
+ this.state = newState;
1033
+ this.emit(EVENTS.STATUS_CHANGE, newState, oldState);
1034
+ }
1035
+ }
1036
+ _ensureNotDestroyed() {
1037
+ if (this.isDestroyed) {
1038
+ throw new CheckoutError('Checkout instance has been destroyed');
1039
+ }
1040
+ }
1041
+ getContainer() {
1042
+ return document.querySelector(this.checkoutConfig.container);
1043
+ }
1044
+ isInState(state) {
1045
+ return this.state === state;
1046
+ }
1047
+ isReady() {
1048
+ return this.state === 'ready' && !this.isDestroyed;
1049
+ }
1050
+ isProcessing() {
1051
+ return ['processing', 'action_required'].includes(this.state);
1052
+ }
1053
+ // Creates containers to render hosted inputs with labels and error messages,
1054
+ // a card holder input with label and error, and a submit button.
1055
+ async createCardElements(container) {
1056
+ await Promise.resolve().then(function () { return index$1; })
1057
+ .then(module => module.default)
1058
+ .then(init => init(this.checkoutConfig.container));
1059
+ const cardNumberContainer = document.querySelector(`${container} #cardNumberInput`);
1060
+ const cardExpiryContainer = document.querySelector(`${container} #expiryInput`);
1061
+ const cardCvvContainer = document.querySelector(`${container} #cvvInput`);
1062
+ const elementsMap = {
1063
+ cardNumber: cardNumberContainer.parentElement,
1064
+ expiryDate: cardExpiryContainer.parentElement,
1065
+ cvv: cardCvvContainer.parentElement,
1066
+ };
1067
+ const onLoaderChange = (isLoading) => {
1068
+ this.primerWrapper.disableButtons(isLoading);
1069
+ document
1070
+ .querySelectorAll(`${container} .loader-container`)
1071
+ ?.forEach(loaderEl => {
1072
+ loaderEl.style.display = isLoading ? 'flex' : 'none';
1073
+ });
946
1074
  };
947
- case 'failed':
948
- throw new APIError(data.failed_message_for_user || 'Payment failed', null, data);
949
- case 'cancelled':
950
- throw new APIError('Payment was cancelled by user', null, data);
951
- case 'processing':
1075
+ this.on(EVENTS.INPUT_ERROR, event => {
1076
+ const { name, error } = event;
1077
+ const errorContainer = elementsMap[name]?.querySelector('.errorContainer');
1078
+ if (errorContainer) {
1079
+ errorContainer.textContent = error || '';
1080
+ }
1081
+ });
1082
+ this.on(EVENTS.STATUS_CHANGE, (state, oldState) => {
1083
+ const isLoading = ['initializing'].includes(state);
1084
+ if (!isLoading && oldState === 'initializing') {
1085
+ onLoaderChange(false);
1086
+ }
1087
+ });
1088
+ function setError(error) {
1089
+ const errorContainer = document.querySelector('.payment-errors-container');
1090
+ if (errorContainer) {
1091
+ errorContainer.textContent = error?.message || '';
1092
+ }
1093
+ }
1094
+ this.on(EVENTS.ERROR, (error) => {
1095
+ setError(error);
1096
+ });
1097
+ this.on(EVENTS.LOADER_CHANGE, onLoaderChange);
1098
+ this.on(EVENTS.DESTROY, () => {
1099
+ this.primerWrapper.validateContainer(container)?.remove();
1100
+ });
1101
+ this.on(EVENTS.METHOD_RENDER, (method) => {
1102
+ const methodContainer = document.querySelector(`.ff-payment-method-${method.replace('_', '-').toLowerCase()}`);
1103
+ methodContainer.classList.add('visible');
1104
+ });
1105
+ this.on(EVENTS.SUCCESS, () => {
1106
+ const successScreenString = document.querySelector('#success-screen')?.innerHTML;
1107
+ const containers = document.querySelectorAll('.ff-payment-container');
1108
+ containers.forEach(container => {
1109
+ container.innerHTML = successScreenString;
1110
+ });
1111
+ onLoaderChange(false);
1112
+ });
952
1113
  return {
953
- type: 'processing',
954
- orderId: data.order_id,
955
- status: 'processing'
1114
+ cardNumber: '#cardNumberInput',
1115
+ expiryDate: '#expiryInput',
1116
+ cvv: '#cvvInput',
1117
+ cardholderName: '#cardHolderInput',
1118
+ button: '#submitButton',
956
1119
  };
957
- default:
958
- throw new APIError(`Unhandled checkout status: ${data.checkout_status}`, null, data);
959
1120
  }
960
- }
961
- throw new APIError('Invalid payment response format', null, data);
962
1121
  }
963
1122
 
964
1123
  /**
965
- * Processes API response and extracts relevant data
966
- * @deprecated Use processSessionResponse() or processPaymentResponse() instead
967
- * @param {Object} response - Raw API response
968
- * @returns {Object} Processed response data
969
- * @throws {APIError} If response indicates an error
1124
+ * @fileoverview Public API with configuration and orchestration logic
970
1125
  */
971
- processResponse(response) {
972
- // Try to detect response type and delegate to specialized methods
973
- const data = response.data || response;
974
- if (data.client_token && data.order_id && !data.checkout_status) {
975
- return this.processSessionResponse(response);
976
- }
977
- if (data.checkout_status || data.action_required_token) {
978
- return this.processPaymentResponse(response);
979
- }
980
-
981
- // Fallback for unknown response types
982
- throw new APIError('Unknown response format', null, response);
1126
+ let defaultConfig = null;
1127
+ function configure(config) {
1128
+ defaultConfig = config;
983
1129
  }
984
- }
985
-
986
- /**
987
- * @fileoverview Checkout instance manager for Funnefox SDK
988
- */
989
-
990
-
991
- /**
992
- * Checkout instance that manages the complete checkout lifecycle
993
- * @typedef {import('./types').CheckoutInstance} CheckoutInstance
994
- * @typedef {import('./types').CheckoutConfig} CheckoutConfig
995
- * @typedef {import('./types').CheckoutConfigWithCallbacks} CheckoutConfigWithCallbacks
996
- * @typedef {import('./types').PaymentResult} PaymentResult
997
- * @typedef {import('./types').CheckoutState} CheckoutState
998
- * @typedef {import('@primer-io/checkout-web').PaymentMethodTokenData} PaymentMethodTokenData
999
- * @typedef {import('@primer-io/checkout-web').ResumeTokenData} ResumeTokenData
1000
- * @typedef {import('@primer-io/checkout-web').ITokenizationHandler} PrimerHandler
1001
- * @typedef {import('@primer-io/checkout-web').UniversalCheckoutOptions} UniversalCheckoutOptions
1002
- */
1003
- class CheckoutInstance extends EventEmitter {
1004
- /**
1005
- * @param {Object} config - Checkout configuration
1006
- * @param {string} config.orgId - Organization ID
1007
- * @param {string} [config.baseUrl] - API base URL
1008
- * @param {string} [config.region] - Region
1009
- * @param {CheckoutConfig | CheckoutConfigWithCallbacks} config.checkoutConfig - Checkout configuration
1010
- */
1011
- constructor(config) {
1012
- super();
1013
- this.id = generateId('checkout_');
1014
- this.orgId = config.orgId;
1015
- this.baseUrl = config.baseUrl;
1016
- this.region = config.region;
1017
- this.checkoutConfig = {
1018
- ...config.checkoutConfig
1019
- };
1020
-
1021
- // Extract callbacks from config
1022
- this.callbacks = {
1023
- onSuccess: this.checkoutConfig.onSuccess,
1024
- onError: this.checkoutConfig.onError,
1025
- onStatusChange: this.checkoutConfig.onStatusChange,
1026
- onDestroy: this.checkoutConfig.onDestroy
1027
- };
1028
-
1029
- // Clean callbacks from config to avoid sending to API
1030
- delete this.checkoutConfig.onSuccess;
1031
- delete this.checkoutConfig.onError;
1032
- delete this.checkoutConfig.onStatusChange;
1033
- delete this.checkoutConfig.onDestroy;
1034
-
1035
- // Internal state
1036
- this.state = 'initializing';
1037
- this.orderId = null;
1038
- this.clientToken = null;
1039
- this.primerWrapper = new PrimerWrapper();
1040
- this.isDestroyed = false;
1041
-
1042
- // Set up callback bridges if provided
1043
- this._setupCallbackBridges();
1044
-
1045
- // Bind methods to preserve context
1046
- this._handleTokenizeSuccess = this._handleTokenizeSuccess.bind(this);
1047
- this._handleResumeSuccess = this._handleResumeSuccess.bind(this);
1048
- }
1049
-
1050
- /**
1051
- * Set up bridges between events and callbacks
1052
- * @private
1053
- */
1054
- _setupCallbackBridges() {
1055
- if (this.callbacks.onSuccess) {
1056
- this.on('success', this.callbacks.onSuccess);
1057
- }
1058
- if (this.callbacks.onError) {
1059
- this.on('error', this.callbacks.onError);
1060
- }
1061
- if (this.callbacks.onStatusChange) {
1062
- this.on('status-change', this.callbacks.onStatusChange);
1063
- }
1064
- if (this.callbacks.onDestroy) {
1065
- this.on('destroy', this.callbacks.onDestroy);
1066
- }
1130
+ function resolveConfig(options, functionName) {
1131
+ const { orgId, apiConfig } = options || {};
1132
+ const finalOrgId = orgId || defaultConfig?.orgId;
1133
+ if (!finalOrgId) {
1134
+ throw new Error(`orgId is required. Pass it to ${functionName}() or call configure() first.`);
1135
+ }
1136
+ const finalBaseUrl = apiConfig?.baseUrl || defaultConfig?.baseUrl || DEFAULTS.BASE_URL;
1137
+ const finalRegion = apiConfig?.region || defaultConfig?.region || DEFAULTS.REGION;
1138
+ return {
1139
+ orgId: finalOrgId,
1140
+ baseUrl: finalBaseUrl,
1141
+ region: finalRegion,
1142
+ };
1067
1143
  }
1068
-
1069
- /**
1070
- * Initialize the checkout instance
1071
- * @returns {Promise<CheckoutInstance>} Returns this instance for chaining
1072
- */
1073
- async initialize() {
1074
- try {
1075
- this._setState('initializing');
1076
-
1077
- // Validate container exists
1078
- validateContainer(this.checkoutConfig.container);
1079
-
1080
- // Create API client and session
1081
- this.apiClient = new APIClient({
1082
- baseUrl: this.baseUrl || DEFAULTS.BASE_URL,
1083
- orgId: this.orgId,
1084
- timeout: DEFAULTS.REQUEST_TIMEOUT,
1085
- retryAttempts: DEFAULTS.RETRY_ATTEMPTS
1086
- });
1087
- const sessionResponse = await this.apiClient.createClientSession({
1088
- priceId: this.checkoutConfig.priceId,
1089
- externalId: this.checkoutConfig.customer.externalId,
1090
- email: this.checkoutConfig.customer.email,
1091
- region: this.region || DEFAULTS.REGION,
1092
- clientMetadata: this.checkoutConfig.clientMetadata,
1093
- countryCode: this.checkoutConfig.customer.countryCode
1144
+ async function createCheckout(options) {
1145
+ const { ...checkoutConfig } = options;
1146
+ const primerWrapper = new PrimerWrapper();
1147
+ primerWrapper.ensurePrimerAvailable();
1148
+ const config = resolveConfig(options, 'createCheckout');
1149
+ const checkout = new CheckoutInstance({
1150
+ ...config,
1151
+ checkoutConfig,
1094
1152
  });
1095
- const sessionData = this.apiClient.processSessionResponse(sessionResponse);
1096
- this.orderId = sessionData.orderId;
1097
- this.clientToken = sessionData.clientToken;
1098
-
1099
- // Initialize Primer checkout
1100
- await this._initializePrimerCheckout();
1101
- this._setState('ready');
1102
- return this;
1103
- } catch (error) {
1104
- this._setState('error');
1105
- this.emit('error', error);
1106
- throw error;
1107
- }
1108
- }
1109
-
1110
- /**
1111
- * Initialize Primer checkout with current client token
1112
- * @private
1113
- */
1114
- async _initializePrimerCheckout() {
1115
- /** @type {Partial<UniversalCheckoutOptions> & {onTokenizeSuccess: function, onResumeSuccess: function}} */
1116
- const checkoutOptions = {
1117
- container: this.checkoutConfig.container,
1118
- onTokenizeSuccess: this._handleTokenizeSuccess,
1119
- onResumeSuccess: this._handleResumeSuccess,
1120
- ...(this.checkoutConfig.universalCheckoutOptions || {})
1121
- };
1122
- await this.primerWrapper.showUniversalCheckout(this.clientToken, checkoutOptions);
1153
+ await checkout.initialize();
1154
+ return checkout;
1123
1155
  }
1124
-
1125
- /**
1126
- * Handle successful payment method tokenization
1127
- * @private
1128
- * @param {PaymentMethodTokenData} paymentMethodTokenData - Payment method token from Primer
1129
- * @param {PrimerHandler} primerHandler - Primer handler for success/failure
1130
- */
1131
- async _handleTokenizeSuccess(paymentMethodTokenData, primerHandler) {
1132
- try {
1133
- this._setState('processing');
1134
- const paymentResponse = await this.apiClient.createPayment({
1135
- orderId: this.orderId,
1136
- paymentMethodToken: paymentMethodTokenData.token
1156
+ async function createClientSession(params) {
1157
+ const { priceId, externalId, email, clientMetadata, countryCode } = params;
1158
+ const config = resolveConfig(params, 'createClientSession');
1159
+ const apiClient = new APIClient({
1160
+ baseUrl: config.baseUrl,
1161
+ orgId: config.orgId,
1162
+ timeout: DEFAULTS.REQUEST_TIMEOUT,
1163
+ retryAttempts: DEFAULTS.RETRY_ATTEMPTS,
1137
1164
  });
1138
- const result = this.apiClient.processPaymentResponse(paymentResponse);
1139
- await this._processPaymentResult(result, primerHandler);
1140
- } catch (error) {
1141
- this._setState('error');
1142
- this.emit('error', error);
1143
- primerHandler.handleFailure(error.message || 'Payment processing failed');
1144
- }
1145
- }
1146
-
1147
- /**
1148
- * Handle successful payment resume (3DS flows)
1149
- * @private
1150
- * @param {ResumeTokenData} resumeTokenData - Resume token from Primer
1151
- * @param {PrimerHandler} primerHandler - Primer handler for success/failure
1152
- */
1153
- async _handleResumeSuccess(resumeTokenData, primerHandler) {
1154
- try {
1155
- this._setState('processing');
1156
- const resumeResponse = await this.apiClient.resumePayment({
1157
- orderId: this.orderId,
1158
- resumeToken: resumeTokenData.resumeToken
1165
+ const sessionResponse = await apiClient.createClientSession({
1166
+ priceId,
1167
+ externalId,
1168
+ email,
1169
+ region: config.region,
1170
+ clientMetadata,
1171
+ countryCode,
1159
1172
  });
1160
- const result = this.apiClient.processPaymentResponse(resumeResponse);
1161
- await this._processPaymentResult(result, primerHandler);
1162
- } catch (error) {
1163
- this._setState('error');
1164
- this.emit('error', error);
1165
- primerHandler.handleFailure(error.message || 'Payment processing failed');
1166
- }
1173
+ return apiClient.processSessionResponse(sessionResponse);
1167
1174
  }
1168
1175
 
1169
1176
  /**
1170
- * Process payment result and handle different scenarios
1171
- * @private
1177
+ * @fileoverview Main entry point for @funnelfox/billing
1172
1178
  */
1173
- async _processPaymentResult(result, primerHandler) {
1174
- // Update order ID if it changed
1175
- if (result.orderId) {
1176
- this.orderId = result.orderId;
1177
- }
1178
- switch (result.type) {
1179
- case 'success':
1180
- this._setState('completed');
1181
- this.emit('success', {
1182
- orderId: result.orderId,
1183
- status: result.status,
1184
- transactionId: result.transactionId,
1185
- metadata: result.metadata
1186
- });
1187
- primerHandler.handleSuccess();
1188
- break;
1189
- case 'action_required':
1190
- this._setState('action_required');
1191
- // Update client token and continue
1192
- this.clientToken = result.clientToken;
1193
- primerHandler.continueWithNewClientToken(result.clientToken);
1194
- break;
1195
- case 'processing':
1196
- this._setState('processing');
1197
- // Let the payment process - usually requires polling or webhook
1198
- setTimeout(() => {
1199
- primerHandler.handleFailure('Payment is still processing. Please check back later.');
1200
- }, 30000);
1201
- break;
1202
- default:
1203
- throw new CheckoutError(`Unknown payment result type: ${result.type}`);
1204
- }
1179
+ const Billing = {
1180
+ configure: configure,
1181
+ createCheckout: createCheckout,
1182
+ createClientSession: createClientSession,
1183
+ };
1184
+ if (typeof window !== 'undefined') {
1185
+ window.Billing = Billing;
1205
1186
  }
1206
1187
 
1207
- /**
1208
- * Update the checkout to use a different price
1209
- * @param {string} newPriceId - New price identifier
1210
- * @returns {Promise<void>}
1211
- */
1212
- async updatePrice(newPriceId) {
1213
- this._ensureNotDestroyed();
1214
- requireString(newPriceId, 'priceId');
1215
- if (this.state === 'processing') {
1216
- throw new CheckoutError('Cannot update price while payment is processing');
1217
- }
1218
- try {
1219
- this._setState('updating');
1220
-
1221
- // Update client session with new price
1222
- await this.apiClient.updateClientSession({
1223
- orderId: this.orderId,
1224
- clientToken: this.clientToken,
1225
- priceId: newPriceId
1188
+ var template = "<div class=\"ff-skin-default\">\n <!-- Apple Pay Section -->\n <div class=\"ff-payment-method-card ff-payment-method-apple-pay\">\n <label class=\"ff-payment-method-label\">\n <input type=\"radio\" name=\"payment-method\" value=\"apple-pay\" class=\"ff-payment-method-radio\">\n <div class=\"ff-payment-method-header\">\n <div class=\"ff-payment-logo ff-apple-pay-logo\">\n <svg class=\"payment-method-icon\" height=\"26\" viewBox=\"0 0 63 26\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M8.41022 4.82398C9.5916 4.92293 10.773 4.23026 11.5113 3.35205C12.2374 2.4491 12.7173 1.23692 12.5943 0C11.5483 0.0494767 10.2561 0.692674 9.51777 1.59562C8.84093 2.37488 8.26255 3.63654 8.41022 4.82398ZM22.4638 20.7555V1.47193H29.6628C33.3793 1.47193 35.9758 4.04471 35.9758 7.80494C35.9758 11.5652 33.33 14.1627 29.5644 14.1627H25.4418V20.7555H22.4638ZM12.5819 5.05898C11.5411 4.99877 10.5914 5.37358 9.82438 5.67633C9.33075 5.87116 8.91274 6.03614 8.59472 6.03614C8.23784 6.03614 7.80257 5.86234 7.31387 5.66719C6.6735 5.4115 5.94138 5.11916 5.17364 5.13319C3.41387 5.15793 1.77716 6.15983 0.878819 7.75545C-0.967091 10.9467 0.398882 15.6717 2.18326 18.2693C3.05699 19.5556 4.10301 20.9657 5.48129 20.9163C6.08765 20.8933 6.52383 20.7072 6.97524 20.5147C7.49492 20.293 8.0348 20.0628 8.87776 20.0628C9.69151 20.0628 10.2078 20.287 10.7033 20.5023C11.1746 20.707 11.6271 20.9036 12.2989 20.8915C13.7264 20.8668 14.6247 19.6051 15.4984 18.3187C16.4413 16.9381 16.8557 15.5906 16.9186 15.3861C16.9222 15.3745 16.9246 15.3665 16.9259 15.3625C16.9244 15.361 16.9128 15.3556 16.8922 15.3462C16.577 15.2011 14.1679 14.0926 14.1448 11.1199C14.1216 8.62473 16.0556 7.36054 16.3601 7.16153C16.3786 7.14944 16.3911 7.14125 16.3968 7.137C15.1662 5.30636 13.2464 5.10845 12.5819 5.05898ZM41.4153 20.9039C43.2858 20.9039 45.0209 19.9515 45.8085 18.4424H45.8701V20.7555H48.6266V11.157C48.6266 8.37393 46.4115 6.5804 43.0027 6.5804C39.8401 6.5804 37.5019 8.39866 37.4158 10.8972H40.0985C40.32 9.70979 41.4153 8.93054 42.9166 8.93054C44.7379 8.93054 45.7593 9.78401 45.7593 11.3549V12.4186L42.0429 12.6413C38.5849 12.8516 36.7143 14.274 36.7143 16.7479C36.7143 19.2464 38.6464 20.9039 41.4153 20.9039ZM42.215 18.6156C40.6275 18.6156 39.6184 17.8487 39.6184 16.6736C39.6184 15.4615 40.5906 14.7564 42.4488 14.6451L45.7591 14.4348V15.5233C45.7591 17.3292 44.2332 18.6156 42.215 18.6156ZM57.7699 21.51C56.5762 24.8868 55.2103 26 52.306 26C52.0845 26 51.3461 25.9753 51.1739 25.9258V23.6127C51.3585 23.6375 51.8138 23.6622 52.0476 23.6622C53.3643 23.6622 54.1027 23.1056 54.558 21.6584L54.8288 20.8049L49.7833 6.76594H52.8967L56.4039 18.1579H56.4655L59.9727 6.76594H63L57.7699 21.51ZM25.4416 3.99524H28.875C31.4592 3.99524 32.936 5.38059 32.936 7.81731C32.936 10.254 31.4592 11.6518 28.8627 11.6518H25.4416V3.99524Z\" fill=\"currentColor\"/>\n </svg>\n </div>\n </div>\n </label>\n <div class=\"ff-payment-method-content\">\n <div class=\"ff-apple-pay-button-container ff-payment-container\" id=\"applePayButton\"></div>\n <div class=\"loader-container payment-button-loader\">\n <div class=\"loader\"></div>\n </div>\n <p class=\"ff-security-message\">\n Your payment information is secure with Apple Pay encryption\n </p>\n </div>\n </div>\n\n <!-- Google Pay Section -->\n <div class=\"ff-payment-method-card ff-payment-method-google-pay\">\n <label class=\"ff-payment-method-label\">\n <input type=\"radio\" name=\"payment-method\" value=\"google-pay\" class=\"ff-payment-method-radio\" checked=\"checked\">\n <div class=\"ff-payment-method-header\">\n <div class=\"ff-payment-logo ff-google-pay-logo\">\n <svg class=\"payment-method-icon\" height=\"26\" viewBox=\"0 0 59 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M27.3601 11.6862V18.4674H25.208V1.72177H30.9132C32.3591 1.72177 33.592 2.20374 34.6008 3.16768C35.632 4.13162 36.1476 5.30852 36.1476 6.69838C36.1476 8.12187 35.632 9.29877 34.6008 10.2515C33.6032 11.2042 32.3703 11.675 30.9132 11.675H27.3601V11.6862ZM27.3601 3.78415V9.62382H30.958C31.8099 9.62382 32.5272 9.3324 33.0876 8.76076C33.6593 8.18912 33.9507 7.49419 33.9507 6.70959C33.9507 5.9362 33.6593 5.25248 33.0876 4.68084C32.5272 4.08678 31.8211 3.79536 30.958 3.79536H27.3601V3.78415Z\" fill=\"currentColor\"/>\n <path d=\"M41.7742 6.63107C43.3658 6.63107 44.6212 7.057 45.5403 7.90885C46.4594 8.7607 46.9189 9.9264 46.9189 11.4059V18.4673H44.8677V16.8757H44.7781C43.8926 18.1871 42.7045 18.8372 41.225 18.8372C39.9584 18.8372 38.9048 18.4673 38.0529 17.7164C37.2011 16.9654 36.7751 16.0351 36.7751 14.9142C36.7751 13.7261 37.2235 12.7846 38.1202 12.0896C39.0169 11.3835 40.2162 11.036 41.7069 11.036C42.9847 11.036 44.0383 11.2714 44.8565 11.7422V11.249C44.8565 10.498 44.5651 9.87035 43.9711 9.34355C43.377 8.81674 42.6821 8.55895 41.8863 8.55895C40.6869 8.55895 39.7342 9.06333 39.0393 10.0833L37.145 8.8952C38.1874 7.38205 39.7342 6.63107 41.7742 6.63107ZM38.9944 14.9478C38.9944 15.5083 39.2298 15.979 39.7118 16.3489C40.1826 16.7188 40.743 16.9093 41.3819 16.9093C42.2898 16.9093 43.0968 16.5731 43.8029 15.9006C44.5091 15.228 44.8677 14.4435 44.8677 13.5356C44.1952 13.0088 43.2649 12.7397 42.0656 12.7397C41.1913 12.7397 40.4628 12.9527 39.8799 13.3674C39.2859 13.8046 38.9944 14.3314 38.9944 14.9478Z\" fill=\"currentColor\"/>\n <path d=\"M58.6207 7.00095L51.4472 23.5H49.2279L51.8955 17.7276L47.1655 7.00095H49.5081L52.9155 15.228H52.9604L56.2781 7.00095H58.6207Z\" fill=\"currentColor\"/>\n <path d=\"M18.8001 10.3187C18.8001 9.61709 18.7373 8.94569 18.6208 8.30008H9.6001V11.9989L14.7953 12C14.5846 13.2307 13.9064 14.2799 12.8674 14.9793V17.379H15.9598C17.7655 15.7078 18.8001 13.2375 18.8001 10.3187Z\" fill=\"#4285F4\"/>\n <path d=\"M12.8685 14.9793C12.0076 15.5599 10.8991 15.8995 9.60228 15.8995C7.09717 15.8995 4.97202 14.2115 4.21096 11.9361H1.021V14.411C2.60141 17.5472 5.84965 19.6992 9.60228 19.6992C12.1959 19.6992 14.3749 18.8462 15.9609 17.3779L12.8685 14.9793Z\" fill=\"#34A853\"/>\n <path d=\"M3.91043 10.1002C3.91043 9.46129 4.01691 8.8437 4.21082 8.26309V5.78824H1.02086C0.367396 7.08507 -0.000244141 8.54891 -0.000244141 10.1002C-0.000244141 11.6514 0.368516 13.1153 1.02086 14.4121L4.21082 11.9373C4.01691 11.3567 3.91043 10.7391 3.91043 10.1002Z\" fill=\"#FABB05\"/>\n <path d=\"M9.60228 4.29974C11.0179 4.29974 12.2856 4.78731 13.2865 5.74004L16.027 3.00179C14.3626 1.45164 12.1926 0.500031 9.60228 0.500031C5.85077 0.500031 2.60141 2.65208 1.021 5.78824L4.21096 8.26309C4.97202 5.98775 7.09717 4.29974 9.60228 4.29974Z\" fill=\"#E94235\"/>\n </svg>\n </div>\n </div>\n </label>\n <div class=\"ff-payment-method-content\">\n <ul class=\"ff-payment-features\">\n <li class=\"ff-payment-feature\">\n <svg class=\"ff-checkmark-icon\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M20 6L9 17L4 12\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n </svg>\n <span>Easy and private payments with Face/Touch ID</span>\n </li>\n <li class=\"ff-payment-feature\">\n <svg class=\"ff-checkmark-icon\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M20 6L9 17L4 12\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n </svg>\n <span>Keeps your financial info safe with end-to-end encryption</span>\n </li>\n <li class=\"ff-payment-feature\">\n <svg class=\"ff-checkmark-icon\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M20 6L9 17L4 12\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n </svg>\n <span>Protected by Google Pay's unique Device Account Number</span>\n </li>\n </ul>\n <div class=\"ff-google-pay-button-container ff-payment-container\" id=\"googlePayButton\"></div>\n <p class=\"ff-security-message\">\n Your payment information is secure with SSL/TLS encryption\n </p>\n <div class=\"loader-container payment-button-loader\">\n <div class=\"loader\"></div>\n </div>\n </div>\n </div>\n <!-- PayPal Section -->\n <div class=\"ff-payment-method-card ff-payment-method-paypal\">\n <label class=\"ff-payment-method-label\">\n <input type=\"radio\" name=\"payment-method\" value=\"paypal\" class=\"ff-payment-method-radio\">\n <div class=\"ff-payment-method-header\">\n <div class=\"ff-payment-logo ff-paypal-logo\">\n <img class=\"payment-method-icon\" style=\"max-height: 22px\" src=\"https://assets.fnlfx.com/common/checkout/paypal.webp\" alt=\"PayPal logo\">\n </div>\n </div>\n </label>\n <div class=\"ff-payment-method-content\">\n <ul class=\"ff-payment-features\">\n <li class=\"ff-payment-feature\">\n <svg class=\"ff-checkmark-icon\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M20 6L9 17L4 12\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n </svg>\n <span> Fast, convenient payment option </span>\n </li>\n <li class=\"ff-payment-feature\">\n <svg class=\"ff-checkmark-icon\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M20 6L9 17L4 12\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n </svg>\n <span>\n Keeps your financial info safe with end-to-end encryption\n </span>\n </li>\n <li class=\"ff-payment-feature\">\n <svg class=\"ff-checkmark-icon\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M20 6L9 17L4 12\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n </svg>\n <span> Backed by PayPal’s industry-leading fraud protection </span>\n </li>\n </ul>\n <div class=\"ff-paypal-button-container ff-payment-container\" id=\"paypalButton\"></div>\n <div class=\"loader-container payment-button-loader\">\n <div class=\"loader\"></div>\n </div>\n </div>\n </div>\n\n <!-- Card Payments Section -->\n <div class=\"ff-payment-method-card ff-payment-method-payment-card\">\n <label class=\"ff-payment-method-label\">\n <input type=\"radio\" name=\"payment-method\" value=\"card\" class=\"ff-payment-method-radio\">\n <div class=\"ff-payment-method-header\">\n <div class=\"ff-card-logos\">\n <img class=\"payment-method-icon\" style=\"max-height: 30px\" src=\"https://assets.fnlfx.com/common/checkout/cards.webp\" alt=\"visa, mastercard\">\n </div>\n </div>\n </label>\n <div class=\"ff-payment-method-content\">\n <div class=\"ff-card-form-container ff-payment-container\" id=\"cardForm\">\n <div class=\"loader-container\">\n <div class=\"loader\"></div>\n </div>\n <div>\n <label class=\"ff-card-form-label\" for=\"cardNumberInput\">Card number</label>\n <div id=\"cardNumberInput\"></div>\n <div class=\"errorContainer\"></div>\n </div>\n <div class=\"card-form-row\">\n <div>\n <label class=\"ff-card-form-label\" for=\"expiryInput\">Expiration date</label>\n <div id=\"expiryInput\"></div>\n <div class=\"errorContainer\"></div>\n </div>\n <div>\n <label class=\"ff-card-form-label\" for=\"cvvInput\">Security code</label>\n <div id=\"cvvInput\">\n <svg width=\"200\" height=\"200\" viewBox=\"0 0 200 200\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <rect width=\"200\" height=\"200\" fill=\"transparent\"/>\n <g clip-path=\"url(#clip0_0_1)\">\n <path d=\"M157.555 23C168.279 23.0002 177 31.7394 177 42.4854V80.5889C171.946 72.0151 164.749 64.8558 156.146 59.8457H166.394V42.4854C166.393 37.6004 162.43 33.6291 157.555 33.6289H27.4453C22.5704 33.6291 18.6066 37.6004 18.6064 42.4854V59.8457H97.8535C88.9153 65.0512 81.4954 72.5771 76.4189 81.5986H18.6064V127.515C18.6066 132.4 22.5704 136.371 27.4453 136.371H75.3281C77.2742 140.177 79.6285 143.739 82.333 147H27.4453C16.7215 147 8.00019 138.261 8 127.515V42.4854C8.0002 31.7394 16.7215 23.0002 27.4453 23H157.555Z\" fill=\"#93939A\"/>\n <mask id=\"path-2-outside-1_0_1\" maskUnits=\"userSpaceOnUse\" x=\"68.5012\" y=\"52.0311\" width=\"135\" height=\"135\" fill=\"black\">\n <rect fill=\"white\" x=\"68.5012\" y=\"52.0311\" width=\"135\" height=\"135\"/>\n <path d=\"M190.242 160.457L170.136 140.351C166.533 145.552 162.023 150.063 156.821 153.666L176.927 173.772C178.693 175.538 181.088 176.53 183.585 176.53C186.082 176.53 188.477 175.538 190.242 173.772C192.008 172.007 193 169.612 193 167.115C193 164.618 192.008 162.223 190.242 160.457ZM126.436 157.901C116.955 157.901 107.688 155.089 99.8048 149.822C91.922 144.555 85.7781 137.069 82.15 128.31C78.5219 119.551 77.5727 109.913 79.4222 100.614C81.2718 91.3158 85.8371 82.7746 92.5409 76.0708C99.2447 69.367 107.786 64.8017 117.084 62.9521C126.383 61.1026 136.021 62.0518 144.78 65.6799C153.539 69.308 161.025 75.4519 166.292 83.3347C171.559 91.2175 174.371 100.485 174.371 109.966C174.371 122.679 169.32 134.871 160.331 143.861C151.341 152.85 139.149 157.901 126.436 157.901ZM163.719 109.966C163.719 102.592 161.532 95.3838 157.435 89.2527C153.339 83.1216 147.516 78.343 140.703 75.5212C133.891 72.6994 126.395 71.9611 119.162 73.3996C111.93 74.8382 105.287 78.389 100.073 83.6031C94.8591 88.8171 91.3083 95.4602 89.8697 102.692C88.4312 109.924 89.1695 117.421 91.9913 124.233C94.8131 131.046 99.5918 136.869 105.723 140.965C111.854 145.062 119.062 147.248 126.436 147.248C136.324 147.248 145.807 143.32 152.799 136.329C159.791 129.337 163.719 119.854 163.719 109.966ZM133.645 102.757C133.398 102.51 133.104 102.313 132.781 102.179C132.458 102.046 132.112 101.977 131.762 101.977C131.412 101.977 131.066 102.046 130.743 102.179C130.42 102.313 130.126 102.51 129.879 102.757L126.436 106.2L122.993 102.757C122.49 102.272 121.818 102.003 121.119 102.01C120.421 102.016 119.753 102.296 119.26 102.789C118.766 103.283 118.486 103.951 118.48 104.649C118.474 105.348 118.742 106.02 119.227 106.523L122.67 109.966L119.227 113.409C118.973 113.655 118.77 113.949 118.63 114.274C118.491 114.598 118.417 114.948 118.414 115.301C118.411 115.655 118.479 116.006 118.612 116.333C118.746 116.66 118.944 116.958 119.194 117.208C119.444 117.458 119.741 117.655 120.069 117.789C120.396 117.923 120.747 117.991 121.1 117.988C121.454 117.985 121.803 117.911 122.128 117.771C122.453 117.632 122.747 117.429 122.993 117.175L126.436 113.732L129.879 117.175C130.382 117.66 131.054 117.928 131.752 117.922C132.451 117.916 133.119 117.636 133.612 117.142C134.106 116.648 134.386 115.981 134.392 115.282C134.398 114.584 134.13 113.911 133.645 113.409L130.202 109.966L133.645 106.523C133.892 106.275 134.088 105.982 134.222 105.659C134.356 105.336 134.425 104.989 134.425 104.64C134.425 104.29 134.356 103.944 134.222 103.621C134.088 103.298 133.892 103.004 133.645 102.757ZM112.34 102.757C112.093 102.51 111.8 102.313 111.477 102.179C111.154 102.046 110.807 101.977 110.458 101.977C110.108 101.977 109.762 102.046 109.439 102.179C109.116 102.313 108.822 102.51 108.575 102.757L105.132 106.2L101.688 102.757C101.186 102.272 100.513 102.003 99.8151 102.01C99.1169 102.016 98.4489 102.296 97.9552 102.789C97.4614 103.283 97.1814 103.951 97.1753 104.649C97.1692 105.348 97.4377 106.02 97.9227 106.523L101.366 109.966L97.9227 113.409C97.6684 113.655 97.4655 113.949 97.326 114.274C97.1864 114.598 97.1129 114.948 97.1098 115.301C97.1068 115.655 97.1742 116.006 97.3081 116.333C97.442 116.66 97.6397 116.958 97.8897 117.208C98.1398 117.458 98.4371 117.655 98.7644 117.789C99.0917 117.923 99.4423 117.991 99.7959 117.988C100.15 117.985 100.499 117.911 100.824 117.771C101.149 117.632 101.443 117.429 101.688 117.175L105.132 113.732L108.575 117.175C109.077 117.66 109.75 117.928 110.448 117.922C111.146 117.916 111.814 117.636 112.308 117.142C112.802 116.648 113.082 115.981 113.088 115.282C113.094 114.584 112.826 113.911 112.34 113.409L108.897 109.966L112.34 106.523C112.588 106.275 112.784 105.982 112.918 105.659C113.052 105.336 113.121 104.989 113.121 104.64C113.121 104.29 113.052 103.944 112.918 103.621C112.784 103.298 112.588 103.004 112.34 102.757ZM151.506 109.966L154.949 106.523C155.434 106.02 155.703 105.348 155.697 104.649C155.691 103.951 155.41 103.283 154.917 102.789C154.423 102.296 153.755 102.016 153.057 102.01C152.359 102.003 151.686 102.272 151.184 102.757L147.74 106.2L144.297 102.757C143.795 102.272 143.122 102.003 142.424 102.01C141.726 102.016 141.058 102.296 140.564 102.789C140.07 103.283 139.79 103.951 139.784 104.649C139.778 105.348 140.046 106.02 140.531 106.523L143.974 109.966L140.531 113.409C140.277 113.655 140.074 113.949 139.935 114.274C139.795 114.598 139.722 114.948 139.719 115.301C139.715 115.655 139.783 116.006 139.917 116.333C140.051 116.66 140.248 116.958 140.498 117.208C140.748 117.458 141.046 117.655 141.373 117.789C141.7 117.923 142.051 117.991 142.405 117.988C142.758 117.985 143.108 117.911 143.433 117.771C143.757 117.632 144.051 117.429 144.297 117.175L147.74 113.732L151.184 117.175C151.686 117.66 152.359 117.928 153.057 117.922C153.755 117.916 154.423 117.636 154.917 117.142C155.41 116.648 155.691 115.981 155.697 115.282C155.703 114.584 155.434 113.911 154.949 113.409L151.506 109.966Z\"/>\n </mask>\n <path d=\"M190.242 160.457L170.136 140.351C166.533 145.552 162.023 150.063 156.821 153.666L176.927 173.772C178.693 175.538 181.088 176.53 183.585 176.53C186.082 176.53 188.477 175.538 190.242 173.772C192.008 172.007 193 169.612 193 167.115C193 164.618 192.008 162.223 190.242 160.457ZM126.436 157.901C116.955 157.901 107.688 155.089 99.8048 149.822C91.922 144.555 85.7781 137.069 82.15 128.31C78.5219 119.551 77.5727 109.913 79.4222 100.614C81.2718 91.3158 85.8371 82.7746 92.5409 76.0708C99.2447 69.367 107.786 64.8017 117.084 62.9521C126.383 61.1026 136.021 62.0518 144.78 65.6799C153.539 69.308 161.025 75.4519 166.292 83.3347C171.559 91.2175 174.371 100.485 174.371 109.966C174.371 122.679 169.32 134.871 160.331 143.861C151.341 152.85 139.149 157.901 126.436 157.901ZM163.719 109.966C163.719 102.592 161.532 95.3838 157.435 89.2527C153.339 83.1216 147.516 78.343 140.703 75.5212C133.891 72.6994 126.395 71.9611 119.162 73.3996C111.93 74.8382 105.287 78.389 100.073 83.6031C94.8591 88.8171 91.3083 95.4602 89.8697 102.692C88.4312 109.924 89.1695 117.421 91.9913 124.233C94.8131 131.046 99.5918 136.869 105.723 140.965C111.854 145.062 119.062 147.248 126.436 147.248C136.324 147.248 145.807 143.32 152.799 136.329C159.791 129.337 163.719 119.854 163.719 109.966ZM133.645 102.757C133.398 102.51 133.104 102.313 132.781 102.179C132.458 102.046 132.112 101.977 131.762 101.977C131.412 101.977 131.066 102.046 130.743 102.179C130.42 102.313 130.126 102.51 129.879 102.757L126.436 106.2L122.993 102.757C122.49 102.272 121.818 102.003 121.119 102.01C120.421 102.016 119.753 102.296 119.26 102.789C118.766 103.283 118.486 103.951 118.48 104.649C118.474 105.348 118.742 106.02 119.227 106.523L122.67 109.966L119.227 113.409C118.973 113.655 118.77 113.949 118.63 114.274C118.491 114.598 118.417 114.948 118.414 115.301C118.411 115.655 118.479 116.006 118.612 116.333C118.746 116.66 118.944 116.958 119.194 117.208C119.444 117.458 119.741 117.655 120.069 117.789C120.396 117.923 120.747 117.991 121.1 117.988C121.454 117.985 121.803 117.911 122.128 117.771C122.453 117.632 122.747 117.429 122.993 117.175L126.436 113.732L129.879 117.175C130.382 117.66 131.054 117.928 131.752 117.922C132.451 117.916 133.119 117.636 133.612 117.142C134.106 116.648 134.386 115.981 134.392 115.282C134.398 114.584 134.13 113.911 133.645 113.409L130.202 109.966L133.645 106.523C133.892 106.275 134.088 105.982 134.222 105.659C134.356 105.336 134.425 104.989 134.425 104.64C134.425 104.29 134.356 103.944 134.222 103.621C134.088 103.298 133.892 103.004 133.645 102.757ZM112.34 102.757C112.093 102.51 111.8 102.313 111.477 102.179C111.154 102.046 110.807 101.977 110.458 101.977C110.108 101.977 109.762 102.046 109.439 102.179C109.116 102.313 108.822 102.51 108.575 102.757L105.132 106.2L101.688 102.757C101.186 102.272 100.513 102.003 99.8151 102.01C99.1169 102.016 98.4489 102.296 97.9552 102.789C97.4614 103.283 97.1814 103.951 97.1753 104.649C97.1692 105.348 97.4377 106.02 97.9227 106.523L101.366 109.966L97.9227 113.409C97.6684 113.655 97.4655 113.949 97.326 114.274C97.1864 114.598 97.1129 114.948 97.1098 115.301C97.1068 115.655 97.1742 116.006 97.3081 116.333C97.442 116.66 97.6397 116.958 97.8897 117.208C98.1398 117.458 98.4371 117.655 98.7644 117.789C99.0917 117.923 99.4423 117.991 99.7959 117.988C100.15 117.985 100.499 117.911 100.824 117.771C101.149 117.632 101.443 117.429 101.688 117.175L105.132 113.732L108.575 117.175C109.077 117.66 109.75 117.928 110.448 117.922C111.146 117.916 111.814 117.636 112.308 117.142C112.802 116.648 113.082 115.981 113.088 115.282C113.094 114.584 112.826 113.911 112.34 113.409L108.897 109.966L112.34 106.523C112.588 106.275 112.784 105.982 112.918 105.659C113.052 105.336 113.121 104.989 113.121 104.64C113.121 104.29 113.052 103.944 112.918 103.621C112.784 103.298 112.588 103.004 112.34 102.757ZM151.506 109.966L154.949 106.523C155.434 106.02 155.703 105.348 155.697 104.649C155.691 103.951 155.41 103.283 154.917 102.789C154.423 102.296 153.755 102.016 153.057 102.01C152.359 102.003 151.686 102.272 151.184 102.757L147.74 106.2L144.297 102.757C143.795 102.272 143.122 102.003 142.424 102.01C141.726 102.016 141.058 102.296 140.564 102.789C140.07 103.283 139.79 103.951 139.784 104.649C139.778 105.348 140.046 106.02 140.531 106.523L143.974 109.966L140.531 113.409C140.277 113.655 140.074 113.949 139.935 114.274C139.795 114.598 139.722 114.948 139.719 115.301C139.715 115.655 139.783 116.006 139.917 116.333C140.051 116.66 140.248 116.958 140.498 117.208C140.748 117.458 141.046 117.655 141.373 117.789C141.7 117.923 142.051 117.991 142.405 117.988C142.758 117.985 143.108 117.911 143.433 117.771C143.757 117.632 144.051 117.429 144.297 117.175L147.74 113.732L151.184 117.175C151.686 117.66 152.359 117.928 153.057 117.922C153.755 117.916 154.423 117.636 154.917 117.142C155.41 116.648 155.691 115.981 155.697 115.282C155.703 114.584 155.434 113.911 154.949 113.409L151.506 109.966Z\" fill=\"#93939A\"/>\n <path d=\"M190.242 160.457L170.136 140.351C166.533 145.552 162.023 150.063 156.821 153.666L176.927 173.772C178.693 175.538 181.088 176.53 183.585 176.53C186.082 176.53 188.477 175.538 190.242 173.772C192.008 172.007 193 169.612 193 167.115C193 164.618 192.008 162.223 190.242 160.457ZM126.436 157.901C116.955 157.901 107.688 155.089 99.8048 149.822C91.922 144.555 85.7781 137.069 82.15 128.31C78.5219 119.551 77.5727 109.913 79.4222 100.614C81.2718 91.3158 85.8371 82.7746 92.5409 76.0708C99.2447 69.367 107.786 64.8017 117.084 62.9521C126.383 61.1026 136.021 62.0518 144.78 65.6799C153.539 69.308 161.025 75.4519 166.292 83.3347C171.559 91.2175 174.371 100.485 174.371 109.966C174.371 122.679 169.32 134.871 160.331 143.861C151.341 152.85 139.149 157.901 126.436 157.901ZM163.719 109.966C163.719 102.592 161.532 95.3838 157.435 89.2527C153.339 83.1216 147.516 78.343 140.703 75.5212C133.891 72.6994 126.395 71.9611 119.162 73.3996C111.93 74.8382 105.287 78.389 100.073 83.6031C94.8591 88.8171 91.3083 95.4602 89.8697 102.692C88.4312 109.924 89.1695 117.421 91.9913 124.233C94.8131 131.046 99.5918 136.869 105.723 140.965C111.854 145.062 119.062 147.248 126.436 147.248C136.324 147.248 145.807 143.32 152.799 136.329C159.791 129.337 163.719 119.854 163.719 109.966ZM133.645 102.757C133.398 102.51 133.104 102.313 132.781 102.179C132.458 102.046 132.112 101.977 131.762 101.977C131.412 101.977 131.066 102.046 130.743 102.179C130.42 102.313 130.126 102.51 129.879 102.757L126.436 106.2L122.993 102.757C122.49 102.272 121.818 102.003 121.119 102.01C120.421 102.016 119.753 102.296 119.26 102.789C118.766 103.283 118.486 103.951 118.48 104.649C118.474 105.348 118.742 106.02 119.227 106.523L122.67 109.966L119.227 113.409C118.973 113.655 118.77 113.949 118.63 114.274C118.491 114.598 118.417 114.948 118.414 115.301C118.411 115.655 118.479 116.006 118.612 116.333C118.746 116.66 118.944 116.958 119.194 117.208C119.444 117.458 119.741 117.655 120.069 117.789C120.396 117.923 120.747 117.991 121.1 117.988C121.454 117.985 121.803 117.911 122.128 117.771C122.453 117.632 122.747 117.429 122.993 117.175L126.436 113.732L129.879 117.175C130.382 117.66 131.054 117.928 131.752 117.922C132.451 117.916 133.119 117.636 133.612 117.142C134.106 116.648 134.386 115.981 134.392 115.282C134.398 114.584 134.13 113.911 133.645 113.409L130.202 109.966L133.645 106.523C133.892 106.275 134.088 105.982 134.222 105.659C134.356 105.336 134.425 104.989 134.425 104.64C134.425 104.29 134.356 103.944 134.222 103.621C134.088 103.298 133.892 103.004 133.645 102.757ZM112.34 102.757C112.093 102.51 111.8 102.313 111.477 102.179C111.154 102.046 110.807 101.977 110.458 101.977C110.108 101.977 109.762 102.046 109.439 102.179C109.116 102.313 108.822 102.51 108.575 102.757L105.132 106.2L101.688 102.757C101.186 102.272 100.513 102.003 99.8151 102.01C99.1169 102.016 98.4489 102.296 97.9552 102.789C97.4614 103.283 97.1814 103.951 97.1753 104.649C97.1692 105.348 97.4377 106.02 97.9227 106.523L101.366 109.966L97.9227 113.409C97.6684 113.655 97.4655 113.949 97.326 114.274C97.1864 114.598 97.1129 114.948 97.1098 115.301C97.1068 115.655 97.1742 116.006 97.3081 116.333C97.442 116.66 97.6397 116.958 97.8897 117.208C98.1398 117.458 98.4371 117.655 98.7644 117.789C99.0917 117.923 99.4423 117.991 99.7959 117.988C100.15 117.985 100.499 117.911 100.824 117.771C101.149 117.632 101.443 117.429 101.688 117.175L105.132 113.732L108.575 117.175C109.077 117.66 109.75 117.928 110.448 117.922C111.146 117.916 111.814 117.636 112.308 117.142C112.802 116.648 113.082 115.981 113.088 115.282C113.094 114.584 112.826 113.911 112.34 113.409L108.897 109.966L112.34 106.523C112.588 106.275 112.784 105.982 112.918 105.659C113.052 105.336 113.121 104.989 113.121 104.64C113.121 104.29 113.052 103.944 112.918 103.621C112.784 103.298 112.588 103.004 112.34 102.757ZM151.506 109.966L154.949 106.523C155.434 106.02 155.703 105.348 155.697 104.649C155.691 103.951 155.41 103.283 154.917 102.789C154.423 102.296 153.755 102.016 153.057 102.01C152.359 102.003 151.686 102.272 151.184 102.757L147.74 106.2L144.297 102.757C143.795 102.272 143.122 102.003 142.424 102.01C141.726 102.016 141.058 102.296 140.564 102.789C140.07 103.283 139.79 103.951 139.784 104.649C139.778 105.348 140.046 106.02 140.531 106.523L143.974 109.966L140.531 113.409C140.277 113.655 140.074 113.949 139.935 114.274C139.795 114.598 139.722 114.948 139.719 115.301C139.715 115.655 139.783 116.006 139.917 116.333C140.051 116.66 140.248 116.958 140.498 117.208C140.748 117.458 141.046 117.655 141.373 117.789C141.7 117.923 142.051 117.991 142.405 117.988C142.758 117.985 143.108 117.911 143.433 117.771C143.757 117.632 144.051 117.429 144.297 117.175L147.74 113.732L151.184 117.175C151.686 117.66 152.359 117.928 153.057 117.922C153.755 117.916 154.423 117.636 154.917 117.142C155.41 116.648 155.691 115.981 155.697 115.282C155.703 114.584 155.434 113.911 154.949 113.409L151.506 109.966Z\" stroke=\"transparent\" stroke-width=\"20\" mask=\"url(#path-2-outside-1_0_1)\"/>\n </g>\n <defs>\n <clipPath id=\"clip0_0_1\">\n <rect width=\"200\" height=\"200\" fill=\"white\"/>\n </clipPath>\n </defs>\n </svg>\n </div>\n <div class=\"errorContainer\"></div>\n </div>\n </div>\n <div class=\"payment-errors-container\"></div>\n <button class=\"ff-card-form-submit-button\" id=\"submitButton\">\n Continue\n </button>\n </div>\n </div>\n </div>\n <div id=\"success-screen\" style=\"display: none\">\n <div class=\"success\">\n <img alt=\"Loading\" src=\"data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTAiIGhlaWdodD0iNTAiIHZpZXdCb3g9IjAgMCA1MCA1MCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTI1IDAuMTExMzI4QzIwLjA3NzQgMC4xMTEzMjggMTUuMjY1NCAxLjU3MTA0IDExLjE3MjUgNC4zMDU4NkM3LjA3OTUyIDcuMDQwNjkgMy44ODk0NSAxMC45Mjc4IDIuMDA1NjYgMTUuNDc1N0MwLjEyMTg4MyAyMC4wMjM1IC0wLjM3MSAyNS4wMjc4IDAuNTg5MzQzIDI5Ljg1NThDMS41NDk2OSAzNC42ODM4IDMuOTIwMTIgMzkuMTE4NSA3LjQwMDkgNDIuNTk5M0MxMC44ODE3IDQ2LjA4MDEgMTUuMzE2NCA0OC40NTA1IDIwLjE0NDQgNDkuNDEwOUMyNC45NzI0IDUwLjM3MTIgMjkuOTc2NyA0OS44NzgzIDM0LjUyNDYgNDcuOTk0NkMzOS4wNzI0IDQ2LjExMDggNDIuOTU5NSA0Mi45MjA3IDQ1LjY5NDQgMzguODI3N0M0OC40MjkyIDM0LjczNDggNDkuODg4OSAyOS45MjI4IDQ5Ljg4ODkgMjUuMDAwMkM0OS44ODg5IDE4LjM5OTMgNDcuMjY2NyAxMi4wNjg3IDQyLjU5OTEgNy40MDExMkMzNy45MzE1IDIuNzMzNTQgMzEuNjAwOSAwLjExMTMyOCAyNSAwLjExMTMyOFpNNDEuMjU1NiAxNi42NDY5TDIwLjgxNTYgMzcuMDcxM0w4Ljc0NDQ0IDI1LjAwMDJDOC4zMzE4OCAyNC41ODc3IDguMTAwMTEgMjQuMDI4MSA4LjEwMDExIDIzLjQ0NDdDOC4xMDAxMSAyMi44NjEyIDguMzMxODggMjIuMzAxNyA4Ljc0NDQ0IDIxLjg4OTFDOS4xNTcgMjEuNDc2NSA5LjcxNjU1IDIxLjI0NDggMTAuMyAyMS4yNDQ4QzEwLjg4MzQgMjEuMjQ0OCAxMS40NDMgMjEuNDc2NSAxMS44NTU2IDIxLjg4OTFMMjAuODQ2NyAzMC44ODAyTDM4LjE3NTYgMTMuNTY2OUMzOC4zNzk4IDEzLjM2MjYgMzguNjIyMyAxMy4yMDA2IDM4Ljg4OTIgMTMuMDlDMzkuMTU2MiAxMi45Nzk1IDM5LjQ0MjIgMTIuOTIyNiAzOS43MzExIDEyLjkyMjZDNDAuMDIgMTIuOTIyNiA0MC4zMDYxIDEyLjk3OTUgNDAuNTczIDEzLjA5QzQwLjgzOTkgMTMuMjAwNiA0MS4wODI0IDEzLjM2MjYgNDEuMjg2NyAxMy41NjY5QzQxLjQ5MDkgMTMuNzcxMiA0MS42NTMgMTQuMDEzNyA0MS43NjM1IDE0LjI4MDZDNDEuODc0MSAxNC41NDc1IDQxLjkzMSAxNC44MzM1IDQxLjkzMSAxNS4xMjI0QzQxLjkzMSAxNS40MTEzIDQxLjg3NDEgMTUuNjk3NCA0MS43NjM1IDE1Ljk2NDNDNDEuNjUzIDE2LjIzMTIgNDEuNDkwOSAxNi40NzM3IDQxLjI4NjcgMTYuNjc4TDQxLjI1NTYgMTYuNjQ2OVoiIGZpbGw9IiM4RURGQzIiLz4KPC9zdmc+Cg==\">\n <div>Payment completed successfully</div>\n </div>\n </div>\n</div>\n";
1189
+
1190
+ if(typeof document!=="undefined")document.head.appendChild(document.createElement("style")).textContent="/* Main container */\n.ff-skin-default {\n display: flex;\n flex-direction: column;\n gap: 16px;\n width: 100%;\n max-width: 400px;\n margin: 0 auto;\n}\n\n/* Payment method cards */\n.ff-payment-method-card {\n display: none;\n border: 1px solid #e0e0e0;\n border-radius: 8px;\n background-color: #ffffff;\n padding: 20px;\n transition: border-color 0.2s ease;\n height: auto;\n}\n\n.ff-payment-method-card.visible {\n display: block;\n}\n\n.ff-payment-method-card:hover {\n border-color: #b0b0b0;\n}\n\n.payment-errors-container {\n background-color: #d1000033;\n color: #d10000;\n font-size: 14px;\n padding: 16px 12px;\n border-radius: 8px;\n}\n.payment-errors-container:empty {\n display: none;\n}\n\n/* Label wrapper */\n.ff-payment-method-label {\n display: flex;\n align-items: flex-start;\n gap: 16px;\n cursor: pointer;\n width: 100%;\n}\n\n/* Custom radio button styling */\n.ff-payment-method-radio {\n appearance: none;\n -webkit-appearance: none;\n -moz-appearance: none;\n width: 24px;\n height: 24px;\n min-width: 24px;\n min-height: 24px;\n border: 1px solid #9e9e9e;\n border-radius: 50%;\n background-color: #ffffff;\n cursor: pointer;\n position: relative;\n margin: 0;\n flex-shrink: 0;\n transition: border-color 0.2s ease;\n}\n\n.ff-card-form-submit-button {\n display: block;\n cursor: pointer;\n width: 100%;\n padding: 16px 0;\n border-radius: 16px;\n background-color: #000000;\n color: #ffffff;\n border: none;\n font-size: 16px;\n margin-top: 16px;\n}\n\n.ff-payment-method-radio:checked {\n border-color: #e32f41;\n background-color: #ffffff;\n}\n\n.ff-payment-method-radio:checked::after {\n content: '';\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 16px;\n height: 16px;\n border-radius: 50%;\n background-color: #e32f41;\n}\n\n/* Payment method content */\n.ff-payment-method-content {\n flex: 1;\n display: flex;\n flex-direction: column;\n gap: 16px;\n max-height: 0;\n height: 0;\n overflow: hidden;\n opacity: 0;\n transition:\n opacity 0.3s ease,\n margin-top 0.3s ease;\n margin-top: 0;\n}\n\n.ff-payment-method-card.expanded .ff-payment-method-content {\n max-height: 2000px;\n height: auto;\n opacity: 1;\n margin-top: 16px;\n}\n.ff-payment-method-card.expanded .ff-payment-method-label {\n margin-bottom: 16px;\n}\n\n/* Payment method header */\n.ff-payment-method-header {\n display: flex;\n align-items: center;\n}\n\n/* Google Pay Logo */\n.ff-google-pay-logo {\n display: flex;\n align-items: center;\n gap: 4px;\n font-weight: 500;\n font-size: 18px;\n}\n\n/* Payment features list */\n.ff-payment-features {\n list-style: none;\n padding: 0;\n margin: 0;\n display: flex;\n flex-direction: column;\n gap: 12px;\n}\n\n.ff-payment-feature {\n display: flex;\n align-items: flex-start;\n gap: 8px;\n}\n\n.ff-checkmark-icon {\n width: 20px;\n height: 20px;\n min-width: 20px;\n color: #e32f41;\n flex-shrink: 0;\n margin-top: 2px;\n}\n\n.ff-payment-feature span {\n color: #333333;\n font-size: 14px;\n line-height: 1.5;\n}\n\n/* Google Pay button container */\n.ff-google-pay-button-container {\n display: flex;\n justify-content: center;\n margin: 8px 0;\n}\n\n/* Security message */\n.ff-security-message {\n text-align: center;\n color: #757575;\n font-size: 12px;\n margin: 8px 0 0 0;\n padding: 0;\n}\n\n/* Card logos container */\n.ff-card-logos {\n display: flex;\n align-items: center;\n gap: 12px;\n flex-wrap: wrap;\n}\n\n/* Card form container */\n.ff-card-form-container {\n position: relative;\n display: flex;\n flex-direction: column;\n gap: 10px;\n}\n\n.ff-card-form-label {\n display: block;\n font-size: 16px;\n margin-bottom: 5px;\n}\n\n.card-form-row {\n display: flex;\n flex-direction: row;\n gap: 10px;\n}\n\n.ff-card-form-cardholder-input {\n padding-left: 10px;\n padding-right: 10px;\n box-sizing: border-box;\n height: 36px;\n width: 100%;\n font-size: 1rem;\n background-color: transparent;\n border: 1px solid rgb(0 0 0 / 10%);\n border-radius: 6px;\n transition: all 0.3s ease;\n box-shadow: none;\n}\n.ff-card-form-cardholder-input.error {\n border-color: #e32f41;\n}\n\n.errorContainer {\n color: #d10000;\n font-size: 16px;\n}\n\n.loader-container {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n display: flex;\n justify-content: center;\n align-items: center;\n background-color: rgba(255, 255, 255);\n}\n\n.payment-button-loader {\n position: relative;\n height: 50px;\n}\n\n.loader {\n width: 24px;\n height: 24px;\n border: 4px solid #e32f41;\n border-top: 4px solid transparent;\n border-radius: 50%;\n animation: spin 1s linear infinite;\n}\n\n@keyframes spin {\n 0% {\n transform: rotate(0deg);\n }\n 100% {\n transform: rotate(360deg);\n }\n}\n\n/* Responsive adjustments */\n@media (max-width: 768px) {\n .ff-payment-method-card {\n padding: 16px;\n }\n\n .ff-payment-method-label {\n gap: 12px;\n }\n\n .ff-card-logos {\n gap: 8px;\n }\n}\n\n.ff-payment-container {\n position: relative;\n}\n\n.success {\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n gap: 16px;\n}\n\n#cvvInput {\n position: relative;\n}\n\n#cvvInput > svg {\n position: absolute;\n top: 5px;\n right: 5px;\n width: 26px;\n height: 26px;\n}\n";
1191
+
1192
+ var index = (container) => {
1193
+ const containerEl = document.querySelector(container);
1194
+ containerEl.innerHTML = template;
1195
+ document.body.appendChild(containerEl);
1196
+ // Initialize accordion behavior
1197
+ const paymentMethodCards = containerEl.querySelectorAll('.ff-payment-method-card');
1198
+ const radioButtons = containerEl.querySelectorAll('.ff-payment-method-radio');
1199
+ // Function to handle accordion behavior
1200
+ const handleAccordion = (checkedRadio) => {
1201
+ paymentMethodCards.forEach(card => {
1202
+ const radio = card.querySelector('.ff-payment-method-radio');
1203
+ if (radio === checkedRadio && radio?.checked) {
1204
+ // Open the selected card
1205
+ card.classList.add('expanded');
1206
+ }
1207
+ else {
1208
+ // Close other cards
1209
+ card.classList.remove('expanded');
1210
+ }
1211
+ });
1212
+ };
1213
+ // Set initial state based on checked radio
1214
+ const checkedRadio = Array.from(radioButtons).find(radio => radio.checked);
1215
+ // Use setTimeout to ensure DOM is fully rendered before measuring
1216
+ setTimeout(() => {
1217
+ handleAccordion(checkedRadio || null);
1218
+ }, 0);
1219
+ // Add event listeners to radio buttons
1220
+ radioButtons.forEach(radio => {
1221
+ radio.addEventListener('change', () => {
1222
+ if (radio.checked) {
1223
+ handleAccordion(radio);
1224
+ }
1225
+ });
1226
+ });
1227
+ return new Promise(resolve => {
1228
+ setTimeout(() => {
1229
+ resolve(true);
1230
+ }, 0);
1226
1231
  });
1227
- this.checkoutConfig.priceId = newPriceId;
1228
- this._setState('ready');
1229
- this.emit('status-change', 'price-updated');
1230
- } catch (error) {
1231
- this._setState('error');
1232
- this.emit('error', error);
1233
- throw error;
1234
- }
1235
- }
1236
-
1237
- /**
1238
- * Get current checkout status
1239
- * @returns {Object} Status information
1240
- */
1241
- getStatus() {
1242
- return {
1243
- id: this.id,
1244
- state: this.state,
1245
- orderId: this.orderId,
1246
- priceId: this.checkoutConfig.priceId,
1247
- isDestroyed: this.isDestroyed
1248
- };
1249
- }
1250
-
1251
- /**
1252
- * Destroy the checkout instance and clean up resources
1253
- * @returns {Promise<void>}
1254
- */
1255
- async destroy() {
1256
- if (this.isDestroyed) {
1257
- return;
1258
- }
1259
- try {
1260
- // Clean up Primer checkout
1261
- await this.primerWrapper.destroy();
1262
-
1263
- // Clear state
1264
- this._setState('destroyed');
1265
- this.orderId = null;
1266
- this.clientToken = null;
1267
- this.isDestroyed = true;
1268
-
1269
- // Emit destroy event
1270
- this.emit('destroy');
1271
-
1272
- // Remove all listeners
1273
- this.removeAllListeners();
1274
- } catch (error) {
1275
- console.warn('Error during checkout cleanup:', error);
1276
- }
1277
- }
1278
-
1279
- /**
1280
- * Set internal state and emit change events
1281
- * @private
1282
- */
1283
- _setState(newState) {
1284
- if (this.state !== newState) {
1285
- const oldState = this.state;
1286
- this.state = newState;
1287
- this.emit('status-change', newState, oldState);
1288
- }
1289
- }
1290
-
1291
- /**
1292
- * Ensure checkout is not destroyed
1293
- * @private
1294
- */
1295
- _ensureNotDestroyed() {
1296
- if (this.isDestroyed) {
1297
- throw new CheckoutError('Checkout instance has been destroyed');
1298
- }
1299
- }
1300
-
1301
- /**
1302
- * Get the container element
1303
- * @returns {Element} Container element
1304
- */
1305
- getContainer() {
1306
- return document.querySelector(this.checkoutConfig.container);
1307
- }
1308
-
1309
- /**
1310
- * Check if checkout is in a given state
1311
- * @param {string} state - State to check
1312
- * @returns {boolean} True if in the specified state
1313
- */
1314
- isInState(state) {
1315
- return this.state === state;
1316
- }
1317
-
1318
- /**
1319
- * Check if checkout is ready for user interaction
1320
- * @returns {boolean} True if ready
1321
- */
1322
- isReady() {
1323
- return this.state === 'ready' && !this.isDestroyed;
1324
- }
1325
-
1326
- /**
1327
- * Check if checkout is currently processing a payment
1328
- * @returns {boolean} True if processing
1329
- */
1330
- isProcessing() {
1331
- return ['processing', 'action_required'].includes(this.state);
1332
- }
1333
- }
1334
-
1335
- /**
1336
- * @fileoverview Public API with configuration and orchestration logic
1337
- */
1338
-
1339
- let defaultConfig = null;
1340
-
1341
- /**
1342
- * Configure global SDK settings
1343
- * @param {import('./types').SDKConfig} config - SDK configuration
1344
- */
1345
- function configure(config) {
1346
- defaultConfig = config;
1347
- }
1348
-
1349
- /**
1350
- * Resolve configuration with fallback chain
1351
- * @private
1352
- * @param {Object} options - Options with optional orgId and apiConfig
1353
- * @param {string} functionName - Name of calling function for error messages
1354
- * @returns {{orgId: string, baseUrl: string, region: string}} Resolved configuration
1355
- */
1356
- function resolveConfig(options, functionName) {
1357
- const {
1358
- orgId,
1359
- apiConfig
1360
- } = options;
1361
-
1362
- // Fallback chain: params > configure() > error
1363
- const finalOrgId = orgId || defaultConfig?.orgId;
1364
- if (!finalOrgId) {
1365
- throw new Error(`orgId is required. Pass it to ${functionName}() or call configure() first.`);
1366
- }
1367
- const finalBaseUrl = apiConfig?.baseUrl || defaultConfig?.baseUrl || DEFAULTS.BASE_URL;
1368
- const finalRegion = apiConfig?.region || defaultConfig?.region || DEFAULTS.REGION;
1369
- return {
1370
- orgId: finalOrgId,
1371
- baseUrl: finalBaseUrl,
1372
- region: finalRegion
1373
1232
  };
1374
- }
1375
-
1376
- /**
1377
- * Create a checkout instance - supports both events and callbacks
1378
- * @param {import('./types').CreateCheckoutOptions} options - Checkout options with optional SDK config
1379
- * @returns {Promise<import('./types').CheckoutInstance>} Checkout instance
1380
- */
1381
- async function createCheckout(options) {
1382
- const {
1383
- ...checkoutConfig
1384
- } = options;
1385
-
1386
- // Verify Primer SDK is available
1387
- const primerWrapper = new PrimerWrapper();
1388
- primerWrapper.ensurePrimerAvailable();
1389
-
1390
- // Resolve configuration with fallback chain
1391
- const config = resolveConfig(options, 'createCheckout');
1392
- const checkout = new CheckoutInstance({
1393
- ...config,
1394
- checkoutConfig
1395
- });
1396
- await checkout.initialize();
1397
- return checkout;
1398
- }
1399
-
1400
- /**
1401
- * Direct checkout creation with Primer (legacy compatible)
1402
- * @param {string} clientToken - Pre-created client token
1403
- * @param {Object} options - Primer options
1404
- * @returns {Promise<Object>} Primer checkout instance
1405
- */
1406
- async function showUniversalCheckout(clientToken, options) {
1407
- // Import dynamically to avoid circular dependencies
1408
- const {
1409
- default: PrimerWrapper
1410
- } = await Promise.resolve().then(function () { return primerWrapper; });
1411
- const primerWrapper$1 = new PrimerWrapper();
1412
- return await primerWrapper$1.showUniversalCheckout(clientToken, options);
1413
- }
1414
1233
 
1415
- /**
1416
- * Create client session for manual checkout integration
1417
- * @param {Object} params - Session creation parameters
1418
- * @param {string} params.priceId - Price identifier
1419
- * @param {string} params.externalId - Customer external ID
1420
- * @param {string} params.email - Customer email
1421
- * @param {string} [params.orgId] - Organization ID (optional if configured)
1422
- * @param {import('./types').APIConfig} [params.apiConfig] - Optional API config override
1423
- * @param {Object} [params.clientMetadata] - Optional client metadata
1424
- * @param {string} [params.countryCode] - Optional country code
1425
- * @returns {Promise<{clientToken: string, orderId: string, type: string}>} Session data
1426
- */
1427
- async function createClientSession(params) {
1428
- const {
1429
- priceId,
1430
- externalId,
1431
- email,
1432
- clientMetadata,
1433
- countryCode
1434
- } = params;
1435
-
1436
- // Resolve configuration with fallback chain
1437
- const config = resolveConfig(params, 'createClientSession');
1438
-
1439
- // Create API client and session
1440
- const apiClient = new APIClient({
1441
- baseUrl: config.baseUrl,
1442
- orgId: config.orgId,
1443
- timeout: DEFAULTS.REQUEST_TIMEOUT,
1444
- retryAttempts: DEFAULTS.RETRY_ATTEMPTS
1445
- });
1446
- const sessionResponse = await apiClient.createClientSession({
1447
- priceId,
1448
- externalId,
1449
- email,
1450
- region: config.region,
1451
- clientMetadata,
1452
- countryCode
1234
+ var index$1 = /*#__PURE__*/Object.freeze({
1235
+ __proto__: null,
1236
+ default: index
1453
1237
  });
1454
- return apiClient.processSessionResponse(sessionResponse);
1455
- }
1456
-
1457
- /**
1458
- * @fileoverview Main entry point for @funnelfox/billing
1459
- *
1460
- * Modern functional SDK for subscription payments with Primer integration
1461
- *
1462
- * @example
1463
- * // Functional style (simple)
1464
- * import { configure, createCheckout } from '@funnelfox/billing';
1465
- *
1466
- * configure({
1467
- * orgId: 'your-org-id'
1468
- * });
1469
- *
1470
- * const checkout = await createCheckout({
1471
- * priceId: 'price_123',
1472
- * customer: {
1473
- * externalId: 'user_456',
1474
- * email: 'user@example.com'
1475
- * },
1476
- * container: '#checkout-container'
1477
- * });
1478
- *
1479
- * @example
1480
- * // Namespace style
1481
- * import { Billing } from '@funnelfox/billing';
1482
- *
1483
- * Billing.configure({ orgId: 'your-org-id' });
1484
- * const checkout = await Billing.createCheckout({ ... });
1485
- *
1486
- * // Handle events
1487
- * checkout.on('success', (result) => {
1488
- * console.log('Payment completed:', result.orderId);
1489
- * });
1490
- */
1491
-
1492
- const Billing = {
1493
- configure: configure,
1494
- createCheckout: createCheckout,
1495
- showUniversalCheckout: showUniversalCheckout,
1496
- createClientSession: createClientSession
1497
- };
1498
- if (typeof window !== 'undefined') {
1499
- window.Billing = Billing;
1500
- }
1501
-
1502
- exports.APIError = APIError;
1503
- exports.Billing = Billing;
1504
- exports.CHECKOUT_STATES = CHECKOUT_STATES;
1505
- exports.CheckoutError = CheckoutError;
1506
- exports.ConfigurationError = ConfigurationError;
1507
- exports.DEFAULTS = DEFAULTS;
1508
- exports.ERROR_CODES = ERROR_CODES;
1509
- exports.EVENTS = EVENTS;
1510
- exports.FunnefoxSDKError = FunnefoxSDKError;
1511
- exports.NetworkError = NetworkError;
1512
- exports.PrimerError = PrimerError;
1513
- exports.SDK_VERSION = SDK_VERSION;
1514
- exports.ValidationError = ValidationError;
1515
- exports.configure = configure;
1516
- exports.createCheckout = createCheckout;
1517
- exports.createClientSession = createClientSession;
1518
- exports.default = Billing;
1519
- exports.showUniversalCheckout = showUniversalCheckout;
1520
1238
 
1521
- Object.defineProperty(exports, '__esModule', { value: true });
1239
+ exports.APIError = APIError;
1240
+ exports.Billing = Billing;
1241
+ exports.CHECKOUT_STATES = CHECKOUT_STATES;
1242
+ exports.CheckoutError = CheckoutError;
1243
+ exports.ConfigurationError = ConfigurationError;
1244
+ exports.DEFAULTS = DEFAULTS;
1245
+ exports.ERROR_CODES = ERROR_CODES;
1246
+ exports.EVENTS = EVENTS;
1247
+ exports.FunnefoxSDKError = FunnefoxSDKError;
1248
+ exports.NetworkError = NetworkError;
1249
+ exports.PrimerError = PrimerError;
1250
+ exports.SDK_VERSION = SDK_VERSION;
1251
+ exports.ValidationError = ValidationError;
1252
+ exports.configure = configure;
1253
+ exports.createCheckout = createCheckout;
1254
+ exports.createClientSession = createClientSession;
1255
+ exports.default = Billing;
1256
+
1257
+ Object.defineProperty(exports, '__esModule', { value: true });
1522
1258
 
1523
1259
  }));