@nehorai/payments 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/dist/config/index.cjs +116 -0
  3. package/dist/config/index.cjs.map +1 -0
  4. package/dist/config/index.d.cts +125 -0
  5. package/dist/config/index.d.ts +125 -0
  6. package/dist/config/index.js +83 -0
  7. package/dist/config/index.js.map +1 -0
  8. package/dist/factory.cjs +807 -0
  9. package/dist/factory.cjs.map +1 -0
  10. package/dist/factory.d.cts +96 -0
  11. package/dist/factory.d.ts +96 -0
  12. package/dist/factory.js +777 -0
  13. package/dist/factory.js.map +1 -0
  14. package/dist/index.cjs +1341 -0
  15. package/dist/index.cjs.map +1 -0
  16. package/dist/index.d.cts +40 -0
  17. package/dist/index.d.ts +40 -0
  18. package/dist/index.js +1260 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/payment-orchestrator-CPaLmDM5.d.ts +404 -0
  21. package/dist/payment-orchestrator-Co_X6T_V.d.cts +404 -0
  22. package/dist/payment-types-68W-PlGg.d.cts +211 -0
  23. package/dist/payment-types-68W-PlGg.d.ts +211 -0
  24. package/dist/providers/interfaces/index.cjs +19 -0
  25. package/dist/providers/interfaces/index.cjs.map +1 -0
  26. package/dist/providers/interfaces/index.d.cts +80 -0
  27. package/dist/providers/interfaces/index.d.ts +80 -0
  28. package/dist/providers/interfaces/index.js +1 -0
  29. package/dist/providers/interfaces/index.js.map +1 -0
  30. package/dist/repository/interfaces/index.cjs +19 -0
  31. package/dist/repository/interfaces/index.cjs.map +1 -0
  32. package/dist/repository/interfaces/index.d.cts +556 -0
  33. package/dist/repository/interfaces/index.d.ts +556 -0
  34. package/dist/repository/interfaces/index.js +1 -0
  35. package/dist/repository/interfaces/index.js.map +1 -0
  36. package/dist/routing-engine.interface-DJzGXor9.d.cts +194 -0
  37. package/dist/routing-engine.interface-h9_GmQ4b.d.ts +194 -0
  38. package/dist/services/index.cjs +806 -0
  39. package/dist/services/index.cjs.map +1 -0
  40. package/dist/services/index.d.cts +75 -0
  41. package/dist/services/index.d.ts +75 -0
  42. package/dist/services/index.js +763 -0
  43. package/dist/services/index.js.map +1 -0
  44. package/dist/state-machine-Cu6_qKnv.d.cts +109 -0
  45. package/dist/state-machine-Cu6_qKnv.d.ts +109 -0
  46. package/dist/types/index.cjs +173 -0
  47. package/dist/types/index.cjs.map +1 -0
  48. package/dist/types/index.d.cts +127 -0
  49. package/dist/types/index.d.ts +127 -0
  50. package/dist/types/index.js +130 -0
  51. package/dist/types/index.js.map +1 -0
  52. package/dist/utils/index.cjs +167 -0
  53. package/dist/utils/index.cjs.map +1 -0
  54. package/dist/utils/index.d.cts +102 -0
  55. package/dist/utils/index.d.ts +102 -0
  56. package/dist/utils/index.js +127 -0
  57. package/dist/utils/index.js.map +1 -0
  58. package/package.json +68 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Nehorai Hadad
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/config/index.ts
21
+ var config_exports = {};
22
+ __export(config_exports, {
23
+ createConfig: () => createConfig,
24
+ createConfigFromEnv: () => createConfigFromEnv,
25
+ createPartialConfig: () => createPartialConfig,
26
+ getConfiguredProviderList: () => getConfiguredProviderList,
27
+ getConfiguredProviders: () => getConfiguredProviders,
28
+ isProductionReady: () => isProductionReady,
29
+ validateConfig: () => validateConfig
30
+ });
31
+ module.exports = __toCommonJS(config_exports);
32
+
33
+ // src/config/payment-config.ts
34
+ function createConfigFromEnv(mapping) {
35
+ if (!mapping) {
36
+ return createPartialConfig({});
37
+ }
38
+ const providers = {};
39
+ for (const providerMapping of mapping.providers) {
40
+ const config = {};
41
+ let hasValue = false;
42
+ for (const [configKey, envVarName] of Object.entries(providerMapping.keys)) {
43
+ const value = process.env[envVarName];
44
+ if (value?.trim()) {
45
+ config[configKey] = value;
46
+ hasValue = true;
47
+ }
48
+ }
49
+ if (providerMapping.requiredKeys) {
50
+ const allRequired = providerMapping.requiredKeys.every(
51
+ (key) => config[key] && typeof config[key] === "string" && config[key].trim()
52
+ );
53
+ if (allRequired) {
54
+ providers[providerMapping.providerName] = config;
55
+ }
56
+ } else if (hasValue) {
57
+ providers[providerMapping.providerName] = config;
58
+ }
59
+ }
60
+ const environment = process.env[mapping.environmentVar ?? "PAYMENT_ENVIRONMENT"] ?? "sandbox";
61
+ const defaultCurrency = process.env[mapping.defaultCurrencyVar ?? "DEFAULT_CURRENCY"] ?? "USD";
62
+ return {
63
+ providers,
64
+ environment,
65
+ defaultCurrency
66
+ };
67
+ }
68
+ function createConfig(config) {
69
+ validateConfig(config);
70
+ return {
71
+ providers: { ...config.providers },
72
+ environment: config.environment,
73
+ defaultCurrency: config.defaultCurrency
74
+ };
75
+ }
76
+ function createPartialConfig(partial) {
77
+ return {
78
+ providers: partial.providers ?? {},
79
+ environment: partial.environment ?? "sandbox",
80
+ defaultCurrency: partial.defaultCurrency ?? "USD"
81
+ };
82
+ }
83
+ function getConfiguredProviders(config) {
84
+ const availability = {};
85
+ for (const [name, providerConfig] of Object.entries(config.providers)) {
86
+ availability[name] = providerConfig !== void 0 && Object.keys(providerConfig).length > 0;
87
+ }
88
+ return availability;
89
+ }
90
+ function getConfiguredProviderList(config) {
91
+ const availability = getConfiguredProviders(config);
92
+ return Object.entries(availability).filter(([, isAvailable]) => isAvailable).map(([name]) => name);
93
+ }
94
+ function isProductionReady(config) {
95
+ const providers = getConfiguredProviderList(config);
96
+ return config.environment === "production" && providers.length > 0;
97
+ }
98
+ function validateConfig(config) {
99
+ const providers = getConfiguredProviderList(config);
100
+ if (providers.length === 0) {
101
+ throw new Error(
102
+ "Payment configuration error: At least one payment provider must be configured"
103
+ );
104
+ }
105
+ }
106
+ // Annotate the CommonJS export names for ESM import in node:
107
+ 0 && (module.exports = {
108
+ createConfig,
109
+ createConfigFromEnv,
110
+ createPartialConfig,
111
+ getConfiguredProviderList,
112
+ getConfiguredProviders,
113
+ isProductionReady,
114
+ validateConfig
115
+ });
116
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/config/index.ts","../../src/config/payment-config.ts"],"sourcesContent":["/**\r\n * @nehorai/payments - Configuration Exports\r\n */\r\n\r\nexport {\r\n createConfig,\r\n createConfigFromEnv,\r\n createPartialConfig,\r\n getConfiguredProviders,\r\n getConfiguredProviderList,\r\n isProductionReady,\r\n validateConfig,\r\n type PaymentConfig,\r\n type ProvidersConfig,\r\n type ProviderConfig,\r\n type ConfiguredProviderAvailability,\r\n type EnvVarMapping,\r\n type ProviderEnvMapping,\r\n type EnvMappingConfig,\r\n} from './payment-config.js'\r\n","/**\r\n * @nehorai/payments - Injectable Configuration\r\n *\r\n * Provides a framework-agnostic configuration interface that can be\r\n * populated from any source (env vars, secrets manager, database, etc.).\r\n *\r\n * Usage:\r\n * ```typescript\r\n * // Option 1: Create from custom source\r\n * const config = createConfig({\r\n * providers: {\r\n * stripe: { secretKey: 'sk_test_...' },\r\n * },\r\n * environment: 'sandbox',\r\n * defaultCurrency: 'USD',\r\n * });\r\n *\r\n * // Option 2: Create from environment variables\r\n * const config = createConfigFromEnv({\r\n * stripeSecretKey: 'STRIPE_SECRET_KEY',\r\n * stripePublishableKey: 'STRIPE_PUBLISHABLE_KEY',\r\n * stripeWebhookSecret: 'STRIPE_WEBHOOK_SECRET',\r\n * environment: 'PAYMENT_ENVIRONMENT',\r\n * defaultCurrency: 'DEFAULT_CURRENCY',\r\n * });\r\n * ```\r\n */\r\n\r\n// ============================================================================\r\n// Provider Configuration Types\r\n// ============================================================================\r\n\r\n/**\r\n * Generic provider configuration - key-value pairs\r\n * Providers can have any config shape.\r\n */\r\nexport interface ProviderConfig {\r\n /** Provider-specific configuration values */\r\n [key: string]: unknown\r\n}\r\n\r\n/**\r\n * Provider configurations map.\r\n * Keys are provider names, values are provider-specific config.\r\n */\r\nexport interface ProvidersConfig {\r\n [providerName: string]: ProviderConfig | undefined\r\n}\r\n\r\n/**\r\n * Main payment configuration interface\r\n *\r\n * This is the injectable configuration that can be provided from any source.\r\n */\r\nexport interface PaymentConfig {\r\n /** Provider-specific configurations */\r\n providers: ProvidersConfig\r\n /** Environment mode */\r\n environment: 'sandbox' | 'production'\r\n /** Default currency for transactions */\r\n defaultCurrency: string\r\n}\r\n\r\n/**\r\n * Provider availability derived from configuration\r\n */\r\nexport interface ConfiguredProviderAvailability {\r\n [providerName: string]: boolean\r\n}\r\n\r\n// ============================================================================\r\n// Environment Variable Mapping\r\n// ============================================================================\r\n\r\n/**\r\n * Mapping of provider config keys to environment variable names.\r\n * Used by createConfigFromEnv().\r\n */\r\nexport interface EnvVarMapping {\r\n [envVarKey: string]: string\r\n}\r\n\r\n/**\r\n * Provider env var mapping: maps config keys to env var names\r\n */\r\nexport interface ProviderEnvMapping {\r\n providerName: string\r\n /** Map of config key -> env var name. Provider is considered configured if any key has a value. */\r\n keys: EnvVarMapping\r\n /** Keys that are all required for the provider to be considered configured */\r\n requiredKeys?: string[]\r\n}\r\n\r\n/**\r\n * Full env mapping configuration\r\n */\r\nexport interface EnvMappingConfig {\r\n providers: ProviderEnvMapping[]\r\n environmentVar?: string\r\n defaultCurrencyVar?: string\r\n}\r\n\r\n// ============================================================================\r\n// Configuration Factory Functions\r\n// ============================================================================\r\n\r\n/**\r\n * Create payment configuration from environment variables\r\n *\r\n * @param mapping - Mapping of config keys to env var names\r\n * @returns PaymentConfig populated from process.env\r\n */\r\nexport function createConfigFromEnv(mapping?: EnvMappingConfig): PaymentConfig {\r\n if (!mapping) {\r\n return createPartialConfig({})\r\n }\r\n\r\n const providers: ProvidersConfig = {}\r\n\r\n for (const providerMapping of mapping.providers) {\r\n const config: ProviderConfig = {}\r\n let hasValue = false\r\n\r\n for (const [configKey, envVarName] of Object.entries(providerMapping.keys)) {\r\n const value = process.env[envVarName]\r\n if (value?.trim()) {\r\n config[configKey] = value\r\n hasValue = true\r\n }\r\n }\r\n\r\n // Check if required keys are all present\r\n if (providerMapping.requiredKeys) {\r\n const allRequired = providerMapping.requiredKeys.every(\r\n (key) => config[key] && typeof config[key] === 'string' && (config[key] as string).trim()\r\n )\r\n if (allRequired) {\r\n providers[providerMapping.providerName] = config\r\n }\r\n } else if (hasValue) {\r\n providers[providerMapping.providerName] = config\r\n }\r\n }\r\n\r\n const environment = (process.env[mapping.environmentVar ?? 'PAYMENT_ENVIRONMENT'] as 'sandbox' | 'production') ?? 'sandbox'\r\n const defaultCurrency = process.env[mapping.defaultCurrencyVar ?? 'DEFAULT_CURRENCY'] ?? 'USD'\r\n\r\n return {\r\n providers,\r\n environment,\r\n defaultCurrency,\r\n }\r\n}\r\n\r\n/**\r\n * Create payment configuration from a custom source\r\n *\r\n * @param config - Custom configuration object\r\n * @returns Validated PaymentConfig\r\n */\r\nexport function createConfig(config: PaymentConfig): PaymentConfig {\r\n validateConfig(config)\r\n return {\r\n providers: { ...config.providers },\r\n environment: config.environment,\r\n defaultCurrency: config.defaultCurrency,\r\n }\r\n}\r\n\r\n/**\r\n * Create a partial configuration (useful for testing)\r\n *\r\n * @param partial - Partial configuration\r\n * @returns Full configuration with defaults\r\n */\r\nexport function createPartialConfig(partial: Partial<PaymentConfig>): PaymentConfig {\r\n return {\r\n providers: partial.providers ?? {},\r\n environment: partial.environment ?? 'sandbox',\r\n defaultCurrency: partial.defaultCurrency ?? 'USD',\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Configuration Utilities\r\n// ============================================================================\r\n\r\n/**\r\n * Check which providers are configured\r\n */\r\nexport function getConfiguredProviders(config: PaymentConfig): ConfiguredProviderAvailability {\r\n const availability: ConfiguredProviderAvailability = {}\r\n\r\n for (const [name, providerConfig] of Object.entries(config.providers)) {\r\n availability[name] = providerConfig !== undefined && Object.keys(providerConfig).length > 0\r\n }\r\n\r\n return availability\r\n}\r\n\r\n/**\r\n * Get list of configured provider names\r\n */\r\nexport function getConfiguredProviderList(config: PaymentConfig): string[] {\r\n const availability = getConfiguredProviders(config)\r\n return Object.entries(availability)\r\n .filter(([, isAvailable]) => isAvailable)\r\n .map(([name]) => name)\r\n}\r\n\r\n/**\r\n * Check if configuration is valid for production\r\n */\r\nexport function isProductionReady(config: PaymentConfig): boolean {\r\n const providers = getConfiguredProviderList(config)\r\n return config.environment === 'production' && providers.length > 0\r\n}\r\n\r\n/**\r\n * Validate configuration has at least one provider\r\n */\r\nexport function validateConfig(config: PaymentConfig): void {\r\n const providers = getConfiguredProviderList(config)\r\n\r\n if (providers.length === 0) {\r\n throw new Error(\r\n 'Payment configuration error: At least one payment provider must be configured'\r\n )\r\n }\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACgHO,SAAS,oBAAoB,SAA2C;AAC7E,MAAI,CAAC,SAAS;AACZ,WAAO,oBAAoB,CAAC,CAAC;AAAA,EAC/B;AAEA,QAAM,YAA6B,CAAC;AAEpC,aAAW,mBAAmB,QAAQ,WAAW;AAC/C,UAAM,SAAyB,CAAC;AAChC,QAAI,WAAW;AAEf,eAAW,CAAC,WAAW,UAAU,KAAK,OAAO,QAAQ,gBAAgB,IAAI,GAAG;AAC1E,YAAM,QAAQ,QAAQ,IAAI,UAAU;AACpC,UAAI,OAAO,KAAK,GAAG;AACjB,eAAO,SAAS,IAAI;AACpB,mBAAW;AAAA,MACb;AAAA,IACF;AAGA,QAAI,gBAAgB,cAAc;AAChC,YAAM,cAAc,gBAAgB,aAAa;AAAA,QAC/C,CAAC,QAAQ,OAAO,GAAG,KAAK,OAAO,OAAO,GAAG,MAAM,YAAa,OAAO,GAAG,EAAa,KAAK;AAAA,MAC1F;AACA,UAAI,aAAa;AACf,kBAAU,gBAAgB,YAAY,IAAI;AAAA,MAC5C;AAAA,IACF,WAAW,UAAU;AACnB,gBAAU,gBAAgB,YAAY,IAAI;AAAA,IAC5C;AAAA,EACF;AAEA,QAAM,cAAe,QAAQ,IAAI,QAAQ,kBAAkB,qBAAqB,KAAkC;AAClH,QAAM,kBAAkB,QAAQ,IAAI,QAAQ,sBAAsB,kBAAkB,KAAK;AAEzF,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAQO,SAAS,aAAa,QAAsC;AACjE,iBAAe,MAAM;AACrB,SAAO;AAAA,IACL,WAAW,EAAE,GAAG,OAAO,UAAU;AAAA,IACjC,aAAa,OAAO;AAAA,IACpB,iBAAiB,OAAO;AAAA,EAC1B;AACF;AAQO,SAAS,oBAAoB,SAAgD;AAClF,SAAO;AAAA,IACL,WAAW,QAAQ,aAAa,CAAC;AAAA,IACjC,aAAa,QAAQ,eAAe;AAAA,IACpC,iBAAiB,QAAQ,mBAAmB;AAAA,EAC9C;AACF;AASO,SAAS,uBAAuB,QAAuD;AAC5F,QAAM,eAA+C,CAAC;AAEtD,aAAW,CAAC,MAAM,cAAc,KAAK,OAAO,QAAQ,OAAO,SAAS,GAAG;AACrE,iBAAa,IAAI,IAAI,mBAAmB,UAAa,OAAO,KAAK,cAAc,EAAE,SAAS;AAAA,EAC5F;AAEA,SAAO;AACT;AAKO,SAAS,0BAA0B,QAAiC;AACzE,QAAM,eAAe,uBAAuB,MAAM;AAClD,SAAO,OAAO,QAAQ,YAAY,EAC/B,OAAO,CAAC,CAAC,EAAE,WAAW,MAAM,WAAW,EACvC,IAAI,CAAC,CAAC,IAAI,MAAM,IAAI;AACzB;AAKO,SAAS,kBAAkB,QAAgC;AAChE,QAAM,YAAY,0BAA0B,MAAM;AAClD,SAAO,OAAO,gBAAgB,gBAAgB,UAAU,SAAS;AACnE;AAKO,SAAS,eAAe,QAA6B;AAC1D,QAAM,YAAY,0BAA0B,MAAM;AAElD,MAAI,UAAU,WAAW,GAAG;AAC1B,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
@@ -0,0 +1,125 @@
1
+ /**
2
+ * @nehorai/payments - Injectable Configuration
3
+ *
4
+ * Provides a framework-agnostic configuration interface that can be
5
+ * populated from any source (env vars, secrets manager, database, etc.).
6
+ *
7
+ * Usage:
8
+ * ```typescript
9
+ * // Option 1: Create from custom source
10
+ * const config = createConfig({
11
+ * providers: {
12
+ * stripe: { secretKey: 'sk_test_...' },
13
+ * },
14
+ * environment: 'sandbox',
15
+ * defaultCurrency: 'USD',
16
+ * });
17
+ *
18
+ * // Option 2: Create from environment variables
19
+ * const config = createConfigFromEnv({
20
+ * stripeSecretKey: 'STRIPE_SECRET_KEY',
21
+ * stripePublishableKey: 'STRIPE_PUBLISHABLE_KEY',
22
+ * stripeWebhookSecret: 'STRIPE_WEBHOOK_SECRET',
23
+ * environment: 'PAYMENT_ENVIRONMENT',
24
+ * defaultCurrency: 'DEFAULT_CURRENCY',
25
+ * });
26
+ * ```
27
+ */
28
+ /**
29
+ * Generic provider configuration - key-value pairs
30
+ * Providers can have any config shape.
31
+ */
32
+ interface ProviderConfig {
33
+ /** Provider-specific configuration values */
34
+ [key: string]: unknown;
35
+ }
36
+ /**
37
+ * Provider configurations map.
38
+ * Keys are provider names, values are provider-specific config.
39
+ */
40
+ interface ProvidersConfig {
41
+ [providerName: string]: ProviderConfig | undefined;
42
+ }
43
+ /**
44
+ * Main payment configuration interface
45
+ *
46
+ * This is the injectable configuration that can be provided from any source.
47
+ */
48
+ interface PaymentConfig {
49
+ /** Provider-specific configurations */
50
+ providers: ProvidersConfig;
51
+ /** Environment mode */
52
+ environment: 'sandbox' | 'production';
53
+ /** Default currency for transactions */
54
+ defaultCurrency: string;
55
+ }
56
+ /**
57
+ * Provider availability derived from configuration
58
+ */
59
+ interface ConfiguredProviderAvailability {
60
+ [providerName: string]: boolean;
61
+ }
62
+ /**
63
+ * Mapping of provider config keys to environment variable names.
64
+ * Used by createConfigFromEnv().
65
+ */
66
+ interface EnvVarMapping {
67
+ [envVarKey: string]: string;
68
+ }
69
+ /**
70
+ * Provider env var mapping: maps config keys to env var names
71
+ */
72
+ interface ProviderEnvMapping {
73
+ providerName: string;
74
+ /** Map of config key -> env var name. Provider is considered configured if any key has a value. */
75
+ keys: EnvVarMapping;
76
+ /** Keys that are all required for the provider to be considered configured */
77
+ requiredKeys?: string[];
78
+ }
79
+ /**
80
+ * Full env mapping configuration
81
+ */
82
+ interface EnvMappingConfig {
83
+ providers: ProviderEnvMapping[];
84
+ environmentVar?: string;
85
+ defaultCurrencyVar?: string;
86
+ }
87
+ /**
88
+ * Create payment configuration from environment variables
89
+ *
90
+ * @param mapping - Mapping of config keys to env var names
91
+ * @returns PaymentConfig populated from process.env
92
+ */
93
+ declare function createConfigFromEnv(mapping?: EnvMappingConfig): PaymentConfig;
94
+ /**
95
+ * Create payment configuration from a custom source
96
+ *
97
+ * @param config - Custom configuration object
98
+ * @returns Validated PaymentConfig
99
+ */
100
+ declare function createConfig(config: PaymentConfig): PaymentConfig;
101
+ /**
102
+ * Create a partial configuration (useful for testing)
103
+ *
104
+ * @param partial - Partial configuration
105
+ * @returns Full configuration with defaults
106
+ */
107
+ declare function createPartialConfig(partial: Partial<PaymentConfig>): PaymentConfig;
108
+ /**
109
+ * Check which providers are configured
110
+ */
111
+ declare function getConfiguredProviders(config: PaymentConfig): ConfiguredProviderAvailability;
112
+ /**
113
+ * Get list of configured provider names
114
+ */
115
+ declare function getConfiguredProviderList(config: PaymentConfig): string[];
116
+ /**
117
+ * Check if configuration is valid for production
118
+ */
119
+ declare function isProductionReady(config: PaymentConfig): boolean;
120
+ /**
121
+ * Validate configuration has at least one provider
122
+ */
123
+ declare function validateConfig(config: PaymentConfig): void;
124
+
125
+ export { type ConfiguredProviderAvailability, type EnvMappingConfig, type EnvVarMapping, type PaymentConfig, type ProviderConfig, type ProviderEnvMapping, type ProvidersConfig, createConfig, createConfigFromEnv, createPartialConfig, getConfiguredProviderList, getConfiguredProviders, isProductionReady, validateConfig };
@@ -0,0 +1,125 @@
1
+ /**
2
+ * @nehorai/payments - Injectable Configuration
3
+ *
4
+ * Provides a framework-agnostic configuration interface that can be
5
+ * populated from any source (env vars, secrets manager, database, etc.).
6
+ *
7
+ * Usage:
8
+ * ```typescript
9
+ * // Option 1: Create from custom source
10
+ * const config = createConfig({
11
+ * providers: {
12
+ * stripe: { secretKey: 'sk_test_...' },
13
+ * },
14
+ * environment: 'sandbox',
15
+ * defaultCurrency: 'USD',
16
+ * });
17
+ *
18
+ * // Option 2: Create from environment variables
19
+ * const config = createConfigFromEnv({
20
+ * stripeSecretKey: 'STRIPE_SECRET_KEY',
21
+ * stripePublishableKey: 'STRIPE_PUBLISHABLE_KEY',
22
+ * stripeWebhookSecret: 'STRIPE_WEBHOOK_SECRET',
23
+ * environment: 'PAYMENT_ENVIRONMENT',
24
+ * defaultCurrency: 'DEFAULT_CURRENCY',
25
+ * });
26
+ * ```
27
+ */
28
+ /**
29
+ * Generic provider configuration - key-value pairs
30
+ * Providers can have any config shape.
31
+ */
32
+ interface ProviderConfig {
33
+ /** Provider-specific configuration values */
34
+ [key: string]: unknown;
35
+ }
36
+ /**
37
+ * Provider configurations map.
38
+ * Keys are provider names, values are provider-specific config.
39
+ */
40
+ interface ProvidersConfig {
41
+ [providerName: string]: ProviderConfig | undefined;
42
+ }
43
+ /**
44
+ * Main payment configuration interface
45
+ *
46
+ * This is the injectable configuration that can be provided from any source.
47
+ */
48
+ interface PaymentConfig {
49
+ /** Provider-specific configurations */
50
+ providers: ProvidersConfig;
51
+ /** Environment mode */
52
+ environment: 'sandbox' | 'production';
53
+ /** Default currency for transactions */
54
+ defaultCurrency: string;
55
+ }
56
+ /**
57
+ * Provider availability derived from configuration
58
+ */
59
+ interface ConfiguredProviderAvailability {
60
+ [providerName: string]: boolean;
61
+ }
62
+ /**
63
+ * Mapping of provider config keys to environment variable names.
64
+ * Used by createConfigFromEnv().
65
+ */
66
+ interface EnvVarMapping {
67
+ [envVarKey: string]: string;
68
+ }
69
+ /**
70
+ * Provider env var mapping: maps config keys to env var names
71
+ */
72
+ interface ProviderEnvMapping {
73
+ providerName: string;
74
+ /** Map of config key -> env var name. Provider is considered configured if any key has a value. */
75
+ keys: EnvVarMapping;
76
+ /** Keys that are all required for the provider to be considered configured */
77
+ requiredKeys?: string[];
78
+ }
79
+ /**
80
+ * Full env mapping configuration
81
+ */
82
+ interface EnvMappingConfig {
83
+ providers: ProviderEnvMapping[];
84
+ environmentVar?: string;
85
+ defaultCurrencyVar?: string;
86
+ }
87
+ /**
88
+ * Create payment configuration from environment variables
89
+ *
90
+ * @param mapping - Mapping of config keys to env var names
91
+ * @returns PaymentConfig populated from process.env
92
+ */
93
+ declare function createConfigFromEnv(mapping?: EnvMappingConfig): PaymentConfig;
94
+ /**
95
+ * Create payment configuration from a custom source
96
+ *
97
+ * @param config - Custom configuration object
98
+ * @returns Validated PaymentConfig
99
+ */
100
+ declare function createConfig(config: PaymentConfig): PaymentConfig;
101
+ /**
102
+ * Create a partial configuration (useful for testing)
103
+ *
104
+ * @param partial - Partial configuration
105
+ * @returns Full configuration with defaults
106
+ */
107
+ declare function createPartialConfig(partial: Partial<PaymentConfig>): PaymentConfig;
108
+ /**
109
+ * Check which providers are configured
110
+ */
111
+ declare function getConfiguredProviders(config: PaymentConfig): ConfiguredProviderAvailability;
112
+ /**
113
+ * Get list of configured provider names
114
+ */
115
+ declare function getConfiguredProviderList(config: PaymentConfig): string[];
116
+ /**
117
+ * Check if configuration is valid for production
118
+ */
119
+ declare function isProductionReady(config: PaymentConfig): boolean;
120
+ /**
121
+ * Validate configuration has at least one provider
122
+ */
123
+ declare function validateConfig(config: PaymentConfig): void;
124
+
125
+ export { type ConfiguredProviderAvailability, type EnvMappingConfig, type EnvVarMapping, type PaymentConfig, type ProviderConfig, type ProviderEnvMapping, type ProvidersConfig, createConfig, createConfigFromEnv, createPartialConfig, getConfiguredProviderList, getConfiguredProviders, isProductionReady, validateConfig };
@@ -0,0 +1,83 @@
1
+ // src/config/payment-config.ts
2
+ function createConfigFromEnv(mapping) {
3
+ if (!mapping) {
4
+ return createPartialConfig({});
5
+ }
6
+ const providers = {};
7
+ for (const providerMapping of mapping.providers) {
8
+ const config = {};
9
+ let hasValue = false;
10
+ for (const [configKey, envVarName] of Object.entries(providerMapping.keys)) {
11
+ const value = process.env[envVarName];
12
+ if (value?.trim()) {
13
+ config[configKey] = value;
14
+ hasValue = true;
15
+ }
16
+ }
17
+ if (providerMapping.requiredKeys) {
18
+ const allRequired = providerMapping.requiredKeys.every(
19
+ (key) => config[key] && typeof config[key] === "string" && config[key].trim()
20
+ );
21
+ if (allRequired) {
22
+ providers[providerMapping.providerName] = config;
23
+ }
24
+ } else if (hasValue) {
25
+ providers[providerMapping.providerName] = config;
26
+ }
27
+ }
28
+ const environment = process.env[mapping.environmentVar ?? "PAYMENT_ENVIRONMENT"] ?? "sandbox";
29
+ const defaultCurrency = process.env[mapping.defaultCurrencyVar ?? "DEFAULT_CURRENCY"] ?? "USD";
30
+ return {
31
+ providers,
32
+ environment,
33
+ defaultCurrency
34
+ };
35
+ }
36
+ function createConfig(config) {
37
+ validateConfig(config);
38
+ return {
39
+ providers: { ...config.providers },
40
+ environment: config.environment,
41
+ defaultCurrency: config.defaultCurrency
42
+ };
43
+ }
44
+ function createPartialConfig(partial) {
45
+ return {
46
+ providers: partial.providers ?? {},
47
+ environment: partial.environment ?? "sandbox",
48
+ defaultCurrency: partial.defaultCurrency ?? "USD"
49
+ };
50
+ }
51
+ function getConfiguredProviders(config) {
52
+ const availability = {};
53
+ for (const [name, providerConfig] of Object.entries(config.providers)) {
54
+ availability[name] = providerConfig !== void 0 && Object.keys(providerConfig).length > 0;
55
+ }
56
+ return availability;
57
+ }
58
+ function getConfiguredProviderList(config) {
59
+ const availability = getConfiguredProviders(config);
60
+ return Object.entries(availability).filter(([, isAvailable]) => isAvailable).map(([name]) => name);
61
+ }
62
+ function isProductionReady(config) {
63
+ const providers = getConfiguredProviderList(config);
64
+ return config.environment === "production" && providers.length > 0;
65
+ }
66
+ function validateConfig(config) {
67
+ const providers = getConfiguredProviderList(config);
68
+ if (providers.length === 0) {
69
+ throw new Error(
70
+ "Payment configuration error: At least one payment provider must be configured"
71
+ );
72
+ }
73
+ }
74
+ export {
75
+ createConfig,
76
+ createConfigFromEnv,
77
+ createPartialConfig,
78
+ getConfiguredProviderList,
79
+ getConfiguredProviders,
80
+ isProductionReady,
81
+ validateConfig
82
+ };
83
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/config/payment-config.ts"],"sourcesContent":["/**\r\n * @nehorai/payments - Injectable Configuration\r\n *\r\n * Provides a framework-agnostic configuration interface that can be\r\n * populated from any source (env vars, secrets manager, database, etc.).\r\n *\r\n * Usage:\r\n * ```typescript\r\n * // Option 1: Create from custom source\r\n * const config = createConfig({\r\n * providers: {\r\n * stripe: { secretKey: 'sk_test_...' },\r\n * },\r\n * environment: 'sandbox',\r\n * defaultCurrency: 'USD',\r\n * });\r\n *\r\n * // Option 2: Create from environment variables\r\n * const config = createConfigFromEnv({\r\n * stripeSecretKey: 'STRIPE_SECRET_KEY',\r\n * stripePublishableKey: 'STRIPE_PUBLISHABLE_KEY',\r\n * stripeWebhookSecret: 'STRIPE_WEBHOOK_SECRET',\r\n * environment: 'PAYMENT_ENVIRONMENT',\r\n * defaultCurrency: 'DEFAULT_CURRENCY',\r\n * });\r\n * ```\r\n */\r\n\r\n// ============================================================================\r\n// Provider Configuration Types\r\n// ============================================================================\r\n\r\n/**\r\n * Generic provider configuration - key-value pairs\r\n * Providers can have any config shape.\r\n */\r\nexport interface ProviderConfig {\r\n /** Provider-specific configuration values */\r\n [key: string]: unknown\r\n}\r\n\r\n/**\r\n * Provider configurations map.\r\n * Keys are provider names, values are provider-specific config.\r\n */\r\nexport interface ProvidersConfig {\r\n [providerName: string]: ProviderConfig | undefined\r\n}\r\n\r\n/**\r\n * Main payment configuration interface\r\n *\r\n * This is the injectable configuration that can be provided from any source.\r\n */\r\nexport interface PaymentConfig {\r\n /** Provider-specific configurations */\r\n providers: ProvidersConfig\r\n /** Environment mode */\r\n environment: 'sandbox' | 'production'\r\n /** Default currency for transactions */\r\n defaultCurrency: string\r\n}\r\n\r\n/**\r\n * Provider availability derived from configuration\r\n */\r\nexport interface ConfiguredProviderAvailability {\r\n [providerName: string]: boolean\r\n}\r\n\r\n// ============================================================================\r\n// Environment Variable Mapping\r\n// ============================================================================\r\n\r\n/**\r\n * Mapping of provider config keys to environment variable names.\r\n * Used by createConfigFromEnv().\r\n */\r\nexport interface EnvVarMapping {\r\n [envVarKey: string]: string\r\n}\r\n\r\n/**\r\n * Provider env var mapping: maps config keys to env var names\r\n */\r\nexport interface ProviderEnvMapping {\r\n providerName: string\r\n /** Map of config key -> env var name. Provider is considered configured if any key has a value. */\r\n keys: EnvVarMapping\r\n /** Keys that are all required for the provider to be considered configured */\r\n requiredKeys?: string[]\r\n}\r\n\r\n/**\r\n * Full env mapping configuration\r\n */\r\nexport interface EnvMappingConfig {\r\n providers: ProviderEnvMapping[]\r\n environmentVar?: string\r\n defaultCurrencyVar?: string\r\n}\r\n\r\n// ============================================================================\r\n// Configuration Factory Functions\r\n// ============================================================================\r\n\r\n/**\r\n * Create payment configuration from environment variables\r\n *\r\n * @param mapping - Mapping of config keys to env var names\r\n * @returns PaymentConfig populated from process.env\r\n */\r\nexport function createConfigFromEnv(mapping?: EnvMappingConfig): PaymentConfig {\r\n if (!mapping) {\r\n return createPartialConfig({})\r\n }\r\n\r\n const providers: ProvidersConfig = {}\r\n\r\n for (const providerMapping of mapping.providers) {\r\n const config: ProviderConfig = {}\r\n let hasValue = false\r\n\r\n for (const [configKey, envVarName] of Object.entries(providerMapping.keys)) {\r\n const value = process.env[envVarName]\r\n if (value?.trim()) {\r\n config[configKey] = value\r\n hasValue = true\r\n }\r\n }\r\n\r\n // Check if required keys are all present\r\n if (providerMapping.requiredKeys) {\r\n const allRequired = providerMapping.requiredKeys.every(\r\n (key) => config[key] && typeof config[key] === 'string' && (config[key] as string).trim()\r\n )\r\n if (allRequired) {\r\n providers[providerMapping.providerName] = config\r\n }\r\n } else if (hasValue) {\r\n providers[providerMapping.providerName] = config\r\n }\r\n }\r\n\r\n const environment = (process.env[mapping.environmentVar ?? 'PAYMENT_ENVIRONMENT'] as 'sandbox' | 'production') ?? 'sandbox'\r\n const defaultCurrency = process.env[mapping.defaultCurrencyVar ?? 'DEFAULT_CURRENCY'] ?? 'USD'\r\n\r\n return {\r\n providers,\r\n environment,\r\n defaultCurrency,\r\n }\r\n}\r\n\r\n/**\r\n * Create payment configuration from a custom source\r\n *\r\n * @param config - Custom configuration object\r\n * @returns Validated PaymentConfig\r\n */\r\nexport function createConfig(config: PaymentConfig): PaymentConfig {\r\n validateConfig(config)\r\n return {\r\n providers: { ...config.providers },\r\n environment: config.environment,\r\n defaultCurrency: config.defaultCurrency,\r\n }\r\n}\r\n\r\n/**\r\n * Create a partial configuration (useful for testing)\r\n *\r\n * @param partial - Partial configuration\r\n * @returns Full configuration with defaults\r\n */\r\nexport function createPartialConfig(partial: Partial<PaymentConfig>): PaymentConfig {\r\n return {\r\n providers: partial.providers ?? {},\r\n environment: partial.environment ?? 'sandbox',\r\n defaultCurrency: partial.defaultCurrency ?? 'USD',\r\n }\r\n}\r\n\r\n// ============================================================================\r\n// Configuration Utilities\r\n// ============================================================================\r\n\r\n/**\r\n * Check which providers are configured\r\n */\r\nexport function getConfiguredProviders(config: PaymentConfig): ConfiguredProviderAvailability {\r\n const availability: ConfiguredProviderAvailability = {}\r\n\r\n for (const [name, providerConfig] of Object.entries(config.providers)) {\r\n availability[name] = providerConfig !== undefined && Object.keys(providerConfig).length > 0\r\n }\r\n\r\n return availability\r\n}\r\n\r\n/**\r\n * Get list of configured provider names\r\n */\r\nexport function getConfiguredProviderList(config: PaymentConfig): string[] {\r\n const availability = getConfiguredProviders(config)\r\n return Object.entries(availability)\r\n .filter(([, isAvailable]) => isAvailable)\r\n .map(([name]) => name)\r\n}\r\n\r\n/**\r\n * Check if configuration is valid for production\r\n */\r\nexport function isProductionReady(config: PaymentConfig): boolean {\r\n const providers = getConfiguredProviderList(config)\r\n return config.environment === 'production' && providers.length > 0\r\n}\r\n\r\n/**\r\n * Validate configuration has at least one provider\r\n */\r\nexport function validateConfig(config: PaymentConfig): void {\r\n const providers = getConfiguredProviderList(config)\r\n\r\n if (providers.length === 0) {\r\n throw new Error(\r\n 'Payment configuration error: At least one payment provider must be configured'\r\n )\r\n }\r\n}\r\n"],"mappings":";AAgHO,SAAS,oBAAoB,SAA2C;AAC7E,MAAI,CAAC,SAAS;AACZ,WAAO,oBAAoB,CAAC,CAAC;AAAA,EAC/B;AAEA,QAAM,YAA6B,CAAC;AAEpC,aAAW,mBAAmB,QAAQ,WAAW;AAC/C,UAAM,SAAyB,CAAC;AAChC,QAAI,WAAW;AAEf,eAAW,CAAC,WAAW,UAAU,KAAK,OAAO,QAAQ,gBAAgB,IAAI,GAAG;AAC1E,YAAM,QAAQ,QAAQ,IAAI,UAAU;AACpC,UAAI,OAAO,KAAK,GAAG;AACjB,eAAO,SAAS,IAAI;AACpB,mBAAW;AAAA,MACb;AAAA,IACF;AAGA,QAAI,gBAAgB,cAAc;AAChC,YAAM,cAAc,gBAAgB,aAAa;AAAA,QAC/C,CAAC,QAAQ,OAAO,GAAG,KAAK,OAAO,OAAO,GAAG,MAAM,YAAa,OAAO,GAAG,EAAa,KAAK;AAAA,MAC1F;AACA,UAAI,aAAa;AACf,kBAAU,gBAAgB,YAAY,IAAI;AAAA,MAC5C;AAAA,IACF,WAAW,UAAU;AACnB,gBAAU,gBAAgB,YAAY,IAAI;AAAA,IAC5C;AAAA,EACF;AAEA,QAAM,cAAe,QAAQ,IAAI,QAAQ,kBAAkB,qBAAqB,KAAkC;AAClH,QAAM,kBAAkB,QAAQ,IAAI,QAAQ,sBAAsB,kBAAkB,KAAK;AAEzF,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAQO,SAAS,aAAa,QAAsC;AACjE,iBAAe,MAAM;AACrB,SAAO;AAAA,IACL,WAAW,EAAE,GAAG,OAAO,UAAU;AAAA,IACjC,aAAa,OAAO;AAAA,IACpB,iBAAiB,OAAO;AAAA,EAC1B;AACF;AAQO,SAAS,oBAAoB,SAAgD;AAClF,SAAO;AAAA,IACL,WAAW,QAAQ,aAAa,CAAC;AAAA,IACjC,aAAa,QAAQ,eAAe;AAAA,IACpC,iBAAiB,QAAQ,mBAAmB;AAAA,EAC9C;AACF;AASO,SAAS,uBAAuB,QAAuD;AAC5F,QAAM,eAA+C,CAAC;AAEtD,aAAW,CAAC,MAAM,cAAc,KAAK,OAAO,QAAQ,OAAO,SAAS,GAAG;AACrE,iBAAa,IAAI,IAAI,mBAAmB,UAAa,OAAO,KAAK,cAAc,EAAE,SAAS;AAAA,EAC5F;AAEA,SAAO;AACT;AAKO,SAAS,0BAA0B,QAAiC;AACzE,QAAM,eAAe,uBAAuB,MAAM;AAClD,SAAO,OAAO,QAAQ,YAAY,EAC/B,OAAO,CAAC,CAAC,EAAE,WAAW,MAAM,WAAW,EACvC,IAAI,CAAC,CAAC,IAAI,MAAM,IAAI;AACzB;AAKO,SAAS,kBAAkB,QAAgC;AAChE,QAAM,YAAY,0BAA0B,MAAM;AAClD,SAAO,OAAO,gBAAgB,gBAAgB,UAAU,SAAS;AACnE;AAKO,SAAS,eAAe,QAA6B;AAC1D,QAAM,YAAY,0BAA0B,MAAM;AAElD,MAAI,UAAU,WAAW,GAAG;AAC1B,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACF;","names":[]}