@commercetools/connect-payments-sdk 0.21.0 → 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,11 @@
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
+
3
9
  ## 0.21.0
4
10
 
5
11
  ### Minor Changes
@@ -1,4 +1,4 @@
1
- import { Payment, PaymentDraft, Transaction } from '@commercetools/platform-sdk';
1
+ import { Payment, PaymentDraft, PaymentUpdateAction, Transaction } from '@commercetools/platform-sdk';
2
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.
@@ -21,5 +21,10 @@ export declare class DefaultPaymentService implements PaymentService {
21
21
  private populateAddInterfaceInteractions;
22
22
  findMatchingTransactions(payment: Payment, transaction: TransactionData): Transaction[];
23
23
  hasTransactionInState(opts: FindTransaction): boolean;
24
- private consolidateTransactionChanges;
24
+ consolidateTransactionChanges(payment: Payment, newTransaction: TransactionData): PaymentUpdateAction[];
25
+ private shouldDiscardTransaction;
26
+ private handleTransactionUpdates;
27
+ private shouldUpdateTransactionState;
28
+ private shouldUpdateInteractionId;
29
+ private throwMultipleMatchingTransactionsError;
25
30
  }
@@ -144,33 +144,54 @@ class DefaultPaymentService {
144
144
  hasTransactionInState(opts) {
145
145
  return opts.payment.transactions.some((tx) => tx.type === opts.transactionType && opts.states.includes(tx.state));
146
146
  }
147
- consolidateTransactionChanges(payment, transaction) {
147
+ consolidateTransactionChanges(payment, newTransaction) {
148
148
  const actions = [];
149
- // If the transaction is in Initial state and has no interactionId, we discard it as it does not provide any value
150
- if (transaction.state === 'Initial' && !transaction.interactionId) {
149
+ // Discard transactions in 'Initial' state without an interactionId as they provide no value
150
+ if (this.shouldDiscardTransaction(newTransaction)) {
151
151
  return [];
152
152
  }
153
- const matchingTxs = this.findMatchingTransactions(payment, transaction);
153
+ // Find matching transactions in the payment
154
+ const matchingTxs = this.findMatchingTransactions(payment, newTransaction);
155
+ // Handle cases where no matching transactions are found
154
156
  if (matchingTxs.length === 0) {
155
- actions.push(this.populateAddTransactionAction(transaction));
157
+ actions.push(this.populateAddTransactionAction(newTransaction));
158
+ return actions;
156
159
  }
157
- else {
158
- if (matchingTxs.length === 1) {
159
- const tx = matchingTxs[0];
160
- if ((tx.state !== transaction.state && tx.state === 'Initial') ||
161
- (tx.state === 'Pending' && (transaction.state === 'Success' || transaction.state === 'Failure'))) {
162
- actions.push(this.populateChangeTransactionState(tx.id, transaction.state));
163
- if (!tx.interactionId && transaction.interactionId) {
164
- actions.push(this.populateChangeTransactionInteractionId(tx.id, transaction.interactionId));
165
- }
166
- }
167
- }
168
- else {
169
- this.logger.error({ paymentId: payment.id, transaction }, 'Multiple transactions found when consolidating payment changes');
170
- throw new Error('Multiple transactions found');
171
- }
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;
172
165
  }
173
- return actions;
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)}`);
174
195
  }
175
196
  }
176
197
  exports.DefaultPaymentService = DefaultPaymentService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercetools/connect-payments-sdk",
3
- "version": "0.21.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",
@@ -16,7 +16,7 @@
16
16
  "license": "ISC",
17
17
  "dependencies": {
18
18
  "@commercetools-backend/loggers": "23.2.2",
19
- "@commercetools/platform-sdk": "8.5.0",
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",