xendit-fn 1.0.0 → 1.0.4

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 (40) hide show
  1. package/README.md +515 -114
  2. package/lib/index.cjs +1273 -43
  3. package/lib/index.d.cts +9180 -6479
  4. package/lib/index.d.ts +1 -1
  5. package/lib/index.d.ts.map +1 -1
  6. package/lib/index.esm.d.ts +9180 -6479
  7. package/lib/index.esm.js +1273 -43
  8. package/lib/sdk/balance/index.d.ts +5 -0
  9. package/lib/sdk/balance/index.d.ts.map +1 -0
  10. package/lib/sdk/balance/schema.d.ts +170 -0
  11. package/lib/sdk/balance/schema.d.ts.map +1 -0
  12. package/lib/sdk/card/index.d.ts +14 -1
  13. package/lib/sdk/card/index.d.ts.map +1 -1
  14. package/lib/sdk/card/schema.d.ts +9 -8
  15. package/lib/sdk/card/schema.d.ts.map +1 -1
  16. package/lib/sdk/common.d.ts.map +1 -1
  17. package/lib/sdk/customer/index.d.ts.map +1 -1
  18. package/lib/sdk/ewallet/create.d.ts +3 -171
  19. package/lib/sdk/ewallet/create.d.ts.map +1 -1
  20. package/lib/sdk/index.d.ts +872 -2
  21. package/lib/sdk/index.d.ts.map +1 -1
  22. package/lib/sdk/invoice/index.d.ts.map +1 -1
  23. package/lib/sdk/payment-method/index.d.ts.map +1 -1
  24. package/lib/sdk/payment-request/index.d.ts +6 -0
  25. package/lib/sdk/payment-request/index.d.ts.map +1 -0
  26. package/lib/sdk/payment-request/schema.d.ts +1341 -0
  27. package/lib/sdk/payment-request/schema.d.ts.map +1 -0
  28. package/lib/sdk/payout/index.d.ts +7 -0
  29. package/lib/sdk/payout/index.d.ts.map +1 -0
  30. package/lib/sdk/payout/schema.d.ts +853 -0
  31. package/lib/sdk/payout/schema.d.ts.map +1 -0
  32. package/lib/sdk/refund/index.d.ts +6 -0
  33. package/lib/sdk/refund/index.d.ts.map +1 -0
  34. package/lib/sdk/refund/schema.d.ts +178 -0
  35. package/lib/sdk/refund/schema.d.ts.map +1 -0
  36. package/lib/utils/pagination.d.ts +1 -1
  37. package/lib/utils/pagination.d.ts.map +1 -1
  38. package/lib/utils/rate-limit.d.ts +3 -3
  39. package/lib/utils/rate-limit.d.ts.map +1 -1
  40. package/package.json +3 -2
package/lib/index.esm.js CHANGED
@@ -258,7 +258,7 @@ const CurrencySchema = z.union([
258
258
  z.literal("THB"),
259
259
  z.literal("VND")
260
260
  ]);
