@commercetools/checkout-payments-processor-sdk 0.0.6

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 (105) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +42 -0
  3. package/biome.json +38 -0
  4. package/package.json +48 -0
  5. package/src/api/context/request-context.helper.test.ts +98 -0
  6. package/src/api/context/request-context.helper.ts +89 -0
  7. package/src/api/context/request-context.provider.ts +27 -0
  8. package/src/api/context/types/request-context.type.ts +18 -0
  9. package/src/api/handlers/payment-components.handler.test.ts +378 -0
  10. package/src/api/handlers/payment-components.handler.ts +80 -0
  11. package/src/api/handlers/payment-intents.handler.test.ts +475 -0
  12. package/src/api/handlers/payment-intents.handler.ts +169 -0
  13. package/src/api/handlers/status.handler.test.ts +551 -0
  14. package/src/api/handlers/status.handler.ts +216 -0
  15. package/src/api/handlers/transaction.handler.ts +86 -0
  16. package/src/api/handlers/types/handler.type.ts +49 -0
  17. package/src/api/hooks/authorize.hook.test.ts +62 -0
  18. package/src/api/hooks/authorize.hook.ts +51 -0
  19. package/src/api/hooks/jwt-auth.hook.test.ts +54 -0
  20. package/src/api/hooks/jwt-auth.hook.ts +56 -0
  21. package/src/api/hooks/oauth2-auth.hook.test.ts +173 -0
  22. package/src/api/hooks/oauth2-auth.hook.ts +46 -0
  23. package/src/api/hooks/session-header-auth.hook.ts +47 -0
  24. package/src/api/hooks/session-query-param-auth.hook.ts +46 -0
  25. package/src/api/hooks/types/hook.type.ts +12 -0
  26. package/src/api/index.ts +11 -0
  27. package/src/api/types.ts +3 -0
  28. package/src/commercetools/api/base-api.ts +28 -0
  29. package/src/commercetools/api/cart-api.test.ts +95 -0
  30. package/src/commercetools/api/cart-api.ts +55 -0
  31. package/src/commercetools/api/custom-type-api.ts +39 -0
  32. package/src/commercetools/api/order-api.ts +22 -0
  33. package/src/commercetools/api/payment-api.ts +64 -0
  34. package/src/commercetools/api/payment-method-api.ts +118 -0
  35. package/src/commercetools/api/root-api.ts +109 -0
  36. package/src/commercetools/errors/ct-api.error.ts +27 -0
  37. package/src/commercetools/helpers/currency.converter.test.ts +121 -0
  38. package/src/commercetools/helpers/currency.converter.test_data.json +2379 -0
  39. package/src/commercetools/helpers/currency.converter.ts +102 -0
  40. package/src/commercetools/helpers/taxrate.converter.test.ts +48 -0
  41. package/src/commercetools/helpers/taxrate.converter.ts +39 -0
  42. package/src/commercetools/index.ts +22 -0
  43. package/src/commercetools/services/ct-cart.service.test.ts +1616 -0
  44. package/src/commercetools/services/ct-cart.service.ts +316 -0
  45. package/src/commercetools/services/ct-custom-type.service.ts +62 -0
  46. package/src/commercetools/services/ct-order.service.test.ts +136 -0
  47. package/src/commercetools/services/ct-order.service.ts +47 -0
  48. package/src/commercetools/services/ct-payment-method.service.test.ts +878 -0
  49. package/src/commercetools/services/ct-payment-method.service.ts +223 -0
  50. package/src/commercetools/services/ct-payment.service.test.ts +1123 -0
  51. package/src/commercetools/services/ct-payment.service.ts +574 -0
  52. package/src/commercetools/types/api.type.ts +162 -0
  53. package/src/commercetools/types/cart.type.ts +83 -0
  54. package/src/commercetools/types/custom-type.type.ts +21 -0
  55. package/src/commercetools/types/order.type.ts +19 -0
  56. package/src/commercetools/types/payment-method.type.ts +130 -0
  57. package/src/commercetools/types/payment.type.ts +74 -0
  58. package/src/commercetools/types/predefined-custom-types.type.ts +240 -0
  59. package/src/commercetools/types.ts +7 -0
  60. package/src/errorx/errorx.ts +625 -0
  61. package/src/errorx/index.ts +1 -0
  62. package/src/errorx/types.ts +5 -0
  63. package/src/fetch/decorators/base.decorator.ts +13 -0
  64. package/src/fetch/decorators/monitoring.decorator.ts +41 -0
  65. package/src/fetch/fetch.test.ts +57 -0
  66. package/src/fetch/index.ts +3 -0
  67. package/src/fetch/types/fetch.type.ts +11 -0
  68. package/src/fetch/types.ts +1 -0
  69. package/src/index.ts +15 -0
  70. package/src/logger/commercetools-logger.ts +69 -0
  71. package/src/logger/index.ts +1 -0
  72. package/src/logger/types/logger.type.ts +6 -0
  73. package/src/logger/types.ts +1 -0
  74. package/src/mocks/auth.mock.ts +6 -0
  75. package/src/mocks/cart.mock.ts +239 -0
  76. package/src/mocks/ct-api-error.mock.ts +23 -0
  77. package/src/mocks/index.ts +4 -0
  78. package/src/mocks/payment.mock.ts +35 -0
  79. package/src/payment-processor.ts +317 -0
  80. package/src/security/authn/authns.ts +219 -0
  81. package/src/security/authn/bearer-utils.test.ts +35 -0
  82. package/src/security/authn/bearer-utils.ts +28 -0
  83. package/src/security/authn/jwt-authn-manager.ts +69 -0
  84. package/src/security/authn/oauth2-authn-manager.ts +105 -0
  85. package/src/security/authn/session-header-authn-manager.ts +58 -0
  86. package/src/security/authn/session-query-param-authn-manager.ts +53 -0
  87. package/src/security/authn/types/authn.type.ts +50 -0
  88. package/src/security/authz/authorization-manager.ts +39 -0
  89. package/src/security/authz/types/authz.type.ts +13 -0
  90. package/src/security/index.ts +13 -0
  91. package/src/security/services/authorization.service.ts +54 -0
  92. package/src/security/services/jwt.service.test.ts +27 -0
  93. package/src/security/services/jwt.service.ts +45 -0
  94. package/src/security/services/oauth2.service.ts +60 -0
  95. package/src/security/services/session.service.ts +141 -0
  96. package/src/security/services/types/authorization.type.ts +10 -0
  97. package/src/security/services/types/jwt.type.ts +3 -0
  98. package/src/security/services/types/oauth2.type.ts +15 -0
  99. package/src/security/services/types/session.type.ts +38 -0
  100. package/src/security/types.ts +6 -0
  101. package/tsconfig.json +28 -0
  102. package/tsconfig.prod.json +9 -0
  103. package/typedoc.json +6 -0
  104. package/vitest.config.ts +15 -0
  105. package/wiki/getting-started.md +13 -0
