@tapsilat/tapsilat-js 1.0.2

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.
@@ -0,0 +1,541 @@
1
+ interface TapsilatConfig {
2
+ bearerToken: string;
3
+ baseURL?: string;
4
+ timeout?: number;
5
+ maxRetries?: number;
6
+ retryDelay?: number;
7
+ version?: string;
8
+ debug?: boolean;
9
+ }
10
+ interface APIResponse<T> {
11
+ success: boolean;
12
+ data?: T;
13
+ error?: {
14
+ message: string;
15
+ code: string;
16
+ details?: any;
17
+ };
18
+ status?: number;
19
+ headers?: Record<string, string>;
20
+ }
21
+ interface APIError {
22
+ message: string;
23
+ code: string;
24
+ details?: Record<string, any>;
25
+ }
26
+ type Currency = "TRY" | "USD" | "EUR" | "GBP";
27
+ type PaymentMethod = "credit_card" | "debit_card" | "bank_transfer" | "digital_wallet";
28
+ type PaymentStatus = "pending" | "completed" | "failed" | "cancelled" | "refunded";
29
+ interface PaymentRequest {
30
+ amount: number;
31
+ currency: Currency;
32
+ paymentMethod: PaymentMethod;
33
+ description?: string;
34
+ returnUrl?: string;
35
+ cancelUrl?: string;
36
+ webhookUrl?: string;
37
+ metadata?: Record<string, any>;
38
+ customerId?: string;
39
+ }
40
+ interface PaymentResponse {
41
+ id: string;
42
+ amount: number;
43
+ currency: Currency;
44
+ paymentMethod: PaymentMethod;
45
+ status: PaymentStatus;
46
+ description?: string;
47
+ returnUrl?: string;
48
+ cancelUrl?: string;
49
+ webhookUrl?: string;
50
+ metadata?: Record<string, any>;
51
+ customerId?: string;
52
+ paymentUrl?: string;
53
+ createdAt: string;
54
+ updatedAt: string;
55
+ }
56
+ interface RefundRequest {
57
+ paymentId: string;
58
+ amount?: number;
59
+ reason?: string;
60
+ metadata?: Record<string, any>;
61
+ }
62
+ interface RefundResponse {
63
+ id: string;
64
+ paymentId: string;
65
+ amount: number;
66
+ currency: Currency;
67
+ status: "pending" | "completed" | "failed";
68
+ reason?: string;
69
+ metadata?: Record<string, any>;
70
+ createdAt: string;
71
+ updatedAt: string;
72
+ }
73
+ interface Customer {
74
+ id?: string;
75
+ email: string;
76
+ name: string;
77
+ phone?: string;
78
+ address?: Address;
79
+ metadata?: Record<string, any>;
80
+ createdAt?: string;
81
+ updatedAt?: string;
82
+ }
83
+ interface Address {
84
+ street: string;
85
+ city: string;
86
+ country: string;
87
+ postalCode: string;
88
+ }
89
+ interface PaginationParams {
90
+ page?: number;
91
+ limit?: number;
92
+ sortBy?: string;
93
+ sortOrder?: "asc" | "desc";
94
+ }
95
+ interface PaginatedResponse<T> {
96
+ page: number;
97
+ per_page: number;
98
+ rows: T[];
99
+ total: number;
100
+ total_pages: number;
101
+ }
102
+ interface WebhookEvent {
103
+ id: string;
104
+ type: "payment.completed" | "payment.failed" | "payment.cancelled" | "refund.completed" | "refund.failed";
105
+ data: PaymentResponse | RefundResponse;
106
+ createdAt: string;
107
+ signature: string;
108
+ }
109
+ /**
110
+ * Represents the buyer information.
111
+ * Based on BuyerDTO from the Python SDK.
112
+ */
113
+ interface Buyer$1 {
114
+ name: string;
115
+ surname: string;
116
+ email: string;
117
+ }
118
+ /**
119
+ * Represents the response received after creating an order.
120
+ */
121
+ interface OrderCreateResponse$1 {
122
+ referenceId: string;
123
+ conversationId: string;
124
+ checkoutUrl: string;
125
+ status: string;
126
+ qrCodeUrl?: string;
127
+ }
128
+ /**
129
+ * Represents a full order object with all details.
130
+ */
131
+ interface Order extends OrderCreateResponse$1 {
132
+ amount: number | string;
133
+ currency: Currency;
134
+ buyer?: Buyer$1;
135
+ description?: string;
136
+ createdAt?: string;
137
+ updatedAt?: string;
138
+ metadata?: Record<string, unknown>;
139
+ id?: string;
140
+ name?: string;
141
+ email?: string;
142
+ total?: string;
143
+ checkout_url?: string;
144
+ organization?: string;
145
+ unpaid_amount?: number;
146
+ }
147
+ /**
148
+ * Represents the data needed to request a refund for an order.
149
+ */
150
+ interface OrderRefundRequest {
151
+ reference_id: string;
152
+ amount: number;
153
+ }
154
+ /**
155
+ * Represents the response received after a refund request.
156
+ */
157
+ interface OrderRefundResponse {
158
+ refundId: string;
159
+ referenceId: string;
160
+ status: string;
161
+ amount: number;
162
+ currency: Currency;
163
+ createdAt: string;
164
+ }
165
+ /**
166
+ * Represents the status of an order.
167
+ */
168
+ interface OrderStatusResponse {
169
+ referenceId: string;
170
+ status: string;
171
+ lastUpdatedAt: string;
172
+ }
173
+ /**
174
+ * Represents the details of a payment transaction associated with an order.
175
+ */
176
+ interface OrderPaymentDetail {
177
+ transactionId: string;
178
+ paymentMethod: string;
179
+ amount: number;
180
+ currency: Currency;
181
+ status: string;
182
+ paidAt: string;
183
+ cardInfo?: {
184
+ binNumber: string;
185
+ lastFourDigits: string;
186
+ cardAssociation: string;
187
+ };
188
+ }
189
+
190
+ /**
191
+ * Supported locales for the Tapsilat API.
192
+ */
193
+ type Locale = "tr" | "en";
194
+ /**
195
+ * Represents the buyer information.
196
+ * Based on BuyerDTO from the Python SDK.
197
+ */
198
+ interface Buyer {
199
+ name: string;
200
+ surname: string;
201
+ email: string;
202
+ phone?: string;
203
+ identityNumber?: string;
204
+ shippingAddress?: Address;
205
+ billingAddress?: Address;
206
+ }
207
+ /**
208
+ * Represents the data required to create a new order.
209
+ * Based on OrderCreateDTO from the Python SDK.
210
+ */
211
+ interface OrderCreateRequest {
212
+ amount: number;
213
+ currency: Currency;
214
+ locale: Locale;
215
+ buyer: Buyer;
216
+ description?: string;
217
+ callbackUrl?: string;
218
+ conversationId?: string;
219
+ metadata?: Record<string, unknown>;
220
+ }
221
+ /**
222
+ * Represents the response received after creating an order.
223
+ */
224
+ interface OrderCreateResponse {
225
+ referenceId: string;
226
+ conversationId: string;
227
+ checkoutUrl: string;
228
+ status: string;
229
+ qrCodeUrl?: string;
230
+ }
231
+ /**
232
+ * Main SDK class for Tapsilat payment operations
233
+ *
234
+ * @example
235
+ * ```typescript
236
+ * const tapsilat = new TapsilatSDK({
237
+ * bearerToken: 'your-bearer-token',
238
+ * baseURL: 'https://api.tapsilat.com/v1'
239
+ * });
240
+ *
241
+ * const payment = await tapsilat.createPayment({
242
+ * amount: 100.50,
243
+ * currency: 'TRY',
244
+ * paymentMethod: 'credit_card'
245
+ * });
246
+ * ```
247
+ */
248
+ declare class TapsilatSDK {
249
+ private readonly httpClient;
250
+ private readonly config;
251
+ /**
252
+ * Creates a new TapsilatSDK instance
253
+ *
254
+ * @param config - SDK configuration options
255
+ * @throws {TapsilatValidationError} When Bearer token is invalid
256
+ */
257
+ constructor(config: TapsilatConfig);
258
+ /**
259
+ * Creates a new order and returns a checkout URL.
260
+ *
261
+ * @param orderRequest - The order details including amount, currency, and buyer information
262
+ * @returns Promise resolving to the created order response including reference ID and checkout URL
263
+ * @throws {TapsilatValidationError} When order request data is invalid
264
+ * @throws {TapsilatNetworkError} When API request fails due to network issues
265
+ * @throws {TapsilatError} When API returns an error response
266
+ *
267
+ * @example
268
+ * ```typescript
269
+ * try {
270
+ * const orderResponse = await tapsilat.createOrder({
271
+ * amount: 100.50,
272
+ * currency: 'TRY',
273
+ * locale: 'en',
274
+ * buyer: {
275
+ * name: 'John',
276
+ * surname: 'Doe',
277
+ * email: 'john.doe@example.com'
278
+ * }
279
+ * });
280
+ * console.log(`Checkout URL: ${orderResponse.checkoutUrl}`);
281
+ * } catch (error) {
282
+ * console.error(`Order creation failed: ${error.message}`);
283
+ * }
284
+ * ```
285
+ */
286
+ createOrder(orderRequest: OrderCreateRequest): Promise<OrderCreateResponse>;
287
+ /**
288
+ * Validates an order request object
289
+ * @private
290
+ */
291
+ private validateOrderRequest;
292
+ /**
293
+ * Basic email validation
294
+ * @private
295
+ */
296
+ private isValidEmail;
297
+ /**
298
+ * Generic response handler - consistent way to handle API responses across SDK methods
299
+ * @private
300
+ *
301
+ * @param response - The API response to process
302
+ * @param errorContext - Descriptive context for error messages
303
+ * @returns The data from a successful response
304
+ * @throws {TapsilatError} On any response error
305
+ */
306
+ private handleResponse;
307
+ /**
308
+ * Generic error handler - consistent way to handle errors across SDK methods
309
+ * @private
310
+ *
311
+ * @param error - The caught error to process
312
+ * @param errorContext - Descriptive context for error messages
313
+ * @throws {TapsilatError} Always throws an appropriate error type
314
+ */
315
+ private handleError;
316
+ /**
317
+ * Gets an order by reference ID.
318
+ *
319
+ * @param referenceId - The unique reference ID of the order
320
+ * @returns Promise resolving to the complete order details
321
+ * @throws {TapsilatValidationError} When referenceId is invalid
322
+ * @throws {TapsilatNetworkError} When API request fails due to network issues
323
+ * @throws {TapsilatError} When API returns an error response
324
+ *
325
+ * @example
326
+ * ```typescript
327
+ * try {
328
+ * const order = await tapsilat.getOrder("ord_123456789");
329
+ * console.log(`Order amount: ${order.amount} ${order.currency}`);
330
+ * } catch (error) {
331
+ * console.error(`Failed to get order: ${error.message}`);
332
+ * }
333
+ * ```
334
+ */
335
+ getOrder(referenceId: string): Promise<Order>;
336
+ /**
337
+ * Lists orders with pagination support.
338
+ *
339
+ * @param params - Optional pagination parameters (page number and items per page)
340
+ * @returns Promise resolving to paginated list of orders
341
+ * @throws {TapsilatNetworkError} When API request fails due to network issues
342
+ * @throws {TapsilatError} When API returns an error response
343
+ *
344
+ * @example
345
+ * ```typescript
346
+ * try {
347
+ * // Get the second page with 20 items per page
348
+ * const orderList = await tapsilat.getOrders({ page: 2, per_page: 20 });
349
+ * console.log(`Total orders: ${orderList.total}`);
350
+ * orderList.rows.forEach(order => {
351
+ * console.log(`Order ${order.referenceId}: ${order.amount} ${order.currency}`);
352
+ * });
353
+ * } catch (error) {
354
+ * console.error(`Failed to list orders: ${error.message}`);
355
+ * }
356
+ * ```
357
+ */
358
+ getOrders(params?: {
359
+ page?: number;
360
+ per_page?: number;
361
+ }): Promise<PaginatedResponse<Order>>;
362
+ /**
363
+ * Cancels an order by reference_id.
364
+ */
365
+ cancelOrder(referenceId: string): Promise<Order>;
366
+ /**
367
+ * Gets the status of an order by reference ID.
368
+ *
369
+ * @param referenceId - The unique reference ID of the order
370
+ * @returns Promise resolving to the current order status, including status code and last update time
371
+ * @throws {TapsilatValidationError} When referenceId is invalid
372
+ * @throws {TapsilatNetworkError} When API request fails due to network issues
373
+ * @throws {TapsilatError} When API returns an error response
374
+ *
375
+ * @example
376
+ * ```typescript
377
+ * try {
378
+ * const orderStatus = await tapsilat.getOrderStatus("ord_123456789");
379
+ * console.log(`Order status: ${orderStatus.status}`);
380
+ * } catch (error) {
381
+ * if (error instanceof TapsilatValidationError) {
382
+ * console.error("Invalid reference ID format");
383
+ * } else if (error instanceof TapsilatError) {
384
+ * console.error(`API error: ${error.message}`);
385
+ * }
386
+ * }
387
+ * ```
388
+ */
389
+ getOrderStatus(referenceId: string): Promise<OrderStatusResponse>;
390
+ /**
391
+ * Partially refunds an order.
392
+ * Based on `refund_order` from the Python SDK.
393
+ *
394
+ * @param refundData - The refund details, including referenceId and amount.
395
+ * @returns Promise resolving to the refund transaction details.
396
+ */
397
+ refundOrder(refundData: OrderRefundRequest): Promise<OrderRefundResponse>;
398
+ /**
399
+ * Fully refunds an order by its reference ID.
400
+ * Based on `refund_all_order` from the Python SDK.
401
+ *
402
+ * @param referenceId - The unique identifier of the order to fully refund.
403
+ * @returns Promise resolving to the refund transaction details.
404
+ */
405
+ refundAllOrder(referenceId: string): Promise<OrderRefundResponse>;
406
+ /**
407
+ * Retrieves the payment details for an order.
408
+ * Based on `get_order_payment_details` from the Python SDK.
409
+ *
410
+ * @param referenceId - The unique identifier of the order.
411
+ * @param conversationId - Optional conversation ID for more specific querying.
412
+ * @returns Promise resolving to a list of payment details.
413
+ */
414
+ getOrderPaymentDetails(referenceId: string, conversationId?: string): Promise<OrderPaymentDetail[]>;
415
+ /**
416
+ * Gets an order by conversation_id.
417
+ * Based on `get_order_by_conversation_id` from the Python SDK.
418
+ */
419
+ getOrderByConversationId(conversationId: string): Promise<Order>;
420
+ /**
421
+ * Gets order transactions by reference_id.
422
+ * Based on `get_order_transactions` from the Python SDK.
423
+ */
424
+ getOrderTransactions(referenceId: string): Promise<any[]>;
425
+ /**
426
+ * Gets order submerchants with pagination.
427
+ * Based on `get_order_submerchants` from the Python SDK.
428
+ */
429
+ getOrderSubmerchants(params?: {
430
+ page?: number;
431
+ per_page?: number;
432
+ }): Promise<any>;
433
+ /**
434
+ * Gets checkout URL for an order.
435
+ * Based on `get_checkout_url` from the Python SDK.
436
+ */
437
+ getCheckoutUrl(referenceId: string): Promise<string>;
438
+ /**
439
+ * Verifies webhook signature for security
440
+ *
441
+ * @param payload - Raw webhook payload string
442
+ * @param signature - Webhook signature from headers
443
+ * @param secret - Your webhook secret key
444
+ * @returns Promise resolving to true if signature is valid
445
+ *
446
+ * @example
447
+ * ```typescript
448
+ * const isValid = await sdk.verifyWebhook(
449
+ * JSON.stringify(req.body),
450
+ * req.headers['x-tapsilat-signature'],
451
+ * 'your-webhook-secret'
452
+ * );
453
+ * ```
454
+ */
455
+ verifyWebhook(payload: string, signature: string, secret: string): Promise<boolean>;
456
+ /**
457
+ * Checks API service health and availability
458
+ *
459
+ * @returns Promise resolving to service status
460
+ */
461
+ healthCheck(): Promise<{
462
+ status: string;
463
+ timestamp: string;
464
+ }>;
465
+ /**
466
+ * Gets current SDK configuration (without sensitive data)
467
+ *
468
+ * @returns Copy of current configuration
469
+ */
470
+ getConfig(): Omit<TapsilatConfig, "bearerToken"> & {
471
+ hasBearerToken: boolean;
472
+ };
473
+ /**
474
+ * Updates SDK configuration
475
+ *
476
+ * @param newConfig - Partial configuration to update
477
+ * @throws {TapsilatValidationError} When Bearer token is invalid
478
+ */
479
+ updateConfig(newConfig: Partial<TapsilatConfig>): void;
480
+ }
481
+
482
+ declare class TapsilatError extends Error {
483
+ readonly code: string;
484
+ readonly details?: Record<string, any>;
485
+ constructor(message: string, code: string, details?: Record<string, any>);
486
+ static fromAPIError(apiError: APIError): TapsilatError;
487
+ }
488
+ declare class TapsilatNetworkError extends TapsilatError {
489
+ readonly statusCode?: number;
490
+ constructor(message: string, code: string, statusCode?: number, details?: Record<string, any>);
491
+ }
492
+ declare class TapsilatValidationError extends TapsilatError {
493
+ constructor(message: string, details?: Record<string, any>);
494
+ }
495
+ declare class TapsilatAuthenticationError extends TapsilatError {
496
+ constructor(message?: string);
497
+ }
498
+ declare class TapsilatRateLimitError extends TapsilatError {
499
+ readonly retryAfter?: number;
500
+ constructor(message?: string, retryAfter?: number);
501
+ }
502
+
503
+ /**
504
+ * Validates payment request data comprehensively
505
+ *
506
+ * @param request - Payment request to validate
507
+ * @throws {TapsilatValidationError} When any field is invalid
508
+ */
509
+ declare function validatePaymentRequest(request: unknown): asserts request is PaymentRequest;
510
+ /**
511
+ * Validates email format using robust regex
512
+ *
513
+ * @param email - Email address to validate
514
+ * @returns True if email format is valid
515
+ */
516
+ declare function validateEmail(email: string): boolean;
517
+ /**
518
+ * @deprecated Use isCurrency type guard instead
519
+ */
520
+ declare function isValidCurrency(currency: string): boolean;
521
+ /**
522
+ * @deprecated Use isPaymentMethod type guard instead
523
+ */
524
+ declare function isValidPaymentMethod(paymentMethod: string): boolean;
525
+ /**
526
+ * Validates URL format and protocol
527
+ *
528
+ * @param url - URL to validate
529
+ * @returns True if URL is valid HTTP/HTTPS
530
+ */
531
+ declare function isValidUrl(url: string): boolean;
532
+ /**
533
+ * Sanitizes metadata by removing sensitive keys and cleaning values
534
+ *
535
+ * @param metadata - Raw metadata object
536
+ * @returns Sanitized metadata object
537
+ */
538
+ declare function sanitizeMetadata(metadata: Record<string, unknown>): Record<string, unknown>;
539
+
540
+ export { TapsilatAuthenticationError, TapsilatError, TapsilatNetworkError, TapsilatRateLimitError, TapsilatSDK, TapsilatValidationError, isValidCurrency, isValidPaymentMethod, isValidUrl, sanitizeMetadata, validateEmail, validatePaymentRequest };
541
+ export type { APIError, APIResponse, Address, Currency, Customer, PaginatedResponse, PaginationParams, PaymentMethod, PaymentRequest, PaymentResponse, PaymentStatus, RefundRequest, RefundResponse, TapsilatConfig, WebhookEvent };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG5C,YAAY,EACV,cAAc,EACd,aAAa,EACb,aAAa,EACb,QAAQ,EACR,cAAc,EACd,eAAe,EACf,QAAQ,EACR,OAAO,EACP,aAAa,EACb,cAAc,EACd,YAAY,EACZ,QAAQ,EACR,WAAW,EACX,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,SAAS,CAAC;AAGjB,OAAO,EACL,aAAa,EACb,oBAAoB,EACpB,uBAAuB,EACvB,2BAA2B,EAC3B,sBAAsB,GACvB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,EACL,sBAAsB,EACtB,aAAa,EACb,eAAe,EACf,oBAAoB,EACpB,UAAU,EACV,gBAAgB,GACjB,MAAM,oBAAoB,CAAC"}