@commercetools/connect-payments-sdk 0.18.0 → 0.19.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.19.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 16d7341: feat(sdk): add and export function to convert coco line item tax rate into minor units
8
+
9
+ ## 0.18.1
10
+
11
+ ### Patch Changes
12
+
13
+ - 1ec3a60: Read logging correlation id from session
14
+
3
15
  ## 0.18.0
4
16
 
5
17
  ### Minor Changes
@@ -16,8 +16,10 @@ class SessionHeaderAuthenticationHook {
16
16
  return async (request) => {
17
17
  const sessionIdAuthn = new security_1.HeaderBasedAuthentication(request.headers['x-session-id']);
18
18
  const authn = await this.authenticationManager.authenticate(sessionIdAuthn);
19
+ const correlationId = authn.getPrincipal().correlationId;
19
20
  this.contextProvider.updateContextData({
20
21
  authentication: authn,
22
+ ...(correlationId && { correlationId }),
21
23
  });
22
24
  };
23
25
  }
@@ -16,8 +16,10 @@ class SessionQueryParamAuthenticationHook {
16
16
  return async (request) => {
17
17
  const sessionIdAuthn = new security_1.QueryParamBasedAuthentication(request.query['ctsid']);
18
18
  const authn = await this.authenticationManager.authenticate(sessionIdAuthn);
19
+ const correlationId = authn.getPrincipal().correlationId;
19
20
  this.contextProvider.updateContextData({
20
21
  authentication: authn,
22
+ ...(correlationId && { correlationId }),
21
23
  });
22
24
  };
23
25
  }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Convert the CoCo tax percentage, for example found on "lineItems", which ranges from 0-1 as floating point numbers to the minor units.
3
+ *
4
+ * @example CoCo taxRate of 0.21, normalized is 21%, in minor-units = 2100
5
+ * @example CoCo taxRate of 0.07, normalized is 7%, in minor-units = 700
6
+ * @example CoCo taxRate of 0.489, normalized is 48.9%, in minor-units = 4891
7
+ *
8
+ * @param decimalTaxRate the tax rate expressed in decimals between 0-1 as floating point numbers taken from the CoCo taxRate (see docs below).
9
+ *
10
+ * @throw "Error" if the provided value is not between 0 and 1;
11
+ *
12
+ * @returns 0 if undefined is provided
13
+ *
14
+ * @see https://docs.commercetools.com/api/projects/taxCategories#ctp:api:type:TaxRate
15
+ */
16
+ declare const convertCoCoTaxPercentage: (decimalTaxRate?: number) => number;
17
+ export { convertCoCoTaxPercentage };
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertCoCoTaxPercentage = void 0;
4
+ /**
5
+ * Convert the CoCo tax percentage, for example found on "lineItems", which ranges from 0-1 as floating point numbers to the minor units.
6
+ *
7
+ * @example CoCo taxRate of 0.21, normalized is 21%, in minor-units = 2100
8
+ * @example CoCo taxRate of 0.07, normalized is 7%, in minor-units = 700
9
+ * @example CoCo taxRate of 0.489, normalized is 48.9%, in minor-units = 4891
10
+ *
11
+ * @param decimalTaxRate the tax rate expressed in decimals between 0-1 as floating point numbers taken from the CoCo taxRate (see docs below).
12
+ *
13
+ * @throw "Error" if the provided value is not between 0 and 1;
14
+ *
15
+ * @returns 0 if undefined is provided
16
+ *
17
+ * @see https://docs.commercetools.com/api/projects/taxCategories#ctp:api:type:TaxRate
18
+ */
19
+ const convertCoCoTaxPercentage = (decimalTaxRate) => {
20
+ if (!decimalTaxRate) {
21
+ return 0;
22
+ }
23
+ if (decimalTaxRate < 0 || decimalTaxRate > 1) {
24
+ throw new Error(`The provided decimal tax rate of ${decimalTaxRate} is invalid. Only allowed values between 0 and 1.0`);
25
+ }
26
+ // First go from the range of 0 - 1 decimal value, to value expressed as a "normal" percentage. I.e. 0.15% from CoCo becomes 15.
27
+ const taxRateNormalized = decimalTaxRate * Math.pow(10, 2);
28
+ // Apply conversion to make it a minor unit "minor-units" format. I.e. 15 becomes 1500.
29
+ const taxRateMinorUnitsFormat = taxRateNormalized * Math.pow(10, 2);
30
+ const taxRateMinorUnit = taxRateMinorUnitsFormat.toFixed(0);
31
+ return parseInt(taxRateMinorUnit);
32
+ };
33
+ exports.convertCoCoTaxPercentage = convertCoCoTaxPercentage;
@@ -6,3 +6,4 @@ 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
8
  export * as CurrencyConverters from './helpers/currency.converter';
