@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.
- package/LICENSE +21 -0
- package/dist/config/index.cjs +116 -0
- package/dist/config/index.cjs.map +1 -0
- package/dist/config/index.d.cts +125 -0
- package/dist/config/index.d.ts +125 -0
- package/dist/config/index.js +83 -0
- package/dist/config/index.js.map +1 -0
- package/dist/factory.cjs +807 -0
- package/dist/factory.cjs.map +1 -0
- package/dist/factory.d.cts +96 -0
- package/dist/factory.d.ts +96 -0
- package/dist/factory.js +777 -0
- package/dist/factory.js.map +1 -0
- package/dist/index.cjs +1341 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +40 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +1260 -0
- package/dist/index.js.map +1 -0
- package/dist/payment-orchestrator-CPaLmDM5.d.ts +404 -0
- package/dist/payment-orchestrator-Co_X6T_V.d.cts +404 -0
- package/dist/payment-types-68W-PlGg.d.cts +211 -0
- package/dist/payment-types-68W-PlGg.d.ts +211 -0
- package/dist/providers/interfaces/index.cjs +19 -0
- package/dist/providers/interfaces/index.cjs.map +1 -0
- package/dist/providers/interfaces/index.d.cts +80 -0
- package/dist/providers/interfaces/index.d.ts +80 -0
- package/dist/providers/interfaces/index.js +1 -0
- package/dist/providers/interfaces/index.js.map +1 -0
- package/dist/repository/interfaces/index.cjs +19 -0
- package/dist/repository/interfaces/index.cjs.map +1 -0
- package/dist/repository/interfaces/index.d.cts +556 -0
- package/dist/repository/interfaces/index.d.ts +556 -0
- package/dist/repository/interfaces/index.js +1 -0
- package/dist/repository/interfaces/index.js.map +1 -0
- package/dist/routing-engine.interface-DJzGXor9.d.cts +194 -0
- package/dist/routing-engine.interface-h9_GmQ4b.d.ts +194 -0
- package/dist/services/index.cjs +806 -0
- package/dist/services/index.cjs.map +1 -0
- package/dist/services/index.d.cts +75 -0
- package/dist/services/index.d.ts +75 -0
- package/dist/services/index.js +763 -0
- package/dist/services/index.js.map +1 -0
- package/dist/state-machine-Cu6_qKnv.d.cts +109 -0
- package/dist/state-machine-Cu6_qKnv.d.ts +109 -0
- package/dist/types/index.cjs +173 -0
- package/dist/types/index.cjs.map +1 -0
- package/dist/types/index.d.cts +127 -0
- package/dist/types/index.d.ts +127 -0
- package/dist/types/index.js +130 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/index.cjs +167 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +102 -0
- package/dist/utils/index.d.ts +102 -0
- package/dist/utils/index.js +127 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +68 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import { k as PaymentProvider, P as PaymentAmount, i as PaymentMetadata } from './payment-types-68W-PlGg.cjs';
|
|
2
|
+
import { c as TransactionStatus } from './state-machine-Cu6_qKnv.cjs';
|
|
3
|
+
import { c as IRoutingEngine, R as RoutingContext, d as RoutingDecision, I as IPaymentProvider } from './routing-engine.interface-DJzGXor9.cjs';
|
|
4
|
+
import { PaymentConfig } from './config/index.cjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @nehorai/payments - Circuit Breaker Storage Interface
|
|
8
|
+
*
|
|
9
|
+
* Abstracts storage for circuit breaker state, enabling:
|
|
10
|
+
* - In-memory storage (default, for single-instance deployments)
|
|
11
|
+
* - Database storage (for multi-instance/serverless deployments)
|
|
12
|
+
* - Redis storage (for high-performance distributed systems)
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Circuit breaker states
|
|
17
|
+
*/
|
|
18
|
+
type StoredCircuitState = 'closed' | 'open' | 'half_open';
|
|
19
|
+
/**
|
|
20
|
+
* Circuit breaker state record
|
|
21
|
+
*
|
|
22
|
+
* This is the state that gets persisted by storage implementations.
|
|
23
|
+
*/
|
|
24
|
+
interface CircuitBreakerStateRecord {
|
|
25
|
+
provider: PaymentProvider;
|
|
26
|
+
state: StoredCircuitState;
|
|
27
|
+
failureCount: number;
|
|
28
|
+
successCount: number;
|
|
29
|
+
lastFailure: Date | null;
|
|
30
|
+
openedAt: Date | null;
|
|
31
|
+
nextRetryAt: Date | null;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Storage interface for circuit breaker state
|
|
35
|
+
*
|
|
36
|
+
* Implement this interface to provide custom storage backends:
|
|
37
|
+
* - InMemoryCircuitBreakerStorage (default)
|
|
38
|
+
* - Database-backed storage (uses provider_health table)
|
|
39
|
+
* - Redis-backed storage (for high-performance needs)
|
|
40
|
+
*/
|
|
41
|
+
interface ICircuitBreakerStorage {
|
|
42
|
+
getState(provider: PaymentProvider): Promise<CircuitBreakerStateRecord | null>;
|
|
43
|
+
setState(provider: PaymentProvider, state: CircuitBreakerStateRecord): Promise<void>;
|
|
44
|
+
getAllStates(): Promise<Map<PaymentProvider, CircuitBreakerStateRecord>>;
|
|
45
|
+
deleteState(provider: PaymentProvider): Promise<void>;
|
|
46
|
+
getOpenCircuits(): Promise<PaymentProvider[]>;
|
|
47
|
+
isHealthy(): Promise<boolean>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Create default (closed) state for a provider
|
|
51
|
+
*/
|
|
52
|
+
declare function createDefaultState(provider: PaymentProvider): CircuitBreakerStateRecord;
|
|
53
|
+
/**
|
|
54
|
+
* Check if state indicates circuit is open
|
|
55
|
+
*/
|
|
56
|
+
declare function isCircuitOpen(state: CircuitBreakerStateRecord | null): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Check if circuit should attempt half-open transition
|
|
59
|
+
*/
|
|
60
|
+
declare function shouldAttemptHalfOpen(state: CircuitBreakerStateRecord | null): boolean;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @nehorai/payments - Circuit Breaker Service
|
|
64
|
+
*
|
|
65
|
+
* Implements the circuit breaker pattern for payment provider resilience.
|
|
66
|
+
* Prevents cascading failures by temporarily disabling unhealthy providers.
|
|
67
|
+
*
|
|
68
|
+
* States:
|
|
69
|
+
* - CLOSED: Normal operation, all requests pass through
|
|
70
|
+
* - OPEN: Provider disabled, all requests fail fast
|
|
71
|
+
* - HALF_OPEN: Testing if provider recovered
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
interface CircuitBreakerConfig {
|
|
75
|
+
/** Number of failures before opening circuit */
|
|
76
|
+
failureThreshold: number;
|
|
77
|
+
/** Time in ms before attempting to close circuit */
|
|
78
|
+
resetTimeoutMs: number;
|
|
79
|
+
/** Max requests allowed in half-open state */
|
|
80
|
+
halfOpenMaxRequests: number;
|
|
81
|
+
}
|
|
82
|
+
type CircuitState = 'closed' | 'open' | 'half_open';
|
|
83
|
+
interface CircuitBreakerState {
|
|
84
|
+
provider: PaymentProvider;
|
|
85
|
+
state: CircuitState;
|
|
86
|
+
failureCount: number;
|
|
87
|
+
successCount: number;
|
|
88
|
+
lastFailure: Date | null;
|
|
89
|
+
openedAt: Date | null;
|
|
90
|
+
nextRetryAt: Date | null;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Circuit breaker dependencies (for dependency injection)
|
|
94
|
+
*/
|
|
95
|
+
interface CircuitBreakerDeps {
|
|
96
|
+
/** Storage implementation (optional, defaults to in-memory) */
|
|
97
|
+
storage?: ICircuitBreakerStorage;
|
|
98
|
+
/** Configuration overrides */
|
|
99
|
+
config?: Partial<CircuitBreakerConfig>;
|
|
100
|
+
}
|
|
101
|
+
declare class CircuitBreaker {
|
|
102
|
+
private config;
|
|
103
|
+
private storage;
|
|
104
|
+
constructor(deps?: CircuitBreakerDeps);
|
|
105
|
+
/**
|
|
106
|
+
* Check if a request can be executed for a provider
|
|
107
|
+
*/
|
|
108
|
+
canExecute(provider: PaymentProvider): Promise<boolean>;
|
|
109
|
+
/**
|
|
110
|
+
* Record a successful request
|
|
111
|
+
*/
|
|
112
|
+
recordSuccess(provider: PaymentProvider): Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* Record a failed request
|
|
115
|
+
*/
|
|
116
|
+
recordFailure(provider: PaymentProvider): Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* Get current state for a provider
|
|
119
|
+
*/
|
|
120
|
+
getState(provider: PaymentProvider): Promise<CircuitBreakerState>;
|
|
121
|
+
/**
|
|
122
|
+
* Check if circuit is open (provider unavailable)
|
|
123
|
+
*/
|
|
124
|
+
isOpen(provider: PaymentProvider): boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Async version of isOpen
|
|
127
|
+
*/
|
|
128
|
+
isOpenAsync(provider: PaymentProvider): Promise<boolean>;
|
|
129
|
+
/**
|
|
130
|
+
* Manually reset a provider's circuit
|
|
131
|
+
*/
|
|
132
|
+
reset(provider: PaymentProvider): Promise<void>;
|
|
133
|
+
/**
|
|
134
|
+
* Get all providers with open circuits
|
|
135
|
+
*/
|
|
136
|
+
getOpenCircuits(): Promise<PaymentProvider[]>;
|
|
137
|
+
/**
|
|
138
|
+
* Get the storage instance (useful for testing)
|
|
139
|
+
*/
|
|
140
|
+
getStorage(): ICircuitBreakerStorage;
|
|
141
|
+
/**
|
|
142
|
+
* Get configuration (useful for testing)
|
|
143
|
+
*/
|
|
144
|
+
getConfig(): CircuitBreakerConfig;
|
|
145
|
+
private transitionTo;
|
|
146
|
+
/**
|
|
147
|
+
* Synchronous check for backward compatibility
|
|
148
|
+
* Note: This always returns false for database-backed storage
|
|
149
|
+
*/
|
|
150
|
+
private isOpenSync;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Get or create singleton CircuitBreaker instance
|
|
154
|
+
*/
|
|
155
|
+
declare function getCircuitBreaker(config?: Partial<CircuitBreakerConfig>): CircuitBreaker;
|
|
156
|
+
/**
|
|
157
|
+
* Create a new CircuitBreaker with custom dependencies
|
|
158
|
+
*/
|
|
159
|
+
declare function createCircuitBreaker(deps?: CircuitBreakerDeps): CircuitBreaker;
|
|
160
|
+
/**
|
|
161
|
+
* Reset the singleton instance (useful for testing)
|
|
162
|
+
*/
|
|
163
|
+
declare function resetCircuitBreaker(): void;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @nehorai/payments - Routing Engine Service
|
|
167
|
+
*
|
|
168
|
+
* Intelligent payment routing based on configurable rules:
|
|
169
|
+
* - Card BIN rules (match card ranges to preferred providers)
|
|
170
|
+
* - Provider health (circuit breaker state)
|
|
171
|
+
* - Transaction fees / provider priorities
|
|
172
|
+
* - Currency support
|
|
173
|
+
*
|
|
174
|
+
* Unlike the Podcasto-specific version, this accepts generic RoutingRules
|
|
175
|
+
* config instead of hardcoded Israeli card BIN ranges.
|
|
176
|
+
*/
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Rule for matching card BIN ranges to preferred providers
|
|
180
|
+
*/
|
|
181
|
+
interface CardBinRule {
|
|
182
|
+
ranges: Array<{
|
|
183
|
+
start: string;
|
|
184
|
+
end: string;
|
|
185
|
+
issuer?: string;
|
|
186
|
+
country?: string;
|
|
187
|
+
}>;
|
|
188
|
+
preferredProvider: string;
|
|
189
|
+
priority?: number;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Provider priority configuration
|
|
193
|
+
*/
|
|
194
|
+
interface ProviderPriorityRule {
|
|
195
|
+
provider: string;
|
|
196
|
+
priority: number;
|
|
197
|
+
maxFeePercent: number;
|
|
198
|
+
supportsCurrency: string[];
|
|
199
|
+
supportsRecurring: boolean;
|
|
200
|
+
isLocalGateway?: boolean;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Currency-specific routing rule
|
|
204
|
+
*/
|
|
205
|
+
interface CurrencyRule {
|
|
206
|
+
currency: string;
|
|
207
|
+
preferredProvider: string;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Generic routing rules configuration.
|
|
211
|
+
* Injected at construction time instead of hardcoded.
|
|
212
|
+
*/
|
|
213
|
+
interface RoutingRules {
|
|
214
|
+
cardBinRules?: CardBinRule[];
|
|
215
|
+
providerPriorities?: ProviderPriorityRule[];
|
|
216
|
+
currencyRules?: CurrencyRule[];
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Routing engine dependencies (for dependency injection)
|
|
220
|
+
*/
|
|
221
|
+
interface RoutingEngineDeps {
|
|
222
|
+
/** Payment configuration (optional, defaults to empty config) */
|
|
223
|
+
config?: PaymentConfig;
|
|
224
|
+
/** Circuit breaker instance (optional, defaults to singleton) */
|
|
225
|
+
circuitBreaker?: CircuitBreaker;
|
|
226
|
+
/** Routing rules (optional, no rules means simple round-robin) */
|
|
227
|
+
routingRules?: RoutingRules;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Routing Engine Implementation
|
|
231
|
+
*
|
|
232
|
+
* Routes payments to optimal providers with automatic failover.
|
|
233
|
+
* Uses injected RoutingRules instead of hardcoded locale-specific logic.
|
|
234
|
+
*/
|
|
235
|
+
declare class RoutingEngine implements IRoutingEngine {
|
|
236
|
+
private config;
|
|
237
|
+
private circuitBreaker;
|
|
238
|
+
private routingRules;
|
|
239
|
+
constructor(deps?: RoutingEngineDeps);
|
|
240
|
+
/**
|
|
241
|
+
* Determine optimal provider for a transaction
|
|
242
|
+
*/
|
|
243
|
+
route(context: RoutingContext): Promise<RoutingDecision>;
|
|
244
|
+
/**
|
|
245
|
+
* Get next provider after a failure
|
|
246
|
+
*/
|
|
247
|
+
getFailoverProvider(failedProvider: PaymentProvider, _context: RoutingContext): Promise<PaymentProvider | null>;
|
|
248
|
+
/**
|
|
249
|
+
* Check if a card BIN matches any configured rule
|
|
250
|
+
*/
|
|
251
|
+
matchCardBin(bin: string): boolean;
|
|
252
|
+
/**
|
|
253
|
+
* Get all available (healthy) providers
|
|
254
|
+
*/
|
|
255
|
+
getAvailableProviders(): Promise<PaymentProvider[]>;
|
|
256
|
+
/**
|
|
257
|
+
* Quick recommendation without full context
|
|
258
|
+
*/
|
|
259
|
+
getQuickRecommendation(currency: string, isRecurring: boolean): Promise<PaymentProvider | null>;
|
|
260
|
+
/**
|
|
261
|
+
* Get current configuration (for testing/debugging)
|
|
262
|
+
*/
|
|
263
|
+
getConfig(): PaymentConfig;
|
|
264
|
+
/**
|
|
265
|
+
* Get circuit breaker instance (for testing/debugging)
|
|
266
|
+
*/
|
|
267
|
+
getCircuitBreaker(): CircuitBreaker;
|
|
268
|
+
/**
|
|
269
|
+
* Get routing rules (for testing/debugging)
|
|
270
|
+
*/
|
|
271
|
+
getRoutingRules(): RoutingRules;
|
|
272
|
+
private getProviderList;
|
|
273
|
+
private getHealthyProviders;
|
|
274
|
+
private routeToSavedMethodProvider;
|
|
275
|
+
private getFallbackProviders;
|
|
276
|
+
private getProviderFee;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Get or create singleton RoutingEngine instance
|
|
280
|
+
*/
|
|
281
|
+
declare function getRoutingEngine(): RoutingEngine;
|
|
282
|
+
/**
|
|
283
|
+
* Create a new RoutingEngine with custom dependencies
|
|
284
|
+
*/
|
|
285
|
+
declare function createRoutingEngine(deps?: RoutingEngineDeps): RoutingEngine;
|
|
286
|
+
/**
|
|
287
|
+
* Reset the singleton instance (useful for testing)
|
|
288
|
+
*/
|
|
289
|
+
declare function resetRoutingEngine(): void;
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* @nehorai/payments - Payment Orchestrator Service
|
|
293
|
+
*
|
|
294
|
+
* Main entry point for payment operations. Coordinates between:
|
|
295
|
+
* - Routing Engine (provider selection)
|
|
296
|
+
* - Payment Providers (actual processing)
|
|
297
|
+
* - Circuit Breaker (resilience)
|
|
298
|
+
*
|
|
299
|
+
* Supports full dependency injection for:
|
|
300
|
+
* - Provider instances
|
|
301
|
+
* - Routing engine
|
|
302
|
+
* - Circuit breaker
|
|
303
|
+
*/
|
|
304
|
+
|
|
305
|
+
interface InitiatePaymentParams {
|
|
306
|
+
userId: string;
|
|
307
|
+
amount: PaymentAmount;
|
|
308
|
+
transactionType: 'one_time_purchase' | 'subscription_initial' | 'subscription_renewal';
|
|
309
|
+
description?: string;
|
|
310
|
+
metadata?: PaymentMetadata;
|
|
311
|
+
preferredProvider?: PaymentProvider;
|
|
312
|
+
cardBin?: string;
|
|
313
|
+
returnUrl: string;
|
|
314
|
+
/** If true, auto-capture immediately (no J5 hold) */
|
|
315
|
+
autoCapture?: boolean;
|
|
316
|
+
}
|
|
317
|
+
interface PaymentInitiationResult {
|
|
318
|
+
success: boolean;
|
|
319
|
+
transactionId: string;
|
|
320
|
+
internalPaymentId: string;
|
|
321
|
+
provider: PaymentProvider;
|
|
322
|
+
clientSecret?: string;
|
|
323
|
+
redirectUrl?: string;
|
|
324
|
+
error?: string;
|
|
325
|
+
}
|
|
326
|
+
interface ConfirmPaymentParams {
|
|
327
|
+
transactionId: string;
|
|
328
|
+
internalPaymentId: string;
|
|
329
|
+
providerIntentId: string;
|
|
330
|
+
provider: PaymentProvider;
|
|
331
|
+
}
|
|
332
|
+
interface PaymentConfirmationResult {
|
|
333
|
+
success: boolean;
|
|
334
|
+
transactionId: string;
|
|
335
|
+
status: TransactionStatus;
|
|
336
|
+
error?: string;
|
|
337
|
+
}
|
|
338
|
+
interface CapturePaymentParams {
|
|
339
|
+
transactionId: string;
|
|
340
|
+
providerIntentId: string;
|
|
341
|
+
provider: PaymentProvider;
|
|
342
|
+
amount?: PaymentAmount;
|
|
343
|
+
}
|
|
344
|
+
interface PaymentCaptureResult {
|
|
345
|
+
success: boolean;
|
|
346
|
+
status: TransactionStatus;
|
|
347
|
+
capturedAmount?: PaymentAmount;
|
|
348
|
+
error?: string;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Payment orchestrator dependencies (for dependency injection)
|
|
352
|
+
*/
|
|
353
|
+
interface PaymentOrchestratorDeps {
|
|
354
|
+
/** Provider instances - required */
|
|
355
|
+
providers: Map<PaymentProvider, IPaymentProvider>;
|
|
356
|
+
/** Routing engine (optional, defaults to singleton) */
|
|
357
|
+
routingEngine?: RoutingEngine;
|
|
358
|
+
/** Circuit breaker (optional, defaults to singleton) */
|
|
359
|
+
circuitBreaker?: CircuitBreaker;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Payment Orchestrator
|
|
363
|
+
*
|
|
364
|
+
* Standalone service for payment orchestration.
|
|
365
|
+
* Supports full dependency injection for testing and customization.
|
|
366
|
+
*/
|
|
367
|
+
declare class PaymentOrchestrator {
|
|
368
|
+
private providers;
|
|
369
|
+
private routingEngine;
|
|
370
|
+
private circuitBreaker;
|
|
371
|
+
constructor(deps: PaymentOrchestratorDeps);
|
|
372
|
+
/**
|
|
373
|
+
* Initiate a new payment
|
|
374
|
+
*/
|
|
375
|
+
initiatePayment(params: InitiatePaymentParams): Promise<PaymentInitiationResult>;
|
|
376
|
+
/**
|
|
377
|
+
* Confirm a payment after user authorization
|
|
378
|
+
*/
|
|
379
|
+
confirmPayment(params: ConfirmPaymentParams): Promise<PaymentConfirmationResult>;
|
|
380
|
+
/**
|
|
381
|
+
* Capture an authorized payment (J5 completion)
|
|
382
|
+
*/
|
|
383
|
+
capturePayment(params: CapturePaymentParams): Promise<PaymentCaptureResult>;
|
|
384
|
+
/**
|
|
385
|
+
* Get the routing engine (for testing/debugging)
|
|
386
|
+
*/
|
|
387
|
+
getRoutingEngine(): RoutingEngine;
|
|
388
|
+
/**
|
|
389
|
+
* Get the circuit breaker (for testing/debugging)
|
|
390
|
+
*/
|
|
391
|
+
getCircuitBreaker(): CircuitBreaker;
|
|
392
|
+
/**
|
|
393
|
+
* Get available providers (for testing/debugging)
|
|
394
|
+
*/
|
|
395
|
+
getProviders(): Map<PaymentProvider, IPaymentProvider>;
|
|
396
|
+
private getProvider;
|
|
397
|
+
private tryFailover;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Create a payment orchestrator with custom dependencies
|
|
401
|
+
*/
|
|
402
|
+
declare function createPaymentOrchestrator(deps: PaymentOrchestratorDeps): PaymentOrchestrator;
|
|
403
|
+
|
|
404
|
+
export { shouldAttemptHalfOpen as A, type CardBinRule as C, type ICircuitBreakerStorage as I, type PaymentCaptureResult as P, RoutingEngine as R, type StoredCircuitState as S, CircuitBreaker as a, type CircuitBreakerConfig as b, type CircuitBreakerDeps as c, type CircuitBreakerState as d, type CircuitBreakerStateRecord as e, type CircuitState as f, type ConfirmPaymentParams as g, type CurrencyRule as h, type InitiatePaymentParams as i, type CapturePaymentParams as j, type PaymentConfirmationResult as k, type PaymentInitiationResult as l, PaymentOrchestrator as m, type PaymentOrchestratorDeps as n, type ProviderPriorityRule as o, type RoutingEngineDeps as p, type RoutingRules as q, createCircuitBreaker as r, createDefaultState as s, createPaymentOrchestrator as t, createRoutingEngine as u, getCircuitBreaker as v, getRoutingEngine as w, isCircuitOpen as x, resetCircuitBreaker as y, resetRoutingEngine as z };
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @nehorai/payments - Core Payment Types
|
|
3
|
+
*
|
|
4
|
+
* Defines the fundamental types for the payment system.
|
|
5
|
+
* Follows TypeScript conventions with literal union types.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Supported payment providers - extensible string identifier
|
|
9
|
+
*/
|
|
10
|
+
type PaymentProvider = string;
|
|
11
|
+
/**
|
|
12
|
+
* Types of payment transactions
|
|
13
|
+
*/
|
|
14
|
+
type TransactionType = 'one_time_purchase' | 'subscription_initial' | 'subscription_renewal' | 'refund';
|
|
15
|
+
/**
|
|
16
|
+
* Tax invoice status
|
|
17
|
+
*/
|
|
18
|
+
type TaxInvoiceStatus = 'pending' | 'generated' | 'sent' | 'failed';
|
|
19
|
+
/**
|
|
20
|
+
* Payment method types
|
|
21
|
+
*/
|
|
22
|
+
type PaymentMethodType = 'card' | 'bank_account' | 'paypal';
|
|
23
|
+
/**
|
|
24
|
+
* Card brands supported
|
|
25
|
+
*/
|
|
26
|
+
type CardBrand = 'visa' | 'mastercard' | 'amex' | 'discover' | 'isracard' | 'diners' | 'unknown';
|
|
27
|
+
/**
|
|
28
|
+
* Monetary amount in smallest currency unit (cents, agorot, etc.)
|
|
29
|
+
* Zero Trust: All amounts validated server-side, never trust client
|
|
30
|
+
*/
|
|
31
|
+
interface PaymentAmount {
|
|
32
|
+
/** Amount in smallest currency unit (e.g., 1000 = $10.00) */
|
|
33
|
+
amountMinor: number;
|
|
34
|
+
/** ISO 4217 currency code (e.g., 'USD', 'ILS') */
|
|
35
|
+
currency: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Result of currency conversion
|
|
39
|
+
*/
|
|
40
|
+
interface CurrencyConversion {
|
|
41
|
+
originalAmount: PaymentAmount;
|
|
42
|
+
convertedAmount: PaymentAmount;
|
|
43
|
+
exchangeRate: number;
|
|
44
|
+
convertedAt: Date;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Parameters for creating a payment intent
|
|
48
|
+
*/
|
|
49
|
+
interface CreatePaymentIntentParams {
|
|
50
|
+
amount: PaymentAmount;
|
|
51
|
+
userId: string;
|
|
52
|
+
idempotencyKey: string;
|
|
53
|
+
description?: string;
|
|
54
|
+
metadata?: PaymentMetadata;
|
|
55
|
+
returnUrl?: string;
|
|
56
|
+
paymentMethodId?: string;
|
|
57
|
+
/** If true, only authorize (J5 hold), don't capture */
|
|
58
|
+
captureMethod?: 'automatic' | 'manual';
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Result from creating a payment intent
|
|
62
|
+
*/
|
|
63
|
+
interface PaymentIntentResult {
|
|
64
|
+
success: boolean;
|
|
65
|
+
/** Provider's payment intent ID */
|
|
66
|
+
providerIntentId?: string;
|
|
67
|
+
/** Client secret for frontend SDK (Stripe Elements) */
|
|
68
|
+
clientSecret?: string;
|
|
69
|
+
/** Redirect URL for redirect-based flows */
|
|
70
|
+
redirectUrl?: string;
|
|
71
|
+
/** Current status of the payment */
|
|
72
|
+
status?: string;
|
|
73
|
+
error?: string;
|
|
74
|
+
errorCode?: string;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Parameters for authorizing a payment (J5 hold)
|
|
78
|
+
*/
|
|
79
|
+
interface AuthorizePaymentParams {
|
|
80
|
+
providerIntentId: string;
|
|
81
|
+
idempotencyKey: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Result from authorization
|
|
85
|
+
*/
|
|
86
|
+
interface AuthorizationResult {
|
|
87
|
+
success: boolean;
|
|
88
|
+
/** Authorization code for capture */
|
|
89
|
+
authorizationCode?: string;
|
|
90
|
+
status?: string;
|
|
91
|
+
/** Deadline to capture before auth expires (typically 7 days) */
|
|
92
|
+
captureDeadline?: Date;
|
|
93
|
+
error?: string;
|
|
94
|
+
errorCode?: string;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Parameters for capturing an authorized payment
|
|
98
|
+
*/
|
|
99
|
+
interface CapturePaymentParams {
|
|
100
|
+
providerIntentId: string;
|
|
101
|
+
authorizationCode: string;
|
|
102
|
+
/** For partial capture */
|
|
103
|
+
amount?: PaymentAmount;
|
|
104
|
+
idempotencyKey: string;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Result from capture
|
|
108
|
+
*/
|
|
109
|
+
interface CaptureResult {
|
|
110
|
+
success: boolean;
|
|
111
|
+
providerTransactionId?: string;
|
|
112
|
+
status?: string;
|
|
113
|
+
capturedAmount?: PaymentAmount;
|
|
114
|
+
error?: string;
|
|
115
|
+
errorCode?: string;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Parameters for voiding an authorization
|
|
119
|
+
*/
|
|
120
|
+
interface VoidPaymentParams {
|
|
121
|
+
providerIntentId: string;
|
|
122
|
+
authorizationCode: string;
|
|
123
|
+
idempotencyKey: string;
|
|
124
|
+
reason?: string;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Result from void
|
|
128
|
+
*/
|
|
129
|
+
interface VoidResult {
|
|
130
|
+
success: boolean;
|
|
131
|
+
status?: string;
|
|
132
|
+
error?: string;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Parameters for refunding a payment
|
|
136
|
+
*/
|
|
137
|
+
interface RefundParams {
|
|
138
|
+
providerTransactionId: string;
|
|
139
|
+
/** For partial refund */
|
|
140
|
+
amount?: PaymentAmount;
|
|
141
|
+
reason?: string;
|
|
142
|
+
idempotencyKey: string;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Result from refund
|
|
146
|
+
*/
|
|
147
|
+
interface RefundResult {
|
|
148
|
+
success: boolean;
|
|
149
|
+
providerRefundId?: string;
|
|
150
|
+
refundedAmount?: PaymentAmount;
|
|
151
|
+
status?: 'pending' | 'succeeded' | 'failed';
|
|
152
|
+
error?: string;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Application-specific metadata attached to payments
|
|
156
|
+
*/
|
|
157
|
+
interface PaymentMetadata {
|
|
158
|
+
/** Credit package being purchased */
|
|
159
|
+
creditPackageId?: string;
|
|
160
|
+
/** Subscription plan being purchased */
|
|
161
|
+
subscriptionPlanId?: string;
|
|
162
|
+
/** Number of credits being purchased */
|
|
163
|
+
creditsAmount?: number;
|
|
164
|
+
/** Custom fields */
|
|
165
|
+
[key: string]: unknown;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Provider-specific metadata (raw response data)
|
|
169
|
+
*/
|
|
170
|
+
interface ProviderMetadata {
|
|
171
|
+
/** Raw response from provider for debugging */
|
|
172
|
+
rawResponse?: Record<string, unknown>;
|
|
173
|
+
/** Provider-specific transaction ID */
|
|
174
|
+
providerTransactionId?: string;
|
|
175
|
+
/** Provider-specific authorization code */
|
|
176
|
+
authorizationCode?: string;
|
|
177
|
+
/** Additional provider data */
|
|
178
|
+
[key: string]: unknown;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Provider health status for circuit breaker
|
|
182
|
+
*/
|
|
183
|
+
interface ProviderHealthStatus {
|
|
184
|
+
provider: PaymentProvider;
|
|
185
|
+
healthy: boolean;
|
|
186
|
+
lastChecked: Date;
|
|
187
|
+
/** Average response time in milliseconds */
|
|
188
|
+
avgLatencyMs?: number;
|
|
189
|
+
/** Error rate (0-1) */
|
|
190
|
+
errorRate?: number;
|
|
191
|
+
/** Whether circuit breaker is open */
|
|
192
|
+
circuitBreakerOpen: boolean;
|
|
193
|
+
/** When circuit breaker will attempt to close */
|
|
194
|
+
nextRetryAt?: Date;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Standardized payment error codes
|
|
198
|
+
*/
|
|
199
|
+
type PaymentErrorCode = 'insufficient_funds' | 'invalid_card' | 'expired_card' | 'card_declined' | 'processing_error' | 'provider_timeout' | 'provider_unavailable' | 'rate_limit' | 'webhook_signature_invalid' | 'idempotency_conflict' | 'invalid_amount' | 'invalid_currency' | 'authentication_required' | 'unknown';
|
|
200
|
+
/**
|
|
201
|
+
* Payment error with structured information
|
|
202
|
+
*/
|
|
203
|
+
interface PaymentError {
|
|
204
|
+
code: PaymentErrorCode;
|
|
205
|
+
message: string;
|
|
206
|
+
provider?: PaymentProvider;
|
|
207
|
+
retryable: boolean;
|
|
208
|
+
details?: Record<string, unknown>;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export type { AuthorizationResult as A, CapturePaymentParams as C, PaymentAmount as P, RefundParams as R, TaxInvoiceStatus as T, VoidPaymentParams as V, AuthorizePaymentParams as a, CaptureResult as b, CardBrand as c, CreatePaymentIntentParams as d, CurrencyConversion as e, PaymentError as f, PaymentErrorCode as g, PaymentIntentResult as h, PaymentMetadata as i, PaymentMethodType as j, PaymentProvider as k, ProviderHealthStatus as l, ProviderMetadata as m, RefundResult as n, TransactionType as o, VoidResult as p };
|