@commercetools/connect-payments-sdk 0.4.3 → 0.4.4

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.4.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 062bbeb: Solved issue validating a payment refund as the code allowed to trigger a refund when the payment did not have a successful charge transaction
8
+
3
9
  ## 0.4.3
4
10
 
5
11
  ### Patch Changes
@@ -20,5 +20,6 @@ export declare class DefaultPaymentService implements PaymentService {
20
20
  private populateSetPaymentMethod;
21
21
  private findMatchingTransactions;
22
22
  private consolidateTransactionChanges;
23
+ private hasTransactionWithState;
23
24
  private calculateTotalAmount;
24
25
  }
@@ -45,16 +45,13 @@ class DefaultPaymentService {
45
45
  throw err;
46
46
  }
47
47
  validatePaymentCancelAuthorization(opts) {
48
- const totalAuthorized = this.calculateTotalAmount(opts.payment, 'Authorization', opts.payment.amountPlanned.currencyCode);
49
- if (totalAuthorized === 0) {
48
+ if (!this.hasTransactionWithState(opts.payment, 'Authorization', ['Success'])) {
50
49
  return { isValid: false, reason: `No authorization transaction found for resource ${opts.payment.id}.` };
51
50
  }
52
- const totalCancelled = this.calculateTotalAmount(opts.payment, 'CancelAuthorization', opts.payment.amountPlanned.currencyCode);
53
- if (totalCancelled > 0) {
51
+ if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Success', 'Pending'])) {
54
52
  return { isValid: false, reason: `Resource ${opts.payment.id} has already been cancelled.` };
55
53
  }
56
- const totalCaptured = this.calculateTotalAmount(opts.payment, 'Charge', opts.payment.amountPlanned.currencyCode);
57
- if (totalCaptured > 0) {
54
+ if (this.hasTransactionWithState(opts.payment, 'Charge', ['Success'])) {
58
55
  return { isValid: false, reason: `Resource ${opts.payment.id} has already been charged.` };
59
56
  }
60
57
  return { isValid: true };
@@ -70,8 +67,7 @@ class DefaultPaymentService {
70
67
  if (totalAuthorized === 0) {
71
68
  return { isValid: false, reason: `No authorization transaction found for resource ${opts.payment.id}.` };
72
69
  }
73
- const totalCancelled = this.calculateTotalAmount(opts.payment, 'CancelAuthorization', opts.payment.amountPlanned.currencyCode);
74
- if (totalCancelled > 0) {
70
+ if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Success', 'Pending'])) {
75
71
  return { isValid: false, reason: `Resource ${opts.payment.id} has already been cancelled.` };
76
72
  }
77
73
  const totalCaptured = this.calculateTotalAmount(opts.payment, 'Charge', opts.amount.currencyCode);
@@ -91,8 +87,7 @@ class DefaultPaymentService {
91
87
  reason: `Invalid currency ${opts.amount.currencyCode} for resource ${opts.payment.id}, expected ${opts.payment.amountPlanned.currencyCode}`,
92
88
  };
93
89
  }
94
- const totalCancelled = this.calculateTotalAmount(opts.payment, 'CancelAuthorization', opts.payment.amountPlanned.currencyCode);
95
- if (totalCancelled > 0) {
90
+ if (this.hasTransactionWithState(opts.payment, 'CancelAuthorization', ['Success', 'Pending'])) {
96
91
  return { isValid: false, reason: `Resource ${opts.payment.id} has already been cancelled.` };
97
92
  }
98
93
  const totalCaptured = this.calculateTotalAmount(opts.payment, 'Charge', opts.amount.currencyCode);
@@ -196,10 +191,13 @@ class DefaultPaymentService {
196
191
  }
197
192
  return actions;
198
193
  }
194
+ hasTransactionWithState(payment, type, state) {
195
+ return payment.transactions.some((transaction) => transaction.type === type && state.includes(transaction.state));
196
+ }
199
197
  calculateTotalAmount(payment, type, currencyCode) {
200
198
  return payment.transactions
201
199
  .filter((transaction) => transaction.type === type &&
202
- (transaction.state === 'Success' || transaction.state === 'Pending') &&
200
+ transaction.state === 'Success' &&
203
201
  transaction.amount.currencyCode === currencyCode)
204
202
  .reduce((total, transaction) => total + transaction.amount.centAmount, 0);
205
203
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercetools/connect-payments-sdk",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "Payment SDK for commercetools payment connectors",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",