feat: add automatic mode detection for Mollie payments

Automatically set Mollie payment mode to 'test' or 'live' based on
the API key prefix (test_ or live_). This ensures payments are
created in the correct mode and helps prevent configuration errors.

Changes:
- Add getMollieMode() helper to detect mode from API key
- Include mode parameter in payment creation
- Use type assertion for Mollie client compatibility

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-05 14:45:04 +01:00
parent 2904d30a5c
commit 291ce255b4
2 changed files with 13 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@xtr-dev/payload-billing", "name": "@xtr-dev/payload-billing",
"version": "0.1.24", "version": "0.1.25",
"description": "PayloadCMS plugin for billing and payment provider integrations with tracking and local testing", "description": "PayloadCMS plugin for billing and payment provider integrations with tracking and local testing",
"license": "MIT", "license": "MIT",
"type": "module", "type": "module",

View File

@@ -17,6 +17,13 @@ import { createContextLogger } from '../utils/logger'
const symbol = Symbol.for('@xtr-dev/payload-billing/mollie') const symbol = Symbol.for('@xtr-dev/payload-billing/mollie')
export type MollieProviderConfig = Parameters<typeof createMollieClient>[0] export type MollieProviderConfig = Parameters<typeof createMollieClient>[0]
/**
* Determine the Mollie mode based on API key prefix
*/
function getMollieMode(apiKey: string): 'test' | 'live' {
return apiKey.startsWith('test_') ? 'test' : 'live'
}
/** /**
* Type-safe mapping of Mollie payment status to internal status * Type-safe mapping of Mollie payment status to internal status
*/ */
@@ -161,6 +168,9 @@ export const mollieProvider = (mollieConfig: MollieProviderConfig & {
validateProductionUrl(redirectUrl, 'Redirect') validateProductionUrl(redirectUrl, 'Redirect')
validateProductionUrl(webhookUrl, 'Webhook') validateProductionUrl(webhookUrl, 'Webhook')
// Determine mode from API key (test_ or live_)
const mode = getMollieMode(mollieConfig.apiKey)
const molliePayment = await singleton.get(payload).payments.create({ const molliePayment = await singleton.get(payload).payments.create({
amount: { amount: {
value: formatAmountForProvider(payment.amount, payment.currency), value: formatAmountForProvider(payment.amount, payment.currency),
@@ -169,7 +179,8 @@ export const mollieProvider = (mollieConfig: MollieProviderConfig & {
description: payment.description || '', description: payment.description || '',
redirectUrl, redirectUrl,
webhookUrl, webhookUrl,
}); mode,
} as any);
payment.providerId = molliePayment.id payment.providerId = molliePayment.id
// Use toPlainObject if available, otherwise spread the object (for compatibility with different Mollie client versions) // Use toPlainObject if available, otherwise spread the object (for compatibility with different Mollie client versions)
payment.providerData = typeof molliePayment.toPlainObject === 'function' payment.providerData = typeof molliePayment.toPlainObject === 'function'