@vicacha-devs/medusa-fulfillment-eu 0.0.0-stage → 0.1.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/README.md +84 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/providers/eu-fulfillment/index.d.ts +2 -0
- package/dist/providers/eu-fulfillment/index.js +5 -0
- package/dist/providers/eu-fulfillment/service.d.ts +48 -0
- package/dist/providers/eu-fulfillment/service.js +111 -0
- package/package.json +52 -3
- package/src/__tests__/eu-fulfillment.spec.ts +84 -0
- package/src/__tests__/medusa-shim.mjs +6 -0
- package/src/__tests__/register-ts-resolver.mjs +32 -0
- package/src/index.ts +9 -0
- package/src/providers/eu-fulfillment/index.ts +6 -0
- package/src/providers/eu-fulfillment/service.ts +195 -0
- package/tsconfig.json +16 -0
package/README.md
CHANGED
|
@@ -1,3 +1,85 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@vicacha-devs/medusa-fulfillment-eu`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
EU & Benelux fulfillment provider plugin for **Medusa v2**, supporting **PostNL Letterbox**, **Sendcloud EU Parcel**, and **Rotterdam LCL Pallet Freight** with configurable free-shipping thresholds by region and weight.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Key Features
|
|
8
|
+
|
|
9
|
+
- **3 Built-in Fulfillment Options**:
|
|
10
|
+
- `postnl-letterbox`: Low-cost letterbox parcel for lightweight shipments (`<= letterboxMaxGrams`).
|
|
11
|
+
- `sendcloud-eu-parcel`: Standard Benelux (`NL`, `BE`, `LU`) and pan-EU parcel delivery.
|
|
12
|
+
- `rotterdam-lcl-pallet`: Palletized LCL freight for heavy orders (`>= palletWeightGrams`) or bulk (`isGreenBulk`) shipments.
|
|
13
|
+
- **Smart Rate & Free-Shipping Calculator**: `calculateEuShippingQuote()` evaluates Benelux vs. EU thresholds, weight qualification (`freeWeightGrams`), and remaining amount needed for free shipping.
|
|
14
|
+
- **SSRF-Safe & Offline Tested**: Pure TypeScript fulfillment provider (`AbstractFulfillmentProviderService`) with unit tests.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Installation & Configuration
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pnpm add @vicacha-devs/medusa-fulfillment-eu
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Register the fulfillment module provider in `medusa-config.ts`:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { defineConfig } from "@medusajs/framework/utils"
|
|
28
|
+
|
|
29
|
+
export default defineConfig({
|
|
30
|
+
plugins: [
|
|
31
|
+
{
|
|
32
|
+
resolve: "@vicacha-devs/medusa-fulfillment-eu",
|
|
33
|
+
options: {},
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
modules: [
|
|
37
|
+
{
|
|
38
|
+
resolve: "@medusajs/medusa/fulfillment",
|
|
39
|
+
options: {
|
|
40
|
+
providers: [
|
|
41
|
+
{
|
|
42
|
+
resolve:
|
|
43
|
+
"@vicacha-devs/medusa-fulfillment-eu/providers/eu-fulfillment",
|
|
44
|
+
id: "eu-fulfillment",
|
|
45
|
+
options: {
|
|
46
|
+
beneluxFreeThresholdEur: 25,
|
|
47
|
+
euFreeThresholdEur: 50,
|
|
48
|
+
freeWeightGrams: 1000,
|
|
49
|
+
palletWeightGrams: 24000,
|
|
50
|
+
beneluxPalletEur: 45,
|
|
51
|
+
euPalletEur: 85,
|
|
52
|
+
letterboxRateEur: 4.25,
|
|
53
|
+
beneluxParcelRateEur: 6.95,
|
|
54
|
+
euParcelRateEur: 12.95,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
})
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Options Reference (`EuFulfillmentOptions`)
|
|
67
|
+
|
|
68
|
+
| Option | Default | Description |
|
|
69
|
+
| :----------------------------------------- | :--------------- | :------------------------------------------------------------- |
|
|
70
|
+
| `beneluxFreeThresholdEur` | `25` | Free-shipping subtotal threshold (EUR) for `NL`, `BE`, `LU` |
|
|
71
|
+
| `euFreeThresholdEur` | `50` | Free-shipping subtotal threshold (EUR) for other EU countries |
|
|
72
|
+
| `freeWeightGrams` | `1000` | Weight threshold (grams) qualifying for free standard shipping |
|
|
73
|
+
| `palletWeightGrams` | `24000` | Weight threshold (grams) triggering `rotterdam-lcl-pallet` |
|
|
74
|
+
| `beneluxPalletEur` / `euPalletEur` | `45` / `85` | Pallet freight rate (EUR) |
|
|
75
|
+
| `letterboxRateEur` | `4.25` | PostNL letterbox rate (EUR) |
|
|
76
|
+
| `beneluxParcelRateEur` / `euParcelRateEur` | `6.95` / `12.95` | Standard parcel rate (EUR) |
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Development & Testing
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pnpm --filter @vicacha-devs/medusa-fulfillment-eu test
|
|
84
|
+
pnpm --filter @vicacha-devs/medusa-fulfillment-eu typecheck
|
|
85
|
+
```
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { calculateEuShippingQuote, EuFulfillmentProviderService, } from "./providers/eu-fulfillment/service.js";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { AbstractFulfillmentProviderService } from "@medusajs/framework/utils";
|
|
2
|
+
import type { CalculatedShippingOptionPrice, CreateFulfillmentResult, FulfillmentOption } from "@medusajs/framework/types";
|
|
3
|
+
export interface EuFulfillmentOptions {
|
|
4
|
+
beneluxFreeThresholdEur?: number;
|
|
5
|
+
euFreeThresholdEur?: number;
|
|
6
|
+
freeWeightGrams?: number;
|
|
7
|
+
palletWeightGrams?: number;
|
|
8
|
+
beneluxPalletEur?: number;
|
|
9
|
+
euPalletEur?: number;
|
|
10
|
+
letterboxRateEur?: number;
|
|
11
|
+
letterboxMaxGrams?: number;
|
|
12
|
+
beneluxParcelRateEur?: number;
|
|
13
|
+
euParcelRateEur?: number;
|
|
14
|
+
defaultItemWeightGrams?: number;
|
|
15
|
+
carrier?: string;
|
|
16
|
+
dispatchedFrom?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface ShippingQuoteInput {
|
|
19
|
+
countryCode: string;
|
|
20
|
+
subtotalEur: number;
|
|
21
|
+
totalWeightGrams: number;
|
|
22
|
+
isGreenBulk: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface ShippingQuoteResult {
|
|
25
|
+
optionId: "postnl-letterbox" | "sendcloud-eu-parcel" | "rotterdam-lcl-pallet";
|
|
26
|
+
priceEur: number;
|
|
27
|
+
freeShippingApplied: boolean;
|
|
28
|
+
amountRemainingForFreeEur: number;
|
|
29
|
+
freeThresholdEur: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Pure EU & Benelux shipping rate calculator supporting PostNL letterbox,
|
|
33
|
+
* Sendcloud EU parcel, free-shipping weight/value thresholds, and LCL pallet freight.
|
|
34
|
+
*/
|
|
35
|
+
export declare function calculateEuShippingQuote(input: ShippingQuoteInput, options?: EuFulfillmentOptions): ShippingQuoteResult;
|
|
36
|
+
export declare class EuFulfillmentProviderService extends AbstractFulfillmentProviderService {
|
|
37
|
+
static identifier: string;
|
|
38
|
+
protected readonly options_: EuFulfillmentOptions;
|
|
39
|
+
constructor(_container: Record<string, unknown>, options?: EuFulfillmentOptions);
|
|
40
|
+
getFulfillmentOptions(): Promise<FulfillmentOption[]>;
|
|
41
|
+
validateFulfillmentData(_optionData: Record<string, unknown>, data: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
42
|
+
validateOption(_data: Record<string, unknown>): Promise<boolean>;
|
|
43
|
+
canCalculate(): Promise<boolean>;
|
|
44
|
+
calculatePrice(optionData: Record<string, unknown>, _data: Record<string, unknown>, context: Record<string, unknown>): Promise<CalculatedShippingOptionPrice>;
|
|
45
|
+
createFulfillment(): Promise<CreateFulfillmentResult>;
|
|
46
|
+
cancelFulfillment(): Promise<Record<string, unknown>>;
|
|
47
|
+
createReturnFulfillment(): Promise<CreateFulfillmentResult>;
|
|
48
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { AbstractFulfillmentProviderService } from "@medusajs/framework/utils";
|
|
2
|
+
const BENELUX = new Set(["NL", "BE", "LU"]);
|
|
3
|
+
/**
|
|
4
|
+
* Pure EU & Benelux shipping rate calculator supporting PostNL letterbox,
|
|
5
|
+
* Sendcloud EU parcel, free-shipping weight/value thresholds, and LCL pallet freight.
|
|
6
|
+
*/
|
|
7
|
+
export function calculateEuShippingQuote(input, options = {}) {
|
|
8
|
+
const country = input.countryCode.toUpperCase();
|
|
9
|
+
const isBenelux = BENELUX.has(country);
|
|
10
|
+
const palletWeightGrams = options.palletWeightGrams ?? 24000;
|
|
11
|
+
if (input.isGreenBulk || input.totalWeightGrams >= palletWeightGrams) {
|
|
12
|
+
const palletPrice = isBenelux
|
|
13
|
+
? (options.beneluxPalletEur ?? 45)
|
|
14
|
+
: (options.euPalletEur ?? 85);
|
|
15
|
+
return {
|
|
16
|
+
optionId: "rotterdam-lcl-pallet",
|
|
17
|
+
priceEur: palletPrice,
|
|
18
|
+
freeShippingApplied: false,
|
|
19
|
+
amountRemainingForFreeEur: 0,
|
|
20
|
+
freeThresholdEur: 0,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const freeThresholdEur = isBenelux
|
|
24
|
+
? (options.beneluxFreeThresholdEur ?? 25)
|
|
25
|
+
: (options.euFreeThresholdEur ?? 50);
|
|
26
|
+
const freeWeightGrams = options.freeWeightGrams ?? 1000;
|
|
27
|
+
const qualifiesByWeight = input.totalWeightGrams >= freeWeightGrams;
|
|
28
|
+
const qualifiesByValue = input.subtotalEur >= freeThresholdEur;
|
|
29
|
+
const freeShippingApplied = qualifiesByWeight || qualifiesByValue;
|
|
30
|
+
const letterboxMaxGrams = options.letterboxMaxGrams ?? 1500;
|
|
31
|
+
const optionId = isBenelux && input.totalWeightGrams <= letterboxMaxGrams
|
|
32
|
+
? "postnl-letterbox"
|
|
33
|
+
: "sendcloud-eu-parcel";
|
|
34
|
+
const baseRate = optionId === "postnl-letterbox"
|
|
35
|
+
? (options.letterboxRateEur ?? 4.25)
|
|
36
|
+
: isBenelux
|
|
37
|
+
? (options.beneluxParcelRateEur ?? 5.95)
|
|
38
|
+
: (options.euParcelRateEur ?? 8.95);
|
|
39
|
+
return {
|
|
40
|
+
optionId,
|
|
41
|
+
priceEur: freeShippingApplied ? 0 : baseRate,
|
|
42
|
+
freeShippingApplied,
|
|
43
|
+
amountRemainingForFreeEur: freeShippingApplied
|
|
44
|
+
? 0
|
|
45
|
+
: Number(Math.max(0, freeThresholdEur - input.subtotalEur).toFixed(2)),
|
|
46
|
+
freeThresholdEur,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export class EuFulfillmentProviderService extends AbstractFulfillmentProviderService {
|
|
50
|
+
static identifier = "eu-fulfillment";
|
|
51
|
+
options_;
|
|
52
|
+
constructor(_container, options = {}) {
|
|
53
|
+
super();
|
|
54
|
+
this.options_ = options;
|
|
55
|
+
}
|
|
56
|
+
async getFulfillmentOptions() {
|
|
57
|
+
return [
|
|
58
|
+
{ id: "postnl-letterbox" },
|
|
59
|
+
{ id: "sendcloud-eu-parcel" },
|
|
60
|
+
{ id: "rotterdam-lcl-pallet" },
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
async validateFulfillmentData(_optionData, data) {
|
|
64
|
+
return data;
|
|
65
|
+
}
|
|
66
|
+
async validateOption(_data) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
async canCalculate() {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
async calculatePrice(optionData, _data, context) {
|
|
73
|
+
const countryCode = String(context.shipping_address
|
|
74
|
+
?.country_code ?? "NL");
|
|
75
|
+
const items = context.items ?? [];
|
|
76
|
+
const defaultWeight = this.options_.defaultItemWeightGrams ?? 250;
|
|
77
|
+
let totalWeightGrams = 0;
|
|
78
|
+
let subtotalEur = 0;
|
|
79
|
+
for (const item of items) {
|
|
80
|
+
const qty = Number(item.quantity ?? 1);
|
|
81
|
+
const weight = Number(item.variant?.weight ?? defaultWeight);
|
|
82
|
+
totalWeightGrams += weight * qty;
|
|
83
|
+
subtotalEur += Number(item.unit_price ?? 0) * qty;
|
|
84
|
+
}
|
|
85
|
+
const quote = calculateEuShippingQuote({
|
|
86
|
+
countryCode,
|
|
87
|
+
subtotalEur,
|
|
88
|
+
totalWeightGrams,
|
|
89
|
+
isGreenBulk: optionData.id === "rotterdam-lcl-pallet",
|
|
90
|
+
}, this.options_);
|
|
91
|
+
return {
|
|
92
|
+
calculated_amount: quote.priceEur,
|
|
93
|
+
is_calculated_price_tax_inclusive: true,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
async createFulfillment() {
|
|
97
|
+
return {
|
|
98
|
+
data: {
|
|
99
|
+
carrier: this.options_.carrier ?? "sendcloud-postnl",
|
|
100
|
+
dispatched_from: this.options_.dispatchedFrom ?? "Rotterdam Hub",
|
|
101
|
+
},
|
|
102
|
+
labels: [],
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async cancelFulfillment() {
|
|
106
|
+
return {};
|
|
107
|
+
}
|
|
108
|
+
async createReturnFulfillment() {
|
|
109
|
+
return { data: {}, labels: [] };
|
|
110
|
+
}
|
|
111
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vicacha-devs/medusa-fulfillment-eu",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "EU & Benelux fulfillment provider plugin for Medusa v2 (PostNL letterbox, Sendcloud EU parcel, and Rotterdam LCL pallet freight).",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"eu",
|
|
7
|
+
"medusa",
|
|
8
|
+
"medusa-plugin",
|
|
9
|
+
"medusa-plugin-fulfillment",
|
|
10
|
+
"medusa-plugin-integration",
|
|
11
|
+
"medusa-v2",
|
|
12
|
+
"plugin",
|
|
13
|
+
"postnl",
|
|
14
|
+
"sendcloud"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": "Vicacha Devs",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/Vicacha-Devs/medusa-plugins.git",
|
|
21
|
+
"directory": "packages/fulfillment-eu"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
".medusa/server",
|
|
25
|
+
"dist",
|
|
26
|
+
"src",
|
|
27
|
+
"tsconfig.json"
|
|
28
|
+
],
|
|
29
|
+
"type": "module",
|
|
30
|
+
"exports": {
|
|
31
|
+
"./package.json": "./package.json",
|
|
32
|
+
".": {
|
|
33
|
+
"import": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts"
|
|
35
|
+
},
|
|
36
|
+
"./providers/*": {
|
|
37
|
+
"import": "./dist/providers/*/index.js",
|
|
38
|
+
"types": "./dist/providers/*/index.d.ts"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@medusajs/framework": "2.23.0",
|
|
43
|
+
"@types/node": "^22.13.0",
|
|
44
|
+
"tsc-alias": "^1.9.2",
|
|
45
|
+
"typescript": "^5.7.3"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@medusajs/framework": "^2.0.0"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsc -p tsconfig.json && tsc-alias -p tsconfig.json --resolve-full-paths",
|
|
52
|
+
"typecheck": "tsc --noEmit",
|
|
53
|
+
"test": "node --import ./src/__tests__/register-ts-resolver.mjs --import ../plugin-testing/register-js-extension.mjs --test src/__tests__/*.spec.ts"
|
|
54
|
+
}
|
|
6
55
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import {
|
|
4
|
+
calculateEuShippingQuote,
|
|
5
|
+
EuFulfillmentProviderService,
|
|
6
|
+
} from "../providers/eu-fulfillment/service.ts"
|
|
7
|
+
|
|
8
|
+
describe("calculateEuShippingQuote", () => {
|
|
9
|
+
it("applies free PostNL letterbox shipping in Benelux at €25+", () => {
|
|
10
|
+
const quote = calculateEuShippingQuote({
|
|
11
|
+
countryCode: "NL",
|
|
12
|
+
subtotalEur: 27,
|
|
13
|
+
totalWeightGrams: 500,
|
|
14
|
+
isGreenBulk: false,
|
|
15
|
+
})
|
|
16
|
+
assert.equal(quote.freeShippingApplied, true)
|
|
17
|
+
assert.equal(quote.optionId, "postnl-letterbox")
|
|
18
|
+
assert.equal(quote.priceEur, 0)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it("charges EU parcel rate under €50 and <1kg, and unlocks free shipping at 1kg+", () => {
|
|
22
|
+
const under = calculateEuShippingQuote({
|
|
23
|
+
countryCode: "DE",
|
|
24
|
+
subtotalEur: 35,
|
|
25
|
+
totalWeightGrams: 500,
|
|
26
|
+
isGreenBulk: false,
|
|
27
|
+
})
|
|
28
|
+
assert.equal(under.freeShippingApplied, false)
|
|
29
|
+
assert.equal(under.amountRemainingForFreeEur, 15)
|
|
30
|
+
assert.equal(under.optionId, "sendcloud-eu-parcel")
|
|
31
|
+
assert.equal(under.priceEur, 8.95)
|
|
32
|
+
|
|
33
|
+
const oneKg = calculateEuShippingQuote({
|
|
34
|
+
countryCode: "DE",
|
|
35
|
+
subtotalEur: 34,
|
|
36
|
+
totalWeightGrams: 1000,
|
|
37
|
+
isGreenBulk: false,
|
|
38
|
+
})
|
|
39
|
+
assert.equal(oneKg.freeShippingApplied, true)
|
|
40
|
+
assert.equal(oneKg.priceEur, 0)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it("calculates Rotterdam LCL pallet freight for green bulk orders", () => {
|
|
44
|
+
const nlPallet = calculateEuShippingQuote({
|
|
45
|
+
countryCode: "NL",
|
|
46
|
+
subtotalEur: 686,
|
|
47
|
+
totalWeightGrams: 70000,
|
|
48
|
+
isGreenBulk: true,
|
|
49
|
+
})
|
|
50
|
+
assert.equal(nlPallet.optionId, "rotterdam-lcl-pallet")
|
|
51
|
+
assert.equal(nlPallet.priceEur, 45)
|
|
52
|
+
|
|
53
|
+
const dePallet = calculateEuShippingQuote({
|
|
54
|
+
countryCode: "DE",
|
|
55
|
+
subtotalEur: 686,
|
|
56
|
+
totalWeightGrams: 70000,
|
|
57
|
+
isGreenBulk: false,
|
|
58
|
+
})
|
|
59
|
+
assert.equal(dePallet.optionId, "rotterdam-lcl-pallet")
|
|
60
|
+
assert.equal(dePallet.priceEur, 85)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it("supports EuFulfillmentProviderService methods", async () => {
|
|
64
|
+
const svc = new EuFulfillmentProviderService(
|
|
65
|
+
{},
|
|
66
|
+
{ dispatchedFrom: "Rotterdam Port" }
|
|
67
|
+
)
|
|
68
|
+
const opts = await svc.getFulfillmentOptions()
|
|
69
|
+
assert.equal(opts.length, 3)
|
|
70
|
+
|
|
71
|
+
const price = await svc.calculatePrice(
|
|
72
|
+
{ id: "postnl-letterbox" },
|
|
73
|
+
{},
|
|
74
|
+
{
|
|
75
|
+
shipping_address: { country_code: "NL" },
|
|
76
|
+
items: [{ quantity: 1, variant: { weight: 250 }, unit_price: 13.5 }],
|
|
77
|
+
}
|
|
78
|
+
)
|
|
79
|
+
assert.equal(price.calculated_amount, 4.25)
|
|
80
|
+
|
|
81
|
+
const fulfillment = await svc.createFulfillment()
|
|
82
|
+
assert.equal(fulfillment.data.dispatched_from, "Rotterdam Port")
|
|
83
|
+
})
|
|
84
|
+
})
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { existsSync, statSync } from "node:fs"
|
|
2
|
+
import { registerHooks } from "node:module"
|
|
3
|
+
import { dirname, join, resolve as resolvePath } from "node:path"
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from "node:url"
|
|
5
|
+
|
|
6
|
+
const currentDir = dirname(fileURLToPath(import.meta.url))
|
|
7
|
+
const shimUrl = pathToFileURL(resolvePath(currentDir, "medusa-shim.mjs")).href
|
|
8
|
+
|
|
9
|
+
function resolveTs(base) {
|
|
10
|
+
for (const c of [base, `${base}.ts`, join(base, "index.ts")]) {
|
|
11
|
+
if (existsSync(c) && statSync(c).isFile()) return c
|
|
12
|
+
}
|
|
13
|
+
return null
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
registerHooks({
|
|
17
|
+
resolve(specifier, context, nextResolve) {
|
|
18
|
+
if (specifier.startsWith("@medusajs/")) {
|
|
19
|
+
return { url: shimUrl, shortCircuit: true }
|
|
20
|
+
}
|
|
21
|
+
if (
|
|
22
|
+
(specifier.startsWith("./") || specifier.startsWith("../")) &&
|
|
23
|
+
context.parentURL
|
|
24
|
+
) {
|
|
25
|
+
const matched = resolveTs(
|
|
26
|
+
resolvePath(dirname(fileURLToPath(context.parentURL)), specifier)
|
|
27
|
+
)
|
|
28
|
+
if (matched) return nextResolve(pathToFileURL(matched).href, context)
|
|
29
|
+
}
|
|
30
|
+
return nextResolve(specifier, context)
|
|
31
|
+
},
|
|
32
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { AbstractFulfillmentProviderService } from "@medusajs/framework/utils"
|
|
2
|
+
import type {
|
|
3
|
+
CalculatedShippingOptionPrice,
|
|
4
|
+
CreateFulfillmentResult,
|
|
5
|
+
FulfillmentOption,
|
|
6
|
+
} from "@medusajs/framework/types"
|
|
7
|
+
|
|
8
|
+
export interface EuFulfillmentOptions {
|
|
9
|
+
beneluxFreeThresholdEur?: number
|
|
10
|
+
euFreeThresholdEur?: number
|
|
11
|
+
freeWeightGrams?: number
|
|
12
|
+
palletWeightGrams?: number
|
|
13
|
+
beneluxPalletEur?: number
|
|
14
|
+
euPalletEur?: number
|
|
15
|
+
letterboxRateEur?: number
|
|
16
|
+
letterboxMaxGrams?: number
|
|
17
|
+
beneluxParcelRateEur?: number
|
|
18
|
+
euParcelRateEur?: number
|
|
19
|
+
defaultItemWeightGrams?: number
|
|
20
|
+
carrier?: string
|
|
21
|
+
dispatchedFrom?: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ShippingQuoteInput {
|
|
25
|
+
countryCode: string
|
|
26
|
+
subtotalEur: number
|
|
27
|
+
totalWeightGrams: number
|
|
28
|
+
isGreenBulk: boolean
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface ShippingQuoteResult {
|
|
32
|
+
optionId: "postnl-letterbox" | "sendcloud-eu-parcel" | "rotterdam-lcl-pallet"
|
|
33
|
+
priceEur: number
|
|
34
|
+
freeShippingApplied: boolean
|
|
35
|
+
amountRemainingForFreeEur: number
|
|
36
|
+
freeThresholdEur: number
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const BENELUX = new Set(["NL", "BE", "LU"])
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Pure EU & Benelux shipping rate calculator supporting PostNL letterbox,
|
|
43
|
+
* Sendcloud EU parcel, free-shipping weight/value thresholds, and LCL pallet freight.
|
|
44
|
+
*/
|
|
45
|
+
export function calculateEuShippingQuote(
|
|
46
|
+
input: ShippingQuoteInput,
|
|
47
|
+
options: EuFulfillmentOptions = {}
|
|
48
|
+
): ShippingQuoteResult {
|
|
49
|
+
const country = input.countryCode.toUpperCase()
|
|
50
|
+
const isBenelux = BENELUX.has(country)
|
|
51
|
+
|
|
52
|
+
const palletWeightGrams = options.palletWeightGrams ?? 24000
|
|
53
|
+
if (input.isGreenBulk || input.totalWeightGrams >= palletWeightGrams) {
|
|
54
|
+
const palletPrice = isBenelux
|
|
55
|
+
? (options.beneluxPalletEur ?? 45)
|
|
56
|
+
: (options.euPalletEur ?? 85)
|
|
57
|
+
return {
|
|
58
|
+
optionId: "rotterdam-lcl-pallet",
|
|
59
|
+
priceEur: palletPrice,
|
|
60
|
+
freeShippingApplied: false,
|
|
61
|
+
amountRemainingForFreeEur: 0,
|
|
62
|
+
freeThresholdEur: 0,
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const freeThresholdEur = isBenelux
|
|
67
|
+
? (options.beneluxFreeThresholdEur ?? 25)
|
|
68
|
+
: (options.euFreeThresholdEur ?? 50)
|
|
69
|
+
const freeWeightGrams = options.freeWeightGrams ?? 1000
|
|
70
|
+
const qualifiesByWeight = input.totalWeightGrams >= freeWeightGrams
|
|
71
|
+
const qualifiesByValue = input.subtotalEur >= freeThresholdEur
|
|
72
|
+
const freeShippingApplied = qualifiesByWeight || qualifiesByValue
|
|
73
|
+
|
|
74
|
+
const letterboxMaxGrams = options.letterboxMaxGrams ?? 1500
|
|
75
|
+
const optionId =
|
|
76
|
+
isBenelux && input.totalWeightGrams <= letterboxMaxGrams
|
|
77
|
+
? "postnl-letterbox"
|
|
78
|
+
: "sendcloud-eu-parcel"
|
|
79
|
+
|
|
80
|
+
const baseRate =
|
|
81
|
+
optionId === "postnl-letterbox"
|
|
82
|
+
? (options.letterboxRateEur ?? 4.25)
|
|
83
|
+
: isBenelux
|
|
84
|
+
? (options.beneluxParcelRateEur ?? 5.95)
|
|
85
|
+
: (options.euParcelRateEur ?? 8.95)
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
optionId,
|
|
89
|
+
priceEur: freeShippingApplied ? 0 : baseRate,
|
|
90
|
+
freeShippingApplied,
|
|
91
|
+
amountRemainingForFreeEur: freeShippingApplied
|
|
92
|
+
? 0
|
|
93
|
+
: Number(Math.max(0, freeThresholdEur - input.subtotalEur).toFixed(2)),
|
|
94
|
+
freeThresholdEur,
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export class EuFulfillmentProviderService extends AbstractFulfillmentProviderService {
|
|
99
|
+
static identifier = "eu-fulfillment"
|
|
100
|
+
|
|
101
|
+
protected readonly options_: EuFulfillmentOptions
|
|
102
|
+
|
|
103
|
+
constructor(
|
|
104
|
+
_container: Record<string, unknown>,
|
|
105
|
+
options: EuFulfillmentOptions = {}
|
|
106
|
+
) {
|
|
107
|
+
super()
|
|
108
|
+
this.options_ = options
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async getFulfillmentOptions(): Promise<FulfillmentOption[]> {
|
|
112
|
+
return [
|
|
113
|
+
{ id: "postnl-letterbox" },
|
|
114
|
+
{ id: "sendcloud-eu-parcel" },
|
|
115
|
+
{ id: "rotterdam-lcl-pallet" },
|
|
116
|
+
]
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async validateFulfillmentData(
|
|
120
|
+
_optionData: Record<string, unknown>,
|
|
121
|
+
data: Record<string, unknown>
|
|
122
|
+
): Promise<Record<string, unknown>> {
|
|
123
|
+
return data
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async validateOption(_data: Record<string, unknown>): Promise<boolean> {
|
|
127
|
+
return true
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async canCalculate(): Promise<boolean> {
|
|
131
|
+
return true
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async calculatePrice(
|
|
135
|
+
optionData: Record<string, unknown>,
|
|
136
|
+
_data: Record<string, unknown>,
|
|
137
|
+
context: Record<string, unknown>
|
|
138
|
+
): Promise<CalculatedShippingOptionPrice> {
|
|
139
|
+
const countryCode = String(
|
|
140
|
+
(context.shipping_address as { country_code?: string } | undefined)
|
|
141
|
+
?.country_code ?? "NL"
|
|
142
|
+
)
|
|
143
|
+
const items =
|
|
144
|
+
(context.items as
|
|
145
|
+
| Array<{
|
|
146
|
+
quantity?: number
|
|
147
|
+
variant?: { weight?: number }
|
|
148
|
+
unit_price?: number
|
|
149
|
+
}>
|
|
150
|
+
| undefined) ?? []
|
|
151
|
+
|
|
152
|
+
const defaultWeight = this.options_.defaultItemWeightGrams ?? 250
|
|
153
|
+
let totalWeightGrams = 0
|
|
154
|
+
let subtotalEur = 0
|
|
155
|
+
for (const item of items) {
|
|
156
|
+
const qty = Number(item.quantity ?? 1)
|
|
157
|
+
const weight = Number(item.variant?.weight ?? defaultWeight)
|
|
158
|
+
totalWeightGrams += weight * qty
|
|
159
|
+
subtotalEur += Number(item.unit_price ?? 0) * qty
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const quote = calculateEuShippingQuote(
|
|
163
|
+
{
|
|
164
|
+
countryCode,
|
|
165
|
+
subtotalEur,
|
|
166
|
+
totalWeightGrams,
|
|
167
|
+
isGreenBulk: optionData.id === "rotterdam-lcl-pallet",
|
|
168
|
+
},
|
|
169
|
+
this.options_
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
calculated_amount: quote.priceEur,
|
|
174
|
+
is_calculated_price_tax_inclusive: true,
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async createFulfillment(): Promise<CreateFulfillmentResult> {
|
|
179
|
+
return {
|
|
180
|
+
data: {
|
|
181
|
+
carrier: this.options_.carrier ?? "sendcloud-postnl",
|
|
182
|
+
dispatched_from: this.options_.dispatchedFrom ?? "Rotterdam Hub",
|
|
183
|
+
},
|
|
184
|
+
labels: [],
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async cancelFulfillment(): Promise<Record<string, unknown>> {
|
|
189
|
+
return {}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async createReturnFulfillment(): Promise<CreateFulfillmentResult> {
|
|
193
|
+
return { data: {}, labels: [] }
|
|
194
|
+
}
|
|
195
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2023",
|
|
4
|
+
"lib": ["es2023"],
|
|
5
|
+
"strict": true,
|
|
6
|
+
"moduleResolution": "Bundler",
|
|
7
|
+
"module": "ESNext",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"outDir": "dist",
|
|
12
|
+
"rootDir": "src"
|
|
13
|
+
},
|
|
14
|
+
"include": ["src"],
|
|
15
|
+
"exclude": ["node_modules", "dist", "src/__tests__"]
|
|
16
|
+
}
|