@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.
- package/CHANGELOG.md +15 -0
- package/README.md +42 -0
- package/biome.json +38 -0
- package/package.json +48 -0
- package/src/api/context/request-context.helper.test.ts +98 -0
- package/src/api/context/request-context.helper.ts +89 -0
- package/src/api/context/request-context.provider.ts +27 -0
- package/src/api/context/types/request-context.type.ts +18 -0
- package/src/api/handlers/payment-components.handler.test.ts +378 -0
- package/src/api/handlers/payment-components.handler.ts +80 -0
- package/src/api/handlers/payment-intents.handler.test.ts +475 -0
- package/src/api/handlers/payment-intents.handler.ts +169 -0
- package/src/api/handlers/status.handler.test.ts +551 -0
- package/src/api/handlers/status.handler.ts +216 -0
- package/src/api/handlers/transaction.handler.ts +86 -0
- package/src/api/handlers/types/handler.type.ts +49 -0
- package/src/api/hooks/authorize.hook.test.ts +62 -0
- package/src/api/hooks/authorize.hook.ts +51 -0
- package/src/api/hooks/jwt-auth.hook.test.ts +54 -0
- package/src/api/hooks/jwt-auth.hook.ts +56 -0
- package/src/api/hooks/oauth2-auth.hook.test.ts +173 -0
- package/src/api/hooks/oauth2-auth.hook.ts +46 -0
- package/src/api/hooks/session-header-auth.hook.ts +47 -0
- package/src/api/hooks/session-query-param-auth.hook.ts +46 -0
- package/src/api/hooks/types/hook.type.ts +12 -0
- package/src/api/index.ts +11 -0
- package/src/api/types.ts +3 -0
- package/src/commercetools/api/base-api.ts +28 -0
- package/src/commercetools/api/cart-api.test.ts +95 -0
- package/src/commercetools/api/cart-api.ts +55 -0
- package/src/commercetools/api/custom-type-api.ts +39 -0
- package/src/commercetools/api/order-api.ts +22 -0
- package/src/commercetools/api/payment-api.ts +64 -0
- package/src/commercetools/api/payment-method-api.ts +118 -0
- package/src/commercetools/api/root-api.ts +109 -0
- package/src/commercetools/errors/ct-api.error.ts +27 -0
- package/src/commercetools/helpers/currency.converter.test.ts +121 -0
- package/src/commercetools/helpers/currency.converter.test_data.json +2379 -0
- package/src/commercetools/helpers/currency.converter.ts +102 -0
- package/src/commercetools/helpers/taxrate.converter.test.ts +48 -0
- package/src/commercetools/helpers/taxrate.converter.ts +39 -0
- package/src/commercetools/index.ts +22 -0
- package/src/commercetools/services/ct-cart.service.test.ts +1616 -0
- package/src/commercetools/services/ct-cart.service.ts +316 -0
- package/src/commercetools/services/ct-custom-type.service.ts +62 -0
- package/src/commercetools/services/ct-order.service.test.ts +136 -0
- package/src/commercetools/services/ct-order.service.ts +47 -0
- package/src/commercetools/services/ct-payment-method.service.test.ts +878 -0
- package/src/commercetools/services/ct-payment-method.service.ts +223 -0
- package/src/commercetools/services/ct-payment.service.test.ts +1123 -0
- package/src/commercetools/services/ct-payment.service.ts +574 -0
- package/src/commercetools/types/api.type.ts +162 -0
- package/src/commercetools/types/cart.type.ts +83 -0
- package/src/commercetools/types/custom-type.type.ts +21 -0
- package/src/commercetools/types/order.type.ts +19 -0
- package/src/commercetools/types/payment-method.type.ts +130 -0
- package/src/commercetools/types/payment.type.ts +74 -0
- package/src/commercetools/types/predefined-custom-types.type.ts +240 -0
- package/src/commercetools/types.ts +7 -0
- package/src/errorx/errorx.ts +625 -0
- package/src/errorx/index.ts +1 -0
- package/src/errorx/types.ts +5 -0
- package/src/fetch/decorators/base.decorator.ts +13 -0
- package/src/fetch/decorators/monitoring.decorator.ts +41 -0
- package/src/fetch/fetch.test.ts +57 -0
- package/src/fetch/index.ts +3 -0
- package/src/fetch/types/fetch.type.ts +11 -0
- package/src/fetch/types.ts +1 -0
- package/src/index.ts +15 -0
- package/src/logger/commercetools-logger.ts +69 -0
- package/src/logger/index.ts +1 -0
- package/src/logger/types/logger.type.ts +6 -0
- package/src/logger/types.ts +1 -0
- package/src/mocks/auth.mock.ts +6 -0
- package/src/mocks/cart.mock.ts +239 -0
- package/src/mocks/ct-api-error.mock.ts +23 -0
- package/src/mocks/index.ts +4 -0
- package/src/mocks/payment.mock.ts +35 -0
- package/src/payment-processor.ts +317 -0
- package/src/security/authn/authns.ts +219 -0
- package/src/security/authn/bearer-utils.test.ts +35 -0
- package/src/security/authn/bearer-utils.ts +28 -0
- package/src/security/authn/jwt-authn-manager.ts +69 -0
- package/src/security/authn/oauth2-authn-manager.ts +105 -0
- package/src/security/authn/session-header-authn-manager.ts +58 -0
- package/src/security/authn/session-query-param-authn-manager.ts +53 -0
- package/src/security/authn/types/authn.type.ts +50 -0
- package/src/security/authz/authorization-manager.ts +39 -0
- package/src/security/authz/types/authz.type.ts +13 -0
- package/src/security/index.ts +13 -0
- package/src/security/services/authorization.service.ts +54 -0
- package/src/security/services/jwt.service.test.ts +27 -0
- package/src/security/services/jwt.service.ts +45 -0
- package/src/security/services/oauth2.service.ts +60 -0
- package/src/security/services/session.service.ts +141 -0
- package/src/security/services/types/authorization.type.ts +10 -0
- package/src/security/services/types/jwt.type.ts +3 -0
- package/src/security/services/types/oauth2.type.ts +15 -0
- package/src/security/services/types/session.type.ts +38 -0
- package/src/security/types.ts +6 -0
- package/tsconfig.json +28 -0
- package/tsconfig.prod.json +9 -0
- package/typedoc.json +6 -0
- package/vitest.config.ts +15 -0
- package/wiki/getting-started.md +13 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PaymentMethod,
|
|
3
|
+
PaymentMethodPagedQueryResponse,
|
|
4
|
+
} from '@commercetools/platform-sdk';
|
|
5
|
+
import {
|
|
6
|
+
ErrorInternalConstraintViolated,
|
|
7
|
+
ErrorResourceNotFound,
|
|
8
|
+
} from '@errorx';
|
|
9
|
+
import type { Logger } from '@logger-types';
|
|
10
|
+
import type { CommercetoolsAPI } from '@/commercetools/types/api.type.js';
|
|
11
|
+
import type {
|
|
12
|
+
DeletePaymentMethodService,
|
|
13
|
+
DoesTokenBelongToCustomer,
|
|
14
|
+
FindPaymentMethod,
|
|
15
|
+
GetByTokenValuePaymentMethod,
|
|
16
|
+
GetPaymentMethodService,
|
|
17
|
+
PaymentMethodService,
|
|
18
|
+
PaymentMethodServiceOptions,
|
|
19
|
+
SavePaymentMethodDraft,
|
|
20
|
+
UpdatePaymentMethodService,
|
|
21
|
+
} from '@/commercetools/types/payment-method.type.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* This is the default implementation of the PaymentMethodService interface.
|
|
25
|
+
*/
|
|
26
|
+
export class DefaultPaymentMethodService implements PaymentMethodService {
|
|
27
|
+
private ctAPI: CommercetoolsAPI;
|
|
28
|
+
private logger: Logger;
|
|
29
|
+
|
|
30
|
+
constructor(opts: PaymentMethodServiceOptions) {
|
|
31
|
+
this.ctAPI = opts.ctAPI;
|
|
32
|
+
this.logger = opts.logger;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async find(
|
|
36
|
+
opts: FindPaymentMethod,
|
|
37
|
+
): Promise<PaymentMethodPagedQueryResponse> {
|
|
38
|
+
const whereFilters = [];
|
|
39
|
+
|
|
40
|
+
whereFilters.push(`customer(id="${opts.customerId}")`);
|
|
41
|
+
whereFilters.push(`paymentInterface="${opts.paymentInterface}"`);
|
|
42
|
+
|
|
43
|
+
if (opts.interfaceAccount) {
|
|
44
|
+
whereFilters.push(`interfaceAccount="${opts.interfaceAccount}"`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const whereFilter = whereFilters.join(' and ');
|
|
48
|
+
|
|
49
|
+
return await this.ctAPI.paymentMethod.find({
|
|
50
|
+
queryString: whereFilter,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async get(opts: GetPaymentMethodService): Promise<PaymentMethod> {
|
|
55
|
+
const paymentMethods = await this.find({
|
|
56
|
+
customerId: opts.customerId,
|
|
57
|
+
paymentInterface: opts.paymentInterface,
|
|
58
|
+
interfaceAccount: opts.interfaceAccount,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const paymentMethodMatchesId = paymentMethods.results.find(
|
|
62
|
+
(pm) => opts.id === pm.id,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
if (!paymentMethodMatchesId) {
|
|
66
|
+
throw new ErrorResourceNotFound('payment-method', opts.id);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return paymentMethodMatchesId;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async getByTokenValue(
|
|
73
|
+
opts: GetByTokenValuePaymentMethod,
|
|
74
|
+
): Promise<PaymentMethod> {
|
|
75
|
+
const paymentMethods = await this.find({
|
|
76
|
+
customerId: opts.customerId,
|
|
77
|
+
paymentInterface: opts.paymentInterface,
|
|
78
|
+
interfaceAccount: opts.interfaceAccount,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const paymentMethodsMatchWithTokenValue = paymentMethods.results.filter(
|
|
82
|
+
(pm) => opts.tokenValue === pm.token?.value,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
if (paymentMethodsMatchWithTokenValue.length > 1) {
|
|
86
|
+
throw new ErrorInternalConstraintViolated(
|
|
87
|
+
'Found more then one payment method with the same token value.',
|
|
88
|
+
{
|
|
89
|
+
privateFields: {
|
|
90
|
+
customerId: opts.customerId,
|
|
91
|
+
paymentMethodIds: paymentMethodsMatchWithTokenValue.map(
|
|
92
|
+
(pm) => pm.id,
|
|
93
|
+
),
|
|
94
|
+
paymentInterface: opts.paymentInterface,
|
|
95
|
+
interfaceAccount: opts.interfaceAccount,
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (paymentMethodsMatchWithTokenValue.length === 0) {
|
|
102
|
+
throw new ErrorResourceNotFound('customer', opts.customerId, {
|
|
103
|
+
privateFields: {
|
|
104
|
+
customerId: opts.customerId,
|
|
105
|
+
paymentInterface: opts.paymentInterface,
|
|
106
|
+
interfaceAccount: opts.interfaceAccount,
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return paymentMethodsMatchWithTokenValue[0];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async doesTokenBelongsToCustomer(
|
|
115
|
+
opts: DoesTokenBelongToCustomer,
|
|
116
|
+
): Promise<boolean> {
|
|
117
|
+
try {
|
|
118
|
+
const paymentMethod = await this.getByTokenValue({
|
|
119
|
+
customerId: opts.customerId,
|
|
120
|
+
paymentInterface: opts.paymentInterface,
|
|
121
|
+
interfaceAccount: opts.interfaceAccount,
|
|
122
|
+
tokenValue: opts.tokenValue,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
return paymentMethod !== undefined;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (error instanceof ErrorResourceNotFound) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async update(opts: UpdatePaymentMethodService): Promise<PaymentMethod> {
|
|
136
|
+
await this.ensurePaymentMethodBelongsToGivenCustomer(
|
|
137
|
+
opts.resource.id,
|
|
138
|
+
opts.customerId,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
return this.ctAPI.paymentMethod.update({
|
|
142
|
+
actions: opts.actions,
|
|
143
|
+
resource: opts.resource,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async save(opts: SavePaymentMethodDraft): Promise<PaymentMethod> {
|
|
148
|
+
const paymentMethodExistsWithSameTokenValue =
|
|
149
|
+
await this.doesTokenBelongsToCustomer({
|
|
150
|
+
customerId: opts.customerId,
|
|
151
|
+
paymentInterface: opts.paymentInterface,
|
|
152
|
+
interfaceAccount: opts.interfaceAccount,
|
|
153
|
+
tokenValue: opts.token,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
if (paymentMethodExistsWithSameTokenValue) {
|
|
157
|
+
throw new ErrorInternalConstraintViolated(
|
|
158
|
+
'A payment method with the same "token.value" already exists.',
|
|
159
|
+
{
|
|
160
|
+
privateFields: {
|
|
161
|
+
customerId: opts.customerId,
|
|
162
|
+
paymentInterface: opts.paymentInterface,
|
|
163
|
+
interfaceAccount: opts.interfaceAccount,
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return await this.ctAPI.paymentMethod.create({
|
|
170
|
+
customer: {
|
|
171
|
+
id: opts.customerId,
|
|
172
|
+
typeId: 'customer',
|
|
173
|
+
},
|
|
174
|
+
paymentInterface: opts.paymentInterface,
|
|
175
|
+
interfaceAccount: opts.interfaceAccount,
|
|
176
|
+
method: opts.method,
|
|
177
|
+
default: false,
|
|
178
|
+
paymentMethodStatus: 'Active',
|
|
179
|
+
token: {
|
|
180
|
+
value: opts.token,
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async delete(opts: DeletePaymentMethodService): Promise<PaymentMethod> {
|
|
186
|
+
await this.ensurePaymentMethodBelongsToGivenCustomer(
|
|
187
|
+
opts.id,
|
|
188
|
+
opts.customerId,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
return await this.ctAPI.paymentMethod.delete({
|
|
192
|
+
id: opts.id,
|
|
193
|
+
version: opts.version,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private async ensurePaymentMethodBelongsToGivenCustomer(
|
|
198
|
+
paymentMethodId: string,
|
|
199
|
+
customerId: string,
|
|
200
|
+
) {
|
|
201
|
+
const predicate = `id="${paymentMethodId}" and customer(id="${customerId}")`;
|
|
202
|
+
|
|
203
|
+
const doesGivenPaymentMethodBelongToGivenCustomer =
|
|
204
|
+
await this.ctAPI.paymentMethod.checkIfExistsByPredicate({
|
|
205
|
+
queryString: predicate,
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
if (!doesGivenPaymentMethodBelongToGivenCustomer) {
|
|
209
|
+
throw new ErrorInternalConstraintViolated(
|
|
210
|
+
`The provided payment-method '${paymentMethodId}' does not have a customer '${customerId}' set`,
|
|
211
|
+
{
|
|
212
|
+
fields: {
|
|
213
|
+
paymentMethodId: paymentMethodId,
|
|
214
|
+
customerId: customerId,
|
|
215
|
+
},
|
|
216
|
+
privateFields: {
|
|
217
|
+
predicateQueryString: predicate,
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|