Files
NightSpire/web-next/lib/pay-with-dpoints.ts
T
Inna 1b0920d11f Permitir pagar los servicios con PD, Stripe o SumUp (elección)
Los 9 servicios con precio en euros (rename, customize, change-race,
change-faction, level-up, gold, transfer, restore-item, send-gift) ahora
ofrecen un selector de forma de pago con las 3 opciones: saldo PD, tarjeta
(Stripe) o tarjeta (SumUp).

- lib/dpoints: spendDPoints() descuenta PD de forma atómica (FOR UPDATE).
- lib/pay-with-dpoints: paga con saldo PD, ejecuta la acción al momento y
  reembolsa si la ejecución falla; 100 PD = 1 €.
- rutas checkout (character/[service] y gift): rama provider='pd' que valida
  saldo, descuenta, ejecuta fulfill y devuelve la URL de éxito (sin pasarela).
- service-success: rama provider='pd' (la entrega ya se hizo en el checkout).
- PaymentMethodSelect: componente compartido con coste por método y saldo;
  desactiva PD si no hay saldo suficiente.
- Formularios (PaidServiceForm, Gold, Transfer, RestoreItems, SendGift) y sus
  páginas pasan el saldo PD y envían el método elegido + locale.
- i18n: namespace Pay (es/en); añadidas claves Paid.restore-item que faltaban.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-14 21:44:31 +00:00

36 lines
1.2 KiB
TypeScript

import { spendDPoints, creditDPoints, PD_PER_UNIT } from './dpoints'
/** Coste en PD de un precio en euros (100 PD = 1 €). */
export function dpointsCost(priceEur: number): number {
return Math.round(priceEur * PD_PER_UNIT)
}
/**
* Paga un servicio con el saldo PD: descuenta el coste de forma atómica y
* ejecuta la acción al momento (sin pasarela). Si la ejecución falla, reembolsa
* el saldo. Devuelve `insufficientPd` si no hay saldo suficiente.
*/
export async function payServiceWithDPoints(
accountId: number,
priceEur: number,
fulfill: () => Promise<boolean>,
): Promise<{ success: boolean; error?: string }> {
const cost = dpointsCost(priceEur)
if (!(cost > 0)) return { success: false, error: 'invalidRequest' }
const spent = await spendDPoints(accountId, cost)
if (!spent.success) {
return { success: false, error: spent.error === 'insufficientFunds' ? 'insufficientPd' : 'genericError' }
}
let ok = false
try {
ok = await fulfill()
} catch {
ok = false
}
if (!ok) {
await creditDPoints(accountId, cost) // reembolso si no se pudo ejecutar
return { success: false, error: 'fulfillFailed' }
}
return { success: true }
}