@w3payments/common 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,375 @@
1
+ # @w3payments/common
2
+
3
+ Foundation package providing shared types, utilities, and theming system for the W3 Payments Platform. This package serves as the core dependency for all other W3 Payments packages.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @w3payments/common
9
+ ```
10
+
11
+ ## Key Features
12
+
13
+ - **Complete TypeScript definitions** for payment processing
14
+ - **W3Appearance theming system** (Stripe Elements-inspired)
15
+ - **Currency and network registries** with metadata
16
+ - **QR code generation utilities** for payment links
17
+ - **Icon management system** for currencies and exchanges
18
+ - **Payment method services** for resolution and filtering
19
+
20
+ ## 🎨 W3Appearance Theming System
21
+
22
+ The centerpiece of this package is the **W3Appearance API** - a comprehensive theming system that provides consistent styling across all W3 Payments components.
23
+
24
+ ### Core Concept
25
+
26
+ The theming system uses **16 carefully chosen variables** that cover all essential styling needs:
27
+
28
+ ```typescript
29
+ interface W3Appearance {
30
+ variables?: {
31
+ // Colors (8 variables)
32
+ primary?: string; // Brand color, buttons, highlights
33
+ background?: string; // Main background color
34
+ text?: string; // Primary text color
35
+ textSecondary?: string; // Secondary text, labels, hints
36
+ danger?: string; // Error states, warnings
37
+ success?: string; // Success states, confirmations
38
+ warning?: string; // Warning states, cautions
39
+ border?: string; // Borders, dividers, outlines
40
+
41
+ // Typography (5 variables)
42
+ fontFamily?: string; // Base font family
43
+ fontSizeBase?: string; // Standard text size (14px)
44
+ fontSizeSmall?: string; // Small text size (12px)
45
+ fontSizeLarge?: string; // Large text size (32px)
46
+ fontWeightNormal?: string; // Normal font weight (400)
47
+
48
+ // Layout (3 variables)
49
+ spacingUnit?: string; // Base spacing unit (4px)
50
+ borderRadius?: string; // Standard border radius (8px)
51
+ buttonRadius?: string; // Button border radius (6px)
52
+ };
53
+ }
54
+ ```
55
+
56
+ ### Built-in Themes
57
+
58
+ #### Default Theme (Light)
59
+ ```typescript
60
+ import { getThemeVariables } from '@w3payments/common';
61
+
62
+ const defaultVars = getThemeVariables('default');
63
+ // Returns CSS variables for light theme with blue primary (#2563eb)
64
+ ```
65
+
66
+ #### Night Theme (Dark)
67
+ ```typescript
68
+ const nightVars = getThemeVariables('night');
69
+ // Returns CSS variables for dark theme with blue accent (#0085FF)
70
+ ```
71
+
72
+ ### Using the Theming System
73
+
74
+ #### With Built-in Themes
75
+ ```typescript
76
+ import { W3PaymentWidget } from '@w3payments/react';
77
+
78
+ // Use built-in theme
79
+ <W3PaymentWidget theme="night" />
80
+ ```
81
+
82
+ #### With Custom Theme
83
+ ```typescript
84
+ import type { W3Appearance } from '@w3payments/common';
85
+
86
+ const customTheme: W3Appearance = {
87
+ variables: {
88
+ primary: '#ff6b35', // Orange brand color
89
+ background: '#f8f9fa', // Light gray background
90
+ borderRadius: '12px', // Rounded corners
91
+ fontFamily: 'SF Pro Display, system-ui, sans-serif'
92
+ }
93
+ };
94
+
95
+ <W3PaymentWidget appearance={customTheme} />
96
+ ```
97
+
98
+ #### With CSS Variables (Advanced)
99
+ ```typescript
100
+ import { getThemeVariables } from '@w3payments/common';
101
+
102
+ // Convert W3Appearance to CSS variables
103
+ const cssVars = getThemeVariables(customTheme);
104
+ // Returns: { '--w3-primary': '#ff6b35', '--w3-background': '#f8f9fa', ... }
105
+
106
+ // Apply to element
107
+ element.style.setProperty('--w3-primary', '#your-color');
108
+ ```
109
+
110
+ ### Theme Conversion Utility
111
+
112
+ The `getThemeVariables` function converts W3Appearance objects to CSS custom properties:
113
+
114
+ ```typescript
115
+ import { getThemeVariables } from '@w3payments/common';
116
+
117
+ // Convert appearance to CSS variables
118
+ const appearance = { variables: { primary: '#ff6b35' } };
119
+ const cssVars = getThemeVariables(appearance);
120
+
121
+ console.log(cssVars);
122
+ // Output: { '--w3-primary': '#ff6b35', '--w3-background': '#ffffff', ... }
123
+ ```
124
+
125
+ ## 💰 Payment Types
126
+
127
+ ### Core Payment Interfaces
128
+
129
+ ```typescript
130
+ import type {
131
+ PaymentIntent,
132
+ PaymentResult,
133
+ CryptoDestination,
134
+ PaymentMethod
135
+ } from '@w3payments/common';
136
+
137
+ // Create payment intent
138
+ const intent: PaymentIntent = {
139
+ amount: '100.00',
140
+ currency: 'USD',
141
+ destinations: [{
142
+ address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
143
+ networkId: 'bitcoin',
144
+ symbol: 'BTC'
145
+ }]
146
+ };
147
+ ```
148
+
149
+ ### Payment Method Types
150
+
151
+ ```typescript
152
+ import type {
153
+ FiatPaymentMethod,
154
+ CryptoPaymentMethod,
155
+ BasePaymentMethod
156
+ } from '@w3payments/common';
157
+
158
+ // Strongly typed payment methods
159
+ const fiatMethod: FiatPaymentMethod = {
160
+ id: 'ach',
161
+ type: 'fiat',
162
+ name: 'Bank Transfer',
163
+ currency: 'USD'
164
+ };
165
+
166
+ const cryptoMethod: CryptoPaymentMethod = {
167
+ id: 'bitcoin',
168
+ type: 'crypto',
169
+ name: 'Bitcoin',
170
+ currency: 'BTC',
171
+ networkId: 'bitcoin'
172
+ };
173
+ ```
174
+
175
+ ## 🌍 Currency and Network Data
176
+
177
+ ### Currency Registry
178
+
179
+ ```typescript
180
+ import { CURRENCIES, currencyService } from '@w3payments/common';
181
+
182
+ // Access currency metadata
183
+ const bitcoin = CURRENCIES.BTC;
184
+ console.log(bitcoin.name); // "Bitcoin"
185
+ console.log(bitcoin.symbol); // "BTC"
186
+ console.log(bitcoin.decimals); // 8
187
+
188
+ // Use currency service
189
+ const usdcData = currencyService.getCurrency('USDC');
190
+ const cryptoCurrencies = currencyService.getCryptoCurrencies();
191
+ ```
192
+
193
+ ### Network Registry
194
+
195
+ ```typescript
196
+ import { NETWORKS, networkService } from '@w3payments/common';
197
+
198
+ // Access network metadata
199
+ const ethereum = NETWORKS.ethereum;
200
+ console.log(ethereum.name); // "Ethereum"
201
+ console.log(ethereum.chainId); // 1
202
+ console.log(ethereum.symbol); // "ETH"
203
+
204
+ // Use network service
205
+ const solanaNetwork = networkService.getNetwork('solana');
206
+ const evmNetworks = networkService.getEvmNetworks();
207
+ ```
208
+
209
+ ## 🎯 Payment Method Services
210
+
211
+ ### Payment Method Resolution
212
+
213
+ ```typescript
214
+ import { PaymentMethodService, paymentMethodService } from '@w3payments/common';
215
+
216
+ // Initialize service
217
+ const pmService = new PaymentMethodService();
218
+
219
+ // Resolve available payment methods
220
+ const methods = await pmService.getAvailablePaymentMethods({
221
+ targetCurrency: 'USDC',
222
+ targetNetwork: 'ethereum',
223
+ amount: '100.00'
224
+ });
225
+
226
+ // Filter methods
227
+ const filteredMethods = pmService.filterPaymentMethods(methods, {
228
+ exchangeFilter: ['coinbase', 'kraken'],
229
+ walletFilter: ['BTC', 'ETH']
230
+ });
231
+ ```
232
+
233
+ ### Payment Method Categories
234
+
235
+ ```typescript
236
+ import { paymentMethodService } from '@w3payments/common';
237
+
238
+ // Get methods by category
239
+ const fiatMethods = paymentMethodService.getFiatMethods();
240
+ const cryptoMethods = paymentMethodService.getCryptoMethods();
241
+ const exchangeMethods = paymentMethodService.getExchangeMethods();
242
+ ```
243
+
244
+ ## 🎨 Icon Utilities
245
+
246
+ ### Currency Icons
247
+
248
+ ```typescript
249
+ import { getCurrencyIcon } from '@w3payments/common';
250
+
251
+ // Get currency icon URL
252
+ const bitcoinIcon = getCurrencyIcon('BTC');
253
+ const ethereumIcon = getCurrencyIcon('ETH');
254
+
255
+ // Icons are optimized SVGs from trusted sources
256
+ ```
257
+
258
+ ### Exchange Icons
259
+
260
+ ```typescript
261
+ import { getExchangeIcon, getFiatIcon } from '@w3payments/common';
262
+
263
+ // Exchange platform icons
264
+ const coinbaseIcon = getExchangeIcon('coinbase');
265
+ const krakenIcon = getExchangeIcon('kraken');
266
+
267
+ // Fiat currency icons
268
+ const usdIcon = getFiatIcon('USD');
269
+ const eurIcon = getFiatIcon('EUR');
270
+ ```
271
+
272
+ ## 🔗 QR Code Generation
273
+
274
+ ```typescript
275
+ import { generateQRCode } from '@w3payments/common';
276
+
277
+ // Generate QR code for payment
278
+ const qrDataUrl = await generateQRCode('bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=0.001');
279
+
280
+ // Use in React component
281
+ <img src={qrDataUrl} alt="Payment QR Code" />
282
+ ```
283
+
284
+ ## 📱 Vendor Adapter Types
285
+
286
+ ### Adapter Interface
287
+
288
+ ```typescript
289
+ import type { VendorAdapter, CheckoutSession } from '@w3payments/common';
290
+
291
+ // Implement custom adapter
292
+ class CustomAdapter implements VendorAdapter {
293
+ async createSession(options: CreatePaymentOptions): Promise<CheckoutSession> {
294
+ // Implementation
295
+ }
296
+
297
+ async getSessionStatus(sessionId: string): Promise<PaymentResult> {
298
+ // Implementation
299
+ }
300
+ }
301
+ ```
302
+
303
+ ## 🔧 Environment Configuration
304
+
305
+ ```typescript
306
+ import type { Environment, PaymentConfig } from '@w3payments/common';
307
+
308
+ // Environment-specific configuration
309
+ const config: PaymentConfig = {
310
+ environment: 'sandbox', // or 'production'
311
+ vendors: {
312
+ meshpay: {
313
+ clientId: 'your-client-id',
314
+ clientSecret: 'your-secret'
315
+ }
316
+ }
317
+ };
318
+ ```
319
+
320
+ ## 📚 Complete Type Exports
321
+
322
+ ```typescript
323
+ // Import all types from single source
324
+ import type {
325
+ // Core payment types
326
+ PaymentIntent,
327
+ PaymentResult,
328
+ PaymentStatus,
329
+ CryptoDestination,
330
+
331
+ // Payment method types
332
+ PaymentMethod,
333
+ FiatPaymentMethod,
334
+ CryptoPaymentMethod,
335
+
336
+ // Configuration types
337
+ PaymentConfig,
338
+ PaymentClientOptions,
339
+ CreatePaymentOptions,
340
+
341
+ // Theming types
342
+ W3Appearance,
343
+
344
+ // Vendor types
345
+ VendorAdapter,
346
+ CheckoutSession,
347
+
348
+ // Data types
349
+ Currency,
350
+ Network,
351
+ Environment
352
+ } from '@w3payments/common';
353
+ ```
354
+
355
+ ## 🎯 Why Use This Package?
356
+
357
+ 1. **Type Safety** - Complete TypeScript definitions prevent runtime errors
358
+ 2. **Consistent Theming** - W3Appearance ensures visual consistency across frameworks
359
+ 3. **Rich Metadata** - Currency and network registries provide complete context
360
+ 4. **Framework Agnostic** - Works with React, Vue, Angular, vanilla JS
361
+ 5. **Production Ready** - Used by all W3 Payments components
362
+
363
+ ## 🔗 Related Packages
364
+
365
+ - **[@w3payments/core](../core/)** - Payment orchestration engine (uses this package)
366
+ - **[@w3payments/react](../react/)** - React components with W3Appearance integration
367
+ - **[@w3payments/adapters](../adapters/)** - Vendor integrations (uses types from this package)
368
+
369
+ ## 📄 License
370
+
371
+ Proprietary - All rights reserved
372
+
373
+ ---
374
+
375
+ **💡 Pro Tip:** Start with the W3Appearance theming system for consistent branding, then leverage the payment method services for dynamic payment options based on your specific requirements.
package/dist/index.d.ts CHANGED
@@ -1,8 +1,3 @@
1
- /**
2
- * Apply theme to container element
3
- */
4
- export declare function applyTheme(themeInput: W3ThemeInput, container: HTMLElement): void;
5
-
6
1
  /**
7
2
  * Currency Types
8
3
  *
@@ -150,20 +145,6 @@ export declare type CryptoPaymentMethod = BasePaymentMethod & {
150
145
  currency: string;
151
146
  };
152
147
 
153
- /**
154
- * W3 Payments Theme System
155
- *
156
- * Stripe-style appearance API for simple, powerful theming
157
- * Framework-agnostic - works with React, Vue, vanilla JS, etc.
158
- */
159
- /**
160
- * CSS properties type (framework-agnostic)
161
- * Simple record of CSS properties - allows any property name and value
162
- */
163
- export declare interface CSSStyleProperties {
164
- [property: string]: string | number | undefined;
165
- }
166
-
167
148
  export declare const CURRENCIES: Record<string, Currency>;
168
149
 
169
150
  /**
@@ -323,6 +304,8 @@ export declare function getExchangeIcon(exchange: string): string | undefined;
323
304
 
324
305
  export declare function getFiatIcon(method: string): string | undefined;
325
306
 
307
+ export declare const getThemeVariables: (themeName?: string, appearance?: W3Appearance) => Record<string, string>;
308
+
326
309
  /**
327
310
  * Network Types
328
311
  *
@@ -666,60 +649,29 @@ export declare interface VendorAdapter {
666
649
  }
667
650
 
668
651
  /**
669
- * Complete theme configuration (Stripe-style)
670
- */
671
- export declare interface W3Theme {
672
- /** Named theme preset */
673
- theme?: 'light' | 'dark' | string;
674
- /** Design token overrides */
675
- variables?: W3ThemeVariables;
676
- /** Component-level style rules */
677
- rules?: W3ThemeRules;
678
- }
679
-
680
- /**
681
- * Theme input - can be just a string for named themes or full config
682
- */
683
- export declare type W3ThemeInput = string | W3Theme;
684
-
685
- /**
686
- * Component-level style overrides
687
- * Record of CSS selectors to style properties
688
- */
689
- export declare type W3ThemeRules = Record<string, CSSStyleProperties>;
690
-
691
- /**
692
- * Design token variables for common customizations
693
- */
694
- export declare interface W3ThemeVariables {
695
- /** Primary brand color */
696
- colorPrimary?: string;
697
- /** Primary color on hover/focus */
698
- colorPrimaryHover?: string;
699
- /** Light variant of primary color */
700
- colorPrimaryLight?: string;
701
- /** Background color */
702
- colorBackground?: string;
703
- /** Primary text color */
704
- colorText?: string;
705
- /** Secondary/muted text color */
706
- colorTextMuted?: string;
707
- /** Border color */
708
- colorBorder?: string;
709
- /** Light border color */
710
- colorBorderLight?: string;
711
- /** Error/danger color */
712
- colorDanger?: string;
713
- /** Success color */
714
- colorSuccess?: string;
715
- /** Font family */
716
- fontFamily?: string;
717
- /** Base font size */
718
- fontSize?: string;
719
- /** Border radius */
720
- borderRadius?: string;
721
- /** Base spacing unit */
722
- spacing?: string;
652
+ * W3 Payment Widget Appearance Configuration
653
+ *
654
+ * Following Stripe's appearance API pattern for simple but complete theming.
655
+ */
656
+ export declare interface W3Appearance {
657
+ variables?: {
658
+ primary?: string;
659
+ background?: string;
660
+ text?: string;
661
+ textSecondary?: string;
662
+ danger?: string;
663
+ success?: string;
664
+ warning?: string;
665
+ border?: string;
666
+ fontFamily?: string;
667
+ fontSizeBase?: string;
668
+ fontSizeSmall?: string;
669
+ fontSizeLarge?: string;
670
+ fontWeightNormal?: string;
671
+ spacingUnit?: string;
672
+ borderRadius?: string;
673
+ buttonRadius?: string;
674
+ };
723
675
  }
724
676
 
725
677
  export { }
package/dist/index.js CHANGED
@@ -1,5 +1,44 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const defaultTheme = {
4
+ primary: "#2563eb",
5
+ background: "#ffffff",
6
+ text: "#000000",
7
+ textSecondary: "#6b7280",
8
+ danger: "#dc2626",
9
+ success: "#16a34a",
10
+ warning: "#ca8a04",
11
+ border: "#e5e7eb",
12
+ fontFamily: "Inter, system-ui, sans-serif",
13
+ fontSizeBase: "14px",
14
+ fontSizeSmall: "12px",
15
+ fontSizeLarge: "32px",
16
+ fontWeightNormal: "400",
17
+ spacingUnit: "4px",
18
+ borderRadius: "8px",
19
+ buttonRadius: "6px"
20
+ };
21
+ const BUILT_IN_THEMES = {
22
+ default: defaultTheme,
23
+ night: {
24
+ ...defaultTheme,
25
+ primary: "#0085FF",
26
+ background: "#14171D",
27
+ text: "#C9CED8",
28
+ textSecondary: "#8C99AD",
29
+ danger: "#F23154",
30
+ border: "#2B3039"
31
+ }
32
+ };
33
+ const getThemeVariables = (themeName = "default", appearance) => {
34
+ const baseTheme = BUILT_IN_THEMES[themeName] || BUILT_IN_THEMES.default;
35
+ const finalVariables = { ...baseTheme, ...appearance?.variables };
36
+ const cssVars = {};
37
+ Object.entries(finalVariables).forEach(([key, value]) => {
38
+ cssVars[`--w3-${key}`] = value;
39
+ });
40
+ return cssVars;
41
+ };
3
42
  class PaymentMethodService {
4
43
  static instance;
5
44
  paymentMethods = /* @__PURE__ */ new Map();
@@ -470,78 +509,6 @@ function truncateToCurrencyDecimals(value, currency) {
470
509
  const truncatedDecimal = parts[1].substring(0, currency.decimals);
471
510
  return truncatedDecimal.length > 0 ? `${parts[0]}.${truncatedDecimal}` : parts[0];
472
511
  }
473
- function camelToKebab(str) {
474
- return str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
475
- }
476
- function getDefaultVariables() {
477
- return {
478
- colorPrimary: "#6366f1",
479
- colorPrimaryHover: "#4f46e5",
480
- colorPrimaryLight: "#e0e7ff",
481
- colorBackground: "#ffffff",
482
- colorText: "#1f2937",
483
- colorTextMuted: "#6b7280",
484
- colorBorder: "#e5e7eb",
485
- colorBorderLight: "#f3f4f6",
486
- colorDanger: "#ef4444",
487
- colorSuccess: "#10b981",
488
- fontFamily: "system-ui, -apple-system, sans-serif",
489
- fontSize: "1rem",
490
- borderRadius: "0.5rem",
491
- spacing: "1rem"
492
- };
493
- }
494
- function getThemePreset(themeName) {
495
- const defaults = getDefaultVariables();
496
- switch (themeName) {
497
- case "dark":
498
- return {
499
- ...defaults,
500
- colorPrimary: "#6366f1",
501
- colorPrimaryHover: "#7c3aed",
502
- colorPrimaryLight: "#1e1b4b",
503
- colorBackground: "#111827",
504
- colorText: "#f9fafb",
505
- colorTextMuted: "#9ca3af",
506
- colorBorder: "#374151",
507
- colorBorderLight: "#4b5563"
508
- };
509
- case "light":
510
- default:
511
- return defaults;
512
- }
513
- }
514
- function applyTheme(themeInput, container) {
515
- const theme = typeof themeInput === "string" ? { theme: themeInput } : themeInput;
516
- const baseVariables = getThemePreset(theme.theme ?? "light");
517
- const finalVariables = { ...baseVariables, ...theme.variables };
518
- Object.entries(finalVariables).forEach(([key, value]) => {
519
- if (value !== void 0) {
520
- const cssVar = `--${camelToKebab(key)}`;
521
- container.style.setProperty(cssVar, value);
522
- }
523
- });
524
- if (theme.theme) {
525
- container.setAttribute("data-theme", theme.theme);
526
- }
527
- if (theme.rules) {
528
- applyComponentRules(theme.rules);
529
- }
530
- }
531
- function applyComponentRules(rules) {
532
- const styleId = "w3-theme-rules";
533
- let styleElement = document.getElementById(styleId);
534
- if (!styleElement) {
535
- styleElement = document.createElement("style");
536
- styleElement.id = styleId;
537
- document.head.appendChild(styleElement);
538
- }
539
- const css = Object.entries(rules).map(([selector, styles]) => {
540
- const cssProperties = Object.entries(styles).map(([prop, value]) => `${camelToKebab(prop)}: ${value};`).join(" ");
541
- return `.w3-widget ${selector} { ${cssProperties} }`;
542
- }).join("\n");
543
- styleElement.textContent = css;
544
- }
545
512
  function getDefaultExportFromCjs(x) {
546
513
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
547
514
  }
@@ -2879,7 +2846,6 @@ function getFiatIcon(method) {
2879
2846
  exports.CURRENCIES = CURRENCIES;
2880
2847
  exports.NETWORKS = NETWORKS;
2881
2848
  exports.PaymentMethodService = PaymentMethodService;
2882
- exports.applyTheme = applyTheme;
2883
2849
  exports.copyToClipboard = copyToClipboard;
2884
2850
  exports.currencyService = currencyService;
2885
2851
  exports.formatCurrencyDisplay = formatCurrencyDisplay;
@@ -2893,6 +2859,7 @@ exports.generateQRCodeSVG = generateQRCodeSVG;
2893
2859
  exports.getCurrencyIcon = getCurrencyIcon;
2894
2860
  exports.getExchangeIcon = getExchangeIcon;
2895
2861
  exports.getFiatIcon = getFiatIcon;
2862
+ exports.getThemeVariables = getThemeVariables;
2896
2863
  exports.networkService = networkService;
2897
2864
  exports.paymentMethodService = paymentMethodService;
2898
2865
  exports.truncateToCurrencyDecimals = truncateToCurrencyDecimals;