From 05d612e606564b5d90ddcc30961f58fe71eca54d Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Fri, 19 Sep 2025 14:00:58 +0200 Subject: [PATCH] feat: make InitPayment support both async and non-async functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Updated InitPayment type to return Promise> | Partial - Modified initProviderPayment hook to handle both async and sync returns using Promise.resolve() - Enables payment providers to use either async or synchronous initPayment implementations 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/collections/hooks.ts | 6 ++++-- src/providers/test.ts | 2 +- src/providers/types.ts | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/collections/hooks.ts b/src/collections/hooks.ts index 0e62ef8..f461159 100644 --- a/src/collections/hooks.ts +++ b/src/collections/hooks.ts @@ -2,10 +2,12 @@ import type { Payment } from '../plugin/types/index' import type { Payload } from 'payload' import { useBillingPlugin } from '../plugin/index' -export const initProviderPayment = (payload: Payload, payment: Partial) => { +export const initProviderPayment = async (payload: Payload, payment: Partial): Promise> => { const billing = useBillingPlugin(payload) if (!payment.provider || !billing.providerConfig[payment.provider]) { throw new Error(`Provider ${payment.provider} not found.`) } - return billing.providerConfig[payment.provider].initPayment(payload, payment) + // Handle both async and non-async initPayment functions + const result = billing.providerConfig[payment.provider].initPayment(payload, payment) + return await Promise.resolve(result) } diff --git a/src/providers/test.ts b/src/providers/test.ts index 9e124e2..b4b653b 100644 --- a/src/providers/test.ts +++ b/src/providers/test.ts @@ -392,7 +392,7 @@ export const testProvider = (testConfig: TestProviderConfig) => { { path: '/payload-billing/test/status/:id', method: 'get', - handler: async (req) => { + handler: (req) => { // Extract payment ID from URL path const urlParts = req.url?.split('/') || [] const paymentId = urlParts[urlParts.length - 1] diff --git a/src/providers/types.ts b/src/providers/types.ts index 311e9ad..cef3858 100644 --- a/src/providers/types.ts +++ b/src/providers/types.ts @@ -2,7 +2,7 @@ import type { Payment } from '../plugin/types/payments' import type { Config, Payload } from 'payload' import type { BillingPluginConfig } from '../plugin/config' -export type InitPayment = (payload: Payload, payment: Partial) => Promise> +export type InitPayment = (payload: Payload, payment: Partial) => Promise> | Partial export type PaymentProvider = { key: string