@theengineerscanvas/payment 0.0.0-stage → 0.1.0-rc.16
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 +201 -0
- package/README.md +323 -2
- package/dist/adapters/nestjs/index.cjs +4979 -0
- package/dist/adapters/nestjs/index.cjs.map +1 -0
- package/dist/adapters/nestjs/index.d.cts +313 -0
- package/dist/adapters/nestjs/index.d.ts +313 -0
- package/dist/adapters/nestjs/index.js +4974 -0
- package/dist/adapters/nestjs/index.js.map +1 -0
- package/dist/index.cjs +4865 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +171 -0
- package/dist/index.d.ts +171 -0
- package/dist/index.js +4822 -0
- package/dist/index.js.map +1 -0
- package/dist/payment-client-DNhLWboh.d.cts +1257 -0
- package/dist/payment-client-DNhLWboh.d.ts +1257 -0
- package/docs/agents/cookbook.md +467 -0
- package/docs/api-stability.md +104 -0
- package/docs/architecture.md +119 -0
- package/docs/bachs-guide.md +359 -0
- package/docs/bachs-sandbox-validation.md +138 -0
- package/docs/concurrency.md +103 -0
- package/docs/extension-guide.md +156 -0
- package/docs/integration-checklist.md +209 -0
- package/docs/migration-guide.md +401 -0
- package/docs/performance.md +86 -0
- package/docs/provider-guide.md +150 -0
- package/docs/public-api.md +1108 -0
- package/docs/security.md +120 -0
- package/docs/versioning.md +95 -0
- package/llms.txt +97 -0
- package/package.json +141 -4
- package/public-api.json +452 -0
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { CanActivate, ExecutionContext, DynamicModule, ModuleMetadata, FactoryProvider, LoggerService } from '@nestjs/common';
|
|
2
|
+
import { P as PaymentClientConfig, a as PaymentClient, H as HealthStatus, L as Logger } from '../../payment-client-DNhLWboh.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Injection token for {@link WebhookRateLimitOptions}.
|
|
6
|
+
*
|
|
7
|
+
* Provided by {@link PaymentModule} when `rateLimit` is configured.
|
|
8
|
+
*/
|
|
9
|
+
declare const WEBHOOK_RATE_LIMIT_OPTIONS = "TEC_PAYMENT_WEBHOOK_RATE_LIMIT_OPTIONS";
|
|
10
|
+
/**
|
|
11
|
+
* Options for {@link WebhookRateLimitGuard}.
|
|
12
|
+
*/
|
|
13
|
+
interface WebhookRateLimitOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Maximum number of allowed requests per IP within `ttlMs`.
|
|
16
|
+
*/
|
|
17
|
+
readonly limit: number;
|
|
18
|
+
/**
|
|
19
|
+
* Window length in milliseconds.
|
|
20
|
+
*/
|
|
21
|
+
readonly ttlMs: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Zero-dependency in-memory fixed-window rate limiter for the webhook
|
|
25
|
+
* endpoint.
|
|
26
|
+
*
|
|
27
|
+
* Keys on `req.ip`, returning `429 Too Many Requests` once a client
|
|
28
|
+
* exceeds `limit` requests within `ttlMs`. Expired buckets are swept
|
|
29
|
+
* lazily when the map grows, so memory stays bounded without a timer.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* In-memory state is per-process: with multiple application instances
|
|
33
|
+
* the effective limit is `limit * instances`. Deployments needing a
|
|
34
|
+
* shared limiter should place a reverse-proxy rate limit in front of
|
|
35
|
+
* the webhook path instead.
|
|
36
|
+
*/
|
|
37
|
+
declare class WebhookRateLimitGuard implements CanActivate {
|
|
38
|
+
private readonly buckets;
|
|
39
|
+
private readonly options;
|
|
40
|
+
constructor(options: WebhookRateLimitOptions);
|
|
41
|
+
canActivate(context: ExecutionContext): boolean;
|
|
42
|
+
private sweep;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Injection token for the `@theengineerscanvas/payment` {@link PaymentClient} instance.
|
|
47
|
+
*
|
|
48
|
+
* Inject the raw client when you need direct access to the SDK's service
|
|
49
|
+
* facades (`payments`, `refunds`, `webhooks`, `events`, `health`).
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { Inject } from "@nestjs/common";
|
|
54
|
+
* import { TEC_PAYMENT_CLIENT } from "@theengineerscanvas/payment/nestjs";
|
|
55
|
+
* import type { PaymentClient } from "@theengineerscanvas/payment";
|
|
56
|
+
*
|
|
57
|
+
* @Injectable()
|
|
58
|
+
* export class MyService {
|
|
59
|
+
* constructor(@Inject(TEC_PAYMENT_CLIENT) private readonly tec: PaymentClient) {}
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare const TEC_PAYMENT_CLIENT = "TEC_PAYMENT_CLIENT";
|
|
64
|
+
/**
|
|
65
|
+
* Default path the built-in {@link WebhookController} is registered on.
|
|
66
|
+
*
|
|
67
|
+
* Used as the `@Controller()` prefix. Consumers overriding the route
|
|
68
|
+
* should subclass {@link WebhookController} with their own `@Controller()` decorator.
|
|
69
|
+
*/
|
|
70
|
+
declare const DEFAULT_WEBHOOK_PATH = "webhooks/payment";
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Options for {@link PaymentModule.forRoot}.
|
|
74
|
+
*/
|
|
75
|
+
interface PaymentModuleOptions {
|
|
76
|
+
/**
|
|
77
|
+
* Whether the built-in {@link WebhookController} is registered.
|
|
78
|
+
*
|
|
79
|
+
* Set to `false` when you need a custom webhook path. Register your
|
|
80
|
+
* own subclass of {@link WebhookController} with a different
|
|
81
|
+
* `@Controller()` decorator instead.
|
|
82
|
+
*
|
|
83
|
+
* @default true
|
|
84
|
+
*/
|
|
85
|
+
registerWebhookController?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Path prefix for the built-in webhook controller.
|
|
88
|
+
*
|
|
89
|
+
* Only used when {@link registerWebhookController} is not `false`.
|
|
90
|
+
*
|
|
91
|
+
* @default "webhooks/payment"
|
|
92
|
+
*/
|
|
93
|
+
webhookPath?: string;
|
|
94
|
+
/**
|
|
95
|
+
* Rate limiting for the built-in webhook controller.
|
|
96
|
+
*
|
|
97
|
+
* When provided, a zero-dependency in-memory fixed-window limiter is
|
|
98
|
+
* applied per `req.ip`, rejecting excess requests with `429`.
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* State is per-process. Multi-instance deployments should rate limit
|
|
102
|
+
* at the reverse proxy instead.
|
|
103
|
+
*/
|
|
104
|
+
rateLimit?: WebhookRateLimitOptions;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Async options for {@link PaymentModule.forRootAsync}.
|
|
108
|
+
*/
|
|
109
|
+
interface PaymentModuleAsyncOptions extends Pick<ModuleMetadata, "imports"> {
|
|
110
|
+
/**
|
|
111
|
+
* Factory returning a {@link PaymentClientConfig}, typically resolving
|
|
112
|
+
* secrets from `ConfigService` or a similar async provider.
|
|
113
|
+
*/
|
|
114
|
+
useFactory: (...args: any[]) => PaymentClientConfig | Promise<PaymentClientConfig>;
|
|
115
|
+
/**
|
|
116
|
+
* Injection tokens passed to `useFactory`.
|
|
117
|
+
*/
|
|
118
|
+
inject?: FactoryProvider["inject"];
|
|
119
|
+
/**
|
|
120
|
+
* @inheritdoc
|
|
121
|
+
*
|
|
122
|
+
* @default true
|
|
123
|
+
*/
|
|
124
|
+
registerWebhookController?: boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Path prefix for the built-in webhook controller.
|
|
127
|
+
*
|
|
128
|
+
* @default "webhooks/payment"
|
|
129
|
+
*/
|
|
130
|
+
webhookPath?: string;
|
|
131
|
+
/**
|
|
132
|
+
* @inheritdoc PaymentModuleOptions.rateLimit
|
|
133
|
+
*/
|
|
134
|
+
rateLimit?: WebhookRateLimitOptions;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* NestJS dynamic module integrating `@theengineerscanvas/payment` into the DI container.
|
|
138
|
+
*
|
|
139
|
+
* Registers {@link PaymentService} and the raw {@link PaymentClient}
|
|
140
|
+
* behind the {@link TEC_PAYMENT_CLIENT} injection token. The built-in
|
|
141
|
+
* {@link WebhookController} is registered at
|
|
142
|
+
* {@link DEFAULT_WEBHOOK_PATH} unless opted out.
|
|
143
|
+
*
|
|
144
|
+
* **Static configuration:**
|
|
145
|
+
*
|
|
146
|
+
* ```ts
|
|
147
|
+
* PaymentModule.forRoot({
|
|
148
|
+
* providers: { paystack: { secretKey: "sk_..." } },
|
|
149
|
+
* defaultProvider: "paystack",
|
|
150
|
+
* });
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* **Async configuration (e.g. from ConfigService):**
|
|
154
|
+
*
|
|
155
|
+
* ```ts
|
|
156
|
+
* PaymentModule.forRootAsync({
|
|
157
|
+
* imports: [ConfigModule],
|
|
158
|
+
* useFactory: (config: ConfigService) => ({
|
|
159
|
+
* providers: { paystack: { secretKey: config.get("PAYSTACK_KEY")! } },
|
|
160
|
+
* }),
|
|
161
|
+
* inject: [ConfigService],
|
|
162
|
+
* });
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* **Opting out of the built-in webhook controller:**
|
|
166
|
+
*
|
|
167
|
+
* ```ts
|
|
168
|
+
* PaymentModule.forRoot(config, { registerWebhookController: false });
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
declare class PaymentModule {
|
|
172
|
+
/**
|
|
173
|
+
* Registers a {@link PaymentClient} synchronously from a static
|
|
174
|
+
* {@link PaymentClientConfig}.
|
|
175
|
+
*/
|
|
176
|
+
static forRoot(config: PaymentClientConfig, options?: PaymentModuleOptions): DynamicModule;
|
|
177
|
+
/**
|
|
178
|
+
* Registers a {@link PaymentClient} asynchronously, typically
|
|
179
|
+
* pulling secrets from `ConfigService` or a similar provider.
|
|
180
|
+
*/
|
|
181
|
+
static forRootAsync(options: PaymentModuleAsyncOptions): DynamicModule;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Shape of an Express/Fastify request after `rawBody: true` is set on
|
|
186
|
+
* `NestFactory.create()`. Consumers *must* enable this option or the
|
|
187
|
+
* built-in {@link WebhookController} will receive an empty body and
|
|
188
|
+
* every webhook will be rejected.
|
|
189
|
+
*
|
|
190
|
+
* @see {@link WebhookController}
|
|
191
|
+
*/
|
|
192
|
+
interface RawBodyRequest {
|
|
193
|
+
/** Nest's raw body is a `Buffer`; typed as `Uint8Array` to avoid a Node types dependency. */
|
|
194
|
+
readonly rawBody?: string | Uint8Array;
|
|
195
|
+
readonly headers?: Record<string, string | readonly string[] | undefined>;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Built-in webhook endpoint registered at `POST /webhooks/payment`.
|
|
200
|
+
*
|
|
201
|
+
* **IMPORTANT:** Consumers **must** enable raw body capture in `main.ts`:
|
|
202
|
+
*
|
|
203
|
+
* ```ts
|
|
204
|
+
* const app = await NestFactory.create(AppModule, { rawBody: true });
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
* Without this, `req.rawBody` is `undefined` and every webhook will be
|
|
208
|
+
* rejected with a 401.
|
|
209
|
+
*
|
|
210
|
+
* Disable this controller by passing `{ registerWebhookController: false }`
|
|
211
|
+
* to {@link PaymentModule.forRoot} and register your own subclass:
|
|
212
|
+
*
|
|
213
|
+
* ```ts
|
|
214
|
+
* @Controller("webhooks/my-path")
|
|
215
|
+
* class MyController extends WebhookController {
|
|
216
|
+
* constructor(@Inject(TEC_PAYMENT_CLIENT) client: PaymentClient) {
|
|
217
|
+
* super(client);
|
|
218
|
+
* }
|
|
219
|
+
* }
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
declare class WebhookController {
|
|
223
|
+
private readonly client;
|
|
224
|
+
private readonly logger;
|
|
225
|
+
constructor(client: PaymentClient, logger?: LoggerService);
|
|
226
|
+
/**
|
|
227
|
+
* Receives a provider webhook, verifies its signature, and emits the
|
|
228
|
+
* corresponding domain event via the configured event bus.
|
|
229
|
+
*
|
|
230
|
+
* The signature header name is not enforced here — the underlying
|
|
231
|
+
* {@link WebhookService.receive} performs a try-all over every
|
|
232
|
+
* configured provider and each adapter reads its own header(s), so
|
|
233
|
+
* multi-provider setups work correctly as long as the full `headers` map is
|
|
234
|
+
* forwarded.
|
|
235
|
+
*/
|
|
236
|
+
handle(req: RawBodyRequest, res: unknown): Promise<void>;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Injectable NestJS service wrapping the `@theengineerscanvas/payment` client.
|
|
241
|
+
*
|
|
242
|
+
* Delegates every sub-service (`payments`, `refunds`, `webhooks`,
|
|
243
|
+
* `transfers`, `products`, `subscriptions`, `customers`, `events`,
|
|
244
|
+
* `health`) directly to the underlying {@link PaymentClient}.
|
|
245
|
+
* {@link https://www.npmjs.com/package/@theengineerscanvas/payment Result} objects
|
|
246
|
+
* are returned as-is — the consuming application decides how to handle
|
|
247
|
+
* success and failure cases.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```ts
|
|
251
|
+
* import { PaymentService } from "@theengineerscanvas/payment/nestjs";
|
|
252
|
+
* import { Money, PaymentReference } from "@theengineerscanvas/payment";
|
|
253
|
+
*
|
|
254
|
+
* @Injectable()
|
|
255
|
+
* export class CheckoutService {
|
|
256
|
+
* constructor(private readonly tec: PaymentService) {}
|
|
257
|
+
*
|
|
258
|
+
* async checkout(amount: number, email: string) {
|
|
259
|
+
* const result = await this.tec.payments.initialize({
|
|
260
|
+
* amount: Money({ amount, currency: "NGN" }),
|
|
261
|
+
* reference: PaymentReference(`order-${Date.now()}`),
|
|
262
|
+
* customer: { kind: "new", email },
|
|
263
|
+
* callbackUrl: "https://myapp.com/verify",
|
|
264
|
+
* });
|
|
265
|
+
* if (!result.ok) return { error: result.error.message };
|
|
266
|
+
* return { url: result.value.authorizationUrl };
|
|
267
|
+
* }
|
|
268
|
+
* }
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
declare class PaymentService {
|
|
272
|
+
private readonly client;
|
|
273
|
+
constructor(client: PaymentClient);
|
|
274
|
+
get payments(): PaymentClient["payments"];
|
|
275
|
+
get refunds(): PaymentClient["refunds"];
|
|
276
|
+
get webhooks(): PaymentClient["webhooks"];
|
|
277
|
+
get transfers(): PaymentClient["transfers"];
|
|
278
|
+
/** Catalog and billing. Returns `UnsupportedProviderError` per call when the provider lacks the capability. */
|
|
279
|
+
get products(): PaymentClient["products"];
|
|
280
|
+
get subscriptions(): PaymentClient["subscriptions"];
|
|
281
|
+
get customers(): PaymentClient["customers"];
|
|
282
|
+
get events(): PaymentClient["events"];
|
|
283
|
+
health(): Promise<ReadonlyArray<HealthStatus>>;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
type LogContext = Record<string, string | number | boolean | undefined>;
|
|
287
|
+
/**
|
|
288
|
+
* Bridges NestJS's `Logger` to the `@theengineerscanvas/payment` SDK's {@link Logger} interface.
|
|
289
|
+
*
|
|
290
|
+
* Pass an instance to the `logger` field of {@link PaymentClientConfig} to
|
|
291
|
+
* route all SDK log output through Nest's structured logger.
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* import { NestLoggerAdapter } from "@theengineerscanvas/payment/nestjs";
|
|
296
|
+
*
|
|
297
|
+
* PaymentModule.forRoot({
|
|
298
|
+
* providers: { paystack: { secretKey: "sk_..." } },
|
|
299
|
+
* logger: new NestLoggerAdapter("PaymentClient"),
|
|
300
|
+
* });
|
|
301
|
+
* ```
|
|
302
|
+
*/
|
|
303
|
+
declare class NestLoggerAdapter implements Logger {
|
|
304
|
+
private readonly logger;
|
|
305
|
+
constructor(context?: string);
|
|
306
|
+
debug(message: string, context?: LogContext): void;
|
|
307
|
+
info(message: string, context?: LogContext): void;
|
|
308
|
+
warn(message: string, context?: LogContext): void;
|
|
309
|
+
error(message: string, error?: unknown, context?: LogContext): void;
|
|
310
|
+
child(bindings: LogContext): Logger;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export { DEFAULT_WEBHOOK_PATH, NestLoggerAdapter, PaymentModule, type PaymentModuleAsyncOptions, type PaymentModuleOptions, PaymentService, type RawBodyRequest, TEC_PAYMENT_CLIENT, WEBHOOK_RATE_LIMIT_OPTIONS, WebhookController, WebhookRateLimitGuard, type WebhookRateLimitOptions };
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { CanActivate, ExecutionContext, DynamicModule, ModuleMetadata, FactoryProvider, LoggerService } from '@nestjs/common';
|
|
2
|
+
import { P as PaymentClientConfig, a as PaymentClient, H as HealthStatus, L as Logger } from '../../payment-client-DNhLWboh.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Injection token for {@link WebhookRateLimitOptions}.
|
|
6
|
+
*
|
|
7
|
+
* Provided by {@link PaymentModule} when `rateLimit` is configured.
|
|
8
|
+
*/
|
|
9
|
+
declare const WEBHOOK_RATE_LIMIT_OPTIONS = "TEC_PAYMENT_WEBHOOK_RATE_LIMIT_OPTIONS";
|
|
10
|
+
/**
|
|
11
|
+
* Options for {@link WebhookRateLimitGuard}.
|
|
12
|
+
*/
|
|
13
|
+
interface WebhookRateLimitOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Maximum number of allowed requests per IP within `ttlMs`.
|
|
16
|
+
*/
|
|
17
|
+
readonly limit: number;
|
|
18
|
+
/**
|
|
19
|
+
* Window length in milliseconds.
|
|
20
|
+
*/
|
|
21
|
+
readonly ttlMs: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Zero-dependency in-memory fixed-window rate limiter for the webhook
|
|
25
|
+
* endpoint.
|
|
26
|
+
*
|
|
27
|
+
* Keys on `req.ip`, returning `429 Too Many Requests` once a client
|
|
28
|
+
* exceeds `limit` requests within `ttlMs`. Expired buckets are swept
|
|
29
|
+
* lazily when the map grows, so memory stays bounded without a timer.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* In-memory state is per-process: with multiple application instances
|
|
33
|
+
* the effective limit is `limit * instances`. Deployments needing a
|
|
34
|
+
* shared limiter should place a reverse-proxy rate limit in front of
|
|
35
|
+
* the webhook path instead.
|
|
36
|
+
*/
|
|
37
|
+
declare class WebhookRateLimitGuard implements CanActivate {
|
|
38
|
+
private readonly buckets;
|
|
39
|
+
private readonly options;
|
|
40
|
+
constructor(options: WebhookRateLimitOptions);
|
|
41
|
+
canActivate(context: ExecutionContext): boolean;
|
|
42
|
+
private sweep;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Injection token for the `@theengineerscanvas/payment` {@link PaymentClient} instance.
|
|
47
|
+
*
|
|
48
|
+
* Inject the raw client when you need direct access to the SDK's service
|
|
49
|
+
* facades (`payments`, `refunds`, `webhooks`, `events`, `health`).
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { Inject } from "@nestjs/common";
|
|
54
|
+
* import { TEC_PAYMENT_CLIENT } from "@theengineerscanvas/payment/nestjs";
|
|
55
|
+
* import type { PaymentClient } from "@theengineerscanvas/payment";
|
|
56
|
+
*
|
|
57
|
+
* @Injectable()
|
|
58
|
+
* export class MyService {
|
|
59
|
+
* constructor(@Inject(TEC_PAYMENT_CLIENT) private readonly tec: PaymentClient) {}
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare const TEC_PAYMENT_CLIENT = "TEC_PAYMENT_CLIENT";
|
|
64
|
+
/**
|
|
65
|
+
* Default path the built-in {@link WebhookController} is registered on.
|
|
66
|
+
*
|
|
67
|
+
* Used as the `@Controller()` prefix. Consumers overriding the route
|
|
68
|
+
* should subclass {@link WebhookController} with their own `@Controller()` decorator.
|
|
69
|
+
*/
|
|
70
|
+
declare const DEFAULT_WEBHOOK_PATH = "webhooks/payment";
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Options for {@link PaymentModule.forRoot}.
|
|
74
|
+
*/
|
|
75
|
+
interface PaymentModuleOptions {
|
|
76
|
+
/**
|
|
77
|
+
* Whether the built-in {@link WebhookController} is registered.
|
|
78
|
+
*
|
|
79
|
+
* Set to `false` when you need a custom webhook path. Register your
|
|
80
|
+
* own subclass of {@link WebhookController} with a different
|
|
81
|
+
* `@Controller()` decorator instead.
|
|
82
|
+
*
|
|
83
|
+
* @default true
|
|
84
|
+
*/
|
|
85
|
+
registerWebhookController?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Path prefix for the built-in webhook controller.
|
|
88
|
+
*
|
|
89
|
+
* Only used when {@link registerWebhookController} is not `false`.
|
|
90
|
+
*
|
|
91
|
+
* @default "webhooks/payment"
|
|
92
|
+
*/
|
|
93
|
+
webhookPath?: string;
|
|
94
|
+
/**
|
|
95
|
+
* Rate limiting for the built-in webhook controller.
|
|
96
|
+
*
|
|
97
|
+
* When provided, a zero-dependency in-memory fixed-window limiter is
|
|
98
|
+
* applied per `req.ip`, rejecting excess requests with `429`.
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* State is per-process. Multi-instance deployments should rate limit
|
|
102
|
+
* at the reverse proxy instead.
|
|
103
|
+
*/
|
|
104
|
+
rateLimit?: WebhookRateLimitOptions;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Async options for {@link PaymentModule.forRootAsync}.
|
|
108
|
+
*/
|
|
109
|
+
interface PaymentModuleAsyncOptions extends Pick<ModuleMetadata, "imports"> {
|
|
110
|
+
/**
|
|
111
|
+
* Factory returning a {@link PaymentClientConfig}, typically resolving
|
|
112
|
+
* secrets from `ConfigService` or a similar async provider.
|
|
113
|
+
*/
|
|
114
|
+
useFactory: (...args: any[]) => PaymentClientConfig | Promise<PaymentClientConfig>;
|
|
115
|
+
/**
|
|
116
|
+
* Injection tokens passed to `useFactory`.
|
|
117
|
+
*/
|
|
118
|
+
inject?: FactoryProvider["inject"];
|
|
119
|
+
/**
|
|
120
|
+
* @inheritdoc
|
|
121
|
+
*
|
|
122
|
+
* @default true
|
|
123
|
+
*/
|
|
124
|
+
registerWebhookController?: boolean;
|
|
125
|
+
/**
|
|
126
|
+
* Path prefix for the built-in webhook controller.
|
|
127
|
+
*
|
|
128
|
+
* @default "webhooks/payment"
|
|
129
|
+
*/
|
|
130
|
+
webhookPath?: string;
|
|
131
|
+
/**
|
|
132
|
+
* @inheritdoc PaymentModuleOptions.rateLimit
|
|
133
|
+
*/
|
|
134
|
+
rateLimit?: WebhookRateLimitOptions;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* NestJS dynamic module integrating `@theengineerscanvas/payment` into the DI container.
|
|
138
|
+
*
|
|
139
|
+
* Registers {@link PaymentService} and the raw {@link PaymentClient}
|
|
140
|
+
* behind the {@link TEC_PAYMENT_CLIENT} injection token. The built-in
|
|
141
|
+
* {@link WebhookController} is registered at
|
|
142
|
+
* {@link DEFAULT_WEBHOOK_PATH} unless opted out.
|
|
143
|
+
*
|
|
144
|
+
* **Static configuration:**
|
|
145
|
+
*
|
|
146
|
+
* ```ts
|
|
147
|
+
* PaymentModule.forRoot({
|
|
148
|
+
* providers: { paystack: { secretKey: "sk_..." } },
|
|
149
|
+
* defaultProvider: "paystack",
|
|
150
|
+
* });
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* **Async configuration (e.g. from ConfigService):**
|
|
154
|
+
*
|
|
155
|
+
* ```ts
|
|
156
|
+
* PaymentModule.forRootAsync({
|
|
157
|
+
* imports: [ConfigModule],
|
|
158
|
+
* useFactory: (config: ConfigService) => ({
|
|
159
|
+
* providers: { paystack: { secretKey: config.get("PAYSTACK_KEY")! } },
|
|
160
|
+
* }),
|
|
161
|
+
* inject: [ConfigService],
|
|
162
|
+
* });
|
|
163
|
+
* ```
|
|
164
|
+
*
|
|
165
|
+
* **Opting out of the built-in webhook controller:**
|
|
166
|
+
*
|
|
167
|
+
* ```ts
|
|
168
|
+
* PaymentModule.forRoot(config, { registerWebhookController: false });
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
declare class PaymentModule {
|
|
172
|
+
/**
|
|
173
|
+
* Registers a {@link PaymentClient} synchronously from a static
|
|
174
|
+
* {@link PaymentClientConfig}.
|
|
175
|
+
*/
|
|
176
|
+
static forRoot(config: PaymentClientConfig, options?: PaymentModuleOptions): DynamicModule;
|
|
177
|
+
/**
|
|
178
|
+
* Registers a {@link PaymentClient} asynchronously, typically
|
|
179
|
+
* pulling secrets from `ConfigService` or a similar provider.
|
|
180
|
+
*/
|
|
181
|
+
static forRootAsync(options: PaymentModuleAsyncOptions): DynamicModule;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Shape of an Express/Fastify request after `rawBody: true` is set on
|
|
186
|
+
* `NestFactory.create()`. Consumers *must* enable this option or the
|
|
187
|
+
* built-in {@link WebhookController} will receive an empty body and
|
|
188
|
+
* every webhook will be rejected.
|
|
189
|
+
*
|
|
190
|
+
* @see {@link WebhookController}
|
|
191
|
+
*/
|
|
192
|
+
interface RawBodyRequest {
|
|
193
|
+
/** Nest's raw body is a `Buffer`; typed as `Uint8Array` to avoid a Node types dependency. */
|
|
194
|
+
readonly rawBody?: string | Uint8Array;
|
|
195
|
+
readonly headers?: Record<string, string | readonly string[] | undefined>;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Built-in webhook endpoint registered at `POST /webhooks/payment`.
|
|
200
|
+
*
|
|
201
|
+
* **IMPORTANT:** Consumers **must** enable raw body capture in `main.ts`:
|
|
202
|
+
*
|
|
203
|
+
* ```ts
|
|
204
|
+
* const app = await NestFactory.create(AppModule, { rawBody: true });
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
* Without this, `req.rawBody` is `undefined` and every webhook will be
|
|
208
|
+
* rejected with a 401.
|
|
209
|
+
*
|
|
210
|
+
* Disable this controller by passing `{ registerWebhookController: false }`
|
|
211
|
+
* to {@link PaymentModule.forRoot} and register your own subclass:
|
|
212
|
+
*
|
|
213
|
+
* ```ts
|
|
214
|
+
* @Controller("webhooks/my-path")
|
|
215
|
+
* class MyController extends WebhookController {
|
|
216
|
+
* constructor(@Inject(TEC_PAYMENT_CLIENT) client: PaymentClient) {
|
|
217
|
+
* super(client);
|
|
218
|
+
* }
|
|
219
|
+
* }
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
declare class WebhookController {
|
|
223
|
+
private readonly client;
|
|
224
|
+
private readonly logger;
|
|
225
|
+
constructor(client: PaymentClient, logger?: LoggerService);
|
|
226
|
+
/**
|
|
227
|
+
* Receives a provider webhook, verifies its signature, and emits the
|
|
228
|
+
* corresponding domain event via the configured event bus.
|
|
229
|
+
*
|
|
230
|
+
* The signature header name is not enforced here — the underlying
|
|
231
|
+
* {@link WebhookService.receive} performs a try-all over every
|
|
232
|
+
* configured provider and each adapter reads its own header(s), so
|
|
233
|
+
* multi-provider setups work correctly as long as the full `headers` map is
|
|
234
|
+
* forwarded.
|
|
235
|
+
*/
|
|
236
|
+
handle(req: RawBodyRequest, res: unknown): Promise<void>;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Injectable NestJS service wrapping the `@theengineerscanvas/payment` client.
|
|
241
|
+
*
|
|
242
|
+
* Delegates every sub-service (`payments`, `refunds`, `webhooks`,
|
|
243
|
+
* `transfers`, `products`, `subscriptions`, `customers`, `events`,
|
|
244
|
+
* `health`) directly to the underlying {@link PaymentClient}.
|
|
245
|
+
* {@link https://www.npmjs.com/package/@theengineerscanvas/payment Result} objects
|
|
246
|
+
* are returned as-is — the consuming application decides how to handle
|
|
247
|
+
* success and failure cases.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```ts
|
|
251
|
+
* import { PaymentService } from "@theengineerscanvas/payment/nestjs";
|
|
252
|
+
* import { Money, PaymentReference } from "@theengineerscanvas/payment";
|
|
253
|
+
*
|
|
254
|
+
* @Injectable()
|
|
255
|
+
* export class CheckoutService {
|
|
256
|
+
* constructor(private readonly tec: PaymentService) {}
|
|
257
|
+
*
|
|
258
|
+
* async checkout(amount: number, email: string) {
|
|
259
|
+
* const result = await this.tec.payments.initialize({
|
|
260
|
+
* amount: Money({ amount, currency: "NGN" }),
|
|
261
|
+
* reference: PaymentReference(`order-${Date.now()}`),
|
|
262
|
+
* customer: { kind: "new", email },
|
|
263
|
+
* callbackUrl: "https://myapp.com/verify",
|
|
264
|
+
* });
|
|
265
|
+
* if (!result.ok) return { error: result.error.message };
|
|
266
|
+
* return { url: result.value.authorizationUrl };
|
|
267
|
+
* }
|
|
268
|
+
* }
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
declare class PaymentService {
|
|
272
|
+
private readonly client;
|
|
273
|
+
constructor(client: PaymentClient);
|
|
274
|
+
get payments(): PaymentClient["payments"];
|
|
275
|
+
get refunds(): PaymentClient["refunds"];
|
|
276
|
+
get webhooks(): PaymentClient["webhooks"];
|
|
277
|
+
get transfers(): PaymentClient["transfers"];
|
|
278
|
+
/** Catalog and billing. Returns `UnsupportedProviderError` per call when the provider lacks the capability. */
|
|
279
|
+
get products(): PaymentClient["products"];
|
|
280
|
+
get subscriptions(): PaymentClient["subscriptions"];
|
|
281
|
+
get customers(): PaymentClient["customers"];
|
|
282
|
+
get events(): PaymentClient["events"];
|
|
283
|
+
health(): Promise<ReadonlyArray<HealthStatus>>;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
type LogContext = Record<string, string | number | boolean | undefined>;
|
|
287
|
+
/**
|
|
288
|
+
* Bridges NestJS's `Logger` to the `@theengineerscanvas/payment` SDK's {@link Logger} interface.
|
|
289
|
+
*
|
|
290
|
+
* Pass an instance to the `logger` field of {@link PaymentClientConfig} to
|
|
291
|
+
* route all SDK log output through Nest's structured logger.
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* import { NestLoggerAdapter } from "@theengineerscanvas/payment/nestjs";
|
|
296
|
+
*
|
|
297
|
+
* PaymentModule.forRoot({
|
|
298
|
+
* providers: { paystack: { secretKey: "sk_..." } },
|
|
299
|
+
* logger: new NestLoggerAdapter("PaymentClient"),
|
|
300
|
+
* });
|
|
301
|
+
* ```
|
|
302
|
+
*/
|
|
303
|
+
declare class NestLoggerAdapter implements Logger {
|
|
304
|
+
private readonly logger;
|
|
305
|
+
constructor(context?: string);
|
|
306
|
+
debug(message: string, context?: LogContext): void;
|
|
307
|
+
info(message: string, context?: LogContext): void;
|
|
308
|
+
warn(message: string, context?: LogContext): void;
|
|
309
|
+
error(message: string, error?: unknown, context?: LogContext): void;
|
|
310
|
+
child(bindings: LogContext): Logger;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export { DEFAULT_WEBHOOK_PATH, NestLoggerAdapter, PaymentModule, type PaymentModuleAsyncOptions, type PaymentModuleOptions, PaymentService, type RawBodyRequest, TEC_PAYMENT_CLIENT, WEBHOOK_RATE_LIMIT_OPTIONS, WebhookController, WebhookRateLimitGuard, type WebhookRateLimitOptions };
|