@commercetools/connect-payments-sdk 0.5.0 → 0.7.0
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 +12 -0
- package/dist/api/handlers/status.handler.d.ts +11 -2
- package/dist/api/handlers/status.handler.js +31 -8
- package/dist/commercetools/services/ct-payment.service.d.ts +1 -6
- package/dist/commercetools/services/ct-payment.service.js +0 -82
- package/dist/commercetools/types/payment.type.d.ts +0 -18
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @commercetools/connect-payments-sdk
|
|
2
2
|
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- e471faf: Adding logs in the status handler
|
|
8
|
+
|
|
9
|
+
## 0.6.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 7c751f2: removed validations for payment modifications (capture, refund and cancel)
|
|
14
|
+
|
|
3
15
|
## 0.5.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CommercetoolsAuthorizationService } from '../../commercetools';
|
|
2
|
+
import { Logger } from '../../logger';
|
|
2
3
|
import { HandlerResponse } from './types/handler.type';
|
|
3
4
|
type HealthCheckStatus = {
|
|
4
5
|
status: 'OK' | 'Partially Available' | 'Unavailable';
|
|
@@ -11,13 +12,21 @@ export type HealthCheckResult = {
|
|
|
11
12
|
name: string;
|
|
12
13
|
status: 'UP' | 'DOWN';
|
|
13
14
|
details?: object;
|
|
15
|
+
message?: string;
|
|
14
16
|
};
|
|
15
17
|
export type HealthCheck = () => Promise<HealthCheckResult> | HealthCheckResult;
|
|
16
|
-
export
|
|
18
|
+
export type StatusHandlerOpts = {
|
|
17
19
|
timeout: number;
|
|
18
20
|
checks: HealthCheck[];
|
|
19
21
|
metadataFn?: () => Promise<object> | object;
|
|
20
|
-
|
|
22
|
+
log: Logger;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Handler to check the status of the service
|
|
26
|
+
* @param opts
|
|
27
|
+
* @returns
|
|
28
|
+
*/
|
|
29
|
+
export declare const statusHandler: (opts: StatusHandlerOpts) => () => Promise<HandlerResponse<HealthCheckStatus>>;
|
|
21
30
|
/**
|
|
22
31
|
* Check if CoCo permissions are available
|
|
23
32
|
* @param opts
|
|
@@ -1,19 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.healthCheckCommercetoolsPermissions = exports.statusHandler = void 0;
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Handler to check the status of the service
|
|
6
|
+
* @param opts
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
const statusHandler = (opts) => async () => {
|
|
5
10
|
const status = {
|
|
6
11
|
timestamp: new Date().toISOString(),
|
|
7
12
|
version: process.env.npm_package_version ?? '0.0.0',
|
|
8
13
|
};
|
|
9
|
-
if (
|
|
10
|
-
status.metadata = await
|
|
14
|
+
if (opts.metadataFn) {
|
|
15
|
+
status.metadata = await opts.metadataFn();
|
|
11
16
|
}
|
|
12
17
|
let timeoutId = null;
|
|
13
|
-
const timeoutPromise = new Promise((_, reject) => (timeoutId = setTimeout(() => reject(new Error('Timeout')),
|
|
18
|
+
const timeoutPromise = new Promise((_, reject) => (timeoutId = setTimeout(() => reject(new Error('Timeout')), opts.timeout)));
|
|
14
19
|
try {
|
|
15
|
-
const results = await Promise.race([Promise.all(
|
|
16
|
-
|
|
20
|
+
const results = await Promise.race([Promise.all(opts.checks.map((check) => check())), timeoutPromise]);
|
|
21
|
+
let allDown = true;
|
|
22
|
+
let atLeastOneDown = false;
|
|
23
|
+
results.forEach((result) => {
|
|
24
|
+
if (result.status === 'DOWN') {
|
|
25
|
+
atLeastOneDown = true;
|
|
26
|
+
opts.log.error({ details: result.details }, result.message ?? `Health check for ${result.name} failed, check details`);
|
|
27
|
+
}
|
|
28
|
+
if (result.status === 'UP') {
|
|
29
|
+
allDown = false;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
if (allDown) {
|
|
33
|
+
status.status = 'Unavailable';
|
|
34
|
+
}
|
|
35
|
+
else if (atLeastOneDown) {
|
|
17
36
|
status.status = 'Partially Available';
|
|
18
37
|
}
|
|
19
38
|
else {
|
|
@@ -21,11 +40,14 @@ const statusHandler = (options) => async () => {
|
|
|
21
40
|
}
|
|
22
41
|
status.checks = results;
|
|
23
42
|
return {
|
|
24
|
-
status: 200,
|
|
43
|
+
status: allDown ? 503 : 200,
|
|
25
44
|
body: status,
|
|
26
45
|
};
|
|
27
46
|
}
|
|
28
|
-
catch (
|
|
47
|
+
catch (e) {
|
|
48
|
+
if (e instanceof Error && e.message === 'Timeout') {
|
|
49
|
+
opts.log.error({ err: e }, 'Health check timed out');
|
|
50
|
+
}
|
|
29
51
|
status.status = 'Unavailable';
|
|
30
52
|
status.checks = [];
|
|
31
53
|
return {
|
|
@@ -60,6 +82,7 @@ const healthCheckCommercetoolsPermissions = (opts) => async () => {
|
|
|
60
82
|
return {
|
|
61
83
|
name: 'CoCo Permissions',
|
|
62
84
|
status: 'DOWN',
|
|
85
|
+
message: `CoCo permissions are not correct, expected scopes: ${opts.requiredPermissions.join(' ')}, actual scopes: ${token.scope}`,
|
|
63
86
|
details: {
|
|
64
87
|
expectedScopes: opts.requiredPermissions,
|
|
65
88
|
actualScopes: token.scope,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Payment, PaymentDraft, Transaction } from '@commercetools/platform-sdk';
|
|
2
|
-
import { GetPayment,
|
|
2
|
+
import { GetPayment, PaymentService, PaymentServiceOptions, TransactionData, UpdatePayment } from '../types/payment.type';
|
|
3
3
|
/**
|
|
4
4
|
* This is the default implementation of the PaymentService interface.
|
|
5
5
|
*/
|
|
@@ -10,9 +10,6 @@ export declare class DefaultPaymentService implements PaymentService {
|
|
|
10
10
|
getPayment(opts: GetPayment): Promise<Payment>;
|
|
11
11
|
createPayment(draft: PaymentDraft): Promise<Payment>;
|
|
12
12
|
updatePayment(opts: UpdatePayment): Promise<Payment>;
|
|
13
|
-
validatePaymentCancelAuthorization(opts: PaymentCancelAuthorizationValidation): PaymentModificationValidationResult;
|
|
14
|
-
validatePaymentCharge(opts: PaymentChargeValidation): PaymentModificationValidationResult;
|
|
15
|
-
validatePaymentRefund(opts: PaymentRefundValidation): PaymentModificationValidationResult;
|
|
16
13
|
private consolidateUpdateActions;
|
|
17
14
|
private populateSetInterfaceIdAction;
|
|
18
15
|
private populateChangeTransactionInteractionId;
|
|
@@ -21,6 +18,4 @@ export declare class DefaultPaymentService implements PaymentService {
|
|
|
21
18
|
private populateSetPaymentMethod;
|
|
22
19
|
findMatchingTransactions(payment: Payment, transaction: TransactionData): Transaction[];
|
|
23
20
|
private consolidateTransactionChanges;
|
|
24
|
-
private hasTransactionWithState;
|
|
25
|
-
private calculateTotalAmount;
|
|
26
21
|
}
|
|
@@ -47,78 +47,6 @@ class DefaultPaymentService {
|
|
|
47
47
|
}
|
|
48
48
|
throw err;
|
|
49
49
|
}
|
|
50
|
-
validatePaymentCancelAuthorization(opts) {
|
|
51
|
-
if (!this.hasTransactionWithState(opts.payment, 'Authorization', ['Success'])) {
|
|
52
|
-
return { isValid: false, reason: `No authorization transaction found for resource ${opts.payment.id}.` };
|
|
53
|
-
}
|
|
54
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Success'])) {
|
|
55
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} has already been cancelled.` };
|
|
56
|
-
}
|
|
57
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Pending'])) {
|
|
58
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} is pending to be cancelled.` };
|
|
59
|
-
}
|
|
60
|
-
if (this.hasTransactionWithState(opts.payment, 'Charge', ['Success'])) {
|
|
61
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} has already been charged.` };
|
|
62
|
-
}
|
|
63
|
-
if (this.hasTransactionWithState(opts.payment, 'Charge', ['Pending'])) {
|
|
64
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} is pending to be charged.` };
|
|
65
|
-
}
|
|
66
|
-
return { isValid: true };
|
|
67
|
-
}
|
|
68
|
-
validatePaymentCharge(opts) {
|
|
69
|
-
if (opts.payment.amountPlanned.currencyCode !== opts.amount.currencyCode) {
|
|
70
|
-
return {
|
|
71
|
-
isValid: false,
|
|
72
|
-
reason: `Invalid currency ${opts.amount.currencyCode} for resource ${opts.payment.id}, expected ${opts.payment.amountPlanned.currencyCode}`,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
const totalAuthorized = this.calculateTotalAmount(opts.payment, 'Authorization', opts.amount.currencyCode);
|
|
76
|
-
if (totalAuthorized === 0) {
|
|
77
|
-
return { isValid: false, reason: `No authorization transaction found for resource ${opts.payment.id}.` };
|
|
78
|
-
}
|
|
79
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Success'])) {
|
|
80
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} has already been cancelled.` };
|
|
81
|
-
}
|
|
82
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Pending'])) {
|
|
83
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} is pending to be cancelled.` };
|
|
84
|
-
}
|
|
85
|
-
const totalCaptured = this.calculateTotalAmount(opts.payment, 'Charge', opts.amount.currencyCode);
|
|
86
|
-
const allowedAmount = totalAuthorized - totalCaptured;
|
|
87
|
-
if (opts.amount.centAmount > allowedAmount) {
|
|
88
|
-
return {
|
|
89
|
-
isValid: false,
|
|
90
|
-
reason: `The amount to capture ${opts.amount.centAmount} exceeds the allowed amount [${allowedAmount}]`,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
return { isValid: true };
|
|
94
|
-
}
|
|
95
|
-
validatePaymentRefund(opts) {
|
|
96
|
-
if (opts.payment.amountPlanned.currencyCode !== opts.amount.currencyCode) {
|
|
97
|
-
return {
|
|
98
|
-
isValid: false,
|
|
99
|
-
reason: `Invalid currency ${opts.amount.currencyCode} for resource ${opts.payment.id}, expected ${opts.payment.amountPlanned.currencyCode}`,
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Success'])) {
|
|
103
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} has already been cancelled.` };
|
|
104
|
-
}
|
|
105
|
-
if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Pending'])) {
|
|
106
|
-
return { isValid: false, reason: `Resource ${opts.payment.id} is pending to be cancelled.` };
|
|
107
|
-
}
|
|
108
|
-
const totalCaptured = this.calculateTotalAmount(opts.payment, 'Charge', opts.amount.currencyCode);
|
|
109
|
-
if (totalCaptured === 0) {
|
|
110
|
-
return { isValid: false, reason: `No charge transaction found for resource ${opts.payment.id}.` };
|
|
111
|
-
}
|
|
112
|
-
const totalRefunded = this.calculateTotalAmount(opts.payment, 'Refund', opts.amount.currencyCode);
|
|
113
|
-
const allowedAmount = totalCaptured - totalRefunded;
|
|
114
|
-
if (opts.amount.centAmount > allowedAmount) {
|
|
115
|
-
return {
|
|
116
|
-
isValid: false,
|
|
117
|
-
reason: `The amount to refund ${opts.amount.centAmount} exceeds the allowed amount [${allowedAmount}]`,
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
return { isValid: true };
|
|
121
|
-
}
|
|
122
50
|
consolidateUpdateActions(payment, updateInfo) {
|
|
123
51
|
const actions = [];
|
|
124
52
|
if (!payment.interfaceId && updateInfo.pspReference) {
|
|
@@ -209,15 +137,5 @@ class DefaultPaymentService {
|
|
|
209
137
|
}
|
|
210
138
|
return actions;
|
|
211
139
|
}
|
|
212
|
-
hasTransactionWithState(payment, type, state) {
|
|
213
|
-
return payment.transactions.some((transaction) => transaction.type === type && state.includes(transaction.state));
|
|
214
|
-
}
|
|
215
|
-
calculateTotalAmount(payment, type, currencyCode) {
|
|
216
|
-
return payment.transactions
|
|
217
|
-
.filter((transaction) => transaction.type === type &&
|
|
218
|
-
transaction.state === 'Success' &&
|
|
219
|
-
transaction.amount.currencyCode === currencyCode)
|
|
220
|
-
.reduce((total, transaction) => total + transaction.amount.centAmount, 0);
|
|
221
|
-
}
|
|
222
140
|
}
|
|
223
141
|
exports.DefaultPaymentService = DefaultPaymentService;
|
|
@@ -30,21 +30,6 @@ export type UpdatePayment = {
|
|
|
30
30
|
transaction?: TransactionData;
|
|
31
31
|
paymentMethod?: string;
|
|
32
32
|
};
|
|
33
|
-
export type PaymentCancelAuthorizationValidation = {
|
|
34
|
-
payment: Payment;
|
|
35
|
-
};
|
|
36
|
-
export type PaymentChargeValidation = {
|
|
37
|
-
payment: Payment;
|
|
38
|
-
amount: Money;
|
|
39
|
-
};
|
|
40
|
-
export type PaymentRefundValidation = {
|
|
41
|
-
payment: Payment;
|
|
42
|
-
amount: Money;
|
|
43
|
-
};
|
|
44
|
-
export type PaymentModificationValidationResult = {
|
|
45
|
-
isValid: boolean;
|
|
46
|
-
reason?: string;
|
|
47
|
-
};
|
|
48
33
|
/**
|
|
49
34
|
* Payment service interface exposes methods to interact with the commercetools platform API.
|
|
50
35
|
*/
|
|
@@ -52,7 +37,4 @@ export interface PaymentService {
|
|
|
52
37
|
getPayment(opts: GetPayment): Promise<Payment>;
|
|
53
38
|
createPayment(draft: PaymentDraft): Promise<Payment>;
|
|
54
39
|
updatePayment(opts: UpdatePayment): Promise<Payment>;
|
|
55
|
-
validatePaymentCancelAuthorization(opts: PaymentCancelAuthorizationValidation): PaymentModificationValidationResult;
|
|
56
|
-
validatePaymentCharge(opts: PaymentChargeValidation): PaymentModificationValidationResult;
|
|
57
|
-
validatePaymentRefund(opts: PaymentRefundValidation): PaymentModificationValidationResult;
|
|
58
40
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@commercetools/connect-payments-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Payment SDK for commercetools payment connectors",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
],
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@commercetools-backend/loggers": "22.
|
|
19
|
-
"@commercetools/platform-sdk": "7.
|
|
20
|
-
"@commercetools/sdk-client-v2": "2.
|
|
18
|
+
"@commercetools-backend/loggers": "22.27.0",
|
|
19
|
+
"@commercetools/platform-sdk": "7.8.0",
|
|
20
|
+
"@commercetools/sdk-client-v2": "2.5.0",
|
|
21
21
|
"jsonwebtoken": "9.0.2",
|
|
22
22
|
"jwks-rsa": "3.1.0",
|
|
23
23
|
"lodash": "4.17.21",
|