mirror of
https://github.com/xtr-dev/payload-billing.git
synced 2025-12-10 02:43:24 +00:00
Compare commits
3 Commits
v0.1.18
...
79de7910d4
| Author | SHA1 | Date | |
|---|---|---|---|
| 79de7910d4 | |||
| bb5ba83bc3 | |||
| 79166f7edf |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/payload-billing",
|
"name": "@xtr-dev/payload-billing",
|
||||||
"version": "0.1.18",
|
"version": "0.1.21",
|
||||||
"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",
|
||||||
|
|||||||
@@ -242,10 +242,15 @@ export const testProvider = (testConfig: TestProviderConfig) => {
|
|||||||
{
|
{
|
||||||
path: '/payload-billing/test/payment/:id',
|
path: '/payload-billing/test/payment/:id',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
handler: (req) => {
|
handler: async (req) => {
|
||||||
// Extract payment ID from URL path
|
// Extract payment ID from URL path
|
||||||
const urlParts = req.url?.split('/') || []
|
const urlParts = req.url?.split('/') || []
|
||||||
const paymentId = urlParts[urlParts.length - 1]
|
let paymentId = urlParts[urlParts.length - 1]
|
||||||
|
|
||||||
|
// Remove query parameters if present
|
||||||
|
if (paymentId?.includes('?')) {
|
||||||
|
paymentId = paymentId.split('?')[0]
|
||||||
|
}
|
||||||
|
|
||||||
if (!paymentId) {
|
if (!paymentId) {
|
||||||
return new Response(JSON.stringify({ error: 'Payment ID required' }), {
|
return new Response(JSON.stringify({ error: 'Payment ID required' }), {
|
||||||
@@ -263,7 +268,41 @@ export const testProvider = (testConfig: TestProviderConfig) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const session = testPaymentSessions.get(paymentId)
|
// Try to get session from memory first (for backward compatibility)
|
||||||
|
let session = testPaymentSessions.get(paymentId)
|
||||||
|
|
||||||
|
// If not in memory, fetch from database
|
||||||
|
if (!session && req.payload) {
|
||||||
|
try {
|
||||||
|
const paymentsConfig = pluginConfig.collections?.payments
|
||||||
|
const paymentSlug = typeof paymentsConfig === 'string' ? paymentsConfig : (paymentsConfig?.slug || 'payments')
|
||||||
|
const result = await req.payload.find({
|
||||||
|
collection: paymentSlug,
|
||||||
|
where: {
|
||||||
|
providerId: {
|
||||||
|
equals: paymentId
|
||||||
|
}
|
||||||
|
},
|
||||||
|
limit: 1
|
||||||
|
})
|
||||||
|
|
||||||
|
if (result.docs && result.docs.length > 0) {
|
||||||
|
const payment = result.docs[0] as Payment
|
||||||
|
// Create session from database payment
|
||||||
|
session = {
|
||||||
|
id: paymentId,
|
||||||
|
payment: payment,
|
||||||
|
createdAt: new Date(payment.createdAt || Date.now()),
|
||||||
|
status: 'pending' as PaymentOutcome
|
||||||
|
}
|
||||||
|
// Store in memory for future requests
|
||||||
|
testPaymentSessions.set(paymentId, session)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching payment from database:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
return new Response(JSON.stringify({ error: 'Payment session not found' }), {
|
return new Response(JSON.stringify({ error: 'Payment session not found' }), {
|
||||||
status: 404,
|
status: 404,
|
||||||
@@ -322,7 +361,41 @@ export const testProvider = (testConfig: TestProviderConfig) => {
|
|||||||
|
|
||||||
const { paymentId, scenarioId, method } = validation.data!
|
const { paymentId, scenarioId, method } = validation.data!
|
||||||
|
|
||||||
const session = testPaymentSessions.get(paymentId)
|
// Try to get session from memory first
|
||||||
|
let session = testPaymentSessions.get(paymentId)
|
||||||
|
|
||||||
|
// If not in memory, fetch from database
|
||||||
|
if (!session && req.payload) {
|
||||||
|
try {
|
||||||
|
const paymentsConfig = pluginConfig.collections?.payments
|
||||||
|
const paymentSlug = typeof paymentsConfig === 'string' ? paymentsConfig : (paymentsConfig?.slug || 'payments')
|
||||||
|
const result = await req.payload.find({
|
||||||
|
collection: paymentSlug,
|
||||||
|
where: {
|
||||||
|
providerId: {
|
||||||
|
equals: paymentId
|
||||||
|
}
|
||||||
|
},
|
||||||
|
limit: 1
|
||||||
|
})
|
||||||
|
|
||||||
|
if (result.docs && result.docs.length > 0) {
|
||||||
|
const payment = result.docs[0] as Payment
|
||||||
|
// Create session from database payment
|
||||||
|
session = {
|
||||||
|
id: paymentId,
|
||||||
|
payment: payment,
|
||||||
|
createdAt: new Date(payment.createdAt || Date.now()),
|
||||||
|
status: 'pending' as PaymentOutcome
|
||||||
|
}
|
||||||
|
// Store in memory for future requests
|
||||||
|
testPaymentSessions.set(paymentId, session)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching payment from database:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
return new Response(JSON.stringify({ error: 'Payment session not found' }), {
|
return new Response(JSON.stringify({ error: 'Payment session not found' }), {
|
||||||
status: 404,
|
status: 404,
|
||||||
@@ -398,10 +471,15 @@ export const testProvider = (testConfig: TestProviderConfig) => {
|
|||||||
{
|
{
|
||||||
path: '/payload-billing/test/status/:id',
|
path: '/payload-billing/test/status/:id',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
handler: (req) => {
|
handler: async (req) => {
|
||||||
// Extract payment ID from URL path
|
// Extract payment ID from URL path
|
||||||
const urlParts = req.url?.split('/') || []
|
const urlParts = req.url?.split('/') || []
|
||||||
const paymentId = urlParts[urlParts.length - 1]
|
let paymentId = urlParts[urlParts.length - 1]
|
||||||
|
|
||||||
|
// Remove query parameters if present
|
||||||
|
if (paymentId?.includes('?')) {
|
||||||
|
paymentId = paymentId.split('?')[0]
|
||||||
|
}
|
||||||
|
|
||||||
if (!paymentId) {
|
if (!paymentId) {
|
||||||
return new Response(JSON.stringify({ error: 'Payment ID required' }), {
|
return new Response(JSON.stringify({ error: 'Payment ID required' }), {
|
||||||
@@ -419,7 +497,41 @@ export const testProvider = (testConfig: TestProviderConfig) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const session = testPaymentSessions.get(paymentId)
|
// Try to get session from memory first
|
||||||
|
let session = testPaymentSessions.get(paymentId)
|
||||||
|
|
||||||
|
// If not in memory, fetch from database
|
||||||
|
if (!session && req.payload) {
|
||||||
|
try {
|
||||||
|
const paymentsConfig = pluginConfig.collections?.payments
|
||||||
|
const paymentSlug = typeof paymentsConfig === 'string' ? paymentsConfig : (paymentsConfig?.slug || 'payments')
|
||||||
|
const result = await req.payload.find({
|
||||||
|
collection: paymentSlug,
|
||||||
|
where: {
|
||||||
|
providerId: {
|
||||||
|
equals: paymentId
|
||||||
|
}
|
||||||
|
},
|
||||||
|
limit: 1
|
||||||
|
})
|
||||||
|
|
||||||
|
if (result.docs && result.docs.length > 0) {
|
||||||
|
const payment = result.docs[0] as Payment
|
||||||
|
// Create session from database payment
|
||||||
|
session = {
|
||||||
|
id: paymentId,
|
||||||
|
payment: payment,
|
||||||
|
createdAt: new Date(payment.createdAt || Date.now()),
|
||||||
|
status: 'pending' as PaymentOutcome
|
||||||
|
}
|
||||||
|
// Store in memory for future requests
|
||||||
|
testPaymentSessions.set(paymentId, session)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching payment from database:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
return new Response(JSON.stringify({ error: 'Payment session not found' }), {
|
return new Response(JSON.stringify({ error: 'Payment session not found' }), {
|
||||||
status: 404,
|
status: 404,
|
||||||
@@ -492,7 +604,10 @@ export const testProvider = (testConfig: TestProviderConfig) => {
|
|||||||
|
|
||||||
// Set provider ID and data
|
// Set provider ID and data
|
||||||
payment.providerId = testPaymentId
|
payment.providerId = testPaymentId
|
||||||
const paymentUrl = `${baseUrl}/api/payload-billing/test/payment/${testPaymentId}`
|
// Use custom UI route if specified, otherwise use built-in UI endpoint
|
||||||
|
const paymentUrl = testConfig.customUiRoute
|
||||||
|
? `${baseUrl}${testConfig.customUiRoute}/${testPaymentId}`
|
||||||
|
: `${baseUrl}/api/payload-billing/test/payment/${testPaymentId}`
|
||||||
const providerData: ProviderData = {
|
const providerData: ProviderData = {
|
||||||
raw: {
|
raw: {
|
||||||
id: testPaymentId,
|
id: testPaymentId,
|
||||||
|
|||||||
Reference in New Issue
Block a user