9
+ export * as TaxRateConverter from './helpers/taxrate.converter';
@@ -33,5 +33,6 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.CurrencyConverters = void 0;
36
+ exports.TaxRateConverter = exports.CurrencyConverters = void 0;
37
37
  exports.CurrencyConverters = __importStar(require("./helpers/currency.converter"));
38
+ exports.TaxRateConverter = __importStar(require("./helpers/taxrate.converter"));
@@ -22,5 +22,6 @@ export declare class DefaultSessionService implements SessionService {
22
22
  getMerchantReturnUrlFromSession(session: Session): string | undefined;
23
23
  getFutureOrderNumberFromSession(session: Session): string | undefined;
24
24
  getGiftCardPlannedAmountFromSession(session: Session): Money | undefined;
25
+ getCorrelationIdFromSession(session: Session): string | undefined;
25
26
  private getSession;
26
27
  }
@@ -78,6 +78,9 @@ class DefaultSessionService {
78
78
  getGiftCardPlannedAmountFromSession(session) {
79
79
  return session.metadata?.giftCardPlannedAmount;
80
80
  }
81
+ getCorrelationIdFromSession(session) {
82
+ return session.metadata?.correlationId;
83
+ }
81
84
  async getSession(sessionId) {
82
85
  return await fetch(`${this.sessionUrl}/${this.projectKey}/sessions/${sessionId}`, {
83
86
  method: 'GET',
@@ -31,4 +31,5 @@ export interface SessionService {
31
31
  getMerchantReturnUrlFromSession(session: Session): string | undefined;
32
32
  getFutureOrderNumberFromSession(session: Session): string | undefined;
33
33
  getGiftCardPlannedAmountFromSession(session: Session): Money | undefined;
34
+ getCorrelationIdFromSession(session: Session): string | undefined;
34
35
  }
@@ -22,6 +22,7 @@ class SessionHeaderAuthenticationManager {
22
22
  merchantReturnUrl: this.sessionService.getMerchantReturnUrlFromSession(session),
23
23
  futureOrderNumber: this.sessionService.getFutureOrderNumberFromSession(session),
24
24
  giftCardPlannedAmount: this.sessionService.getGiftCardPlannedAmountFromSession(session),
25
+ correlationId: this.sessionService.getCorrelationIdFromSession(session),
25
26
  });
26
27
  }
27
28
  catch (e) {
@@ -24,6 +24,7 @@ export type SessionPrincipal = {
24
24
  merchantReturnUrl?: string;
25
25
  futureOrderNumber?: string;
26
26
  giftCardPlannedAmount?: Money;
27
+ correlationId?: string;
27
28
  };
28
29
  export type Oauth2Principal = {
29
30
  clientId: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commercetools/connect-payments-sdk",
3
- "version": "0.18.0",
3
+ "version": "0.19.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.39.0",
18
+ "@commercetools-backend/loggers": "22.41.0",
19
19
  "@commercetools/platform-sdk": "8.1.0",
20
20
  "@commercetools/sdk-client-v2": "2.5.0",
21
21
  "jsonwebtoken": "9.0.2",