@unchainedshop/plugins 5.0.0-alpha.2 → 5.0.0-alpha.3

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.
Files changed (99) hide show
  1. package/lib/events/aws-eventbridge.d.ts +7 -1
  2. package/lib/events/aws-eventbridge.js +5 -11
  3. package/lib/events/redis.d.ts +3 -1
  4. package/lib/events/redis.js +5 -5
  5. package/lib/files/gridfs/api.js +1 -1
  6. package/lib/files/gridfs/handler-express.js +1 -1
  7. package/lib/payment/datatrans-v2/adapter.js +12 -9
  8. package/lib/payment/datatrans-v2/build3DSCardholder.d.ts +12 -0
  9. package/lib/payment/datatrans-v2/build3DSCardholder.js +44 -0
  10. package/lib/payment/paypal-checkout.js +17 -5
  11. package/lib/payment/stripe/stripe.js +1 -1
  12. package/lib/presets/base.js +4 -0
  13. package/lib/presets/countries/eu.d.ts +1 -0
  14. package/lib/presets/countries/eu.js +7 -0
  15. package/lib/presets/countries/uk.d.ts +1 -0
  16. package/lib/presets/countries/uk.js +7 -0
  17. package/lib/presets/countries/us.d.ts +1 -0
  18. package/lib/presets/countries/us.js +7 -0
  19. package/lib/pricing/delivery-eu-tax/adapter.d.ts +3 -0
  20. package/lib/pricing/delivery-eu-tax/adapter.js +55 -0
  21. package/lib/pricing/delivery-eu-tax/index.d.ts +4 -0
  22. package/lib/pricing/delivery-eu-tax/index.js +10 -0
  23. package/lib/pricing/delivery-eu-tax.d.ts +2 -0
  24. package/lib/pricing/delivery-eu-tax.js +55 -0
  25. package/lib/pricing/delivery-swiss-tax/adapter.js +7 -26
  26. package/lib/pricing/delivery-swiss-tax.js +7 -26
  27. package/lib/pricing/delivery-uk-tax/adapter.d.ts +3 -0
  28. package/lib/pricing/delivery-uk-tax/adapter.js +43 -0
  29. package/lib/pricing/delivery-uk-tax/index.d.ts +4 -0
  30. package/lib/pricing/delivery-uk-tax/index.js +10 -0
  31. package/lib/pricing/delivery-uk-tax.d.ts +2 -0
  32. package/lib/pricing/delivery-uk-tax.js +43 -0
  33. package/lib/pricing/delivery-us-sales-tax/adapter.d.ts +3 -0
  34. package/lib/pricing/delivery-us-sales-tax/adapter.js +58 -0
  35. package/lib/pricing/delivery-us-sales-tax/index.d.ts +4 -0
  36. package/lib/pricing/delivery-us-sales-tax/index.js +10 -0
  37. package/lib/pricing/delivery-us-sales-tax.d.ts +2 -0
  38. package/lib/pricing/delivery-us-sales-tax.js +58 -0
  39. package/lib/pricing/product-eu-tax/adapter.d.ts +3 -0
  40. package/lib/pricing/product-eu-tax/adapter.js +56 -0
  41. package/lib/pricing/product-eu-tax/index.d.ts +4 -0
  42. package/lib/pricing/product-eu-tax/index.js +10 -0
  43. package/lib/pricing/product-eu-tax.d.ts +2 -0
  44. package/lib/pricing/product-eu-tax.js +56 -0
  45. package/lib/pricing/product-quotation-price.d.ts +2 -0
  46. package/lib/pricing/product-quotation-price.js +40 -0
  47. package/lib/pricing/product-swiss-tax/adapter.js +7 -26
  48. package/lib/pricing/product-swiss-tax.js +7 -26
  49. package/lib/pricing/product-uk-tax/adapter.d.ts +3 -0
  50. package/lib/pricing/product-uk-tax/adapter.js +57 -0
  51. package/lib/pricing/product-uk-tax/index.d.ts +4 -0
  52. package/lib/pricing/product-uk-tax/index.js +10 -0
  53. package/lib/pricing/product-uk-tax.d.ts +2 -0
  54. package/lib/pricing/product-uk-tax.js +57 -0
  55. package/lib/pricing/product-us-sales-tax/adapter.d.ts +3 -0
  56. package/lib/pricing/product-us-sales-tax/adapter.js +52 -0
  57. package/lib/pricing/product-us-sales-tax/index.d.ts +4 -0
  58. package/lib/pricing/product-us-sales-tax/index.js +10 -0
  59. package/lib/pricing/product-us-sales-tax.d.ts +2 -0
  60. package/lib/pricing/product-us-sales-tax.js +52 -0
  61. package/lib/pricing/tax/applyTaxRateToTaxableRows.d.ts +23 -0
  62. package/lib/pricing/tax/applyTaxRateToTaxableRows.js +30 -0
  63. package/lib/pricing/tax/ch-tax-rates.json +24 -0
  64. package/lib/pricing/tax/ch.js +6 -30
  65. package/lib/pricing/tax/eraRates.d.ts +10 -0
  66. package/lib/pricing/tax/eraRates.js +15 -0
  67. package/lib/pricing/tax/eu-tax-rates.json +797 -0
  68. package/lib/pricing/tax/eu.d.ts +12 -0
  69. package/lib/pricing/tax/eu.js +38 -0
  70. package/lib/pricing/tax/uk-tax-rates.json +18 -0
  71. package/lib/pricing/tax/uk.d.ts +10 -0
  72. package/lib/pricing/tax/uk.js +23 -0
  73. package/lib/pricing/tax/us-tax-rates.json +334 -0
  74. package/lib/pricing/tax/us.d.ts +11 -0
  75. package/lib/pricing/tax/us.js +20 -0
  76. package/lib/pricing/utils/isDeliveryAddressInCountry.d.ts +1 -1
  77. package/lib/pricing/utils/isDeliveryAddressInCountry.js +3 -8
  78. package/lib/pricing/utils/resolveDeliveryLocation.d.ts +10 -0
  79. package/lib/pricing/utils/resolveDeliveryLocation.js +9 -0
  80. package/lib/quotations/manual/adapter.js +6 -1
  81. package/lib/quotations/manual.js +6 -1
  82. package/lib/worker/gc-guests/adapter.d.ts +9 -0
  83. package/lib/worker/gc-guests/adapter.js +56 -0
  84. package/lib/worker/gc-guests/index.d.ts +4 -0
  85. package/lib/worker/gc-guests/index.js +13 -0
  86. package/lib/worker/gc-guests.d.ts +8 -0
  87. package/lib/worker/gc-guests.js +55 -0
  88. package/lib/worker/invalidate-carts/adapter.d.ts +9 -0
  89. package/lib/worker/invalidate-carts/adapter.js +52 -0
  90. package/lib/worker/invalidate-carts/index.d.ts +4 -0
  91. package/lib/worker/invalidate-carts/index.js +13 -0
  92. package/lib/worker/invalidate-carts.d.ts +8 -0
  93. package/lib/worker/invalidate-carts.js +51 -0
  94. package/lib/worker/zombie-killer/adapter.d.ts +2 -0
  95. package/lib/worker/zombie-killer/adapter.js +26 -3
  96. package/lib/worker/zombie-killer/index.js +4 -1
  97. package/lib/worker/zombie-killer.d.ts +1 -0
  98. package/lib/worker/zombie-killer.js +24 -3
  99. package/package.json +24 -7
