final
This commit is contained in:
@@ -192,23 +192,35 @@ export async function POST(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
// Get points_to_chf setting
|
||||
// Get points_to_eur setting (fallback to points_to_chf for backward compatibility)
|
||||
const [settingsRows] = await connection.execute(
|
||||
'SELECT setting_value FROM referral_settings WHERE setting_key = ?',
|
||||
['points_to_chf']
|
||||
'SELECT setting_key, setting_value FROM referral_settings WHERE setting_key IN (?, ?)',
|
||||
['points_to_eur', 'points_to_chf']
|
||||
)
|
||||
const settings = settingsRows as any[]
|
||||
const pointsToChf = parseFloat(settings[0]?.setting_value || '100')
|
||||
|
||||
// Calculate discount in CHF, then convert to user's currency
|
||||
const discountChf = pointsToUse / pointsToChf
|
||||
let pointsToEur = parseFloat(settings.find(s => s.setting_key === 'points_to_eur')?.setting_value || '0')
|
||||
|
||||
// Convert discount based on user's currency
|
||||
// If points_to_eur not found, use points_to_chf and convert
|
||||
if (pointsToEur === 0) {
|
||||
const pointsToChf = parseFloat(settings.find(s => s.setting_key === 'points_to_chf')?.setting_value || '100')
|
||||
// Convert CHF-based points to EUR-based (1 CHF ≈ 1.0309 EUR)
|
||||
pointsToEur = pointsToChf / 1.030927835
|
||||
}
|
||||
|
||||
if (pointsToEur === 0) {
|
||||
pointsToEur = 100 // Default fallback
|
||||
}
|
||||
|
||||
// Calculate discount in EUR first (universal base currency)
|
||||
const discountEur = pointsToUse / pointsToEur
|
||||
|
||||
// Convert discount to user's currency
|
||||
if (currency === 'CHF') {
|
||||
pointsDiscount = discountChf
|
||||
// Convert EUR to CHF (1 EUR = 0.97 CHF)
|
||||
pointsDiscount = discountEur * 0.97
|
||||
} else {
|
||||
// Convert CHF to EUR (1 CHF ≈ 1.03 EUR)
|
||||
pointsDiscount = discountChf * 1.03
|
||||
// Already in EUR
|
||||
pointsDiscount = discountEur
|
||||
}
|
||||
|
||||
// Don't allow discount to exceed the product price (before shipping)
|
||||
|
||||
Reference in New Issue
Block a user