@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,57 @@
1
+ import { HttpResponse, http } from 'msw';
2
+ import { setupServer } from 'msw/node';
3
+ import { RequestContextProvider } from '@/api/context/request-context.provider.js';
4
+ import { BasicFetcher } from '@/fetch/decorators/base.decorator.js';
5
+ import { MonitoringFetcherDecorator } from '@/fetch/decorators/monitoring.decorator.js';
6
+ import type { Fetcher } from '@/fetch/types/fetch.type.js';
7
+
8
+ describe('fetch', () => {
9
+ const requestContextProvider = new RequestContextProvider({
10
+ getContextFn: vi.fn(),
11
+ updateContextFn: vi.fn(),
12
+ });
13
+ const fetch: Fetcher = new MonitoringFetcherDecorator(
14
+ new BasicFetcher(),
15
+ requestContextProvider,
16
+ );
17
+ const customFetch = fetch.run.bind(fetch);
18
+
19
+ const mockServer = setupServer(
20
+ http.get('https://some-url.com', ({ request }) => {
21
+ return HttpResponse.json({
22
+ correlationId: request.headers.get('x-correlation-id'),
23
+ requestId: request.headers.get('x-request-id'),
24
+ });
25
+ }),
26
+ );
27
+
28
+ beforeAll(async () => {
29
+ mockServer.listen();
30
+ });
31
+
32
+ afterEach(() => {
33
+ mockServer.restoreHandlers();
34
+ });
35
+
36
+ afterAll(async () => {
37
+ mockServer.close();
38
+ });
39
+
40
+ test('it should inject the correlation id and request id', async () => {
41
+ // Given
42
+ vi.spyOn(requestContextProvider, 'getContextData').mockReturnValue({
43
+ correlationId: 'correlation-id',
44
+ requestId: 'request-id',
45
+ });
46
+
47
+ // When
48
+ const request = await customFetch('https://some-url.com');
49
+ const response = await request.json();
50
+
51
+ // Then
52
+ expect(response).toEqual({
53
+ requestId: 'request-id',
54
+ correlationId: 'correlation-id',
55
+ });
56
+ });
57
+ });
@@ -0,0 +1,3 @@
1
+ // Decorators
2
+ export * from '@/fetch/decorators/base.decorator.js';
3
+ export * from '@/fetch/decorators/monitoring.decorator.js';
@@ -0,0 +1,11 @@
1
+ export interface Fetcher {
2
+ run(
3
+ input: string | URL | globalThis.Request,
4
+ init?: RequestInit,
5
+ ): Promise<Response>;
6
+ }
7
+
8
+ export type Fetch = (
9
+ input: string | URL | globalThis.Request,
10
+ init?: RequestInit,
11
+ ) => Promise<Response>;
@@ -0,0 +1 @@
1
+ export * from '@/fetch/types/fetch.type.js';
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ // Core modules
2
+ export * from '@api';
3
+ export * from '@api-types';
4
+ export * from '@commercetools';
5
+ export * from '@commercetools-types';
6
+ export * from '@errorx';
7
+ export * from '@errorx-types';
8
+ export * from '@fetch';
9
+ export * from '@fetch-types';
10
+ export * from '@logger';
11
+ export * from '@logger-types';
12
+ export * from '@mocks';
13
+ export * from '@security';
14
+ export * from '@security-types';
15
+ export * from './payment-processor.js';
@@ -0,0 +1,69 @@
1
+ import { createApplicationLogger } from '@commercetools-backend/loggers';
2
+ import cloneDeep from 'lodash/cloneDeep.js';
3
+ import setIn from 'lodash/set.js';
4
+ import { format } from 'logform';
5
+ import type { RequestContextProvider } from '@/api/index.js';
6
+
7
+ type DefaultFields = {
8
+ [key: string]:
9
+ | undefined
10
+ | string
11
+ | object
12
+ | (() => object | string | undefined);
13
+ };
14
+
15
+ export const defaultFieldsFormatter = (defaults: DefaultFields) => {
16
+ return format((info) => {
17
+ const clone = cloneDeep(info);
18
+
19
+ Object.entries(defaults).forEach(([key, value]) => {
20
+ if (value === undefined) {
21
+ // Do nothing
22
+ } else if (typeof value === 'object' || typeof value === 'string') {
23
+ setIn(clone, key, value);
24
+ } else if (typeof value === 'function') {
25
+ const evaluated = value();
26
+ if (evaluated !== undefined) {
27
+ setIn(clone, key, evaluated);
28
+ }
29
+ }
30
+ });
31
+
32
+ return clone;
33
+ })(defaults);
34
+ };
35
+
36
+ export class CommercetoolsLogger {
37
+ private logger;
38
+
39
+ constructor(opts: {
40
+ contextProvider: RequestContextProvider;
41
+ projectKey: string;
42
+ }) {
43
+ this.logger = createApplicationLogger({
44
+ formatters: [
45
+ defaultFieldsFormatter({
46
+ projectKey: opts.projectKey,
47
+ correlationId: () =>
48
+ opts.contextProvider.getContextData().correlationId,
49
+ }),
50
+ ],
51
+ });
52
+ }
53
+
54
+ debug(obj: object, message: string): void {
55
+ this.logger.debug(message, obj);
56
+ }
57
+
58
+ info(obj: object, message: string): void {
59
+ this.logger.info(message, obj);
60
+ }
61
+
62
+ warn(obj: object, message: string): void {
63
+ this.logger.warn(message, obj);
64
+ }
65
+
66
+ error(obj: object, message: string): void {
67
+ this.logger.error(message, obj);
68
+ }
69
+ }
@@ -0,0 +1 @@
1
+ export * from '@/logger/commercetools-logger.js';
@@ -0,0 +1,6 @@
1
+ export interface Logger {
2
+ debug(obj: object, message: string): void;
3
+ info(obj: object, message: string): void;
4
+ warn(obj: object, message: string): void;
5
+ error(obj: object, message: string): void;
6
+ }
@@ -0,0 +1 @@
1
+ export * from '@/logger/types/logger.type.js';
@@ -0,0 +1,6 @@
1
+ export const authToken = {
2
+ access_token: 'token',
3
+ token_type: 'Bearer',
4
+ expires_in: 172800,
5
+ scope: 'manage_project:test-project',
6
+ };
@@ -0,0 +1,239 @@
1
+ import type { Cart, TaxedItemPrice } from '@commercetools/platform-sdk';
2
+
3
+ export const cartMock: Cart = {
4
+ id: 'cartId',
5
+ version: 1,
6
+ createdAt: '2023-10-17T07:08:44.338Z',
7
+ lastModifiedAt: '2023-10-17T07:19:25.365Z',
8
+ lastModifiedBy: {
9
+ clientId: 'clientId',
10
+ },
11
+ createdBy: {
12
+ clientId: 'clientId',
13
+ },
14
+ country: 'NL',
15
+ lineItems: [
16
+ {
17
+ id: 'lineItemId',
18
+ productId: 'productId',
19
+ name: {
20
+ en: 'Bag medium GUM black',
21
+ },
22
+ productType: {
23
+ typeId: 'product-type',
24
+ id: 'bc16228e-0196-44ab-b5aa-079c49e4bc08',
25
+ },
26
+ productSlug: {
27
+ en: 'gum-bag-medium-BS1900-black',
28
+ },
29
+ variant: {
30
+ id: 1,
31
+ sku: 'variantSku',
32
+ prices: [
33
+ {
34
+ id: '47159d65-69da-4def-bd5b-2346f2d06c2a',
35
+ value: {
36
+ type: 'centPrecision',
37
+ currencyCode: 'EUR',
38
+ centAmount: 11875,
39
+ fractionDigits: 2,
40
+ },
41
+ discounted: {
42
+ value: {
43
+ type: 'centPrecision',
44
+ currencyCode: 'EUR',
45
+ centAmount: 11637,
46
+ fractionDigits: 2,
47
+ },
48
+ discount: {
49
+ typeId: 'product-discount',
50
+ id: '9b61aeff-0848-4fab-aeb5-8c0bad74365c',
51
+ },
52
+ },
53
+ },
54
+ ],
55
+ images: [],
56
+ attributes: [],
57
+ assets: [],
58
+ },
59
+ price: {
60
+ id: '47159d65-69da-4def-bd5b-2346f2d06c2a',
61
+ value: {
62
+ type: 'centPrecision',
63
+ currencyCode: 'EUR',
64
+ centAmount: 11875,
65
+ fractionDigits: 2,
66
+ },
67
+ discounted: {
68
+ value: {
69
+ type: 'centPrecision',
70
+ currencyCode: 'EUR',
71
+ centAmount: 11637,
72
+ fractionDigits: 2,
73
+ },
74
+ discount: {
75
+ typeId: 'product-discount',
76
+ id: '9b61aeff-0848-4fab-aeb5-8c0bad74365c',
77
+ },
78
+ },
79
+ },
80
+ quantity: 1,
81
+ discountedPricePerQuantity: [],
82
+ taxRate: {
83
+ name: '19% incl.',
84
+ amount: 0.19,
85
+ includedInPrice: false,
86
+ country: 'DE',
87
+ id: 'X-75EvFb',
88
+ subRates: [],
89
+ },
90
+ perMethodTaxRate: [],
91
+ addedAt: '2023-10-17T07:15:06.819Z',
92
+ lastModifiedAt: '2023-10-17T07:15:06.819Z',
93
+ state: [
94
+ {
95
+ quantity: 1,
96
+ state: {
97
+ typeId: 'state',
98
+ id: 'd776bd88-2db7-421f-8372-393fb493c704',
99
+ },
100
+ },
101
+ ],
102
+ priceMode: 'Platform',
103
+ lineItemMode: 'Standard',
104
+ totalPrice: {
105
+ type: 'centPrecision',
106
+ currencyCode: 'EUR',
107
+ centAmount: 11637,
108
+ fractionDigits: 2,
109
+ },
110
+ taxedPrice: {
111
+ totalNet: {
112
+ type: 'centPrecision',
113
+ currencyCode: 'EUR',
114
+ centAmount: 11637,
115
+ fractionDigits: 2,
116
+ },
117
+ totalGross: {
118
+ type: 'centPrecision',
119
+ currencyCode: 'EUR',
120
+ centAmount: 13848,
121
+ fractionDigits: 2,
122
+ },
123
+ totalTax: {
124
+ type: 'centPrecision',
125
+ currencyCode: 'EUR',
126
+ centAmount: 2211,
127
+ fractionDigits: 2,
128
+ },
129
+ } as TaxedItemPrice,
130
+ taxedPricePortions: [],
131
+ },
132
+ ],
133
+ cartState: 'Active',
134
+ totalPrice: {
135
+ type: 'centPrecision',
136
+ currencyCode: 'EUR',
137
+ centAmount: 11637,
138
+ fractionDigits: 2,
139
+ },
140
+ taxedPrice: {
141
+ totalNet: {
142
+ type: 'centPrecision',
143
+ currencyCode: 'EUR',
144
+ centAmount: 11637,
145
+ fractionDigits: 2,
146
+ },
147
+ totalGross: {
148
+ type: 'centPrecision',
149
+ currencyCode: 'EUR',
150
+ centAmount: 13848,
151
+ fractionDigits: 2,
152
+ },
153
+ taxPortions: [
154
+ {
155
+ rate: 0.19,
156
+ amount: {
157
+ type: 'centPrecision',
158
+ currencyCode: 'EUR',
159
+ centAmount: 2211,
160
+ fractionDigits: 2,
161
+ },
162
+ name: '19% incl.',
163
+ },
164
+ ],
165
+ totalTax: {
166
+ type: 'centPrecision',
167
+ currencyCode: 'EUR',
168
+ centAmount: 2211,
169
+ fractionDigits: 2,
170
+ },
171
+ },
172
+ shippingMode: 'Single',
173
+ shippingAddress: {
174
+ title: 'My Address',
175
+ salutation: 'Mr.',
176
+ firstName: 'Example',
177
+ lastName: 'Person',
178
+ streetName: 'Example Street',
179
+ streetNumber: '4711',
180
+ additionalStreetInfo: 'Backhouse',
181
+ postalCode: '80933',
182
+ city: 'Exemplary City',
183
+ region: 'Exemplary Region',
184
+ state: 'Exemplary State',
185
+ country: 'DE',
186
+ company: 'My Company Name',
187
+ department: 'Sales',
188
+ building: 'Hightower 1',
189
+ apartment: '247',
190
+ pOBox: '2471',
191
+ phone: '+49 89 12345678',
192
+ mobile: '+49 171 2345678',
193
+ email: 'email@example.com',
194
+ fax: '+49 89 12345679',
195
+ additionalAddressInfo: 'no additional Info',
196
+ externalId: 'Information not needed',
197
+ key: 'exampleKey',
198
+ },
199
+ shipping: [],
200
+ customLineItems: [],
201
+ discountCodes: [],
202
+ directDiscounts: [],
203
+ inventoryMode: 'None',
204
+ taxMode: 'Platform',
205
+ taxRoundingMode: 'HalfEven',
206
+ taxCalculationMode: 'LineItemLevel',
207
+ deleteDaysAfterLastModification: 90,
208
+ refusedGifts: [],
209
+ origin: 'Customer',
210
+ billingAddress: {
211
+ title: 'My Address',
212
+ salutation: 'Mr.',
213
+ firstName: 'Example',
214
+ lastName: 'Person',
215
+ streetName: 'Example Street',
216
+ streetNumber: '4711',
217
+ additionalStreetInfo: 'Backhouse',
218
+ postalCode: '80933',
219
+ city: 'Exemplary City',
220
+ region: 'Exemplary Region',
221
+ state: 'Exemplary State',
222
+ country: 'DE',
223
+ company: 'My Company Name',
224
+ department: 'Sales',
225
+ building: 'Hightower 1',
226
+ apartment: '247',
227
+ pOBox: '2471',
228
+ phone: '+49 89 12345678',
229
+ mobile: '+49 171 2345678',
230
+ email: 'email@example.com',
231
+ fax: '+49 89 12345679',
232
+ additionalAddressInfo: 'no additional Info',
233
+ externalId: 'Information not needed',
234
+ key: 'exampleKey',
235
+ },
236
+ itemShippingAddresses: [],
237
+ totalLineItemQuantity: 1,
238
+ priceRoundingMode: 'HalfEven',
239
+ };
@@ -0,0 +1,23 @@
1
+ export const ctAPIErrorMock = (
2
+ statusCode: number,
3
+ errorCode: string,
4
+ errorMessage: string,
5
+ ) => {
6
+ return {
7
+ code: statusCode,
8
+ statusCode,
9
+ status: statusCode,
10
+ message: errorMessage,
11
+ body: {
12
+ statusCode,
13
+ message: errorMessage,
14
+ errors: [
15
+ {
16
+ code: errorCode,
17
+ message: 'more specific error message',
18
+ },
19
+ ],
20
+ },
21
+ name: errorCode,
22
+ };
23
+ };
@@ -0,0 +1,4 @@
1
+ export * from '@/mocks/auth.mock.js';
2
+ export * from '@/mocks/cart.mock.js';
3
+ export * from '@/mocks/ct-api-error.mock.js';
4
+ export * from '@/mocks/payment.mock.js';
@@ -0,0 +1,35 @@
1
+ import type { Payment } from '@commercetools/platform-sdk';
2
+
3
+ export const paymentMock = {
4
+ id: 'paymentId',
5
+ version: 1,
6
+ createdAt: '2023-10-19T15:02:03.337Z',
7
+ lastModifiedAt: '2023-10-19T15:02:03.860Z',
8
+ key: 'paymentKey',
9
+ amountPlanned: {
10
+ type: 'centPrecision',
11
+ currencyCode: 'EUR',
12
+ centAmount: 2000,
13
+ fractionDigits: 2,
14
+ },
15
+ paymentMethodInfo: {
16
+ paymentInterface: 'ct-adyen',
17
+ method: 'card',
18
+ },
19
+ paymentStatus: {},
20
+ transactions: [
21
+ {
22
+ id: '1',
23
+ timestamp: '2023-10-19T15:02:03.355Z',
24
+ type: 'Authorization',
25
+ amount: {
26
+ type: 'centPrecision',
27
+ currencyCode: 'EUR',
28
+ centAmount: 2000,
29
+ fractionDigits: 2,
30
+ },
31
+ state: 'Initial',
32
+ },
33
+ ],
34
+ interfaceInteractions: [],
35
+ } as Payment;