mirror of
https://github.com/xtr-dev/payload-billing.git
synced 2025-12-10 10:53:23 +00:00
feat: Add Mollie payment provider support
- Introduce `mollieProvider` for handling Mollie payments - Add configurable payment hooks for initialization and processing - Implement `initPayment` logic to create Mollie payments and update metadata - Include types for Mollie integration in payments and refunds - Update `package.json` to include `@mollie/api-client` dependency - Refactor existing payment-related types into modular files for better maintainability
This commit is contained in:
2
src/providers/index.ts
Normal file
2
src/providers/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './mollie'
|
||||
export * from './types'
|
||||
40
src/providers/mollie.ts
Normal file
40
src/providers/mollie.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { Payment } from '@/plugin/types/payments'
|
||||
import type { InitPayment, PaymentProvider } from '@/plugin/types'
|
||||
import type { Payload } from 'payload'
|
||||
import { createSingleton } from '@/plugin/singleton'
|
||||
import type { createMollieClient, MollieClient } from '@mollie/api-client'
|
||||
|
||||
const symbol = Symbol('mollie')
|
||||
export type MollieProviderConfig = Parameters<typeof createMollieClient>[0]
|
||||
|
||||
export const mollieProvider = (config: MollieProviderConfig) => {
|
||||
const singleton = createSingleton<MollieClient>(symbol)
|
||||
return {
|
||||
key: 'mollie',
|
||||
onInit: async (payload: Payload) => {
|
||||
const createMollieClient = (await import('@mollie/api-client')).default
|
||||
const mollieClient = createMollieClient(config)
|
||||
singleton.set(payload, mollieClient)
|
||||
},
|
||||
initPayment: async (payload, payment) => {
|
||||
if (!payment.amount) {
|
||||
throw new Error('Amount is required')
|
||||
}
|
||||
if (!payment.currency) {
|
||||
throw new Error('Currency is required')
|
||||
}
|
||||
const molliePayment = await singleton.get(payload).payments.create({
|
||||
amount: {
|
||||
value: (payment.amount / 100).toFixed(2),
|
||||
currency: payment.currency
|
||||
},
|
||||
description: payment.description || '',
|
||||
redirectUrl: 'https://localhost:3000/payment/success',
|
||||
webhookUrl: 'https://localhost:3000',
|
||||
});
|
||||
payment.providerId = molliePayment.id
|
||||
payment.providerData = molliePayment.toPlainObject()
|
||||
return payment
|
||||
},
|
||||
} satisfies PaymentProvider
|
||||
}
|
||||
10
src/providers/types.ts
Normal file
10
src/providers/types.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { Payment } from '@/plugin/types/payments'
|
||||
import type { Payload } from 'payload'
|
||||
|
||||
export type InitPayment = (payload: Payload, payment: Partial<Payment>) => Promise<Partial<Payment>>
|
||||
|
||||
export type PaymentProvider = {
|
||||
key: string
|
||||
onInit: (payload: Payload) => Promise<void> | void
|
||||
initPayment: InitPayment
|
||||
}
|
||||
Reference in New Issue
Block a user