@@ -0,0 +1,52 @@
1
+ import { ProductPricingAdapter, ProductPricingRowCategory, } from '@unchainedshop/core';
2
+ import { US_COUNTRY_CODE, isProductExemptFromUsSalesTax, resolveUsSalesTaxRate } from "../tax/us.js";
3
+ import resolveDeliveryLocation from "../utils/resolveDeliveryLocation.js";
4
+ import { applyTaxRateToTaxableRows } from "../tax/applyTaxRateToTaxableRows.js";
5
+ export const ProductUsSalesTax = {
6
+ ...ProductPricingAdapter,
7
+ key: 'shop.unchained.pricing.product-us-sales-tax',
8
+ version: '1.0.0',
9
+ label: 'Apply US State Sales Tax on Product',
10
+ orderIndex: 80,
11
+ isActivatedFor: () => {
12
+ return true;
13
+ },
14
+ actions: (params) => {
15
+ const pricingAdapter = ProductPricingAdapter.actions(params);
16
+ const { context } = params;
17
+ return {
18
+ ...pricingAdapter,
19
+ calculate: async () => {
20
+ if (isProductExemptFromUsSalesTax(context.product)) {
21
+ return pricingAdapter.calculate();
22
+ }
23
+ const orderDelivery = context.order?.deliveryId
24
+ ? await context.modules.orders.deliveries.findDelivery({
25
+ orderDeliveryId: context.order?.deliveryId,
26
+ })
27
+ : null;
28
+ const { countryCode, regionCode } = resolveDeliveryLocation({ ...context, orderDelivery });
29
+ if (countryCode !== US_COUNTRY_CODE)
30
+ return pricingAdapter.calculate();
31
+ const taxRate = resolveUsSalesTaxRate({
32
+ regionCode,
33
+ referenceDate: context.order?.ordered,
34
+ });
35
+ if (taxRate === null) {
36
+ ProductPricingAdapter.log(`ProductUsSalesTax -> unknown state '${regionCode}', no sales tax applied`);
37
+ return pricingAdapter.calculate();
38
+ }
39
+ ProductPricingAdapter.log(`ProductUsSalesTax -> Tax Multiplicator: ${taxRate}`);
40
+ applyTaxRateToTaxableRows({
41
+ calculationSheet: params.calculationSheet,
42
+ resultSheet: pricingAdapter.resultSheet(),
43
+ taxRate,
44
+ baseCategory: ProductPricingRowCategory.Item,
45
+ adapterKey: ProductUsSalesTax.key,
46
+ });
47
+ return pricingAdapter.calculate();
48
+ },
49
+ };
50
+ },
51
+ };
52
+ export default ProductUsSalesTax;
@@ -0,0 +1,4 @@
1
+ import { type IPlugin } from '@unchainedshop/core';
2
+ export declare const ProductUsSalesTaxPlugin: IPlugin;
3
+ export default ProductUsSalesTaxPlugin;
4
+ export { ProductUsSalesTax } from './adapter.ts';
@@ -0,0 +1,10 @@
1
+ import {} from '@unchainedshop/core';
2
+ import { ProductUsSalesTax } from "./adapter.js";
3
+ export const ProductUsSalesTaxPlugin = {
4
+ key: 'shop.unchained.pricing.product-us-sales-tax',
5
+ label: 'Product US Sales Tax Plugin',
6
+ version: '1.0.0',
7
+ adapters: [ProductUsSalesTax],
8
+ };
9
+ export default ProductUsSalesTaxPlugin;
10
+ export { ProductUsSalesTax } from "./adapter.js";
@@ -0,0 +1,2 @@
1
+ import { type IProductPricingAdapter } from '@unchainedshop/core';
2
+ export declare const ProductUsSalesTax: IProductPricingAdapter;
@@ -0,0 +1,52 @@
1
+ import { ProductPricingDirector, ProductPricingAdapter, ProductPricingRowCategory, } from '@unchainedshop/core';
2
+ import { US_COUNTRY_CODE, isProductExemptFromUsSalesTax, resolveUsSalesTaxRate } from "./tax/us.js";
3
+ import resolveDeliveryLocation from "./utils/resolveDeliveryLocation.js";
4
+ import { applyTaxRateToTaxableRows } from "./tax/applyTaxRateToTaxableRows.js";
5
+ export const ProductUsSalesTax = {
6
+ ...ProductPricingAdapter,
7
+ key: 'shop.unchained.pricing.product-us-sales-tax',
8
+ version: '1.0.0',
9
+ label: 'Apply US State Sales Tax on Product',
10
+ orderIndex: 80,
11
+ isActivatedFor: () => {
12
+ return true;
13
+ },
14
+ actions: (params) => {
15
+ const pricingAdapter = ProductPricingAdapter.actions(params);
16
+ const { context } = params;
17
+ return {
18
+ ...pricingAdapter,
19
+ calculate: async () => {
20
+ if (isProductExemptFromUsSalesTax(context.product)) {
21
+ return pricingAdapter.calculate();
22
+ }
23
+ const orderDelivery = context.order?.deliveryId
24
+ ? await context.modules.orders.deliveries.findDelivery({
25
+ orderDeliveryId: context.order?.deliveryId,
26
+ })
27
+ : null;
28
+ const { countryCode, regionCode } = resolveDeliveryLocation({ ...context, orderDelivery });
29
+ if (countryCode !== US_COUNTRY_CODE)
30
+ return pricingAdapter.calculate();
31
+ const taxRate = resolveUsSalesTaxRate({
32
+ regionCode,
33
+ referenceDate: context.order?.ordered,
34
+ });
35
+ if (taxRate === null) {
36
+ ProductPricingAdapter.log(`ProductUsSalesTax -> unknown state '${regionCode}', no sales tax applied`);
37
+ return pricingAdapter.calculate();
38
+ }
39
+ ProductPricingAdapter.log(`ProductUsSalesTax -> Tax Multiplicator: ${taxRate}`);
40
+ applyTaxRateToTaxableRows({
41
+ calculationSheet: params.calculationSheet,
42
+ resultSheet: pricingAdapter.resultSheet(),
43
+ taxRate,
44
+ baseCategory: ProductPricingRowCategory.Item,
45
+ adapterKey: ProductUsSalesTax.key,
46
+ });
47
+ return pricingAdapter.calculate();
48
+ },
49
+ };
50
+ },
51
+ };
52
+ ProductPricingDirector.registerAdapter(ProductUsSalesTax);
@@ -0,0 +1,23 @@
1
+ import type { PricingCalculation } from '@unchainedshop/utils';
2
+ export interface TaxableCalculation extends PricingCalculation {
3
+ isTaxable?: boolean;
4
+ isNetPrice?: boolean;
5
+ }
6
+ export interface TaxResultSheet {
7
+ calculation: PricingCalculation[];
8
+ addTax: (row: {
9
+ amount: number;
10
+ rate: number;
11
+ baseCategory: string;
12
+ meta?: unknown;
13
+ }) => void;
14
+ }
15
+ export declare const applyTaxRateToTaxableRows: <Calculation extends TaxableCalculation>({ calculationSheet, resultSheet, taxRate, baseCategory, adapterKey, }: {
16
+ calculationSheet: {
17
+ filterBy: (filter?: Partial<Calculation>) => Calculation[];
18
+ };
19
+ resultSheet: TaxResultSheet;
20
+ taxRate: number;
21
+ baseCategory: string;
22
+ adapterKey: string;
23
+ }) => void;
@@ -0,0 +1,30 @@
1
+ export const applyTaxRateToTaxableRows = ({ calculationSheet, resultSheet, taxRate, baseCategory, adapterKey, }) => {
2
+ calculationSheet
3
+ .filterBy({ isTaxable: true })
4
+ .forEach(({ isNetPrice, ...row }) => {
5
+ if (!isNetPrice) {
6
+ const taxAmount = row.amount - row.amount / (1 + taxRate);
7
+ resultSheet.calculation.push({
8
+ ...row,
9
+ amount: -taxAmount,
10
+ isTaxable: false,
11
+ isNetPrice: false,
12
+ meta: { adapter: adapterKey },
13
+ });
14
+ resultSheet.addTax({
15
+ amount: taxAmount,
16
+ rate: taxRate,
17
+ baseCategory,
18
+ meta: { adapter: adapterKey },
19
+ });
20
+ }
21
+ else {
22
+ resultSheet.addTax({
23
+ amount: row.amount * taxRate,
24
+ rate: taxRate,
25
+ baseCategory,
26
+ meta: { adapter: adapterKey },
27
+ });
28
+ }
29
+ });
30
+ };
@@ -0,0 +1,24 @@
1
+ {
2
+ "source": "https://www.estv.admin.ch/estv/en/home/value-added-tax/vat-rates-switzerland.html",
3
+ "verifiedAt": "2026-08-20",
4
+ "timezone": "+01:00",
5
+ "categories": {
6
+ "default": [
7
+ { "validFrom": "2001-01-01", "rate": 0.076 },
8
+ { "validFrom": "2011-01-01", "rate": 0.08 },
9
+ { "validFrom": "2018-01-01", "rate": 0.077 },
10
+ { "validFrom": "2024-01-01", "rate": 0.081 }
11
+ ],
12
+ "reduced": [
13
+ { "validFrom": "2001-01-01", "rate": 0.024 },
14
+ { "validFrom": "2011-01-01", "rate": 0.025 },
15
+ { "validFrom": "2024-01-01", "rate": 0.026 }
16
+ ],
17
+ "special": [
18
+ { "validFrom": "2001-01-01", "rate": 0.036 },
19
+ { "validFrom": "2011-01-01", "rate": 0.038 },
20
+ { "validFrom": "2018-01-01", "rate": 0.037 },
21
+ { "validFrom": "2024-01-01", "rate": 0.038 }
22
+ ]
23
+ }
24
+ }
@@ -1,33 +1,9 @@
1
- const startOf2024 = new Date('2024-01-01T00:00:00.000+0100');
2
- export const SwissTaxCategories = {
3
- DEFAULT: {
4
- value: 'default',
5
- rate: (referenceDate = new Date()) => {
6
- if (referenceDate.getTime() < startOf2024.getTime()) {
7
- return 0.077;
8
- }
9
- return 0.081;
10
- },
11
- },
12
- REDUCED: {
13
- value: 'reduced',
14
- rate: (referenceDate = new Date()) => {
15
- if (referenceDate.getTime() < startOf2024.getTime()) {
16
- return 0.025;
17
- }
18
- return 0.026;
19
- },
20
- },
21
- SPECIAL: {
22
- value: 'special',
23
- rate: (referenceDate = new Date()) => {
24
- if (referenceDate.getTime() < startOf2024.getTime()) {
25
- return 0.037;
26
- }
27
- return 0.038;
28
- },
29
- },
30
- };
1
+ import swissTaxRates from './ch-tax-rates.json' with { type: 'json' };
2
+ import { compileEraRates, rateForDate } from "./eraRates.js";
3
+ export const SwissTaxCategories = Object.fromEntries(Object.entries(swissTaxRates.categories).map(([value, eras]) => [
4
+ value.toUpperCase(),
5
+ { value, rate: rateForDate(compileEraRates(eras, swissTaxRates.timezone)) },
6
+ ]));
31
7
  export const resolveTaxCategoryFromDeliveryProvider = (provider) => {
32
8
  const taxCategoryFromProvider = provider?.configuration
33
9
  ?.find(({ key }) => {
@@ -0,0 +1,10 @@
1
+ export interface TaxRateEra {
2
+ validFrom: string;
3
+ rate: number;
4
+ }
5
+ export interface CompiledTaxRateEra {
6
+ validFrom: number;
7
+ rate: number;
8
+ }
9
+ export declare const compileEraRates: (eras: TaxRateEra[], utcOffset: string) => CompiledTaxRateEra[];
10
+ export declare const rateForDate: (eras: CompiledTaxRateEra[]) => (referenceDate?: Date) => number;
@@ -0,0 +1,15 @@
1
+ export const compileEraRates = (eras, utcOffset) => eras
2
+ .map(({ validFrom, rate }) => ({
3
+ validFrom: new Date(`${validFrom}T00:00:00.000${utcOffset}`).getTime(),
4
+ rate,
5
+ }))
6
+ .sort((left, right) => left.validFrom - right.validFrom);
7
+ export const rateForDate = (eras) => (referenceDate = new Date()) => {
8
+ const time = referenceDate.getTime();
9
+ let applicable = eras[0];
10
+ for (const era of eras) {
11
+ if (era.validFrom <= time)
12
+ applicable = era;
13
+ }
14
+ return applicable.rate;
15
+ };