261
- z.object({
261
+ const BillingDetailSchema = z.object({
262
262
  given_names: z.string(),
263
263
  surname: z.string().optional(),
264
264
  email: z.string().email().optional(),
@@ -320,7 +320,7 @@ const AccountTypeSchema = z.union([
320
320
  z.literal("QR_CODE"),
321
321
  z.literal("SOCIAL_MEDIA")
322
322
  ]);
323
- const BankAccountSchema = z.object({
323
+ const BankAccountSchema$1 = z.object({
324
324
  account_number: z.string(),
325
325
  account_holder_name: z.string(),
326
326
  swift_code: z.string().optional(),
@@ -355,7 +355,7 @@ const SocialMediaAccountSchema = z.object({
355
355
  const PropertiesSchema = z.discriminatedUnion("type", [
356
356
  z.object({
357
357
  type: z.literal("BANK_ACCOUNT"),
358
- properties: BankAccountSchema
358
+ properties: BankAccountSchema$1
359
359
  }),
360
360
  z.object({
361
361
  type: z.literal("EWALLET"),
@@ -563,8 +563,228 @@ const updateCustomer = async (params, axiosInstance, config)=>{
563
563
  }
564
564
  };
565
565
 
566
- const createEwalletCharge = async (params, axiosInstance, config)=>(await axiosInstance.post(config?.url ?? "/ewallets/charges", params, config)).data;
567
- const getEwalletCharge = async (params, axiosInstance, config)=>(await axiosInstance.get(config?.url ?? `/ewallets/charges/${params.id}`, config)).data;
566
+ const CheckoutMethodSchema = z.union([
567
+ z.literal("ONE_TIME_PAYMENT"),
568
+ z.literal("TOKENIZED_PAYMENT")
569
+ ]);
570
+ const ChannelCodeSchema = z.union([
571
+ z.literal("ID_OVO"),
572
+ z.literal("ID_DANA"),
573
+ z.literal("ID_LINKAJA"),
574
+ z.literal("ID_SHOPEEPAY"),
575
+ z.literal("ID_ASTRAPAY"),
576
+ z.literal("ID_JENIUSPAY"),
577
+ z.literal("ID_SAKUKU"),
578
+ z.literal("PH_PAYMAYA"),
579
+ z.literal("PH_GCASH"),
580
+ z.literal("PH_GRABPAY"),
581
+ z.literal("PH_SHOPEEPAY"),
582
+ z.literal("VN_APPOTA"),
583
+ z.literal("VN_MOMO"),
584
+ z.literal("VN_SHOPEEPAY"),
585
+ z.literal("VN_VNPTWALLET"),
586
+ z.literal("VN_VIETTELPAY"),
587
+ z.literal("VN_ZALOPAY"),
588
+ z.literal("TH_WECHATPAY"),
589
+ z.literal("TH_LINEPAY"),
590
+ z.literal("TH_TRUEMONEY"),
591
+ z.literal("TH_SHOPEEPAY"),
592
+ z.literal("MY_TOUCHNGO"),
593
+ z.literal("MY_SHOPEEPAY"),
594
+ z.literal("MY_GRABPAY")
595
+ ]);
596
+ const OVOChannelSchema = z.object({
597
+ mobile_number: PhoneSchema
598
+ });
599
+ const ChannelsSchema = z.object({
600
+ cashtag: z.string().refine((value)=>value.startsWith("$")).optional(),
601
+ mobile_number: PhoneSchema.optional(),
602
+ success_redirect_url: z.string().url().optional(),
603
+ failure_redirect_url: z.string().url().optional(),
604
+ cancel_redirect_url: z.string().url().optional(),
605
+ redeem_points: z.union([
606
+ z.literal("REDEEM_ALL"),
607
+ z.literal("REDEEM_NONE")
608
+ ]).default("REDEEM_NONE").optional()
609
+ });
610
+ const ChannelPropertiesSchema$1 = z.discriminatedUnion("channel_code", [
611
+ z.object({
612
+ channel_code: z.literal("ID_OVO"),
613
+ channel_properties: OVOChannelSchema
614
+ }),
615
+ z.object({
616
+ channel_code: z.literal("ID_JENIUSPAY"),
617
+ properties: z.discriminatedUnion("method", [
618
+ z.object({
619
+ method: z.literal("ONE_TIME_PAYMENT"),
620
+ properties: z.object({
621
+ cashtag: z.string().refine((value)=>value.startsWith("$"))
622
+ })
623
+ }),
624
+ z.object({
625
+ method: z.literal("TOKENIZED_PAYMENT"),
626
+ properties: z.object({
627
+ success_redirect_url: z.string().url(),
628
+ failure_redirect_url: z.string().url(),
629
+ redeem_points: z.union([
630
+ z.literal("REDEEM_ALL"),
631
+ z.literal("REDEEM_NONE")
632
+ ]).default("REDEEM_NONE").optional()
633
+ })
634
+ })
635
+ ])
636
+ }),
637
+ z.object({
638
+ channel_code: z.union([
639
+ z.literal("ID_DANA"),
640
+ z.literal("ID_LINKAJA"),
641
+ z.literal("ID_SHOPEEPAY"),
642
+ z.literal("PH_SHOPEEPAY"),
643
+ z.literal("TH_SHOPEEPAY"),
644
+ z.literal("TH_WECHATPAY"),
645
+ z.literal("TH_TOUCHNGO"),
646
+ z.literal("TH_TRUEMONEY"),
647
+ z.literal("ID_SAKUKU")
648
+ ]),
649
+ properties: z.discriminatedUnion("method", [
650
+ z.object({
651
+ method: z.literal("ONE_TIME_PAYMENT"),
652
+ properties: z.object({
653
+ success_redirect_url: z.string().url()
654
+ })
655
+ }),
656
+ z.object({
657
+ method: z.literal("TOKENIZED_PAYMENT"),
658
+ properties: z.object({
659
+ success_redirect_url: z.string().url(),
660
+ failure_redirect_url: z.string().url(),
661
+ redeem_points: z.union([
662
+ z.literal("REDEEM_ALL"),
663
+ z.literal("REDEEM_NONE")
664
+ ]).default("REDEEM_NONE").optional()
665
+ })
666
+ })
667
+ ])
668
+ }),
669
+ z.object({
670
+ channel_code: z.literal("PH_PAYMAYA"),
671
+ properties: z.object({
672
+ success_redirect_url: z.string().url(),
673
+ failure_redirect_url: z.string().url(),
674
+ cancel_redirect_url: z.string().url()
675
+ })
676
+ })
677
+ ]);
678
+ const LineItemSchema = z.object({
679
+ reference_id: z.string(),
680
+ name: z.string(),
681
+ category: z.string(),
682
+ currency: CurrencySchema,
683
+ price: z.number(),
684
+ quantity: z.number(),
685
+ type: z.union([
686
+ z.literal("PRODUCT"),
687
+ z.literal("SERVICE")
688
+ ]),
689
+ url: z.string().url().optional(),
690
+ description: z.string().optional(),
691
+ subcategory: z.string().optional(),
692
+ metadata: z.object({}).optional()
693
+ });
694
+ // Create a discriminated union based on checkout method
695
+ const EWalletChargeSchema = z.discriminatedUnion("checkout_method", [
696
+ z.object({
697
+ reference_id: z.string(),
698
+ currency: CurrencySchema,
699
+ amount: z.number(),
700
+ checkout_method: z.literal("ONE_TIME_PAYMENT"),
701
+ channel_code: ChannelCodeSchema,
702
+ channel_properties: ChannelsSchema,
703
+ payment_method_id: z.undefined().optional(),
704
+ customer_id: z.string().optional(),
705
+ basket: z.array(LineItemSchema).optional(),
706
+ metadata: z.object({}).optional()
707
+ }),
708
+ z.object({
709
+ reference_id: z.string(),
710
+ currency: CurrencySchema,
711
+ amount: z.number(),
712
+ checkout_method: z.literal("TOKENIZED_PAYMENT"),
713
+ channel_code: ChannelCodeSchema.optional(),
714
+ channel_properties: ChannelsSchema,
715
+ payment_method_id: z.string(),
716
+ customer_id: z.string().optional(),
717
+ basket: z.array(LineItemSchema).optional(),
718
+ metadata: z.object({}).optional()
719
+ })
720
+ ]);
721
+ const EWalletStatusSchema = z.union([
722
+ z.literal("SUCCEEDED"),
723
+ z.literal("PENDING"),
724
+ z.literal("FAILED"),
725
+ z.literal("VOIDED"),
726
+ z.literal("REFUNDED")
727
+ ]);
728
+ const EWalletResourceActionsSchema = z.object({
729
+ desktop_web_checkout_url: z.string().url().optional(),
730
+ mobile_web_checkout_url: z.string().url().optional(),
731
+ mobile_deeplink_checkout_url: z.string().url().optional(),
732
+ qr_checkout_string: z.string().url().optional()
733
+ });
734
+ const EWalletChargeResourceSchema = z.object({
735
+ id: z.string(),
736
+ business_id: z.string(),
737
+ reference_id: z.string(),
738
+ status: EWalletStatusSchema,
739
+ currency: CurrencySchema,
740
+ charge_amount: z.number(),
741
+ capture_amount: z.number().optional(),
742
+ refunded_amount: z.number().nullable(),
743
+ checkout_method: CheckoutMethodSchema,
744
+ channel_code: ChannelCodeSchema,
745
+ channel_properties: ChannelPropertiesSchema$1,
746
+ actions: EWalletResourceActionsSchema.optional(),
747
+ is_redirect_required: z.boolean(),
748
+ callback_url: z.string(),
749
+ created: z.string().datetime(),
750
+ updated: z.string().datetime(),
751
+ void_status: z.string().nullable(),
752
+ voided_at: z.string().nullable(),
753
+ capture_now: z.boolean(),
754
+ customer_id: z.string().nullable(),
755
+ payment_method_id: z.string().nullable(),
756
+ failure_code: z.string().nullable(),
757
+ basket: z.array(LineItemSchema).optional(),
758
+ metadata: z.object({}).optional()
759
+ });
760
+ const GetEWalletChargeSchema = z.object({
761
+ id: z.string().startsWith("ewc")
762
+ });
763
+
764
+ const createEwalletCharge = async (params, axiosInstance, config)=>{
765
+ try {
766
+ const validatedParams = validateInput(EWalletChargeSchema, params, "ewallet charge params");
767
+ const response = await axiosInstance.post(config?.url ?? "/ewallets/charges", validatedParams, config);
768
+ return validateInput(EWalletChargeResourceSchema, response.data, "ewallet charge response");
769
+ } catch (error) {
770
+ if (error instanceof Error && error.name === "AxiosError") {
771
+ handleAxiosError(error);
772
+ }
773
+ throw error;
774
+ }
775
+ };
776
+ const getEwalletCharge = async (params, axiosInstance, config)=>{
777
+ try {
778
+ const validatedParams = validateInput(GetEWalletChargeSchema, params, "get ewallet charge params");
779
+ const response = await axiosInstance.get(config?.url ?? `/ewallets/charges/${validatedParams.id}`, config);
780
+ return validateInput(EWalletChargeResourceSchema, response.data, "ewallet charge response");
781
+ } catch (error) {
782
+ if (error instanceof Error && error.name === "AxiosError") {
783
+ handleAxiosError(error);
784
+ }
785
+ throw error;
786
+ }
787
+ };
568
788
 
569
789
  // Payment Method Types
570
790
  const PaymentMethodTypeSchema = z.union([
@@ -800,6 +1020,9 @@ const listPaymentMethods = async (params, axiosInstance, config)=>{
800
1020
  }
801
1021
  const queryString = queryParams.toString();
802
1022
  const url = queryString ? `/v2/payment_methods?${queryString}` : "/v2/payment_methods";
1023
+ if (!axiosInstance) {
1024
+ throw new Error("axiosInstance is required");
1025
+ }
803
1026
  const response = await axiosInstance.get(config?.url ?? url, config);
804
1027
  return response.data;
805
1028
  } catch (error) {
@@ -1116,6 +1339,9 @@ const listInvoices = async (params, axiosInstance, config)=>{
1116
1339
  }
1117
1340
  const queryString = queryParams.toString();
1118
1341
  const url = queryString ? `/v2/invoices?${queryString}` : "/v2/invoices";
1342
+ if (!axiosInstance) {
1343
+ throw new Error("axiosInstance is required");
1344
+ }
1119
1345
  const response = await axiosInstance.get(config?.url ?? url, config);
1120
1346
  return response.data;
1121
1347
  } catch (error) {
@@ -1150,51 +1376,1055 @@ const expireInvoice = async (params, axiosInstance, config)=>{
1150
1376
  }
1151
1377
  };
1152
1378
 
1153
- const btoa = (string)=>{
1154
- if (typeof window === "undefined") {
1155
- return Buffer.from(string).toString("base64");
1379
+ // Payment Request Types
1380
+ const PaymentRequestTypeSchema = z.union([
1381
+ z.literal("PAY"),
1382
+ z.literal("PAY_AND_SAVE"),
1383
+ z.literal("REUSABLE_PAYMENT_CODE")
1384
+ ]);
1385
+ // Capture Method
1386
+ const CaptureMethodSchema = z.union([
1387
+ z.literal("AUTOMATIC"),
1388
+ z.literal("MANUAL")
1389
+ ]);
1390
+ // Payment Request Status
1391
+ const PaymentRequestStatusSchema = z.union([
1392
+ z.literal("PENDING"),
1393
+ z.literal("REQUIRES_ACTION"),
1394
+ z.literal("SUCCEEDED"),
1395
+ z.literal("FAILED"),
1396
+ z.literal("VOIDED"),
1397
+ z.literal("CANCELED")
1398
+ ]);
1399
+ // Channel Code (simplified - can be expanded)
1400
+ z.string();
1401
+ // Channel Properties
1402
+ const ChannelPropertiesSchema = z.record(z.unknown());
1403
+ // Payment Method
1404
+ const PaymentMethodSchema = z.object({
1405
+ type: z.string(),
1406
+ card_information: z.object({
1407
+ token_id: z.string()
1408
+ }).optional(),
1409
+ ewallet: z.object({
1410
+ channel_code: z.string(),
1411
+ channel_properties: ChannelPropertiesSchema.optional()
1412
+ }).optional(),
1413
+ direct_debit: z.object({
1414
+ channel_code: z.string(),
1415
+ channel_properties: ChannelPropertiesSchema.optional()
1416
+ }).optional(),
1417
+ over_the_counter: z.object({
1418
+ channel_code: z.string(),
1419
+ channel_properties: ChannelPropertiesSchema.optional()
1420
+ }).optional(),
1421
+ qr_code: z.object({
1422
+ channel_code: z.string(),
1423
+ channel_properties: ChannelPropertiesSchema.optional()
1424
+ }).optional(),
1425
+ virtual_account: z.object({
1426
+ channel_code: z.string(),
1427
+ channel_properties: ChannelPropertiesSchema.optional()
1428
+ }).optional()
1429
+ });
1430
+ // Create Payment Request Schema
1431
+ const CreatePaymentRequestSchema = z.object({
1432
+ reference_id: z.string(),
1433
+ type: PaymentRequestTypeSchema,
1434
+ country: CountrySchema,
1435
+ currency: CurrencySchema,
1436
+ request_amount: z.number().positive(),
1437
+ payment_method: PaymentMethodSchema,
1438
+ description: z.string().optional(),
1439
+ metadata: z.record(z.unknown()).optional(),
1440
+ customer_id: z.string().optional(),
1441
+ customer: z.object({
1442
+ given_names: z.string(),
1443
+ surname: z.string().optional(),
1444
+ email: z.string().email().optional(),
1445
+ mobile_number: z.string().optional()
1446
+ }).optional(),
1447
+ shipping_information: z.object({
1448
+ name: z.string(),
1449
+ phone_number: z.string().optional(),
1450
+ address: z.string().optional(),
1451
+ city: z.string().optional(),
1452
+ province: z.string().optional(),
1453
+ postal_code: z.string().optional(),
1454
+ country_code: CountrySchema.optional()
1455
+ }).optional(),
1456
+ items: z.array(z.object({
1457
+ name: z.string(),
1458
+ quantity: z.number().positive(),
1459
+ price: z.number().positive(),
1460
+ category: z.string().optional(),
1461
+ reference_id: z.string().optional(),
1462
+ url: z.string().url().optional()
1463
+ })).optional(),
1464
+ capture_method: CaptureMethodSchema.optional(),
1465
+ success_redirect_url: z.string().url().optional(),
1466
+ failure_redirect_url: z.string().url().optional()
1467
+ });
1468
+ // Payment Request Resource
1469
+ const PaymentRequestResourceSchema = z.object({
1470
+ id: z.string(),
1471
+ reference_id: z.string(),
1472
+ type: PaymentRequestTypeSchema,
1473
+ country: CountrySchema,
1474
+ currency: CurrencySchema,
1475
+ request_amount: z.number(),
1476
+ paid_amount: z.number().optional(),
1477
+ status: PaymentRequestStatusSchema,
1478
+ payment_method: PaymentMethodSchema,
1479
+ description: z.string().optional(),
1480
+ metadata: z.record(z.unknown()).optional(),
1481
+ customer_id: z.string().optional(),
1482
+ customer: z.object({
1483
+ given_names: z.string(),
1484
+ surname: z.string().optional(),
1485
+ email: z.string().email().optional(),
1486
+ mobile_number: z.string().optional()
1487
+ }).optional(),
1488
+ shipping_information: z.object({
1489
+ name: z.string(),
1490
+ phone_number: z.string().optional(),
1491
+ address: z.string().optional(),
1492
+ city: z.string().optional(),
1493
+ province: z.string().optional(),
1494
+ postal_code: z.string().optional(),
1495
+ country_code: CountrySchema.optional()
1496
+ }).optional(),
1497
+ items: z.array(z.object({
1498
+ name: z.string(),
1499
+ quantity: z.number().positive(),
1500
+ price: z.number().positive(),
1501
+ category: z.string().optional(),
1502
+ reference_id: z.string().optional(),
1503
+ url: z.string().url().optional()
1504
+ })).optional(),
1505
+ actions: z.object({
1506
+ desktop_web_checkout_url: z.string().url().optional(),
1507
+ mobile_web_checkout_url: z.string().url().optional(),
1508
+ mobile_deeplink_checkout_url: z.string().url().optional(),
1509
+ qr_checkout_string: z.string().optional()
1510
+ }).optional(),
1511
+ created: z.string().datetime(),
1512
+ updated: z.string().datetime(),
1513
+ failure_reason: z.string().optional(),
1514
+ payment_request_id: z.string().optional()
1515
+ });
1516
+ // Get Payment Request Schema
1517
+ const GetPaymentRequestSchema = z.object({
1518
+ id: z.string()
1519
+ });
1520
+ // List Payment Requests Schema
1521
+ const ListPaymentRequestsSchema = z.object({
1522
+ reference_id: z.string().optional(),
1523
+ customer_id: z.string().optional(),
1524
+ status: z.array(PaymentRequestStatusSchema).optional(),
1525
+ limit: z.number().min(1).max(100).optional(),
1526
+ after_id: z.string().optional(),
1527
+ before_id: z.string().optional(),
1528
+ created_after: z.string().datetime().optional(),
1529
+ created_before: z.string().datetime().optional()
1530
+ });
1531
+ // List Payment Requests Response
1532
+ z.object({
1533
+ data: z.array(PaymentRequestResourceSchema),
1534
+ has_more: z.boolean(),
1535
+ links: z.array(z.object({
1536
+ href: z.string(),
1537
+ rel: z.string(),
1538
+ method: z.string()
1539
+ })).optional()
1540
+ });
1541
+
1542
+ const createPaymentRequest = async (params, axiosInstance, config)=>{
1543
+ try {
1544
+ const validatedParams = validateInput(CreatePaymentRequestSchema, params, "payment request params");
1545
+ const response = await axiosInstance.post(config?.url ?? "/v3/payment_requests", validatedParams, {
1546
+ ...config,
1547
+ headers: {
1548
+ "api-version": "2024-11-11",
1549
+ ...config?.headers
1550
+ }
1551
+ });
1552
+ return validateInput(PaymentRequestResourceSchema, response.data, "payment request response");
1553
+ } catch (error) {
1554
+ if (error instanceof Error && error.name === "AxiosError") {
1555
+ handleAxiosError(error);
1556
+ }
1557
+ throw error;
1156
1558
  }
1157
- return window.btoa(string);
1158
1559
  };
1159
- const createFn = (fn, axiosInstance)=>{
1160
- return (data)=>fn(data, axiosInstance);
1560
+ const getPaymentRequest = async (params, axiosInstance, config)=>{
1561
+ try {
1562
+ const validatedParams = validateInput(GetPaymentRequestSchema, params, "get payment request params");
1563
+ const response = await axiosInstance.get(config?.url ?? `/v3/payment_requests/${validatedParams.id}`, {
1564
+ ...config,
1565
+ headers: {
1566
+ "api-version": "2024-11-11",
1567
+ ...config?.headers
1568
+ }
1569
+ });
1570
+ return validateInput(PaymentRequestResourceSchema, response.data, "payment request response");
1571
+ } catch (error) {
1572
+ if (error instanceof Error && error.name === "AxiosError") {
1573
+ handleAxiosError(error);
1574
+ }
1575
+ throw error;
1576
+ }
1161
1577
  };
1162
- const Xendit = (key, options = {})=>{
1163
- const axiosInstance = createAxiosInstance({
1164
- headers: {
1165
- Authorization: `Basic ${btoa(key + ":")}`
1578
+ const listPaymentRequests = async (params, axiosInstance, config)=>{
1579
+ try {
1580
+ const validatedParams = params ? validateInput(ListPaymentRequestsSchema, params, "list payment requests params") : {};
1581
+ const queryParams = new URLSearchParams();
1582
+ if (validatedParams.reference_id) {
1583
+ queryParams.append("reference_id", validatedParams.reference_id);
1166
1584
  }
1167
- });
1168
- // Apply rate limiting if configured
1169
- if (options.rateLimit) {
1170
- setupRateLimit(axiosInstance, options.rateLimit);
1585
+ if (validatedParams.customer_id) {
1586
+ queryParams.append("customer_id", validatedParams.customer_id);
1587
+ }
1588
+ if (validatedParams.status) {
1589
+ validatedParams.status.forEach((status)=>queryParams.append("status[]", status));
1590
+ }
1591
+ if (validatedParams.limit) {
1592
+ queryParams.append("limit", validatedParams.limit.toString());
1593
+ }
1594
+ if (validatedParams.after_id) {
1595
+ queryParams.append("after_id", validatedParams.after_id);
1596
+ }
1597
+ if (validatedParams.before_id) {
1598
+ queryParams.append("before_id", validatedParams.before_id);
1599
+ }
1600
+ if (validatedParams.created_after) {
1601
+ queryParams.append("created_after", validatedParams.created_after);
1602
+ }
1603
+ if (validatedParams.created_before) {
1604
+ queryParams.append("created_before", validatedParams.created_before);
1605
+ }
1606
+ const queryString = queryParams.toString();
1607
+ const url = queryString ? `/v3/payment_requests?${queryString}` : "/v3/payment_requests";
1608
+ if (!axiosInstance) {
1609
+ throw new Error("axiosInstance is required");
1610
+ }
1611
+ const response = await axiosInstance.get(config?.url ?? url, {
1612
+ ...config,
1613
+ headers: {
1614
+ "api-version": "2024-11-11",
1615
+ ...config?.headers
1616
+ }
1617
+ });
1618
+ return response.data;
1619
+ } catch (error) {
1620
+ if (error instanceof Error && error.name === "AxiosError") {
1621
+ handleAxiosError(error);
1622
+ }
1623
+ throw error;
1171
1624
  }
1172
- if (key.includes("development")) {
1173
- console.log("👾 You are on → TEST MODE");
1625
+ };
1626
+
1627
+ // Refund Reason
1628
+ const RefundReasonSchema = z.union([
1629
+ z.literal("REQUESTED_BY_CUSTOMER"),
1630
+ z.literal("FRAUDULENT"),
1631
+ z.literal("DUPLICATE"),
1632
+ z.literal("CANCELLATION"),
1633
+ z.literal("OTHERS")
1634
+ ]);
1635
+ // Refund Status
1636
+ const RefundStatusSchema = z.union([
1637
+ z.literal("PENDING"),
1638
+ z.literal("SUCCEEDED"),
1639
+ z.literal("FAILED")
1640
+ ]);
1641
+ // Create Refund Schema
1642
+ const CreateRefundSchema = z.object({
1643
+ payment_request_id: z.string(),
1644
+ amount: z.number().positive().optional(),
1645
+ reason: RefundReasonSchema,
1646
+ metadata: z.record(z.unknown()).optional()
1647
+ });
1648
+ // Refund Resource
1649
+ const RefundResourceSchema = z.object({
1650
+ id: z.string(),
1651
+ payment_request_id: z.string(),
1652
+ amount: z.number(),
1653
+ currency: z.string(),
1654
+ reason: RefundReasonSchema,
1655
+ status: RefundStatusSchema,
1656
+ metadata: z.record(z.unknown()).optional(),
1657
+ created: z.string().datetime(),
1658
+ updated: z.string().datetime(),
1659
+ failure_reason: z.string().optional()
1660
+ });
1661
+ // Get Refund Schema
1662
+ const GetRefundSchema = z.object({
1663
+ id: z.string()
1664
+ });
1665
+ // List Refunds Schema
1666
+ const ListRefundsSchema = z.object({
1667
+ payment_request_id: z.string().optional(),
1668
+ limit: z.number().min(1).max(100).optional(),
1669
+ after_id: z.string().optional(),
1670
+ before_id: z.string().optional(),
1671
+ created_after: z.string().datetime().optional(),
1672
+ created_before: z.string().datetime().optional()
1673
+ });
1674
+ // List Refunds Response
1675
+ z.object({
1676
+ data: z.array(RefundResourceSchema),
1677
+ has_more: z.boolean(),
1678
+ links: z.array(z.object({
1679
+ href: z.string(),
1680
+ rel: z.string(),
1681
+ method: z.string()
1682
+ })).optional()
1683
+ });
1684
+
1685
+ const createRefund = async (params, axiosInstance, config)=>{
1686
+ try {
1687
+ const validatedParams = validateInput(CreateRefundSchema, params, "refund params");
1688
+ const response = await axiosInstance.post(config?.url ?? "/v3/refunds", validatedParams, {
1689
+ ...config,
1690
+ headers: {
1691
+ "api-version": "2024-11-11",
1692
+ ...config?.headers
1693
+ }
1694
+ });
1695
+ return validateInput(RefundResourceSchema, response.data, "refund response");
1696
+ } catch (error) {
1697
+ if (error instanceof Error && error.name === "AxiosError") {
1698
+ handleAxiosError(error);
1699
+ }
1700
+ throw error;
1174
1701
  }
1175
- return {
1176
- customer: {
1177
- create: createFn(createCustomer, axiosInstance),
1178
- getById: createFn(getCustomerId, axiosInstance),
1179
- getByRefId: createFn(getCustomerRefId, axiosInstance),
1180
- update: createFn(updateCustomer, axiosInstance)
1181
- },
1182
- ewallet: {
1183
- charge: createFn(createEwalletCharge, axiosInstance),
1184
- get: createFn(getEwalletCharge, axiosInstance)
1185
- },
1186
- paymentMethod: {
1187
- create: createFn(createPaymentMethod, axiosInstance),
1188
- get: createFn(getPaymentMethod, axiosInstance),
1189
- list: (params)=>listPaymentMethods(params, axiosInstance),
1190
- update: createFn(updatePaymentMethod, axiosInstance)
1191
- },
1192
- invoice: {
1193
- create: createFn(createInvoice, axiosInstance),
1194
- get: createFn(getInvoice, axiosInstance),
1195
- list: (params)=>listInvoices(params, axiosInstance),
1196
- update: createFn(updateInvoice, axiosInstance),
1702
+ };
1703
+ const getRefund = async (params, axiosInstance, config)=>{
1704
+ try {
1705
+ const validatedParams = validateInput(GetRefundSchema, params, "get refund params");
1706
+ const response = await axiosInstance.get(config?.url ?? `/v3/refunds/${validatedParams.id}`, {
1707
+ ...config,
1708
+ headers: {
1709
+ "api-version": "2024-11-11",
1710
+ ...config?.headers
1711
+ }
1712
+ });
1713
+ return validateInput(RefundResourceSchema, response.data, "refund response");
1714
+ } catch (error) {
1715
+ if (error instanceof Error && error.name === "AxiosError") {
1716
+ handleAxiosError(error);
1717
+ }
1718
+ throw error;
1719
+ }
1720
+ };
1721
+ const listRefunds = async (params, axiosInstance, config)=>{
1722
+ try {
1723
+ const validatedParams = params ? validateInput(ListRefundsSchema, params, "list refunds params") : {};
1724
+ const queryParams = new URLSearchParams();
1725
+ if (validatedParams.payment_request_id) {
1726
+ queryParams.append("payment_request_id", validatedParams.payment_request_id);
1727
+ }
1728
+ if (validatedParams.limit) {
1729
+ queryParams.append("limit", validatedParams.limit.toString());
1730
+ }
1731
+ if (validatedParams.after_id) {
1732
+ queryParams.append("after_id", validatedParams.after_id);
1733
+ }
1734
+ if (validatedParams.before_id) {
1735
+ queryParams.append("before_id", validatedParams.before_id);
1736
+ }
1737
+ if (validatedParams.created_after) {
1738
+ queryParams.append("created_after", validatedParams.created_after);
1739
+ }
1740
+ if (validatedParams.created_before) {
1741
+ queryParams.append("created_before", validatedParams.created_before);
1742
+ }
1743
+ const queryString = queryParams.toString();
1744
+ const url = queryString ? `/v3/refunds?${queryString}` : "/v3/refunds";
1745
+ if (!axiosInstance) {
1746
+ throw new Error("axiosInstance is required");
1747
+ }
1748
+ const response = await axiosInstance.get(config?.url ?? url, {
1749
+ ...config,
1750
+ headers: {
1751
+ "api-version": "2024-11-11",
1752
+ ...config?.headers
1753
+ }
1754
+ });
1755
+ return response.data;
1756
+ } catch (error) {
1757
+ if (error instanceof Error && error.name === "AxiosError") {
1758
+ handleAxiosError(error);
1759
+ }
1760
+ throw error;
1761
+ }
1762
+ };
1763
+
1764
+ // Payout Status
1765
+ const PayoutStatusSchema = z.union([
1766
+ z.literal("ACCEPTED"),
1767
+ z.literal("PENDING"),
1768
+ z.literal("PROCESSING"),
1769
+ z.literal("COMPLETED"),
1770
+ z.literal("FAILED"),
1771
+ z.literal("CANCELLED"),
1772
+ z.literal("REVERSED")
1773
+ ]);
1774
+ // Payout Channel Code
1775
+ const PayoutChannelCodeSchema = z.union([
1776
+ z.literal("BANK"),
1777
+ z.literal("EWALLET"),
1778
+ z.literal("CASH")
1779
+ ]);
1780
+ // Bank Account
1781
+ const BankAccountSchema = z.object({
1782
+ account_holder_name: z.string(),
1783
+ account_number: z.string(),
1784
+ bank_code: z.string(),
1785
+ account_type: z.string().optional()
1786
+ });
1787
+ // E-wallet Account
1788
+ const EwalletAccountSchema = z.object({
1789
+ account_holder_name: z.string(),
1790
+ account_number: z.string(),
1791
+ ewallet_type: z.string()
1792
+ });
1793
+ // Payout Method
1794
+ const PayoutMethodSchema = z.discriminatedUnion("channel_code", [
1795
+ z.object({
1796
+ channel_code: z.literal("BANK"),
1797
+ bank_account: BankAccountSchema
1798
+ }),
1799
+ z.object({
1800
+ channel_code: z.literal("EWALLET"),
1801
+ ewallet: EwalletAccountSchema
1802
+ }),
1803
+ z.object({
1804
+ channel_code: z.literal("CASH"),
1805
+ cash: z.object({
1806
+ account_holder_name: z.string(),
1807
+ account_number: z.string()
1808
+ })
1809
+ })
1810
+ ]);
1811
+ // Create Payout Schema
1812
+ const CreatePayoutSchema = z.object({
1813
+ reference_id: z.string(),
1814
+ channel_code: PayoutChannelCodeSchema,
1815
+ channel_properties: PayoutMethodSchema,
1816
+ amount: z.number().positive(),
1817
+ currency: CurrencySchema,
1818
+ description: z.string().optional(),
1819
+ metadata: z.record(z.unknown()).optional(),
1820
+ receipt_notification: z.object({
1821
+ email_to: z.array(z.string().email()).optional(),
1822
+ email_cc: z.array(z.string().email()).optional(),
1823
+ email_bcc: z.array(z.string().email()).optional()
1824
+ }).optional()
1825
+ });
1826
+ // Payout Resource
1827
+ const PayoutResourceSchema = z.object({
1828
+ id: z.string(),
1829
+ reference_id: z.string(),
1830
+ channel_code: PayoutChannelCodeSchema,
1831
+ channel_properties: PayoutMethodSchema,
1832
+ amount: z.number(),
1833
+ currency: CurrencySchema,
1834
+ status: PayoutStatusSchema,
1835
+ description: z.string().optional(),
1836
+ metadata: z.record(z.unknown()).optional(),
1837
+ created: z.string().datetime(),
1838
+ updated: z.string().datetime(),
1839
+ failure_reason: z.string().optional(),
1840
+ estimated_arrival_time: z.string().datetime().optional(),
1841
+ receipt_notification: z.object({
1842
+ email_to: z.array(z.string().email()).optional(),
1843
+ email_cc: z.array(z.string().email()).optional(),
1844
+ email_bcc: z.array(z.string().email()).optional()
1845
+ }).optional()
1846
+ });
1847
+ // Get Payout Schema
1848
+ const GetPayoutSchema = z.object({
1849
+ id: z.string()
1850
+ });
1851
+ // List Payouts Schema
1852
+ const ListPayoutsSchema = z.object({
1853
+ reference_id: z.string().optional(),
1854
+ channel_code: z.array(PayoutChannelCodeSchema).optional(),
1855
+ status: z.array(PayoutStatusSchema).optional(),
1856
+ limit: z.number().min(1).max(100).optional(),
1857
+ after_id: z.string().optional(),
1858
+ before_id: z.string().optional(),
1859
+ created_after: z.string().datetime().optional(),
1860
+ created_before: z.string().datetime().optional()
1861
+ });
1862
+ // List Payouts Response
1863
+ z.object({
1864
+ data: z.array(PayoutResourceSchema),
1865
+ has_more: z.boolean(),
1866
+ links: z.array(z.object({
1867
+ href: z.string(),
1868
+ rel: z.string(),
1869
+ method: z.string()
1870
+ })).optional()
1871
+ });
1872
+ // Cancel Payout Schema
1873
+ const CancelPayoutSchema = z.object({
1874
+ id: z.string()
1875
+ });
1876
+
1877
+ const createPayout = async (params, axiosInstance, config)=>{
1878
+ try {
1879
+ const validatedParams = validateInput(CreatePayoutSchema, params, "payout params");
1880
+ const response = await axiosInstance.post(config?.url ?? "/v2/payouts", validatedParams, config);
1881
+ return validateInput(PayoutResourceSchema, response.data, "payout response");
1882
+ } catch (error) {
1883
+ if (error instanceof Error && error.name === "AxiosError") {
1884
+ handleAxiosError(error);
1885
+ }
1886
+ throw error;
1887
+ }
1888
+ };
1889
+ const getPayout = async (params, axiosInstance, config)=>{
1890
+ try {
1891
+ const validatedParams = validateInput(GetPayoutSchema, params, "get payout params");
1892
+ const response = await axiosInstance.get(config?.url ?? `/v2/payouts/${validatedParams.id}`, config);
1893
+ return validateInput(PayoutResourceSchema, response.data, "payout response");
1894
+ } catch (error) {
1895
+ if (error instanceof Error && error.name === "AxiosError") {
1896
+ handleAxiosError(error);
1897
+ }
1898
+ throw error;
1899
+ }
1900
+ };
1901
+ const listPayouts = async (params, axiosInstance, config)=>{
1902
+ try {
1903
+ const validatedParams = params ? validateInput(ListPayoutsSchema, params, "list payouts params") : {};
1904
+ const queryParams = new URLSearchParams();
1905
+ if (validatedParams.reference_id) {
1906
+ queryParams.append("reference_id", validatedParams.reference_id);
1907
+ }
1908
+ if (validatedParams.channel_code) {
1909
+ validatedParams.channel_code.forEach((code)=>queryParams.append("channel_code[]", code));
1910
+ }
1911
+ if (validatedParams.status) {
1912
+ validatedParams.status.forEach((status)=>queryParams.append("status[]", status));
1913
+ }
1914
+ if (validatedParams.limit) {
1915
+ queryParams.append("limit", validatedParams.limit.toString());
1916
+ }
1917
+ if (validatedParams.after_id) {
1918
+ queryParams.append("after_id", validatedParams.after_id);
1919
+ }
1920
+ if (validatedParams.before_id) {
1921
+ queryParams.append("before_id", validatedParams.before_id);
1922
+ }
1923
+ if (validatedParams.created_after) {
1924
+ queryParams.append("created_after", validatedParams.created_after);
1925
+ }
1926
+ if (validatedParams.created_before) {
1927
+ queryParams.append("created_before", validatedParams.created_before);
1928
+ }
1929
+ const queryString = queryParams.toString();
1930
+ const url = queryString ? `/v2/payouts?${queryString}` : "/v2/payouts";
1931
+ if (!axiosInstance) {
1932
+ throw new Error("axiosInstance is required");
1933
+ }
1934
+ const response = await axiosInstance.get(config?.url ?? url, config);
1935
+ return response.data;
1936
+ } catch (error) {
1937
+ if (error instanceof Error && error.name === "AxiosError") {
1938
+ handleAxiosError(error);
1939
+ }
1940
+ throw error;
1941
+ }
1942
+ };
1943
+ const cancelPayout = async (params, axiosInstance, config)=>{
1944
+ try {
1945
+ const validatedParams = validateInput(CancelPayoutSchema, params, "cancel payout params");
1946
+ const response = await axiosInstance.post(config?.url ?? `/v2/payouts/${validatedParams.id}/cancel`, {}, config);
1947
+ return validateInput(PayoutResourceSchema, response.data, "payout response");
1948
+ } catch (error) {
1949
+ if (error instanceof Error && error.name === "AxiosError") {
1950
+ handleAxiosError(error);
1951
+ }
1952
+ throw error;
1953
+ }
1954
+ };
1955
+
1956
+ // Balance Resource
1957
+ const BalanceResourceSchema = z.object({
1958
+ balance: z.number(),
1959
+ currency: CurrencySchema,
1960
+ account_type: z.string().optional()
1961
+ });
1962
+ // Transaction Type
1963
+ const TransactionTypeSchema = z.union([
1964
+ z.literal("PAYMENT"),
1965
+ z.literal("PAYOUT"),
1966
+ z.literal("REFUND"),
1967
+ z.literal("FEE"),
1968
+ z.literal("ADJUSTMENT")
1969
+ ]);
1970
+ // Transaction Status
1971
+ const TransactionStatusSchema = z.union([
1972
+ z.literal("PENDING"),
1973
+ z.literal("SUCCEEDED"),
1974
+ z.literal("FAILED"),
1975
+ z.literal("CANCELLED")
1976
+ ]);
1977
+ // Transaction Resource
1978
+ const TransactionResourceSchema = z.object({
1979
+ id: z.string(),
1980
+ reference_id: z.string().optional(),
1981
+ type: TransactionTypeSchema,
1982
+ status: TransactionStatusSchema,
1983
+ amount: z.number(),
1984
+ currency: CurrencySchema,
1985
+ created: z.string().datetime(),
1986
+ updated: z.string().datetime(),
1987
+ description: z.string().optional(),
1988
+ metadata: z.record(z.unknown()).optional()
1989
+ });
1990
+ // List Transactions Schema
1991
+ const ListTransactionsSchema = z.object({
1992
+ types: z.array(TransactionTypeSchema).optional(),
1993
+ statuses: z.array(TransactionStatusSchema).optional(),
1994
+ limit: z.number().min(1).max(100).optional(),
1995
+ after_id: z.string().optional(),
1996
+ before_id: z.string().optional(),
1997
+ created_after: z.string().datetime().optional(),
1998
+ created_before: z.string().datetime().optional()
1999
+ });
2000
+ // List Transactions Response
2001
+ z.object({
2002
+ data: z.array(TransactionResourceSchema),
2003
+ has_more: z.boolean(),
2004
+ links: z.array(z.object({
2005
+ href: z.string(),
2006
+ rel: z.string(),
2007
+ method: z.string()
2008
+ })).optional()
2009
+ });
2010
+
2011
+ const getBalance = async (axiosInstance, config)=>{
2012
+ try {
2013
+ const response = await axiosInstance.get(config?.url ?? "/balance", config);
2014
+ return validateInput(BalanceResourceSchema, response.data, "balance response");
2015
+ } catch (error) {
2016
+ if (error instanceof Error && error.name === "AxiosError") {
2017
+ handleAxiosError(error);
2018
+ }
2019
+ throw error;
2020
+ }
2021
+ };
2022
+ const listTransactions = async (params, axiosInstance, config)=>{
2023
+ try {
2024
+ const validatedParams = params ? validateInput(ListTransactionsSchema, params, "list transactions params") : {};
2025
+ const queryParams = new URLSearchParams();
2026
+ if (validatedParams.types) {
2027
+ validatedParams.types.forEach((type)=>queryParams.append("types[]", type));
2028
+ }
2029
+ if (validatedParams.statuses) {
2030
+ validatedParams.statuses.forEach((status)=>queryParams.append("statuses[]", status));
2031
+ }
2032
+ if (validatedParams.limit) {
2033
+ queryParams.append("limit", validatedParams.limit.toString());
2034
+ }
2035
+ if (validatedParams.after_id) {
2036
+ queryParams.append("after_id", validatedParams.after_id);
2037
+ }
2038
+ if (validatedParams.before_id) {
2039
+ queryParams.append("before_id", validatedParams.before_id);
2040
+ }
2041
+ if (validatedParams.created_after) {
2042
+ queryParams.append("created_after", validatedParams.created_after);
2043
+ }
2044
+ if (validatedParams.created_before) {
2045
+ queryParams.append("created_before", validatedParams.created_before);
2046
+ }
2047
+ const queryString = queryParams.toString();
2048
+ const url = queryString ? `/transactions?${queryString}` : "/transactions";
2049
+ if (!axiosInstance) {
2050
+ throw new Error("axiosInstance is required");
2051
+ }
2052
+ const response = await axiosInstance.get(config?.url ?? url, config);
2053
+ return response.data;
2054
+ } catch (error) {
2055
+ if (error instanceof Error && error.name === "AxiosError") {
2056
+ handleAxiosError(error);
2057
+ }
2058
+ throw error;
2059
+ }
2060
+ };
2061
+
2062
+ const CardDataSchema = z.object({
2063
+ account_number: z.string(),
2064
+ exp_month: z.string(),
2065
+ exp_year: z.string(),
2066
+ card_holder_first_name: z.string(),
2067
+ card_holder_last_name: z.string(),
2068
+ card_holder_email: z.string().email(),
2069
+ card_holder_phone_number: PhoneSchema
2070
+ });
2071
+ const TokenParamsSchema = z.object({
2072
+ amount: z.string().optional(),
2073
+ card_data: CardDataSchema.optional(),
2074
+ external_id: z.string().optional(),
2075
+ card_cvn: z.string().min(3).max(3).optional(),
2076
+ is_multiple_use: z.boolean().optional().default(false),
2077
+ currency: CurrencySchema.optional(),
2078
+ should_authenticate: z.boolean().default(true),
2079
+ billing_details: AddressSchema.optional(),
2080
+ mid_label: z.string()
2081
+ });
2082
+ const TokenStatusSchema = z.union([
2083
+ z.literal("IN_REVIEW"),
2084
+ z.literal("VERIFIED"),
2085
+ z.literal("FAILED"),
2086
+ z.literal("SUCCEEDED"),
2087
+ z.literal("CAPTURED"),
2088
+ z.literal("REVERSED"),
2089
+ z.literal("AUTHORISED")
2090
+ ]);
2091
+ const CardTypeSchema = z.union([
2092
+ z.literal("CREDIT"),
2093
+ z.literal("DEBIT"),
2094
+ z.literal("PREPAID"),
2095
+ z.literal("UNKNOWN")
2096
+ ]);
2097
+ const CardBrandSchema = z.union([
2098
+ z.literal("VISA"),
2099
+ z.literal("MASTERCARD"),
2100
+ z.literal("JCB"),
2101
+ z.literal("AMEX")
2102
+ ]);
2103
+ const TokenFailureReasonSchema = z.union([
2104
+ z.literal("AUTHENTICATION_FAILED"),
2105
+ z.literal("REVERSE_AUTHORIZATION_REJECTED_BY_BANK"),
2106
+ z.literal("PROCESSOR_ERROR")
2107
+ ]);
2108
+ z.union([
2109
+ z.literal("API_VALIDATION_ERROR"),
2110
+ z.literal("INVALID_JSON_FORMAT"),
2111
+ z.literal("ACCOUNT_NUMBER_INVALID_ERROR"),
2112
+ z.literal("VALIDATION_ERROR"),
2113
+ z.literal("BRAND_NOT_SUPPORTED_ERROR"),
2114
+ z.literal("AUTHENTICATION_REQUIRED_ERROR"),
2115
+ z.literal("REQUEST_FORBIDDEN_ERROR"),
2116
+ z.literal("VERIFICATION_TIMEOUT_ERROR"),
2117
+ z.literal("TEMPORARY_SERVICE_ERROR"),
2118
+ z.literal("CONNECTION_ERROR")
2119
+ ]);
2120
+ const CardInfoSchema = z.object({
2121
+ bank: z.string().optional(),
2122
+ country: CountrySchema.optional(),
2123
+ type: CardTypeSchema.optional(),
2124
+ brand: CardBrandSchema.optional(),
2125
+ fingerprint: z.string().optional(),
2126
+ card_art_url: z.string().url().optional()
2127
+ });
2128
+ const TokenResourceSchema = z.object({
2129
+ id: z.string(),
2130
+ business_id: z.string().optional(),
2131
+ created: z.string().datetime(),
2132
+ authentication_id: z.string(),
2133
+ external_id: z.string(),
2134
+ masked_card_number: z.string(),
2135
+ status: z.string(),
2136
+ payer_authentication_url: z.string().url().optional(),
2137
+ failure_reason: z.string().optional(),
2138
+ card_info: CardInfoSchema.optional()
2139
+ });
2140
+ const GetTokemParamsSchema = z.object({
2141
+ credit_card_token_id: z.string()
2142
+ });
2143
+ const TokenAuthenticationSchema = z.object({
2144
+ amount: z.string().optional(),
2145
+ token_id: z.string().optional(),
2146
+ card_data: CardDataSchema.optional(),
2147
+ external_id: z.string().optional(),
2148
+ currency: CurrencySchema.optional(),
2149
+ mid_label: z.string().optional()
2150
+ });
2151
+ const TokenAuthenticationResourceSchema = z.object({
2152
+ id: z.string(),
2153
+ status: TokenStatusSchema,
2154
+ external_id: z.string().optional(),
2155
+ payer_authentication_url: z.string().url().optional(),
2156
+ mid_label: z.string().optional(),
2157
+ failure_reason: TokenFailureReasonSchema.optional()
2158
+ });
2159
+ const TokenAuthorizationSchema = z.object({
2160
+ amount: z.string().optional(),
2161
+ token_id: z.string().optional(),
2162
+ capture: z.boolean().default(false),
2163
+ external_id: z.string().optional(),
2164
+ authentication_id: z.string().optional()
2165
+ });
2166
+ const ZeroAuthorizationSchema = z.object({
2167
+ amount: z.string().default("0"),
2168
+ token_id: z.string().optional(),
2169
+ capture: z.boolean().default(false),
2170
+ external_id: z.string().optional(),
2171
+ authentication_id: z.string().optional()
2172
+ });
2173
+ const ReverseAuthorizationSchema = z.object({
2174
+ external_id: z.string()
2175
+ });
2176
+ const PromotionSummarySchema = z.object({
2177
+ reference_id: z.string(),
2178
+ original_amount: z.number()
2179
+ });
2180
+ const IntervalSchema = z.literal("month");
2181
+ const InstallmentSummary = z.object({
2182
+ count: z.number().optional(),
2183
+ interval: IntervalSchema.optional()
2184
+ });
2185
+ const ECICodeSchema = z.union([
2186
+ z.literal("0"),
2187
+ z.literal("1"),
2188
+ z.literal("2"),
2189
+ z.literal("3"),
2190
+ z.literal("4"),
2191
+ z.literal("5")
2192
+ ]);
2193
+ const CVNCodeSchema = z.union([
2194
+ z.literal("M"),
2195
+ z.literal("N"),
2196
+ z.literal("P")
2197
+ ]);
2198
+ const CreateChargeSchema = z.object({
2199
+ token_id: z.string(),
2200
+ external_id: z.string(),
2201
+ amount: z.number(),
2202
+ authentication_id: z.string().optional(),
2203
+ capture: z.boolean().optional().default(false),
2204
+ descriptor: z.string().optional(),
2205
+ currency: CurrencySchema.optional(),
2206
+ mid_label: z.string().optional(),
2207
+ billing_details: BillingDetailSchema.optional(),
2208
+ promotion: PromotionSummarySchema.optional(),
2209
+ installment: InstallmentSummary.optional(),
2210
+ metadata: z.object({}).optional()
2211
+ });
2212
+ const ChargeFailureReasonSchema = z.union([
2213
+ z.literal("AUTHENTICATION_FAILED"),
2214
+ z.literal("DECLINED_BY_ISSUER"),
2215
+ z.literal("DECLINED_BY_PROCESSOR"),
2216
+ z.literal("EXPIRED_CARD"),
2217
+ z.literal("ISSUER_SUSPECT_FRAUD"),
2218
+ z.literal("INACTIVE_OR_UNAUTHORIZED_CARD"),
2219
+ z.literal("INSUFFICIENT_BALANCE"),
2220
+ z.literal("INVALID_CARD"),
2221
+ z.literal("INVALID_CVV"),
2222
+ z.literal("ISSUER_UNAVAILABLE"),
2223
+ z.literal("PROCESSOR_ERROR"),
2224
+ z.literal("STOLEN_CARD"),
2225
+ z.literal("PROCESSOR_TIMEOUT"),
2226
+ z.literal("FRAUD_RISK_BLOCKED")
2227
+ ]);
2228
+ const ChargeTypeSchema = z.union([
2229
+ z.literal("SINGLE_USE_TOKEN"),
2230
+ z.literal("MULTIPLE_USE_TOKEN"),
2231
+ z.literal("RECURRING")
2232
+ ]);
2233
+ const ChargeResourceSchema = z.object({
2234
+ created: z.string().datetime(),
2235
+ status: TokenStatusSchema,
2236
+ business_id: z.string(),
2237
+ authorized_amount: z.number(),
2238
+ external_id: z.string(),
2239
+ merchant_id: z.string(),
2240
+ merchant_reference_code: z.string(),
2241
+ card_type: CardTypeSchema,
2242
+ masked_card_number: z.string(),
2243
+ charge_type: ChargeTypeSchema,
2244
+ card_brand: CardBrandSchema,
2245
+ bank_reconciliation_id: z.string(),
2246
+ failure_reason: ChargeFailureReasonSchema.optional(),
2247
+ eci: ECICodeSchema.optional(),
2248
+ cvn_code: CVNCodeSchema.optional(),
2249
+ capture_amount: z.number().optional(),
2250
+ descriptor: z.string().optional(),
2251
+ id: z.string(),
2252
+ mid_label: z.string().optional(),
2253
+ promotion: PromotionSummarySchema.optional(),
2254
+ installment: InstallmentSummary.optional()
2255
+ });
2256
+
2257
+ const createToken = async (params, axiosInstance, config)=>{
2258
+ try {
2259
+ const validatedParams = validateInput(TokenParamsSchema, params, "token params");
2260
+ const response = await axiosInstance.post(config?.url ?? "/credit_card_tokens", validatedParams, config);
2261
+ return validateInput(TokenResourceSchema, response.data, "token response");
2262
+ } catch (error) {
2263
+ if (error instanceof Error && error.name === "AxiosError") {
2264
+ handleAxiosError(error);
2265
+ }
2266
+ throw error;
2267
+ }
2268
+ };
2269
+ const getToken = async (params, axiosInstance, config)=>{
2270
+ try {
2271
+ const validatedParams = validateInput(GetTokemParamsSchema, params, "get token params");
2272
+ const response = await axiosInstance.get(config?.url ?? `/credit_card_tokens/${validatedParams.credit_card_token_id}`, config);
2273
+ return validateInput(TokenResourceSchema, response.data, "token response");
2274
+ } catch (error) {
2275
+ if (error instanceof Error && error.name === "AxiosError") {
2276
+ handleAxiosError(error);
2277
+ }
2278
+ throw error;
2279
+ }
2280
+ };
2281
+ const authenticateToken = async (params, axiosInstance, config)=>{
2282
+ try {
2283
+ const validatedParams = validateInput(TokenAuthenticationSchema, params, "token authentication params");
2284
+ const response = await axiosInstance.post(config?.url ?? "/credit_card_tokens/authenticate", validatedParams, config);
2285
+ return validateInput(TokenAuthenticationResourceSchema, response.data, "token authentication response");
2286
+ } catch (error) {
2287
+ if (error instanceof Error && error.name === "AxiosError") {
2288
+ handleAxiosError(error);
2289
+ }
2290
+ throw error;
2291
+ }
2292
+ };
2293
+ const authorizeToken = async (params, axiosInstance, config)=>{
2294
+ try {
2295
+ const validatedParams = validateInput(TokenAuthorizationSchema, params, "token authorization params");
2296
+ const response = await axiosInstance.post(config?.url ?? "/credit_card_charges", validatedParams, config);
2297
+ return validateInput(ChargeResourceSchema, response.data, "charge response");
2298
+ } catch (error) {
2299
+ if (error instanceof Error && error.name === "AxiosError") {
2300
+ handleAxiosError(error);
2301
+ }
2302
+ throw error;
2303
+ }
2304
+ };
2305
+ const zeroAuthorization = async (params, axiosInstance, config)=>{
2306
+ try {
2307
+ const validatedParams = validateInput(ZeroAuthorizationSchema, params, "zero authorization params");
2308
+ const response = await axiosInstance.post(config?.url ?? "/credit_card_charges", validatedParams, config);
2309
+ return validateInput(ChargeResourceSchema, response.data, "charge response");
2310
+ } catch (error) {
2311
+ if (error instanceof Error && error.name === "AxiosError") {
2312
+ handleAxiosError(error);
2313
+ }
2314
+ throw error;
2315
+ }
2316
+ };
2317
+ const reverseAuthorization = async (params, axiosInstance, config)=>{
2318
+ try {
2319
+ const validatedParams = validateInput(ReverseAuthorizationSchema, params, "reverse authorization params");
2320
+ const response = await axiosInstance.post(config?.url ?? `/credit_card_charges/${validatedParams.external_id}/reverse_authorization`, {}, config);
2321
+ return validateInput(ChargeResourceSchema, response.data, "charge response");
2322
+ } catch (error) {
2323
+ if (error instanceof Error && error.name === "AxiosError") {
2324
+ handleAxiosError(error);
2325
+ }
2326
+ throw error;
2327
+ }
2328
+ };
2329
+ const createCharge = async (params, axiosInstance, config)=>{
2330
+ try {
2331
+ const validatedParams = validateInput(CreateChargeSchema, params, "charge params");
2332
+ const response = await axiosInstance.post(config?.url ?? "/credit_card_charges", validatedParams, config);
2333
+ return validateInput(ChargeResourceSchema, response.data, "charge response");
2334
+ } catch (error) {
2335
+ if (error instanceof Error && error.name === "AxiosError") {
2336
+ handleAxiosError(error);
2337
+ }
2338
+ throw error;
2339
+ }
2340
+ };
2341
+ const getCharge = async (params, axiosInstance, config)=>{
2342
+ try {
2343
+ const response = await axiosInstance.get(config?.url ?? `/credit_card_charges/${params.id}`, config);
2344
+ return validateInput(ChargeResourceSchema, response.data, "charge response");
2345
+ } catch (error) {
2346
+ if (error instanceof Error && error.name === "AxiosError") {
2347
+ handleAxiosError(error);
2348
+ }
2349
+ throw error;
2350
+ }
2351
+ };
2352
+
2353
+ const btoa = (string)=>{
2354
+ if (typeof window === "undefined") {
2355
+ return Buffer.from(string).toString("base64");
2356
+ }
2357
+ return window.btoa(string);
2358
+ };
2359
+ const createFn = (fn, axiosInstance)=>{
2360
+ return (data)=>fn(data, axiosInstance);
2361
+ };
2362
+ const Xendit = (key, options = {})=>{
2363
+ const axiosInstance = createAxiosInstance({
2364
+ headers: {
2365
+ Authorization: `Basic ${btoa(key + ":")}`
2366
+ }
2367
+ });
2368
+ // Apply rate limiting if configured
2369
+ if (options.rateLimit) {
2370
+ setupRateLimit(axiosInstance, options.rateLimit);
2371
+ }
2372
+ if (key.includes("development")) {
2373
+ console.log("👾 You are on → TEST MODE");
2374
+ }
2375
+ return {
2376
+ customer: {
2377
+ create: createFn(createCustomer, axiosInstance),
2378
+ getById: createFn(getCustomerId, axiosInstance),
2379
+ getByRefId: createFn(getCustomerRefId, axiosInstance),
2380
+ update: createFn(updateCustomer, axiosInstance)
2381
+ },
2382
+ ewallet: {
2383
+ charge: createFn(createEwalletCharge, axiosInstance),
2384
+ get: createFn(getEwalletCharge, axiosInstance)
2385
+ },
2386
+ paymentMethod: {
2387
+ create: createFn(createPaymentMethod, axiosInstance),
2388
+ get: createFn(getPaymentMethod, axiosInstance),
2389
+ list: (params)=>listPaymentMethods(params, axiosInstance),
2390
+ update: createFn(updatePaymentMethod, axiosInstance)
2391
+ },
2392
+ invoice: {
2393
+ create: createFn(createInvoice, axiosInstance),
2394
+ get: createFn(getInvoice, axiosInstance),
2395
+ list: (params)=>listInvoices(params, axiosInstance),
2396
+ update: createFn(updateInvoice, axiosInstance),
1197
2397
  expire: createFn(expireInvoice, axiosInstance)
2398
+ },
2399
+ paymentRequest: {
2400
+ create: createFn(createPaymentRequest, axiosInstance),
2401
+ get: createFn(getPaymentRequest, axiosInstance),
2402
+ list: (params)=>listPaymentRequests(params, axiosInstance)
2403
+ },
2404
+ refund: {
2405
+ create: createFn(createRefund, axiosInstance),
2406
+ get: createFn(getRefund, axiosInstance),
2407
+ list: (params)=>listRefunds(params, axiosInstance)
2408
+ },
2409
+ payout: {
2410
+ create: createFn(createPayout, axiosInstance),
2411
+ get: createFn(getPayout, axiosInstance),
2412
+ list: (params)=>listPayouts(params, axiosInstance),
2413
+ cancel: createFn(cancelPayout, axiosInstance)
2414
+ },
2415
+ balance: {
2416
+ get: ()=>getBalance(axiosInstance),
2417
+ listTransactions: (params)=>listTransactions(params, axiosInstance)
2418
+ },
2419
+ card: {
2420
+ createToken: createFn(createToken, axiosInstance),
2421
+ getToken: createFn(getToken, axiosInstance),
2422
+ authenticateToken: createFn(authenticateToken, axiosInstance),
2423
+ authorizeToken: createFn(authorizeToken, axiosInstance),
2424
+ zeroAuthorization: createFn(zeroAuthorization, axiosInstance),
2425
+ reverseAuthorization: createFn(reverseAuthorization, axiosInstance),
2426
+ createCharge: createFn(createCharge, axiosInstance),
2427
+ getCharge: createFn(getCharge, axiosInstance)
1198
2428
  }
1199
2429
  };
1200
2430
  };