@commercetools/connect-payments-sdk 0.1.0 → 0.2.1

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.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - c5997cb: avoid counting reverted payments as paid amount during cart available amount calculation
8
+
9
+ ## 0.2.0
10
+
11
+ ### Minor Changes
12
+
13
+ - cdeb0af: allow to retrieve processor URL from the session
14
+
3
15
  ## 0.1.0
4
16
 
5
17
  ### Minor Changes
@@ -13,4 +13,5 @@ export declare class DefaultCartService implements CartService {
13
13
  addPayment(opts: AddPayment): Promise<Cart>;
14
14
  private calculateTotalPaidAmount;
15
15
  private calculatePaymentAmount;
16
+ private wasPaymentReverted;
16
17
  }
@@ -78,10 +78,18 @@ class DefaultCartService {
78
78
  return payments.reduce((total, payment) => total + this.calculatePaymentAmount(payment), 0);
79
79
  }
80
80
  calculatePaymentAmount(payment) {
81
+ //If an "approved" payment has been reverted, the amount should not be considered as paid
82
+ if (this.wasPaymentReverted(payment)) {
83
+ return 0;
84
+ }
81
85
  return payment.transactions
82
86
  .filter((transaction) => (transaction.state === 'Success' || transaction.state === 'Pending') &&
83
87
  (transaction.type === 'Authorization' || transaction.type === 'Charge'))
84
88
  .reduce((total, transaction) => total + transaction.amount.centAmount, 0);
85
89
  }
90
+ wasPaymentReverted(payment) {
91
+ return payment.transactions.some((tx) => (tx.type === 'CancelAuthorization' || tx.type === 'Refund') &&
92
+ (tx.state === 'Success' || tx.state === 'Pending'));
93
+ }
86
94
  }
87
95
  exports.DefaultCartService = DefaultCartService;
@@ -106,7 +106,10 @@ class DefaultPaymentService {
106
106
  actions.push(this.populateSetPaymentMethod(updateInfo.paymentMethod));
107
107
  }
108
108
  if (updateInfo.transaction) {
109
- actions.push(...this.consolidateTransactionChanges(payment, updateInfo.transaction));
109
+ const transactionChanges = this.consolidateTransactionChanges(payment, updateInfo.transaction);
110
+ if (transactionChanges.length > 0) {
111
+ actions.push(...transactionChanges);
112
+ }
110
113
  }
111
114
  return actions;
112
115
  }
@@ -157,6 +160,10 @@ class DefaultPaymentService {
157
160
  }
158
161
  consolidateTransactionChanges(payment, transaction) {
159
162
  const actions = [];
163
+ // If the transaction is in Initial state and has no interactionId, we discard it as it does not provide any value
164
+ if (transaction.state === 'Initial' && !transaction.interactionId) {
165
+ return [];
166
+ }
160
167
  const matchingTxs = this.findMatchingTransactions(payment, transaction);
161
168
  if (matchingTxs.length === 0) {
162
169
  actions.push(this.populateAddTransactionAction(transaction));
@@ -13,5 +13,6 @@ export declare class DefaultSessionService implements SessionService {
13
13
  verifySession(sessionId: string): Promise<Session>;
14
14
  getCartFromSession(session: Session): string;
15
15
  getAllowedPaymentMethodsFromSession(session: Session): string[];
16
+ getProcessorUrlFromSession(session: Session): string;
16
17
  getPaymentInterfaceFromSession(session: Session): string | undefined;
17
18
  }
@@ -41,6 +41,9 @@ class DefaultSessionService {
41
41
  getAllowedPaymentMethodsFromSession(session) {
42
42
  return session.metadata?.allowedPaymentMethods || [];
43
43
  }
44
+ getProcessorUrlFromSession(session) {
45
+ return session.metadata?.processorUrl;
46
+ }
44
47
  getPaymentInterfaceFromSession(session) {
45
48
  return session.metadata?.paymentInterface;
46
49
  }
@@ -25,5 +25,6 @@ export interface SessionService {
25
25
  verifySession(sessionId: string): Promise<Session>;
26
26
  getCartFromSession(session: Session): string;
27
27
  getAllowedPaymentMethodsFromSession(session: Session): string[];
28
+ getProcessorUrlFromSession(session: Session): string;
28
29
  getPaymentInterfaceFromSession(session: Session): string | undefined;
29
30
  }
@@ -15,6 +15,7 @@ class SessionAuthenticationManager {
15
15
  return new authns_1.SessionAuthentication(principal.authHeader, {
16
16
  cartId: this.sessionService.getCartFromSession(session),
17
17
  allowedPaymentMethods: this.sessionService.getAllowedPaymentMethodsFromSession(session),
18
+ processorUrl: this.sessionService.getProcessorUrlFromSession(session),
18
19
  paymentInterface: this.sessionService.getPaymentInterfaceFromSession(session),
19
20
  });
20
21
  }
@@ -15,6 +15,7 @@ export type HeaderPrincipal = {
15
15
  export type SessionPrincipal = {
16
16
  cartId: string;
17
17
  allowedPaymentMethods: string[];
18
+ processorUrl: string;
18
19
  paymentInterface?: string;
19
20
  };
20
21
  export type Oauth2Principal = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercetools/connect-payments-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Payment SDK for commercetools payment connectors",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",