This document outlines the complete migration plan to replace the custom payment_reference order ID system with the database table's primary id field as the order identifier throughout IndexNow Studio.
- Midtrans Create Payment:
ORDER-${Date.now()}-${Math.random().toString(36).substr(2, 9).toUpperCase()} - Snap Handler:
SNAP-${Date.now()}-${user_id.slice(0, 8)} - Bank Transfer:
BT-${Date.now()}-${user_id.slice(0, 8)} - Core Processor:
${method}_${userPrefix}_${timestamp}
- Current Primary Key:
id(UUID) - Already unique and secure - Current Order Reference:
payment_reference(TEXT) - Custom generated, to be removed - Gateway Reference:
gateway_transaction_id(TEXT) - Remains unchanged
- File:
app/api/v1/billing/midtrans/create-payment/route.ts- Line 79:
const orderId = ORDER-${Date.now()}-${Math.random()...} - Line 87:
order_id: orderIdin Midtrans payload - Line 204:
order_id: orderIdin response data - Action: Remove generation, use database
idfrom transaction creation
- Line 79:
-
File:
app/api/v1/billing/channels/midtrans-snap/handler.ts- Line 29:
const orderId = SNAP-${Date.now()}-${user_id...} - Line 32:
createPendingTransaction(orderId, ...) - Line 70:
redirect_url: /dashboard/settings/plans-billing/order/${orderId} - Line 72:
order_id: orderIdin response - Action: Create transaction first, then use returned
id
- Line 29:
-
File:
app/api/v1/billing/channels/bank-transfer/handler.ts- Line 20:
const orderId = BT-${Date.now()}-${user_id...} - Line 42:
.eq('payment_reference', orderId)for database update - Line 48:
orderId: orderIdin email service - Line 70:
redirect_url: /order/${orderId} - Line 72:
order_id: orderIdin response - Action: Create transaction first, then use returned
id
- Line 20:
-
File:
app/api/v1/billing/channels/midtrans-recurring/handler.ts- Similar pattern to above handlers
- Action: Create transaction first, then use returned
id
-
File:
app/api/v1/billing/orders/[order_id]/route.ts- Line 11: Route parameter
{ order_id: string } - Line 48:
console.log('[ORDER-API] Searching for payment_reference:', order_id...) - Line 56:
.eq('payment_reference', order_id)database query - Line 89:
order_id: transaction.payment_referencein response - Action: Change route to
[id], query byidfield, returnidas order_id
- Line 11: Route parameter
-
File:
app/api/v1/billing/transactions/[id]/route.ts- Line 40:
.eq('id', (await params).id)- Already uses table id - Action: No changes needed - this is the target pattern
- Line 40:
- File:
app/api/v1/billing/history/route.ts- Line 44:
payment_reference,in SELECT query - Line 90-94: Response mapping includes
payment_reference - Action: Remove
payment_referencefrom SELECT, mapidasorder_id
- Line 44:
-
File:
app/api/v1/admin/orders/route.ts- Line 60:
payment_reference,in SELECT query - Line 148-168: Response transformation with
payment_reference - Action: Remove
payment_referencefrom SELECT, mapidasorder_id
- Line 60:
-
File:
app/api/v1/admin/orders/[id]/route.ts- Line 45:
.eq('id', orderId)- Already uses table id - Action: No changes needed - this is the target pattern
- Line 45:
-
File:
app/api/v1/billing/midtrans-3ds-callback/route.ts- Line 9:
const { transaction_id, order_id } = await request.json() - Line 26:
.eq('gateway_transaction_id', transaction_id)for lookup - Action: Update to use table
idfor internal references
- Line 9:
-
File:
app/api/midtrans/webhook/route.ts- Contains webhook processing logic
- Action: Review and update order lookup logic
- File:
app/dashboard/settings/plans-billing/order/[id]/page.tsx- Line 24:
payment_reference: stringin Transaction interface - Line 75:
fetch(/api/v1/billing/transactions/${params.id})- Uses correct API - Line 275:
{transaction.payment_reference}in Order ID display - Line 433:
{transaction.payment_reference}in Reference Number display - Action: Update interface, remove payment_reference displays, show
idas Order ID
- Line 24:
-
File:
app/dashboard/settings/plans-billing/page.tsx- Line 46:
payment_reference: stringin Transaction interface - Action: Remove from interface, handle in BillingHistory component
- Line 46:
-
File:
app/dashboard/settings/plans-billing/components/BillingHistory.tsx- Contains transaction display logic
- Action: Update to display
idas Order ID
-
File:
app/dashboard/settings/plans-billing/history/HistoryTab.tsx- Contains history tab display logic
- Action: Update Order ID column to show
id
-
File:
app/backend/admin/orders/page.tsx- Contains admin order list display
- Action: Update Order ID column to show
id
-
File:
app/backend/admin/orders/[id]/page.tsx- Contains admin order detail display
- Action: Update Order ID display to show
id
- File:
lib/email/templates/billing-confirmation.html- Line 222:
<span class="stats-value">{{orderId}}</span> - Line 299:
include your Order ID ({{orderId}}) in the payment reference - Action: Update template to use table
idas {{orderId}}
- Line 222:
- File:
lib/email/emailService.ts- Line 229:
orderId: data.orderId,in email data - Action: Update email service to pass table
idas orderId
- Line 229:
- File:
app/api/v1/billing/channels/shared/base-handler.ts- Contains transaction creation logic
- Action: Update to create transaction first, return
idfor order operations
- File:
lib/payment-services/payment-router.ts- Contains payment processing coordination
- Action: Update response handling to use table
id
- File:
lib/services/payments/midtrans/MidtransSnapService.ts- Contains Snap payment processing
- Action: Update order_id handling in responses
-
Add migration safety check
- Verify all existing
payment_referencevalues are unique - Check for any external dependencies on
payment_referenceformat
- Verify all existing
-
Update database queries preparation
- Create backup of current
payment_referencedata - Prepare SQL queries for column removal
- Create backup of current
-
Update transaction creation flow
- Modify base handlers to create transaction record first
- Use returned database
idfor all subsequent operations - Remove custom order ID generation logic
-
Update payment channel handlers
midtrans-snap/handler.ts: Create transaction → useidbank-transfer/handler.ts: Create transaction → useidmidtrans-recurring/handler.ts: Create transaction → useid
-
Update order lookup APIs
- Change
/api/v1/billing/orders/[order_id]/route.tsto useidqueries - Update response mapping to use
idasorder_id - Update all
.eq('payment_reference', ...)to.eq('id', ...)
- Change
-
Update billing and admin APIs
- Remove
payment_referencefrom SELECT queries - Update response transformations to map
idasorder_id - Update admin order management to use
id
- Remove
-
Update TypeScript interfaces
- Remove
payment_referencefrom Transaction interfaces - Update all components to use
idas order identifier
- Remove
-
Update order detail pages
/order/[id]/page.tsx: Displayidas Order ID- Remove
payment_referencereferences - Update API calls to use
idparameter
-
Update billing history displays
- Update table columns to show
idas Order ID - Remove
payment_referencecolumn displays - Update sorting and filtering logic
- Update table columns to show
-
Update admin panels
- Update order list to display
idas Order ID - Update order detail pages
- Update bulk operations to use
id
- Update order list to display
-
Update email templates
- Replace
{{orderId}}references to use tableid - Update payment instructions to reference table
id
- Replace
-
Update email service
- Pass table
idasorderIdin email data - Update all email notification calls
- Pass table
-
Update Midtrans integration
- Use table
idin Midtransorder_idfield - Update webhook processing to handle UUID order IDs
- Test gateway compatibility with UUID format
- Use table
-
Update redirect URLs
- Change all redirect URLs to use table
id - Update success/failure page routing
- Change all redirect URLs to use table
- Remove payment_reference column
- Drop column from
indb_payment_transactions - Update any remaining database constraints
- Clean up any related indexes
- Drop column from
- Gateway Compatibility: Some payment gateways may have restrictions on order ID format
- External References: Any external systems storing the old
payment_referenceformat - Email Tracking: Existing emails with old order IDs may become untrackable
- URL Breaking: Existing order detail URLs with
payment_referencewill break
- Customer Experience: UUID order IDs are less user-friendly
- Support Issues: Support team may prefer readable order IDs
- Webhook Processing: External webhooks may reference old order ID format
- Database Performance: UUID vs VARCHAR query performance differences
- Logging: Log analysis tools may expect specific order ID formats
- Keep
payment_referencecolumn during migration - Maintain dual-reference system temporarily
- Switch back to
payment_referencequeries if needed
- Backup all transaction data before migration
- Keep mapping between old
payment_referenceand newidusage - Maintain email template versioning for troubleshooting
- Test all payment channel handlers with new
idsystem - Test order lookup APIs with UUID parameters
- Test email generation with table
idas order reference
- Test complete payment flow from checkout to completion
- Test webhook processing with new order ID format
- Test admin order management functionality
- Test checkout process with new order ID format
- Test order detail page display
- Test email notifications with new order IDs
- Test billing history display
- Day 1-2: Database backup and safety checks
- Day 3-4: Backend API updates (Phase 2)
- Day 5: Testing and validation
- Day 1-3: Frontend component updates (Phase 3)
- Day 4-5: Email and notification updates (Phase 4)
- Day 1-3: Gateway integration updates (Phase 5)
- Day 4-5: Comprehensive testing
- Day 1-2: Database schema cleanup (Phase 6)
- Day 3-5: Monitoring and issue resolution
app/api/v1/billing/midtrans/create-payment/route.tsapp/api/v1/billing/channels/midtrans-snap/handler.tsapp/api/v1/billing/channels/bank-transfer/handler.tsapp/api/v1/billing/channels/midtrans-recurring/handler.tsapp/api/v1/billing/channels/shared/base-handler.tsapp/api/v1/billing/orders/[order_id]/route.ts→ rename to[id]/route.tsapp/api/v1/billing/history/route.tsapp/api/v1/billing/upload-proof/route.tsapp/api/v1/billing/midtrans-3ds-callback/route.tsapp/api/v1/admin/orders/route.tsapp/api/v1/admin/orders/[id]/route.tsapp/api/v1/admin/orders/[id]/status/route.tsapp/api/midtrans/webhook/route.tslib/email/emailService.tslib/services/payments/core/PaymentProcessor.tslib/services/payments/midtrans/MidtransSnapService.tslib/payment-services/payment-router.tslib/types/services/Database.ts
app/dashboard/settings/plans-billing/page.tsxapp/dashboard/settings/plans-billing/order/[id]/page.tsxapp/dashboard/settings/plans-billing/history/page.tsxapp/dashboard/settings/plans-billing/history/HistoryTab.tsxapp/dashboard/settings/plans-billing/components/BillingHistory.tsxapp/backend/admin/orders/page.tsxapp/backend/admin/orders/[id]/page.tsx
lib/email/templates/billing-confirmation.html
- Remove column:
payment_referencefromindb_payment_transactions
File: app/api/v1/billing/channels/shared/base-handler.ts
- Modify
createPendingTransactionmethod to return the created transaction - Update all child handlers to create transaction first, then use returned
id
Files:
app/api/v1/billing/channels/midtrans-snap/handler.tsapp/api/v1/billing/channels/bank-transfer/handler.tsapp/api/v1/billing/channels/midtrans-recurring/handler.tsapp/api/v1/billing/midtrans/create-payment/route.ts
Changes per file:
- Remove custom order ID generation logic
- Create transaction record first
- Use returned transaction
idfor all operations - Update redirect URLs to use table
id - Update response data to use table
idasorder_id
File: app/api/v1/billing/orders/[order_id]/route.ts → Rename to: [id]/route.ts
- Change route parameter from
order_idtoid - Update database query:
.eq('payment_reference', order_id)→.eq('id', id) - Update response mapping:
order_id: transaction.payment_reference→order_id: transaction.id
File: app/api/v1/billing/history/route.ts
- Remove
payment_referencefrom SELECT query (line 44) - Update response transformation to map
idasorder_id - Update all response objects to use table
id
Files:
app/api/v1/admin/orders/route.tsapp/api/v1/admin/orders/[id]/route.tsapp/api/v1/admin/orders/[id]/status/route.ts
Changes:
- Remove
payment_referencefrom SELECT queries - Update response transformations to use table
idas order identifier - Ensure admin interfaces use table
idfor order operations
Files:
app/api/v1/billing/midtrans-3ds-callback/route.tsapp/api/midtrans/webhook/route.ts
Changes:
- Update order lookup logic to use table
id - Update webhook processing to handle UUID order IDs
- Test gateway compatibility with new order ID format
File: lib/email/emailService.ts
- Update
sendBillingConfirmationmethod to use tableidasorderId - Update all email service calls to pass table
id
File: lib/email/templates/billing-confirmation.html
- Ensure
{{orderId}}displays the tableid - Update payment reference instructions to use table
id
Files: All frontend components
- Remove
payment_reference: stringfrom Transaction interfaces - Update all components to use
idas the order identifier - Update Order ID display elements to show
transaction.id
File: app/dashboard/settings/plans-billing/order/[id]/page.tsx
- Update Transaction interface (remove
payment_reference) - Update Order ID display:
{transaction.payment_reference}→{transaction.id} - Update Reference Number display:
{transaction.payment_reference}→{transaction.id}
Files:
app/dashboard/settings/plans-billing/components/BillingHistory.tsxapp/dashboard/settings/plans-billing/history/HistoryTab.tsxapp/dashboard/settings/plans-billing/page.tsx
Changes:
- Remove
payment_referencefrom interfaces - Update Order ID column displays to show
id - Update click handlers and navigation to use
id
Files:
app/backend/admin/orders/page.tsxapp/backend/admin/orders/[id]/page.tsx
Changes:
- Update Order ID column to display table
id - Update navigation links to use table
id - Remove
payment_referencereferences
Files:
lib/services/payments/core/PaymentProcessor.tslib/services/payments/midtrans/MidtransSnapService.tslib/payment-services/payment-router.ts
Changes:
- Remove custom order ID generation methods
- Update service responses to use table
id - Update order tracking to use table
id
Action: Remove payment_reference column from indb_payment_transactions
SQL Query for Supabase SQL Editor:
ALTER TABLE indb_payment_transactions
DROP COLUMN payment_reference;- Test complete payment flow end-to-end
- Verify all order lookups work with table
id - Test email notifications with new order IDs
- Verify admin panel functionality
- Test webhook and callback processing
- All payment flows use table
idas order identifier - No references to
payment_referenceremain in codebase - All order lookups work correctly with UUID format
- Email notifications display correct order IDs
- Admin panel fully functional with new system
- No payment processing disruptions
- Customer order tracking continues to work
- Support team can reference orders by UUID
- All existing orders remain accessible
- Gateway integration remains stable
- Monitor payment success rates
- Check customer support ticket volume
- Verify email delivery and formatting
- Monitor gateway callback processing
- Performance monitoring of UUID-based queries
- Customer feedback on new order ID format
- Support team adaptation to UUID references
- Gateway integration stability
- Immediate: Stop payment processing temporarily
- Rollback: Restore
payment_referencecolumn and revert code - Investigation: Identify root cause while system is stable
- Re-plan: Adjust migration plan based on findings
- Notify support team of new order ID format
- Update customer documentation if needed
- Prepare FAQ for UUID-based order IDs
- Train support staff on new order lookup process
Migration plan created on January 31, 2025 Total estimated effort: 2-3 weeks with testing Risk level: Medium (due to payment system criticality)