@commercetools/connect-payments-sdk 0.20.1 → 0.22.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
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @commercetools/connect-payments-sdk
|
|
2
2
|
|
|
3
|
+
## 0.22.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 8a98393: Improve logic for managing payment updates
|
|
8
|
+
|
|
9
|
+
## 0.21.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 9946b9e: introduces hasTransactionInState method for checking if a payment has a transaction with some set filters, (type and state)
|
|
14
|
+
|
|
3
15
|
## 0.20.1
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Payment, PaymentDraft, Transaction } from '@commercetools/platform-sdk';
|
|
2
|
-
import { FindPaymentsByInterfaceId, GetPayment, PaymentService, PaymentServiceOptions, TransactionData, UpdatePayment } from '../types/payment.type';
|
|
1
|
+
import { Payment, PaymentDraft, PaymentUpdateAction, Transaction } from '@commercetools/platform-sdk';
|
|
2
|
+
import { FindPaymentsByInterfaceId, FindTransaction, GetPayment, PaymentService, PaymentServiceOptions, TransactionData, UpdatePayment } from '../types/payment.type';
|
|
3
3
|
/**
|
|
4
4
|
* This is the default implementation of the PaymentService interface.
|
|
5
5
|
*/
|
|
@@ -20,5 +20,11 @@ export declare class DefaultPaymentService implements PaymentService {
|
|
|
20
20
|
private populateSetCustomType;
|
|
21
21
|
private populateAddInterfaceInteractions;
|
|
22
22
|
findMatchingTransactions(payment: Payment, transaction: TransactionData): Transaction[];
|
|
23
|
-
|
|
23
|
+
hasTransactionInState(opts: FindTransaction): boolean;
|
|
24
|
+
consolidateTransactionChanges(payment: Payment, newTransaction: TransactionData): PaymentUpdateAction[];
|
|
25
|
+
private shouldDiscardTransaction;
|
|
26
|
+
private handleTransactionUpdates;
|
|
27
|
+
private shouldUpdateTransactionState;
|
|
28
|
+
private shouldUpdateInteractionId;
|
|
29
|
+
private throwMultipleMatchingTransactionsError;
|
|
24
30
|
}
|
|
@@ -141,33 +141,57 @@ class DefaultPaymentService {
|
|
|
141
141
|
tx.state === 'Initial')));
|
|
142
142
|
});
|
|
143
143
|
}
|
|
144
|
-
|
|
144
|
+
hasTransactionInState(opts) {
|
|
145
|
+
return opts.payment.transactions.some((tx) => tx.type === opts.transactionType && opts.states.includes(tx.state));
|
|
146
|
+
}
|
|
147
|
+
consolidateTransactionChanges(payment, newTransaction) {
|
|
145
148
|
const actions = [];
|
|
146
|
-
//
|
|
147
|
-
if (
|
|
149
|
+
// Discard transactions in 'Initial' state without an interactionId as they provide no value
|
|
150
|
+
if (this.shouldDiscardTransaction(newTransaction)) {
|
|
148
151
|
return [];
|
|
149
152
|
}
|
|
150
|
-
|
|
153
|
+
// Find matching transactions in the payment
|
|
154
|
+
const matchingTxs = this.findMatchingTransactions(payment, newTransaction);
|
|
155
|
+
// Handle cases where no matching transactions are found
|
|
151
156
|
if (matchingTxs.length === 0) {
|
|
152
|
-
actions.push(this.populateAddTransactionAction(
|
|
157
|
+
actions.push(this.populateAddTransactionAction(newTransaction));
|
|
158
|
+
return actions;
|
|
153
159
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
actions.push(this.populateChangeTransactionState(tx.id, transaction.state));
|
|
160
|
-
if (!tx.interactionId && transaction.interactionId) {
|
|
161
|
-
actions.push(this.populateChangeTransactionInteractionId(tx.id, transaction.interactionId));
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
this.logger.error({ paymentId: payment.id, transaction }, 'Multiple transactions found when consolidating payment changes');
|
|
167
|
-
throw new Error('Multiple transactions found');
|
|
168
|
-
}
|
|
160
|
+
// Handle cases where exactly one matching transaction is found
|
|
161
|
+
if (matchingTxs.length === 1) {
|
|
162
|
+
const existingTx = matchingTxs[0];
|
|
163
|
+
this.handleTransactionUpdates(existingTx, newTransaction, actions);
|
|
164
|
+
return actions;
|
|
169
165
|
}
|
|
170
|
-
|
|
166
|
+
// Handle cases where multiple matching transactions are found
|
|
167
|
+
this.throwMultipleMatchingTransactionsError(payment, newTransaction, matchingTxs);
|
|
168
|
+
}
|
|
169
|
+
shouldDiscardTransaction(newTransaction) {
|
|
170
|
+
return newTransaction.state === 'Initial' && !newTransaction.interactionId;
|
|
171
|
+
}
|
|
172
|
+
handleTransactionUpdates(existingTx, newTransaction, actions) {
|
|
173
|
+
if (this.shouldUpdateTransactionState(existingTx, newTransaction)) {
|
|
174
|
+
actions.push(this.populateChangeTransactionState(existingTx.id, newTransaction.state));
|
|
175
|
+
}
|
|
176
|
+
if (this.shouldUpdateInteractionId(existingTx, newTransaction)) {
|
|
177
|
+
actions.push(this.populateChangeTransactionInteractionId(existingTx.id, newTransaction.interactionId));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
shouldUpdateTransactionState(existingTx, newTransaction) {
|
|
181
|
+
return ((existingTx.state !== newTransaction.state && existingTx.state === 'Initial') ||
|
|
182
|
+
(existingTx.state === 'Pending' && (newTransaction.state === 'Success' || newTransaction.state === 'Failure')) ||
|
|
183
|
+
(existingTx.state === 'Failure' && newTransaction.state === 'Success'));
|
|
184
|
+
}
|
|
185
|
+
shouldUpdateInteractionId(existingTx, newTransaction) {
|
|
186
|
+
return !existingTx.interactionId && !!newTransaction.interactionId;
|
|
187
|
+
}
|
|
188
|
+
throwMultipleMatchingTransactionsError(payment, newTransaction, matchingTxs) {
|
|
189
|
+
this.logger.error({
|
|
190
|
+
paymentId: payment.id,
|
|
191
|
+
transaction: newTransaction,
|
|
192
|
+
matchingTransactions: matchingTxs,
|
|
193
|
+
}, 'Multiple matching transactions found when consolidating payment changes');
|
|
194
|
+
throw new Error(`Multiple matching transactions found for payment ${payment.id} and transaction ${JSON.stringify(newTransaction)}`);
|
|
171
195
|
}
|
|
172
196
|
}
|
|
173
197
|
exports.DefaultPaymentService = DefaultPaymentService;
|
|
@@ -35,12 +35,18 @@ export type UpdatePayment = {
|
|
|
35
35
|
paymentMethod?: string;
|
|
36
36
|
customFields?: CustomFieldsDraft;
|
|
37
37
|
};
|
|
38
|
+
export type FindTransaction = {
|
|
39
|
+
payment: Payment;
|
|
40
|
+
transactionType: TransactionType;
|
|
41
|
+
states: TransactionState[];
|
|
42
|
+
};
|
|
38
43
|
/**
|
|
39
44
|
* Payment service interface exposes methods to interact with the commercetools platform API.
|
|
40
45
|
*/
|
|
41
46
|
export interface PaymentService {
|
|
42
47
|
getPayment(opts: GetPayment): Promise<Payment>;
|
|
43
48
|
findPaymentsByInterfaceId(opts: FindPaymentsByInterfaceId): Promise<Payment[]>;
|
|
49
|
+
hasTransactionInState(opts: FindTransaction): boolean;
|
|
44
50
|
createPayment(draft: PaymentDraft): Promise<Payment>;
|
|
45
51
|
updatePayment(opts: UpdatePayment): Promise<Payment>;
|
|
46
52
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@commercetools/connect-payments-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"description": "Payment SDK for commercetools payment connectors",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
],
|
|
16
16
|
"license": "ISC",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@commercetools-backend/loggers": "23.2.
|
|
19
|
-
"@commercetools/platform-sdk": "8.
|
|
18
|
+
"@commercetools-backend/loggers": "23.2.2",
|
|
19
|
+
"@commercetools/platform-sdk": "8.8.0",
|
|
20
20
|
"@commercetools/sdk-client-v2": "2.5.0",
|
|
21
21
|
"jsonwebtoken": "9.0.2",
|
|
22
22
|
"jwks-rsa": "3.2.0",
|