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>
This commit is contained in:
@@ -85,3 +85,43 @@ export async function transferDPoints(
|
||||
conn.release()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Descuenta `points` PD del saldo de una cuenta de forma atómica (bloqueo de
|
||||
* fila para evitar dobles gastos concurrentes). Se usa para pagar servicios con
|
||||
* el saldo PD. Devuelve `insufficientFunds` si no hay saldo suficiente.
|
||||
* Si la acción posterior falla, reembolsa con `creditDPoints`.
|
||||
*/
|
||||
export async function spendDPoints(
|
||||
accountId: number,
|
||||
points: number,
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
if (!accountId || !Number.isInteger(points) || points <= 0) {
|
||||
return { success: false, error: 'invalidRequest' }
|
||||
}
|
||||
const conn = await db(DB.default).getConnection()
|
||||
try {
|
||||
await conn.beginTransaction()
|
||||
const [src] = await conn.query<RowDataPacket[]>(
|
||||
'SELECT dp FROM home_api_points WHERE accountID = ? FOR UPDATE',
|
||||
[accountId],
|
||||
)
|
||||
const balance = src[0] ? Number(src[0].dp) : 0
|
||||
if (balance < points) {
|
||||
await conn.rollback()
|
||||
return { success: false, error: 'insufficientFunds' }
|
||||
}
|
||||
await conn.query('UPDATE home_api_points SET dp = dp - ? WHERE accountID = ?', [points, accountId])
|
||||
await conn.commit()
|
||||
return { success: true }
|
||||
} catch {
|
||||
try {
|
||||
await conn.rollback()
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
return { success: false, error: 'genericError' }
|
||||
} finally {
|
||||
conn.release()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
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 }
|
||||
}
|
||||
Reference in New Issue
Block a user