Midtrans-Middleware/src/features/payments/shared/OrderManager.ts

85 lines
2.6 KiB
TypeScript

import { api } from '../../../services/api'
import { TransactionLogger } from './TransactionLogger'
import { getPaymentMode } from '../lib/paymentMode'
export class OrderManager {
static async validateOrder(orderId: string, amount: number): Promise<boolean> {
const mode = getPaymentMode()
try {
// Basic validation
if (!orderId || amount <= 0) {
TransactionLogger.log(mode, 'order.validation.failed', {
orderId,
amount,
reason: 'invalid_parameters'
})
return false
}
// Additional business rules can be added here
// For now, just check if order exists in our system
const orderDetails = await this.getOrderDetails(orderId)
if (!orderDetails) {
TransactionLogger.log(mode, 'order.validation.failed', {
orderId,
reason: 'order_not_found'
})
return false
}
// Check amount matches
if (orderDetails.amount !== amount) {
TransactionLogger.log(mode, 'order.validation.failed', {
orderId,
expectedAmount: orderDetails.amount,
providedAmount: amount,
reason: 'amount_mismatch'
})
return false
}
TransactionLogger.log(mode, 'order.validation.success', { orderId, amount })
return true
} catch (error) {
TransactionLogger.logPaymentError(mode, orderId, error)
return false
}
}
static async getOrderDetails(orderId: string) {
try {
// This would typically call your ERP or order management system
// For now, return mock data or call existing API
const response = await api.get(`/orders/${orderId}`)
return response.data
} catch (error) {
// If API doesn't exist yet, return null (will be implemented in Epic 5)
console.warn(`Order details API not available for ${orderId}:`, error instanceof Error ? error.message : String(error))
return null
}
}
static async updateOrderStatus(orderId: string, status: string, source: string) {
const mode = getPaymentMode()
try {
// This would update your ERP system to unfreeze inventory, etc.
// For now, just log the update
TransactionLogger.log(mode, 'order.status.updated', {
orderId,
status,
source,
timestamp: new Date().toISOString()
})
// TODO: Implement actual ERP integration in Epic 5
// await api.post('/erp/orders/update-status', { orderId, status, source })
} catch (error) {
TransactionLogger.logPaymentError(mode, orderId, error)
throw error
}
}
}