@parsrun/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/README.md ADDED
@@ -0,0 +1,184 @@
1
+ # @parsrun/payments
2
+
3
+ Edge-compatible payment processing for Pars with subscription and billing support.
4
+
5
+ ## Features
6
+
7
+ - **Multi-Provider**: Stripe, Paddle, iyzico
8
+ - **Subscriptions**: Full subscription lifecycle management
9
+ - **Usage Billing**: Metered billing support
10
+ - **Webhooks**: Secure webhook handling
11
+ - **Dunning**: Failed payment recovery
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pnpm add @parsrun/payments
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ ```typescript
22
+ import { createPaymentService } from '@parsrun/payments';
23
+
24
+ const payments = createPaymentService({
25
+ provider: 'stripe',
26
+ secretKey: process.env.STRIPE_SECRET_KEY,
27
+ webhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
28
+ });
29
+
30
+ // Create checkout session
31
+ const session = await payments.createCheckout({
32
+ customerId: 'cus_xxx',
33
+ priceId: 'price_xxx',
34
+ successUrl: 'https://example.com/success',
35
+ cancelUrl: 'https://example.com/cancel',
36
+ });
37
+ ```
38
+
39
+ ## API Overview
40
+
41
+ ### Providers
42
+
43
+ #### Stripe
44
+
45
+ ```typescript
46
+ import { createStripeProvider } from '@parsrun/payments/providers/stripe';
47
+
48
+ const stripe = createStripeProvider({
49
+ secretKey: process.env.STRIPE_SECRET_KEY,
50
+ webhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
51
+ });
52
+ ```
53
+
54
+ #### Paddle
55
+
56
+ ```typescript
57
+ import { createPaddleProvider } from '@parsrun/payments/providers/paddle';
58
+
59
+ const paddle = createPaddleProvider({
60
+ vendorId: process.env.PADDLE_VENDOR_ID,
61
+ apiKey: process.env.PADDLE_API_KEY,
62
+ });
63
+ ```
64
+
65
+ #### iyzico
66
+
67
+ ```typescript
68
+ import { createIyzicoProvider } from '@parsrun/payments/providers/iyzico';
69
+
70
+ const iyzico = createIyzicoProvider({
71
+ apiKey: process.env.IYZICO_API_KEY,
72
+ secretKey: process.env.IYZICO_SECRET_KEY,
73
+ sandbox: true,
74
+ });
75
+ ```
76
+
77
+ ### Subscriptions
78
+
79
+ ```typescript
80
+ // Create subscription
81
+ const subscription = await payments.createSubscription({
82
+ customerId: 'cus_xxx',
83
+ priceId: 'price_xxx',
84
+ trialDays: 14,
85
+ });
86
+
87
+ // Cancel subscription
88
+ await payments.cancelSubscription(subscriptionId, {
89
+ cancelAtPeriodEnd: true,
90
+ });
91
+
92
+ // Update subscription
93
+ await payments.updateSubscription(subscriptionId, {
94
+ priceId: 'price_new',
95
+ });
96
+ ```
97
+
98
+ ### Billing
99
+
100
+ ```typescript
101
+ import { createBillingManager } from '@parsrun/payments/billing';
102
+
103
+ const billing = createBillingManager({
104
+ provider: stripeProvider,
105
+ database: db,
106
+ });
107
+
108
+ // Get customer billing info
109
+ const info = await billing.getCustomerBilling(customerId);
110
+
111
+ // Create invoice
112
+ const invoice = await billing.createInvoice({
113
+ customerId,
114
+ items: [{ description: 'Service', amount: 1000 }],
115
+ });
116
+ ```
117
+
118
+ ### Usage Billing
119
+
120
+ ```typescript
121
+ import { createUsageTracker } from '@parsrun/payments/usage';
122
+
123
+ const usage = createUsageTracker({
124
+ provider: stripeProvider,
125
+ });
126
+
127
+ // Record usage
128
+ await usage.record({
129
+ subscriptionId: 'sub_xxx',
130
+ quantity: 100,
131
+ timestamp: new Date(),
132
+ });
133
+ ```
134
+
135
+ ### Webhooks
136
+
137
+ ```typescript
138
+ import { createWebhookHandler } from '@parsrun/payments/webhooks';
139
+
140
+ const webhooks = createWebhookHandler({
141
+ provider: stripeProvider,
142
+ handlers: {
143
+ 'checkout.session.completed': async (event) => {
144
+ // Handle successful checkout
145
+ },
146
+ 'invoice.payment_failed': async (event) => {
147
+ // Handle failed payment
148
+ },
149
+ },
150
+ });
151
+
152
+ app.post('/webhooks/stripe', webhooks.handle);
153
+ ```
154
+
155
+ ### Dunning (Payment Recovery)
156
+
157
+ ```typescript
158
+ import { createDunningManager } from '@parsrun/payments/dunning';
159
+
160
+ const dunning = createDunningManager({
161
+ provider: stripeProvider,
162
+ retrySchedule: [1, 3, 7, 14], // Days
163
+ onFinalFailure: async (subscription) => {
164
+ // Handle subscription cancellation
165
+ },
166
+ });
167
+ ```
168
+
169
+ ## Exports
170
+
171
+ ```typescript
172
+ import { ... } from '@parsrun/payments'; // Main exports
173
+ import { ... } from '@parsrun/payments/providers/stripe'; // Stripe
174
+ import { ... } from '@parsrun/payments/providers/paddle'; // Paddle
175
+ import { ... } from '@parsrun/payments/providers/iyzico'; // iyzico
176
+ import { ... } from '@parsrun/payments/billing'; // Billing
177
+ import { ... } from '@parsrun/payments/usage'; // Usage billing
178
+ import { ... } from '@parsrun/payments/webhooks'; // Webhooks
179
+ import { ... } from '@parsrun/payments/dunning'; // Dunning
180
+ ```
181
+
182
+ ## License
183
+
184
+ MIT
@@ -0,0 +1,121 @@
1
+ import { g as ProviderStrategyConfig, k as BillingLogger, f as RegionDetectionContext, p as ProviderSelection, d as BillingRegion, F as FallbackConfig, h as FallbackOperation } from '../billing-service-LsAFesou.js';
2
+ export { o as BillingCustomer, a as BillingError, q as BillingErrorCode, b as BillingErrorCodes, B as BillingService, j as BillingServiceConfig, n as BillingSubscription, C as CancelOptions, m as CancelResult, i as FallbackContext, G as GetSubscriptionOptions, P as ProviderRoutingRule, R as RegionDetectionResult, e as RegionDetector, S as SubscribeOptions, l as SubscribeResult, c as createBillingService } from '../billing-service-LsAFesou.js';
3
+ import { PaymentProviderType, PaymentProvider } from '../types.js';
4
+ import '../webhooks/index.js';
5
+ import '@parsrun/types';
6
+
7
+ /**
8
+ * @parsrun/payments - Provider Strategy
9
+ * Region-based provider routing and selection
10
+ */
11
+
12
+ /**
13
+ * Provider Strategy
14
+ * Handles region-based provider routing and selection
15
+ */
16
+ declare class ProviderStrategy {
17
+ private readonly defaultProvider;
18
+ private readonly regionProviders;
19
+ private readonly rules;
20
+ private readonly regionDetector;
21
+ private readonly logger;
22
+ constructor(config: ProviderStrategyConfig, logger?: BillingLogger);
23
+ /**
24
+ * Select provider for given context
25
+ */
26
+ selectProvider(context: RegionDetectionContext, forceProvider?: PaymentProviderType): Promise<ProviderSelection>;
27
+ /**
28
+ * Detect region from context
29
+ */
30
+ detectRegion(context: RegionDetectionContext): Promise<BillingRegion>;
31
+ /**
32
+ * Get all configured providers
33
+ */
34
+ getAllProviders(): PaymentProvider[];
35
+ /**
36
+ * Get provider by type
37
+ */
38
+ getProviderByType(type: PaymentProviderType): PaymentProvider | undefined;
39
+ /**
40
+ * Get default provider
41
+ */
42
+ getDefaultProvider(): PaymentProvider;
43
+ /**
44
+ * Get provider for region
45
+ */
46
+ getProviderForRegion(region: BillingRegion): PaymentProvider;
47
+ /**
48
+ * Check if region is supported
49
+ */
50
+ isRegionSupported(region: BillingRegion): boolean;
51
+ /**
52
+ * Get supported regions
53
+ */
54
+ getSupportedRegions(): BillingRegion[];
55
+ /**
56
+ * Find provider by type across all configured providers
57
+ */
58
+ private findProviderByType;
59
+ }
60
+ /**
61
+ * Create provider strategy
62
+ */
63
+ declare function createProviderStrategy(config: ProviderStrategyConfig, logger?: BillingLogger): ProviderStrategy;
64
+
65
+ /**
66
+ * @parsrun/payments - Fallback Executor
67
+ * Handles provider fallback with safety controls
68
+ */
69
+
70
+ /**
71
+ * Fallback Executor
72
+ * Handles provider fallback with safety controls and logging
73
+ */
74
+ declare class FallbackExecutor {
75
+ private readonly config;
76
+ private readonly logger;
77
+ constructor(config: FallbackConfig, logger?: BillingLogger);
78
+ /**
79
+ * Check if fallback is enabled
80
+ */
81
+ get isEnabled(): boolean;
82
+ /**
83
+ * Execute operation with fallback support
84
+ *
85
+ * @param operation - Operation name for safety checks
86
+ * @param primaryProvider - Primary provider to try first
87
+ * @param fallbackProviders - Fallback providers in order of preference
88
+ * @param execute - Function that executes the operation on a provider
89
+ */
90
+ execute<T>(operation: FallbackOperation, primaryProvider: PaymentProvider, fallbackProviders: PaymentProvider[], execute: (provider: PaymentProvider) => Promise<T>): Promise<{
91
+ result: T;
92
+ provider: PaymentProvider;
93
+ usedFallback: boolean;
94
+ }>;
95
+ /**
96
+ * Check if fallback is allowed for operation
97
+ */
98
+ canFallback(operation: FallbackOperation): boolean;
99
+ /**
100
+ * Check if error is retryable
101
+ */
102
+ isRetryableError(error: Error): boolean;
103
+ /**
104
+ * Notify fallback callback
105
+ */
106
+ private notifyFallback;
107
+ /**
108
+ * Notify all failed callback
109
+ */
110
+ private notifyAllFailed;
111
+ }
112
+ /**
113
+ * Create a disabled fallback executor (default behavior)
114
+ */
115
+ declare function createDisabledFallback(): FallbackExecutor;
116
+ /**
117
+ * Create fallback executor
118
+ */
119
+ declare function createFallbackExecutor(config: FallbackConfig, logger?: BillingLogger): FallbackExecutor;
120
+
121
+ export { BillingLogger, BillingRegion, FallbackConfig, FallbackExecutor, FallbackOperation, ProviderSelection, ProviderStrategy, ProviderStrategyConfig, RegionDetectionContext, createDisabledFallback, createFallbackExecutor, createProviderStrategy };