@commercetools/connect-payments-sdk 0.13.0 → 0.14.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.14.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 012a346: Add currency conversion utility functions
8
+
9
+ ## 0.13.1
10
+
11
+ ### Patch Changes
12
+
13
+ - a3bbaab: exporting NormalizedShipping type
14
+
3
15
  ## 0.13.0
4
16
 
5
17
  ### Minor Changes
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Applies the fraction digit to the given amount.
3
+ * Positive fraction digit moves the decimal to the right, negative to the left. (i.e. adding or removing zeros from either end)
4
+ *
5
+ * @example (10, -2) => 0.1
6
+ * @example (10, -1) => 1
7
+ * @example (10, 0) => 10
8
+ * @example (10, 1) => 100
9
+ * @example (10, 2) => 1000
10
+ * @example (12345, -3) => 12,345
11
+ * @example (12345, -2) => 123,45
12
+ * @example (12345, -1) => 1234,5
13
+ * @example (12345, 0) => 12345
14
+ * @example (12345, 1) => 123450
15
+ * @example (12345, 2) => 1234500
16
+ * @example (12345, 3) => 12345000
17
+ *
18
+ * @throws an Error if the given fraction digit (either via mapping or directly) is not a integer value
19
+ */
20
+ declare const convert: (amount: number, fractionDigit: number) => number;
21
+ type ConvertWithMappingOptions = {
22
+ /**
23
+ * A mapping where the key is the ISO 4217 currency code value and the value is the fraction digit (integer value) to apply.
24
+ */
25
+ mapping: Map<string, number>;
26
+ /**
27
+ * The amount to apply the fraction digit to
28
+ */
29
+ amount: number;
30
+ /**
31
+ * The currency code that belongs to this amount. This is used to lookup the value in the mapping to see if it needs to be overruled
32
+ */
33
+ currencyCode: string;
34
+ /**
35
+ * The fraction digit
36
+ */
37
+ fractionDigit?: number;
38
+ };
39
+ /**
40
+ * Applies the given fraction digit to the amount with the possibility to overrule the fraction digit via the supplied mapping.
41
+ *
42
+ * @param options The options to use with converting
43
+ *
44
+ * @function convert for more information how the fractionDigit is applied
45
+ *
46
+ * @returns The original value if neither the overruling fraction digit is found or no fraction digit is given, otherwise returns the converted value based on either the overruling fraction digit or fraction digit
47
+ *
48
+ * @throws an Error if the given fraction digit (either via mapping or directly) is not a integer value
49
+ */
50
+ declare const convertWithMapping: (options: ConvertWithMappingOptions) => number;
51
+ export { convert, convertWithMapping };
52
+ export type { ConvertWithMappingOptions };
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertWithMapping = exports.convert = void 0;
4
+ const validateFractionDigit = (fractionDigit) => {
5
+ if (!Number.isInteger(fractionDigit)) {
6
+ throw new Error(`The given fraction digit of "${fractionDigit}" is not a integer. Fraction digit must be an integer value.`);
7
+ }
8
+ };
9
+ /**
10
+ * Applies the fraction digit to the given amount.
11
+ * Positive fraction digit moves the decimal to the right, negative to the left. (i.e. adding or removing zeros from either end)
12
+ *
13
+ * @example (10, -2) => 0.1
14
+ * @example (10, -1) => 1
15
+ * @example (10, 0) => 10
16
+ * @example (10, 1) => 100
17
+ * @example (10, 2) => 1000
18
+ * @example (12345, -3) => 12,345
19
+ * @example (12345, -2) => 123,45
20
+ * @example (12345, -1) => 1234,5
21
+ * @example (12345, 0) => 12345
22
+ * @example (12345, 1) => 123450
23
+ * @example (12345, 2) => 1234500
24
+ * @example (12345, 3) => 12345000
25
+ *
26
+ * @throws an Error if the given fraction digit (either via mapping or directly) is not a integer value
27
+ */
28
+ const convert = (amount, fractionDigit) => {
29
+ validateFractionDigit(fractionDigit);
30
+ return amount * (1 * Math.pow(10, fractionDigit));
31
+ };
32
+ exports.convert = convert;
33
+ /**
34
+ * Applies the given fraction digit to the amount with the possibility to overrule the fraction digit via the supplied mapping.
35
+ *
36
+ * @param options The options to use with converting
37
+ *
38
+ * @function convert for more information how the fractionDigit is applied
39
+ *
40
+ * @returns The original value if neither the overruling fraction digit is found or no fraction digit is given, otherwise returns the converted value based on either the overruling fraction digit or fraction digit
41
+ *
42
+ * @throws an Error if the given fraction digit (either via mapping or directly) is not a integer value
43
+ */
44
+ const convertWithMapping = (options) => {
45
+ const { amount, currencyCode, mapping, fractionDigit: fractionDigits } = options;
46
+ const overrulingFractionDigits = mapping.get(currencyCode);
47
+ if (overrulingFractionDigits) {
48
+ validateFractionDigit(overrulingFractionDigits);
49
+ return convert(amount, overrulingFractionDigits);
50
+ }
51
+ if (fractionDigits) {
52
+ validateFractionDigit(fractionDigits);
53
+ return convert(amount, fractionDigits);
54
+ }
55
+ return amount;
56
+ };
57
+ exports.convertWithMapping = convertWithMapping;
@@ -1,7 +1,8 @@
1
- export { CartService as CommercetoolsCartService } from './types/cart.type';
1
+ export { CartService as CommercetoolsCartService, NormalizedShipping } from './types/cart.type';
2
2
  export { PaymentService as CommercetoolsPaymentService, TransactionData, UpdatePayment } from './types/payment.type';
