@vicacha-devs/medusa-payment-mollie 0.0.0-stage → 0.0.1
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/dist/providers/mollie-payment/index.d.ts +2 -0
- package/dist/providers/mollie-payment/index.js +5 -0
- package/dist/providers/mollie-payment/service.d.ts +36 -0
- package/dist/providers/mollie-payment/service.js +161 -0
- package/package.json +44 -3
- package/src/__tests__/medusa-shim.mjs +15 -0
- package/src/__tests__/mollie-plugin.spec.ts +31 -0
- package/src/__tests__/register-ts-resolver.mjs +32 -0
- package/src/providers/mollie-payment/index.ts +9 -0
- package/src/providers/mollie-payment/service.ts +260 -0
- package/tsconfig.json +16 -0
- package/README.md +0 -3
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ModuleProvider, Modules } from "@medusajs/framework/utils";
|
|
2
|
+
import { B2BInvoiceProviderService, MolliePaymentProviderService, } from "./service.js";
|
|
3
|
+
export default ModuleProvider(Modules.PAYMENT, {
|
|
4
|
+
services: [MolliePaymentProviderService, B2BInvoiceProviderService],
|
|
5
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { AbstractPaymentProvider } from "@medusajs/framework/utils";
|
|
2
|
+
import type { AuthorizePaymentInput, AuthorizePaymentOutput, CancelPaymentInput, CancelPaymentOutput, CapturePaymentInput, CapturePaymentOutput, DeletePaymentInput, DeletePaymentOutput, GetPaymentStatusInput, GetPaymentStatusOutput, InitiatePaymentInput, InitiatePaymentOutput, ProviderWebhookPayload, RefundPaymentInput, RefundPaymentOutput, RetrievePaymentInput, RetrievePaymentOutput, UpdatePaymentInput, UpdatePaymentOutput, WebhookActionResult } from "@medusajs/framework/types";
|
|
3
|
+
export type MolliePluginOptions = {
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
webhookUrl?: string;
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare class MolliePaymentProviderService extends AbstractPaymentProvider<MolliePluginOptions> {
|
|
9
|
+
static identifier: string;
|
|
10
|
+
protected options_: MolliePluginOptions;
|
|
11
|
+
constructor(container: Record<string, unknown>, options: MolliePluginOptions);
|
|
12
|
+
initiatePayment(input: InitiatePaymentInput): Promise<InitiatePaymentOutput>;
|
|
13
|
+
authorizePayment(input: AuthorizePaymentInput): Promise<AuthorizePaymentOutput>;
|
|
14
|
+
capturePayment(input: CapturePaymentInput): Promise<CapturePaymentOutput>;
|
|
15
|
+
refundPayment(input: RefundPaymentInput): Promise<RefundPaymentOutput>;
|
|
16
|
+
cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput>;
|
|
17
|
+
deletePayment(input: DeletePaymentInput): Promise<DeletePaymentOutput>;
|
|
18
|
+
getPaymentStatus(_input: GetPaymentStatusInput): Promise<GetPaymentStatusOutput>;
|
|
19
|
+
retrievePayment(input: RetrievePaymentInput): Promise<RetrievePaymentOutput>;
|
|
20
|
+
updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput>;
|
|
21
|
+
getWebhookActionAndData(payload: ProviderWebhookPayload["payload"]): Promise<WebhookActionResult>;
|
|
22
|
+
}
|
|
23
|
+
export declare class B2BInvoiceProviderService extends AbstractPaymentProvider {
|
|
24
|
+
static identifier: string;
|
|
25
|
+
constructor(container: Record<string, unknown>, options: Record<string, unknown>);
|
|
26
|
+
initiatePayment(input: InitiatePaymentInput): Promise<InitiatePaymentOutput>;
|
|
27
|
+
authorizePayment(input: AuthorizePaymentInput): Promise<AuthorizePaymentOutput>;
|
|
28
|
+
capturePayment(input: CapturePaymentInput): Promise<CapturePaymentOutput>;
|
|
29
|
+
refundPayment(input: RefundPaymentInput): Promise<RefundPaymentOutput>;
|
|
30
|
+
cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput>;
|
|
31
|
+
deletePayment(input: DeletePaymentInput): Promise<DeletePaymentOutput>;
|
|
32
|
+
getPaymentStatus(): Promise<GetPaymentStatusOutput>;
|
|
33
|
+
retrievePayment(input: RetrievePaymentInput): Promise<RetrievePaymentOutput>;
|
|
34
|
+
updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput>;
|
|
35
|
+
getWebhookActionAndData(): Promise<WebhookActionResult>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { AbstractPaymentProvider, PaymentSessionStatus, } from "@medusajs/framework/utils";
|
|
2
|
+
const MOLLIE_PAYMENT_ID_PATTERN = /^tr_[A-Za-z0-9]{6,40}$/;
|
|
3
|
+
function resolveSafeBaseUrl(override) {
|
|
4
|
+
if (!override)
|
|
5
|
+
return "https://api.mollie.com";
|
|
6
|
+
const parsed = new URL(override);
|
|
7
|
+
const isLoopback = parsed.hostname === "127.0.0.1" || parsed.hostname === "localhost";
|
|
8
|
+
if (parsed.protocol === "https:" ||
|
|
9
|
+
(process.env.NODE_ENV !== "production" && isLoopback)) {
|
|
10
|
+
return parsed.origin;
|
|
11
|
+
}
|
|
12
|
+
throw new Error("Unsafe Mollie baseUrl override rejected");
|
|
13
|
+
}
|
|
14
|
+
export class MolliePaymentProviderService extends AbstractPaymentProvider {
|
|
15
|
+
static identifier = "mollie-ideal";
|
|
16
|
+
options_;
|
|
17
|
+
constructor(container, options) {
|
|
18
|
+
super(container, options);
|
|
19
|
+
this.options_ = options;
|
|
20
|
+
}
|
|
21
|
+
async initiatePayment(input) {
|
|
22
|
+
const apiKey = this.options_.apiKey || process.env.MOLLIE_API_KEY;
|
|
23
|
+
if (!apiKey) {
|
|
24
|
+
throw new Error("MOLLIE_API_KEY is required to initiate Mollie payments");
|
|
25
|
+
}
|
|
26
|
+
const sessionId = `tr_mollie_${Date.now()}`;
|
|
27
|
+
return {
|
|
28
|
+
id: sessionId,
|
|
29
|
+
data: {
|
|
30
|
+
id: sessionId,
|
|
31
|
+
provider: "mollie",
|
|
32
|
+
methods: [
|
|
33
|
+
"ideal",
|
|
34
|
+
"bancontact",
|
|
35
|
+
"creditcard",
|
|
36
|
+
"applepay",
|
|
37
|
+
"banktransfer",
|
|
38
|
+
],
|
|
39
|
+
amount: input.amount,
|
|
40
|
+
currency_code: input.currency_code,
|
|
41
|
+
checkout_url: `https://www.mollie.com/checkout/select-method/${sessionId}`,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async authorizePayment(input) {
|
|
46
|
+
return {
|
|
47
|
+
status: PaymentSessionStatus.AUTHORIZED,
|
|
48
|
+
data: input.data ?? {},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
async capturePayment(input) {
|
|
52
|
+
return { data: { ...input.data, captured: true } };
|
|
53
|
+
}
|
|
54
|
+
async refundPayment(input) {
|
|
55
|
+
return { data: { ...input.data, refunded_amount: input.amount } };
|
|
56
|
+
}
|
|
57
|
+
async cancelPayment(input) {
|
|
58
|
+
return { data: { ...input.data, cancelled: true } };
|
|
59
|
+
}
|
|
60
|
+
async deletePayment(input) {
|
|
61
|
+
return { data: input.data ?? {} };
|
|
62
|
+
}
|
|
63
|
+
async getPaymentStatus(_input) {
|
|
64
|
+
return { status: PaymentSessionStatus.AUTHORIZED };
|
|
65
|
+
}
|
|
66
|
+
async retrievePayment(input) {
|
|
67
|
+
return { data: input.data ?? {} };
|
|
68
|
+
}
|
|
69
|
+
async updatePayment(input) {
|
|
70
|
+
return {
|
|
71
|
+
data: {
|
|
72
|
+
...input.data,
|
|
73
|
+
amount: input.amount,
|
|
74
|
+
currency_code: input.currency_code,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
async getWebhookActionAndData(payload) {
|
|
79
|
+
const rawId = String(payload.data?.id ?? "");
|
|
80
|
+
if (!MOLLIE_PAYMENT_ID_PATTERN.test(rawId)) {
|
|
81
|
+
return { action: "not_supported" };
|
|
82
|
+
}
|
|
83
|
+
const apiKey = this.options_.apiKey || process.env.MOLLIE_API_KEY;
|
|
84
|
+
if (!apiKey) {
|
|
85
|
+
throw new Error("MOLLIE_API_KEY is required to verify Mollie webhooks");
|
|
86
|
+
}
|
|
87
|
+
const baseUrl = resolveSafeBaseUrl(this.options_.baseUrl);
|
|
88
|
+
const response = await fetch(`${baseUrl}/v2/payments/${rawId}`, {
|
|
89
|
+
method: "GET",
|
|
90
|
+
headers: {
|
|
91
|
+
Authorization: `Bearer ${apiKey}`,
|
|
92
|
+
Accept: "application/json",
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
if (!response.ok) {
|
|
96
|
+
return { action: "failed" };
|
|
97
|
+
}
|
|
98
|
+
const payment = (await response.json());
|
|
99
|
+
const amount = Number(payment.amount?.value ?? 0);
|
|
100
|
+
if (payment.status === "paid") {
|
|
101
|
+
return { action: "captured", data: { session_id: payment.id, amount } };
|
|
102
|
+
}
|
|
103
|
+
if (payment.status === "authorized") {
|
|
104
|
+
return { action: "authorized", data: { session_id: payment.id, amount } };
|
|
105
|
+
}
|
|
106
|
+
if (payment.status === "failed" ||
|
|
107
|
+
payment.status === "canceled" ||
|
|
108
|
+
payment.status === "expired") {
|
|
109
|
+
return { action: "failed", data: { session_id: payment.id, amount } };
|
|
110
|
+
}
|
|
111
|
+
return { action: "not_supported" };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export class B2BInvoiceProviderService extends AbstractPaymentProvider {
|
|
115
|
+
static identifier = "b2b-invoice";
|
|
116
|
+
// Explicit constructor keeps the class assignable to Medusa's Constructor<any>.
|
|
117
|
+
// oxlint-disable-next-line no-useless-constructor
|
|
118
|
+
constructor(container, options) {
|
|
119
|
+
super(container, options);
|
|
120
|
+
}
|
|
121
|
+
async initiatePayment(input) {
|
|
122
|
+
const invoiceRef = `VIC-NL-${Date.now()}`;
|
|
123
|
+
return {
|
|
124
|
+
id: invoiceRef,
|
|
125
|
+
data: {
|
|
126
|
+
invoice_reference: invoiceRef,
|
|
127
|
+
payment_terms_days: 14,
|
|
128
|
+
sepa_iban_note: "SEPA Bank Transfer — Rotterdam B.V.",
|
|
129
|
+
amount: input.amount,
|
|
130
|
+
currency_code: input.currency_code,
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
async authorizePayment(input) {
|
|
135
|
+
return { status: PaymentSessionStatus.AUTHORIZED, data: input.data ?? {} };
|
|
136
|
+
}
|
|
137
|
+
async capturePayment(input) {
|
|
138
|
+
return { data: { ...input.data, settled: true } };
|
|
139
|
+
}
|
|
140
|
+
async refundPayment(input) {
|
|
141
|
+
return { data: input.data ?? {} };
|
|
142
|
+
}
|
|
143
|
+
async cancelPayment(input) {
|
|
144
|
+
return { data: input.data ?? {} };
|
|
145
|
+
}
|
|
146
|
+
async deletePayment(input) {
|
|
147
|
+
return { data: input.data ?? {} };
|
|
148
|
+
}
|
|
149
|
+
async getPaymentStatus() {
|
|
150
|
+
return { status: PaymentSessionStatus.AUTHORIZED };
|
|
151
|
+
}
|
|
152
|
+
async retrievePayment(input) {
|
|
153
|
+
return { data: input.data ?? {} };
|
|
154
|
+
}
|
|
155
|
+
async updatePayment(input) {
|
|
156
|
+
return { data: input.data ?? {} };
|
|
157
|
+
}
|
|
158
|
+
async getWebhookActionAndData() {
|
|
159
|
+
return { action: "not_supported" };
|
|
160
|
+
}
|
|
161
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vicacha-devs/medusa-payment-mollie",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Mollie (iDEAL, Bancontact, SEPA, Cards) and B2B Net-14 Invoice payment provider plugin for Medusa v2.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bancontact",
|
|
7
|
+
"ideal",
|
|
8
|
+
"medusa",
|
|
9
|
+
"medusa-plugin",
|
|
10
|
+
"medusa-plugin-integration",
|
|
11
|
+
"medusa-plugin-payment",
|
|
12
|
+
"medusa-v2",
|
|
13
|
+
"mollie",
|
|
14
|
+
"plugin",
|
|
15
|
+
"sepa"
|
|
16
|
+
],
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Vicacha Devs",
|
|
19
|
+
"files": [
|
|
20
|
+
".medusa/server",
|
|
21
|
+
"dist",
|
|
22
|
+
"src",
|
|
23
|
+
"tsconfig.json"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
"./package.json": "./package.json",
|
|
28
|
+
"./providers/*": {
|
|
29
|
+
"import": "./dist/providers/*/index.js",
|
|
30
|
+
"types": "./dist/providers/*/index.d.ts"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@medusajs/framework": "2.23.0",
|
|
35
|
+
"@types/node": "^22.13.0",
|
|
36
|
+
"tsc-alias": "^1.9.2",
|
|
37
|
+
"typescript": "^5.7.3"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@medusajs/framework": "^2.0.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -p tsconfig.json && tsc-alias -p tsconfig.json --resolve-full-paths",
|
|
44
|
+
"typecheck": "tsc --noEmit",
|
|
45
|
+
"test": "node --import ./src/__tests__/register-ts-resolver.mjs --import ../plugin-testing/register-js-extension.mjs --test src/__tests__/*.spec.ts"
|
|
46
|
+
}
|
|
6
47
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export class AbstractPaymentProvider {
|
|
2
|
+
constructor(container, options) {
|
|
3
|
+
this.container_ = container
|
|
4
|
+
this.options_ = options
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const PaymentSessionStatus = {
|
|
9
|
+
AUTHORIZED: "authorized",
|
|
10
|
+
CAPTURED: "captured",
|
|
11
|
+
CANCELED: "canceled",
|
|
12
|
+
ERROR: "error",
|
|
13
|
+
PENDING: "pending",
|
|
14
|
+
REQUIRES_MORE: "requires_more",
|
|
15
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import { MolliePaymentProviderService } from "../providers/mollie-payment/service.ts"
|
|
4
|
+
|
|
5
|
+
describe("@vicacha-devs/medusa-payment-mollie plugin", () => {
|
|
6
|
+
it("rejects malformed payment IDs in webhook to prevent path/SSRF injection", async () => {
|
|
7
|
+
const svc = new MolliePaymentProviderService({}, { apiKey: "test_123" })
|
|
8
|
+
const res = await svc.getWebhookActionAndData({
|
|
9
|
+
data: { id: "../../admin/users" },
|
|
10
|
+
rawData: "",
|
|
11
|
+
headers: {},
|
|
12
|
+
})
|
|
13
|
+
assert.equal(res.action, "not_supported")
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it("rejects non-https non-loopback baseUrl overrides", async () => {
|
|
17
|
+
const svc = new MolliePaymentProviderService(
|
|
18
|
+
{},
|
|
19
|
+
{ apiKey: "test_123", baseUrl: "http://169.254.169.254" }
|
|
20
|
+
)
|
|
21
|
+
await assert.rejects(
|
|
22
|
+
() =>
|
|
23
|
+
svc.getWebhookActionAndData({
|
|
24
|
+
data: { id: "tr_WDqYK6vllg" },
|
|
25
|
+
rawData: "",
|
|
26
|
+
headers: {},
|
|
27
|
+
}),
|
|
28
|
+
/Unsafe Mollie baseUrl override rejected/
|
|
29
|
+
)
|
|
30
|
+
})
|
|
31
|
+
})
|
|
@@ -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
|
+
})
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ModuleProvider, Modules } from "@medusajs/framework/utils"
|
|
2
|
+
import {
|
|
3
|
+
B2BInvoiceProviderService,
|
|
4
|
+
MolliePaymentProviderService,
|
|
5
|
+
} from "./service.js"
|
|
6
|
+
|
|
7
|
+
export default ModuleProvider(Modules.PAYMENT, {
|
|
8
|
+
services: [MolliePaymentProviderService, B2BInvoiceProviderService],
|
|
9
|
+
})
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AbstractPaymentProvider,
|
|
3
|
+
PaymentSessionStatus,
|
|
4
|
+
} from "@medusajs/framework/utils"
|
|
5
|
+
import type {
|
|
6
|
+
AuthorizePaymentInput,
|
|
7
|
+
AuthorizePaymentOutput,
|
|
8
|
+
CancelPaymentInput,
|
|
9
|
+
CancelPaymentOutput,
|
|
10
|
+
CapturePaymentInput,
|
|
11
|
+
CapturePaymentOutput,
|
|
12
|
+
DeletePaymentInput,
|
|
13
|
+
DeletePaymentOutput,
|
|
14
|
+
GetPaymentStatusInput,
|
|
15
|
+
GetPaymentStatusOutput,
|
|
16
|
+
InitiatePaymentInput,
|
|
17
|
+
InitiatePaymentOutput,
|
|
18
|
+
ProviderWebhookPayload,
|
|
19
|
+
RefundPaymentInput,
|
|
20
|
+
RefundPaymentOutput,
|
|
21
|
+
RetrievePaymentInput,
|
|
22
|
+
RetrievePaymentOutput,
|
|
23
|
+
UpdatePaymentInput,
|
|
24
|
+
UpdatePaymentOutput,
|
|
25
|
+
WebhookActionResult,
|
|
26
|
+
} from "@medusajs/framework/types"
|
|
27
|
+
|
|
28
|
+
export type MolliePluginOptions = {
|
|
29
|
+
apiKey?: string
|
|
30
|
+
webhookUrl?: string
|
|
31
|
+
baseUrl?: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const MOLLIE_PAYMENT_ID_PATTERN = /^tr_[A-Za-z0-9]{6,40}$/
|
|
35
|
+
|
|
36
|
+
function resolveSafeBaseUrl(override?: string): string {
|
|
37
|
+
if (!override) return "https://api.mollie.com"
|
|
38
|
+
const parsed = new URL(override)
|
|
39
|
+
const isLoopback =
|
|
40
|
+
parsed.hostname === "127.0.0.1" || parsed.hostname === "localhost"
|
|
41
|
+
if (
|
|
42
|
+
parsed.protocol === "https:" ||
|
|
43
|
+
(process.env.NODE_ENV !== "production" && isLoopback)
|
|
44
|
+
) {
|
|
45
|
+
return parsed.origin
|
|
46
|
+
}
|
|
47
|
+
throw new Error("Unsafe Mollie baseUrl override rejected")
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class MolliePaymentProviderService extends AbstractPaymentProvider<MolliePluginOptions> {
|
|
51
|
+
static identifier = "mollie-ideal"
|
|
52
|
+
protected options_: MolliePluginOptions
|
|
53
|
+
|
|
54
|
+
constructor(
|
|
55
|
+
container: Record<string, unknown>,
|
|
56
|
+
options: MolliePluginOptions
|
|
57
|
+
) {
|
|
58
|
+
super(container, options)
|
|
59
|
+
this.options_ = options
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async initiatePayment(
|
|
63
|
+
input: InitiatePaymentInput
|
|
64
|
+
): Promise<InitiatePaymentOutput> {
|
|
65
|
+
const apiKey = this.options_.apiKey || process.env.MOLLIE_API_KEY
|
|
66
|
+
if (!apiKey) {
|
|
67
|
+
throw new Error("MOLLIE_API_KEY is required to initiate Mollie payments")
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const sessionId = `tr_mollie_${Date.now()}`
|
|
71
|
+
return {
|
|
72
|
+
id: sessionId,
|
|
73
|
+
data: {
|
|
74
|
+
id: sessionId,
|
|
75
|
+
provider: "mollie",
|
|
76
|
+
methods: [
|
|
77
|
+
"ideal",
|
|
78
|
+
"bancontact",
|
|
79
|
+
"creditcard",
|
|
80
|
+
"applepay",
|
|
81
|
+
"banktransfer",
|
|
82
|
+
],
|
|
83
|
+
amount: input.amount,
|
|
84
|
+
currency_code: input.currency_code,
|
|
85
|
+
checkout_url: `https://www.mollie.com/checkout/select-method/${sessionId}`,
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async authorizePayment(
|
|
91
|
+
input: AuthorizePaymentInput
|
|
92
|
+
): Promise<AuthorizePaymentOutput> {
|
|
93
|
+
return {
|
|
94
|
+
status: PaymentSessionStatus.AUTHORIZED,
|
|
95
|
+
data: input.data ?? {},
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async capturePayment(
|
|
100
|
+
input: CapturePaymentInput
|
|
101
|
+
): Promise<CapturePaymentOutput> {
|
|
102
|
+
return { data: { ...input.data, captured: true } }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async refundPayment(input: RefundPaymentInput): Promise<RefundPaymentOutput> {
|
|
106
|
+
return { data: { ...input.data, refunded_amount: input.amount } }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput> {
|
|
110
|
+
return { data: { ...input.data, cancelled: true } }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async deletePayment(input: DeletePaymentInput): Promise<DeletePaymentOutput> {
|
|
114
|
+
return { data: input.data ?? {} }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async getPaymentStatus(
|
|
118
|
+
_input: GetPaymentStatusInput
|
|
119
|
+
): Promise<GetPaymentStatusOutput> {
|
|
120
|
+
return { status: PaymentSessionStatus.AUTHORIZED }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async retrievePayment(
|
|
124
|
+
input: RetrievePaymentInput
|
|
125
|
+
): Promise<RetrievePaymentOutput> {
|
|
126
|
+
return { data: input.data ?? {} }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput> {
|
|
130
|
+
return {
|
|
131
|
+
data: {
|
|
132
|
+
...input.data,
|
|
133
|
+
amount: input.amount,
|
|
134
|
+
currency_code: input.currency_code,
|
|
135
|
+
},
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async getWebhookActionAndData(
|
|
140
|
+
payload: ProviderWebhookPayload["payload"]
|
|
141
|
+
): Promise<WebhookActionResult> {
|
|
142
|
+
const rawId = String(
|
|
143
|
+
(payload.data as { id?: string } | undefined)?.id ?? ""
|
|
144
|
+
)
|
|
145
|
+
if (!MOLLIE_PAYMENT_ID_PATTERN.test(rawId)) {
|
|
146
|
+
return { action: "not_supported" }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const apiKey = this.options_.apiKey || process.env.MOLLIE_API_KEY
|
|
150
|
+
if (!apiKey) {
|
|
151
|
+
throw new Error("MOLLIE_API_KEY is required to verify Mollie webhooks")
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const baseUrl = resolveSafeBaseUrl(this.options_.baseUrl)
|
|
155
|
+
const response = await fetch(`${baseUrl}/v2/payments/${rawId}`, {
|
|
156
|
+
method: "GET",
|
|
157
|
+
headers: {
|
|
158
|
+
Authorization: `Bearer ${apiKey}`,
|
|
159
|
+
Accept: "application/json",
|
|
160
|
+
},
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
if (!response.ok) {
|
|
164
|
+
return { action: "failed" }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const payment = (await response.json()) as {
|
|
168
|
+
id: string
|
|
169
|
+
status: string
|
|
170
|
+
amount?: { value?: string }
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const amount = Number(payment.amount?.value ?? 0)
|
|
174
|
+
if (payment.status === "paid") {
|
|
175
|
+
return { action: "captured", data: { session_id: payment.id, amount } }
|
|
176
|
+
}
|
|
177
|
+
if (payment.status === "authorized") {
|
|
178
|
+
return { action: "authorized", data: { session_id: payment.id, amount } }
|
|
179
|
+
}
|
|
180
|
+
if (
|
|
181
|
+
payment.status === "failed" ||
|
|
182
|
+
payment.status === "canceled" ||
|
|
183
|
+
payment.status === "expired"
|
|
184
|
+
) {
|
|
185
|
+
return { action: "failed", data: { session_id: payment.id, amount } }
|
|
186
|
+
}
|
|
187
|
+
return { action: "not_supported" }
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export class B2BInvoiceProviderService extends AbstractPaymentProvider {
|
|
192
|
+
static identifier = "b2b-invoice"
|
|
193
|
+
|
|
194
|
+
// Explicit constructor keeps the class assignable to Medusa's Constructor<any>.
|
|
195
|
+
// oxlint-disable-next-line no-useless-constructor
|
|
196
|
+
constructor(
|
|
197
|
+
container: Record<string, unknown>,
|
|
198
|
+
options: Record<string, unknown>
|
|
199
|
+
) {
|
|
200
|
+
super(container, options)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async initiatePayment(
|
|
204
|
+
input: InitiatePaymentInput
|
|
205
|
+
): Promise<InitiatePaymentOutput> {
|
|
206
|
+
const invoiceRef = `VIC-NL-${Date.now()}`
|
|
207
|
+
return {
|
|
208
|
+
id: invoiceRef,
|
|
209
|
+
data: {
|
|
210
|
+
invoice_reference: invoiceRef,
|
|
211
|
+
payment_terms_days: 14,
|
|
212
|
+
sepa_iban_note: "SEPA Bank Transfer — Rotterdam B.V.",
|
|
213
|
+
amount: input.amount,
|
|
214
|
+
currency_code: input.currency_code,
|
|
215
|
+
},
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async authorizePayment(
|
|
220
|
+
input: AuthorizePaymentInput
|
|
221
|
+
): Promise<AuthorizePaymentOutput> {
|
|
222
|
+
return { status: PaymentSessionStatus.AUTHORIZED, data: input.data ?? {} }
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async capturePayment(
|
|
226
|
+
input: CapturePaymentInput
|
|
227
|
+
): Promise<CapturePaymentOutput> {
|
|
228
|
+
return { data: { ...input.data, settled: true } }
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async refundPayment(input: RefundPaymentInput): Promise<RefundPaymentOutput> {
|
|
232
|
+
return { data: input.data ?? {} }
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput> {
|
|
236
|
+
return { data: input.data ?? {} }
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
async deletePayment(input: DeletePaymentInput): Promise<DeletePaymentOutput> {
|
|
240
|
+
return { data: input.data ?? {} }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async getPaymentStatus(): Promise<GetPaymentStatusOutput> {
|
|
244
|
+
return { status: PaymentSessionStatus.AUTHORIZED }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async retrievePayment(
|
|
248
|
+
input: RetrievePaymentInput
|
|
249
|
+
): Promise<RetrievePaymentOutput> {
|
|
250
|
+
return { data: input.data ?? {} }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput> {
|
|
254
|
+
return { data: input.data ?? {} }
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async getWebhookActionAndData(): Promise<WebhookActionResult> {
|
|
258
|
+
return { action: "not_supported" }
|
|
259
|
+
}
|
|
260
|
+
}
|
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
|
+
}
|
package/README.md
DELETED