mirror of
https://github.com/xtr-dev/payload-billing.git
synced 2025-12-10 19:03:23 +00:00
- Add comprehensive test provider with configurable payment outcomes (paid, failed, cancelled, expired, pending) - Support multiple payment methods (iDEAL, Credit Card, PayPal, Apple Pay, Bank Transfer) - Interactive test payment UI with responsive design and real-time processing simulation - Test mode indicators including warning banners, badges, and console warnings - React components for admin UI integration (TestModeWarningBanner, TestModeBadge, TestPaymentControls) - API endpoints for test payment processing and status polling - Configurable scenarios with custom delays and outcomes - Production safety mechanisms and clear test mode indicators - Complete documentation and usage examples Implements GitHub issue #20 for advanced test provider functionality. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
4.1 KiB
4.1 KiB
Advanced Test Provider Example
The advanced test provider allows you to test complex payment scenarios with an interactive UI for development purposes.
Basic Configuration
import { billingPlugin, testProvider } from '@xtr-dev/payload-billing'
// Configure the test provider
const testProviderConfig = {
enabled: true, // Enable the test provider
defaultDelay: 2000, // Default delay in milliseconds
baseUrl: process.env.PAYLOAD_PUBLIC_SERVER_URL || 'http://localhost:3000',
customUiRoute: '/test-payment', // Custom route for test payment UI
testModeIndicators: {
showWarningBanners: true, // Show warning banners in test mode
showTestBadges: true, // Show test badges
consoleWarnings: true, // Show console warnings
}
}
// Add to your payload config
export default buildConfig({
plugins: [
billingPlugin({
providers: [
testProvider(testProviderConfig)
]
})
]
})
Custom Scenarios
You can define custom payment scenarios:
const customScenarios = [
{
id: 'quick-success',
name: 'Quick Success',
description: 'Payment succeeds in 1 second',
outcome: 'paid' as const,
delay: 1000,
method: 'creditcard' as const
},
{
id: 'network-timeout',
name: 'Network Timeout',
description: 'Simulates network timeout',
outcome: 'failed' as const,
delay: 10000
},
{
id: 'user-abandonment',
name: 'User Abandonment',
description: 'User closes payment window',
outcome: 'cancelled' as const,
delay: 5000
}
]
const testProviderConfig = {
enabled: true,
scenarios: customScenarios,
// ... other config
}
Available Payment Outcomes
paid- Payment succeedsfailed- Payment failscancelled- Payment is cancelled by userexpired- Payment expirespending- Payment remains pending
Available Payment Methods
ideal- iDEAL (Dutch banking)creditcard- Credit/Debit Cardspaypal- PayPalapplepay- Apple Paybanktransfer- Bank Transfer
Using the Test UI
- Create a payment using the test provider
- The payment will return a
paymentUrlin the provider data - Navigate to this URL to access the interactive test interface
- Select a payment method and scenario
- Click "Process Test Payment" to simulate the payment
- The payment status will update automatically based on the selected scenario
React Components
Use the provided React components in your admin interface:
import { TestModeWarningBanner, TestModeBadge, TestPaymentControls } from '@xtr-dev/payload-billing/client'
// Show warning banner when in test mode
<TestModeWarningBanner visible={isTestMode} />
// Add test badge to payment status
<div>
Payment Status: {status}
<TestModeBadge visible={isTestMode} />
</div>
// Payment testing controls
<TestPaymentControls
paymentId={paymentId}
onScenarioSelect={(scenario) => console.log('Selected scenario:', scenario)}
onMethodSelect={(method) => console.log('Selected method:', method)}
/>
API Endpoints
The test provider automatically registers these endpoints:
GET /api/payload-billing/test/payment/:id- Test payment UIPOST /api/payload-billing/test/process- Process test paymentGET /api/payload-billing/test/status/:id- Get payment status
Development Tips
- Console Warnings: Keep
consoleWarnings: trueto get notifications about test mode - Visual Indicators: Use warning banners and badges to clearly mark test payments
- Custom Scenarios: Create scenarios that match your specific use cases
- Automated Testing: Use the test provider in your e2e tests for predictable payment outcomes
- Method Testing: Test different payment methods to ensure your UI handles them correctly
Production Safety
The test provider includes several safety mechanisms:
- Must be explicitly enabled with
enabled: true - Clearly marked with test indicators
- Console warnings when active
- Separate endpoint namespace (
/payload-billing/test/) - No real payment processing
Important: Never use the test provider in production environments!