@@ -0,0 +1,102 @@
1
+ const validateFractionDigit = (fractionDigit: number): void => {
2
+ if (!Number.isSafeInteger(fractionDigit)) {
3
+ throw new Error(
4
+ `The given fraction digit of "${fractionDigit}" is not a integer. Fraction digit must be an integer value.`,
5
+ );
6
+ }
7
+ };
8
+
9
+ const validateAmount = (amount: number): void => {
10
+ if (!Number.isSafeInteger(amount)) {
11
+ throw new Error(
12
+ `The given value of ${amount} is not a safe integer number`,
13
+ );
14
+ }
15
+ };
16
+
17
+ /**
18
+ * Applies the fraction digit to the given amount.
19
+ * Positive fraction digit moves the decimal to the right, negative to the left. (i.e. adding or removing zeros from either end)
20
+ *
21
+ * @example (10, -2) => 0.1
22
+ * @example (10, -1) => 1
23
+ * @example (10, 0) => 10
24
+ * @example (10, 1) => 100
25
+ * @example (10, 2) => 1000
26
+ * @example (12345, -3) => 12,345
27
+ * @example (12345, -2) => 123,45
28
+ * @example (12345, -1) => 1234,5
29
+ * @example (12345, 0) => 12345
30
+ * @example (12345, 1) => 123450
31
+ * @example (12345, 2) => 1234500
32
+ * @example (12345, 3) => 12345000
33
+ *
34
+ *
35
+ * @param amount Integer value to apply the fraction digit to
36
+ * @param fractionDigit The fraction digit to apply
37
+ * @throws an Error if the given fraction digit (either via mapping or directly) is not a integer value
38
+ */
39
+ const convert = (amount: number, fractionDigit: number): number => {
40
+ validateFractionDigit(fractionDigit);
41
+ validateAmount(amount);
42
+
43
+ const result = amount * 10 ** fractionDigit;
44
+ const strResult = result.toFixed(Math.abs(fractionDigit));
45
+ return parseFloat(strResult);
46
+ };
47
+
48
+ type ConvertWithMappingOptions = {
49
+ /**
50
+ * A mapping where the key is the ISO 4217 currency code value and the value is the fraction digit (integer value) to apply.
51
+ */
52
+ mapping: Map<string, number>;
53
+ /**
54
+ * The amount to apply the fraction digit to
55
+ */
56
+ amount: number;
57
+ /**
58
+ * The currency code that belongs to this amount. This is used to lookup the value in the mapping to see if it needs to be overruled
59
+ */
60
+ currencyCode: string;
61
+ /**
62
+ * The fraction digit
63
+ */
64
+ fractionDigit?: number;
65
+ };
66
+
67
+ /**
68
+ * Applies the given fraction digit to the amount with the possibility to overrule the fraction digit via the supplied mapping.
69
+ *
70
+ * @param options The options to use with converting
71
+ *
72
+ * @function convert for more information how the fractionDigit is applied
73
+ *
74
+ * @returns The original value if neither the overruling fraction digit is found or no fraction digit is given, otherwise returns the converted value based on either the overruling fraction digit or fraction digit
75
+ *
76
+ * @throws an Error if the given fraction digit (either via mapping or directly) is not a integer value
77
+ */
78
+ const convertWithMapping = (options: ConvertWithMappingOptions) => {
79
+ const {
80
+ amount,
81
+ currencyCode,
82
+ mapping,
83
+ fractionDigit: fractionDigits,
84
+ } = options;
85
+
86
+ const overrulingFractionDigits = mapping.get(currencyCode);
87
+
88
+ if (overrulingFractionDigits) {
89
+ validateFractionDigit(overrulingFractionDigits);
90
+ return convert(amount, overrulingFractionDigits);
91
+ }
92
+
93
+ if (fractionDigits) {
94
+ validateFractionDigit(fractionDigits);
95
+ return convert(amount, fractionDigits);
96
+ }
97
+
98
+ return amount;
99
+ };
100
+
101
+ export { convert, convertWithMapping };
102
+ export type { ConvertWithMappingOptions };
@@ -0,0 +1,48 @@
1
+ import { convertCoCoTaxPercentage } from '@/commercetools/helpers/taxrate.converter.js';
2
+
3
+ describe('taxrate.converter.ts', () => {
4
+ test('should return 0 if no decimalTaxRate is provided', () => {
5
+ const got = convertCoCoTaxPercentage(undefined);
6
+
7
+ expect(got).toBe(0);
8
+ });
9
+
10
+ test('should throw an error if the decimalTaxRate is below 0', () => {
11
+ expect(() => convertCoCoTaxPercentage(-0.0001)).toThrow(
12
+ `The provided decimal tax rate of -0.0001 is invalid. Only allowed values between 0 and 1.0`,
13
+ );
14
+ });
15
+
16
+ test('should throw an error if the decimalTaxRate is above 1', () => {
17
+ expect(() => convertCoCoTaxPercentage(1.01)).toThrow(
18
+ `The provided decimal tax rate of 1.01 is invalid. Only allowed values between 0 and 1.0`,
19
+ );
20
+ });
21
+
22
+ test('should correctly convert a set of tax rates from CoCo', () => {
23
+ // [expectedValue, decimalTaxRate]
24
+ const cocoDefinedTaxRates = [
25
+ [100, 0.01], // 1%
26
+ [200, 0.02], // 2%
27
+ [1000, 0.1], // 10%
28
+ [1100, 0.11], // 11%
29
+ [2100, 0.21], // 21%
30
+ [770, 0.077], // 7.7%
31
+ [700, 0.07], // 7.0%
32
+ [475, 0.0475], // 4.75%
33
+ [550, 0.055], // 5.5%
34
+ [4891, 0.4891], // 48.91%
35
+ [1500, 0.15], // 15%
36
+ [0, 0], // 0%
37
+ ];
38
+
39
+ for (const testCase of cocoDefinedTaxRates) {
40
+ const expectedValue = testCase[0];
41
+ const decimalTaxRate = testCase[1];
42
+
43
+ const got = convertCoCoTaxPercentage(decimalTaxRate);
44
+
45
+ expect(got).toBe(expectedValue);
46
+ }
47
+ });
48
+ });
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Convert the CoCo tax percentage, for example found on "lineItems", which ranges from 0-1 as floating point numbers to the minor units.
3
+ *
4
+ * @example CoCo taxRate of 0.21, normalized is 21%, in minor-units = 2100
5
+ * @example CoCo taxRate of 0.07, normalized is 7%, in minor-units = 700
6
+ * @example CoCo taxRate of 0.489, normalized is 48.9%, in minor-units = 4891
7
+ *
8
+ * @param decimalTaxRate the tax rate expressed in decimals between 0-1 as floating point numbers taken from the CoCo taxRate (see docs below).
9
+ *
10
+ * @throw "Error" if the provided value is not between 0 and 1;
11
+ *
12
+ * @returns 0 if undefined is provided
13
+ *
14
+ * @see https://docs.commercetools.com/api/projects/taxCategories#ctp:api:type:TaxRate
15
+ */
16
+ const convertCoCoTaxPercentage = (
17
+ decimalTaxRate?: number, // decimal allowed value
18
+ ): number => {
19
+ if (!decimalTaxRate) {
20
+ return 0;
21
+ }
22
+
23
+ if (decimalTaxRate < 0 || decimalTaxRate > 1) {
24
+ throw new Error(
25
+ `The provided decimal tax rate of ${decimalTaxRate} is invalid. Only allowed values between 0 and 1.0`,
26
+ );
27
+ }
28
+
29
+ // First go from the range of 0 - 1 decimal value, to value expressed as a "normal" percentage. I.e. 0.15% from CoCo becomes 15.
30
+ const taxRateNormalized = decimalTaxRate * 10 ** 2;
31
+
32
+ // Apply conversion to make it a minor unit "minor-units" format. I.e. 15 becomes 1500.
33
+ const taxRateMinorUnitsFormat = taxRateNormalized * 10 ** 2;
34
+ const taxRateMinorUnit = taxRateMinorUnitsFormat.toFixed(0);
35
+
36
+ return parseInt(taxRateMinorUnit, 10);
37
+ };
38
+
39
+ export { convertCoCoTaxPercentage };
@@ -0,0 +1,22 @@
1
+ // API Layer
2
+ export * from '@/commercetools/api/base-api.js';
3
+ export * from '@/commercetools/api/cart-api.js';
4
+ export * from '@/commercetools/api/custom-type-api.js';
5
+ export * from '@/commercetools/api/order-api.js';
6
+ export * from '@/commercetools/api/payment-api.js';
7
+ export * from '@/commercetools/api/payment-method-api.js';
8
+ export * from '@/commercetools/api/root-api.js';
9
+ // Errors
10
+ export * from '@/commercetools/errors/ct-api.error.js';
11
+ // Helpers
12
+ export * from '@/commercetools/helpers/currency.converter.js';
13
+ export * from '@/commercetools/helpers/taxrate.converter.js';
14
+ // Services Layer
15
+ export * from '@/commercetools/services/ct-cart.service.js';
16
+ export * from '@/commercetools/services/ct-custom-type.service.js';
17
+ export * from '@/commercetools/services/ct-order.service.js';
18
+ export * from '@/commercetools/services/ct-payment.service.js';
19
+ export * from '@/commercetools/services/ct-payment-method.service.js';
20
+
21
+ // Predefined Custom Types
22
+ export * from '@/commercetools/types/predefined-custom-types.type.js';