@commercetools/connect-payments-sdk 0.21.0 → 0.23.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.23.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 45ed8ad: Upgrade dependencies to latest versions
8
+
9
+ ## 0.22.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 8a98393: Improve logic for managing payment updates
14
+
3
15
  ## 0.21.0
4
16
 
5
17
  ### Minor Changes
@@ -15,5 +15,6 @@ export declare class DefaultCommercetoolsAPI implements CommercetoolsAPI {
15
15
  projectKey: string;
16
16
  contextProvider: ContextProvider<RequestContextData>;
17
17
  logger: Logger;
18
+ httpClient?: Function;
18
19
  });
19
20
  }
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DefaultCommercetoolsAPI = void 0;
4
4
  const platform_sdk_1 = require("@commercetools/platform-sdk");
5
- const sdk_client_v2_1 = require("@commercetools/sdk-client-v2");
5
+ const ts_client_1 = require("@commercetools/ts-client");
6
6
  const cart_api_1 = require("./cart-api");
7
7
  const payment_api_1 = require("./payment-api");
8
8
  const order_api_1 = require("./order-api");
@@ -30,11 +30,13 @@ const createClient = (opts) => {
30
30
  clientId: opts.clientId,
31
31
  clientSecret: opts.clientSecret,
32
32
  },
33
+ ...(opts.httpClient ? { httpClient: opts.httpClient } : {}),
33
34
  };
34
35
  const httpMiddlewareOptions = {
35
36
  host: opts.apiUrl,
36
37
  //Enables SDK retries when CoCo returns a 503 error. It retries up to 10 times with an 200ms backoff.
37
38
  enableRetry: true,
39
+ ...(opts.httpClient ? { httpClient: opts.httpClient } : {}),
38
40
  };
39
41
  const correlationIdMiddlewareOptions = {
40
42
  generate: () => {
@@ -43,7 +45,7 @@ const createClient = (opts) => {
43
45
  return correlationID;
44
46
  },
45
47
  };
46
- const ctpClient = new sdk_client_v2_1.ClientBuilder()
48
+ const ctpClient = new ts_client_1.ClientBuilder()
47
49
  .withClientCredentialsFlow(authMiddlewareOptions)
48
50
  .withCorrelationIdMiddleware(correlationIdMiddlewareOptions)
49
51
  .withHttpMiddleware(httpMiddlewareOptions)
@@ -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.23.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": "23.2.2",
19
- "@commercetools/platform-sdk": "8.5.0",
20
- "@commercetools/sdk-client-v2": "2.5.0",
18
+ "@commercetools-backend/loggers": "23.3.0",
19
+ "@commercetools/platform-sdk": "8.9.0",
20
+ "@commercetools/ts-client": "3.3.1",
21
21
  "jsonwebtoken": "9.0.2",
22
22
  "jwks-rsa": "3.2.0",
23
23
  "lodash": "4.17.21",