mirror of
https://github.com/xtr-dev/payload-billing.git
synced 2025-12-10 02:43:24 +00:00
fix: add better error handling for uninitialized billing plugin
- Fix TypeError when accessing providerConfig on undefined billing plugin - Add proper type safety: useBillingPlugin now returns BillingPlugin | undefined - Add clear error message when plugin hasn't been initialized - Update README quickstart with concise provider response examples 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
11
README.md
11
README.md
@@ -100,18 +100,21 @@ export default buildConfig({
|
|||||||
const payment = await payload.create({
|
const payment = await payload.create({
|
||||||
collection: 'payments',
|
collection: 'payments',
|
||||||
data: {
|
data: {
|
||||||
provider: 'stripe',
|
provider: 'stripe', // or 'mollie' or 'test'
|
||||||
amount: 5000, // $50.00 in cents
|
amount: 5000, // $50.00 in cents
|
||||||
currency: 'USD',
|
currency: 'USD',
|
||||||
description: 'Product purchase',
|
description: 'Product purchase',
|
||||||
status: 'pending',
|
status: 'pending',
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Payment is automatically initialized with Stripe
|
|
||||||
console.log(payment.providerId) // Stripe PaymentIntent ID
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**What you get back:**
|
||||||
|
|
||||||
|
- **Stripe**: `providerId` = PaymentIntent ID, `providerData.raw.client_secret` for Stripe.js
|
||||||
|
- **Mollie**: `providerId` = Transaction ID, `providerData.raw._links.checkout.href` for redirect URL
|
||||||
|
- **Test**: `providerId` = Test payment ID, `providerData.raw.paymentUrl` for interactive test UI
|
||||||
|
|
||||||
## Payment Providers
|
## Payment Providers
|
||||||
|
|
||||||
### Stripe
|
### Stripe
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/payload-billing",
|
"name": "@xtr-dev/payload-billing",
|
||||||
"version": "0.1.14",
|
"version": "0.1.15",
|
||||||
"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",
|
||||||
|
|||||||
@@ -4,6 +4,13 @@ import { useBillingPlugin } from '../plugin/index'
|
|||||||
|
|
||||||
export const initProviderPayment = async (payload: Payload, payment: Partial<Payment>): Promise<Partial<Payment>> => {
|
export const initProviderPayment = async (payload: Payload, payment: Partial<Payment>): Promise<Partial<Payment>> => {
|
||||||
const billing = useBillingPlugin(payload)
|
const billing = useBillingPlugin(payload)
|
||||||
|
|
||||||
|
if (!billing) {
|
||||||
|
throw new Error(
|
||||||
|
'Billing plugin not initialized. Make sure the billingPlugin is properly configured in your Payload config and that Payload has finished initializing.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (!payment.provider || !billing.providerConfig[payment.provider]) {
|
if (!payment.provider || !billing.providerConfig[payment.provider]) {
|
||||||
throw new Error(`Provider ${payment.provider} not found.`)
|
throw new Error(`Provider ${payment.provider} not found.`)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ type BillingPlugin = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useBillingPlugin = (payload: Payload) => singleton.get(payload) as BillingPlugin
|
export const useBillingPlugin = (payload: Payload) => singleton.get(payload) as BillingPlugin | undefined
|
||||||
|
|
||||||
export const billingPlugin = (pluginConfig: BillingPluginConfig = {}) => (config: Config): Config => {
|
export const billingPlugin = (pluginConfig: BillingPluginConfig = {}) => (config: Config): Config => {
|
||||||
if (pluginConfig.disabled) {
|
if (pluginConfig.disabled) {
|
||||||
|
|||||||
Reference in New Issue
Block a user