3
3
  export { SessionService as CommercetoolsSessionService, Session } from './types/session.type';
4
4
  export { AuthorizationService as CommercetoolsAuthorizationService } from './types/authorization.type';
5
5
  export { OrderService as CommercetoolsOrderService } from './types/order.type';
6
6
  export { CommercetoolsClient } from './types/api.type';
7
7
  export { Cart, Order, OrderPagedQueryResponse, Payment, PaymentDraft, Money, LineItem, CustomLineItem, Address, Transaction, TransactionType, TransactionState, ShippingInfo, } from '@commercetools/platform-sdk';
8
+ export * as CurrencyConverters from './helpers/currency.converter';
@@ -1,2 +1,37 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.CurrencyConverters = void 0;
37
+ exports.CurrencyConverters = __importStar(require("./helpers/currency.converter"));
@@ -37,12 +37,14 @@ class DefaultCartService {
37
37
  cartAmount = {
38
38
  currencyCode: opts.cart.taxedPrice.totalGross.currencyCode,
39
39
  centAmount: opts.cart.taxedPrice.totalGross.centAmount,
40
+ fractionDigits: opts.cart.taxedPrice.totalGross.fractionDigits,
40
41
  };
41
42
  }
42
43
  else {
43
44
  cartAmount = {
44
45
  currencyCode: opts.cart.totalPrice.currencyCode,
45
46
  centAmount: opts.cart.totalPrice.centAmount,
47
+ fractionDigits: opts.cart.totalPrice.fractionDigits,
46
48
  };
47
49
  }
48
50
  if (paidAmount >= cartAmount.centAmount) {
@@ -58,6 +60,7 @@ class DefaultCartService {
58
60
  return {
59
61
  currencyCode: cartAmount.currencyCode,
60
62
  centAmount: cartAmount.centAmount - paidAmount,
63
+ fractionDigits: cartAmount.fractionDigits,
61
64
  };
62
65
  }
63
66
  }
@@ -4,6 +4,7 @@ import { Logger } from '../../logger';
4
4
  export type PaymentAmount = {
5
5
  centAmount: number;
6
6
  currencyCode: string;
7
+ fractionDigits: number;
7
8
  };
8
9
  export type PaymentServiceOptions = {
9
10
  ctAPI: CommercetoolsAPI;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercetools/connect-payments-sdk",
3
- "version": "0.13.0",
3
+ "version": "0.14.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": "22.37.0",
19
- "@commercetools/platform-sdk": "7.23.0",
19
+ "@commercetools/platform-sdk": "7.25.1",
20
20
  "@commercetools/sdk-client-v2": "2.5.0",
21
21
  "jsonwebtoken": "9.0.2",
22
22
  "jwks-rsa": "3.1.0",