@commercetools/connect-payments-sdk 0.16.0 → 0.17.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.17.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 02d2994: creating getPlannedPaymentAmount which takes into account giftcard amount to create session
8
+
9
+ ## 0.16.1
10
+
11
+ ### Patch Changes
12
+
13
+ - 36f0d66: Fix the issue with rounding currency amounts
14
+
3
15
  ## 0.16.0
4
16
 
5
17
  ### Minor Changes
@@ -15,6 +15,9 @@
15
15
  * @example (12345, 2) => 1234500
16
16
  * @example (12345, 3) => 12345000
17
17
  *
18
+ *
19
+ * @param amount Integer value to apply the fraction digit to
20
+ * @param fractionDigit The fraction digit to apply
18
21
  * @throws an Error if the given fraction digit (either via mapping or directly) is not a integer value
19
22
  */
20
23
  declare const convert: (amount: number, fractionDigit: number) => number;
@@ -23,11 +23,16 @@ const validateFractionDigit = (fractionDigit) => {
23
23
  * @example (12345, 2) => 1234500
24
24
  * @example (12345, 3) => 12345000
25
25
  *
26
+ *
27
+ * @param amount Integer value to apply the fraction digit to
28
+ * @param fractionDigit The fraction digit to apply
26
29
  * @throws an Error if the given fraction digit (either via mapping or directly) is not a integer value
27
30
  */
28
31
  const convert = (amount, fractionDigit) => {
29
32
  validateFractionDigit(fractionDigit);
30
- return amount * (1 * Math.pow(10, fractionDigit));
33
+ const result = amount * Math.pow(10, fractionDigit);
34
+ const strResult = result.toFixed(Math.abs(fractionDigit));
35
+ return parseFloat(strResult);
31
36
  };
32
37
  exports.convert = convert;
33
38
  /**
@@ -12,6 +12,7 @@ export declare class DefaultCartService implements CartService {
12
12
  constructor(opts: CartServiceOptions);
13
13
  getCartByPaymentId(opts: GetCartByPaymentIdRequest): Promise<Cart>;
14
14
  getCart(opts: GetCart): Promise<Cart>;
15
+ getPlannedPaymentAmount(opts: GetPaymentAmount): Promise<PaymentAmount>;
15
16
  getPaymentAmount(opts: GetPaymentAmount): Promise<PaymentAmount>;
16
17
  /**
17
18
  * Get only one shipping address from the cart. If the cart has multiple shipping addresses, it returns the first one.
@@ -32,7 +32,7 @@ class DefaultCartService {
32
32
  async getCart(opts) {
33
33
  return await this.ctAPI.cart.getCartById(opts.id);
34
34
  }
35
- async getPaymentAmount(opts) {
35
+ async getPlannedPaymentAmount(opts) {
36
36
  const giftCardPlannedAmount = (0, __1.getGiftCardPlannedAmountFromContext)(this.contextProvider.getContextData());
37
37
  const paidAmount = await this.calculateTotalPaidAmount(opts.cart);
38
38
  let cartAmount;
@@ -71,6 +71,42 @@ class DefaultCartService {
71
71
  fractionDigits: cartAmount.fractionDigits,
72
72
  };
73
73
  }
74
+ async getPaymentAmount(opts) {
75
+ const paidAmount = await this.calculateTotalPaidAmount(opts.cart);
76
+ let cartAmount;
77
+ if (opts.cart.taxedPrice) {
78
+ cartAmount = {
79
+ currencyCode: opts.cart.taxedPrice.totalGross.currencyCode,
80
+ centAmount: opts.cart.taxedPrice.totalGross.centAmount,
81
+ fractionDigits: opts.cart.taxedPrice.totalGross.fractionDigits,
82
+ };
83
+ }
84
+ else {
85
+ cartAmount = {
86
+ currencyCode: opts.cart.totalPrice.currencyCode,
87
+ centAmount: opts.cart.totalPrice.centAmount,
88
+ fractionDigits: opts.cart.totalPrice.fractionDigits,
89
+ };
90
+ }
91
+ if (paidAmount >= cartAmount.centAmount) {
92
+ throw new __1.ErrorInvalidOperation('The cart has already been paid in full', {
93
+ fields: {
94
+ resource: opts.cart.id,
95
+ paidAmount,
96
+ cartAmount,
97
+ },
98
+ });
99
+ }
100
+ const currentAmountToPay = cartAmount.centAmount - paidAmount;
101
+ this.logger.info({
102
+ currentAmountToPay,
103
+ }, 'current amount to pay');
104
+ return {
105
+ currencyCode: cartAmount.currencyCode,
106
+ centAmount: currentAmountToPay,
107
+ fractionDigits: cartAmount.fractionDigits,
108
+ };
109
+ }
74
110
  /**
75
111
  * Get only one shipping address from the cart. If the cart has multiple shipping addresses, it returns the first one.
76
112
  * @param cart
@@ -41,6 +41,18 @@ export type GetCartByPaymentIdRequest = {
41
41
  export interface CartService {
42
42
  getCartByPaymentId(opts: GetCartByPaymentIdRequest): Promise<Cart>;
43
43
  getCart(opts: GetCart): Promise<Cart>;
44
+ /**
45
+ * Get payment amount for a cart. This method is used to calculate the payment amount for a cart.
46
+ * It takes into account existing successful payments.
47
+ * @param opts
48
+ */
44
49
  getPaymentAmount(opts: GetPaymentAmount): Promise<PaymentAmount>;
50
+ /**
51
+ * Get payment amount for a cart. This method is used to calculate the payment amount for a cart.
52
+ * It takes into account existing successful payments and giftcards connectors.
53
+ * This method is specifically used for calculating the payment amount for a cart when a giftcard is applied and the payment session is created upfront.
54
+ * @param opts
55
+ */
56
+ getPlannedPaymentAmount(opts: GetPaymentAmount): Promise<PaymentAmount>;
45
57
  addPayment(opts: AddPayment): Promise<Cart>;
46
58
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercetools/connect-payments-sdk",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "Payment SDK for commercetools payment connectors",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,7 +15,7 @@
15
15
  ],
16
16
  "license": "ISC",
17
17
  "dependencies": {
18
- "@commercetools-backend/loggers": "22.38.1",
18
+ "@commercetools-backend/loggers": "22.39.0",
19
19
  "@commercetools/platform-sdk": "8.1.0",
20
20
  "@commercetools/sdk-client-v2": "2.5.0",
21
21
  "jsonwebtoken": "9.0.2",