fix(payments): reemplazar PayU LatAm por ePayco como pasarela PSE
PayU LatAm (integrado en el commit anterior) resultó inviable: Rapyd compró ese negocio completo en LatAm/África en 2023, y el registro real del usuario quedó en la plataforma de Rapyd (board.rapyd.net, credenciales access_key/secret_key incompatibles con el módulo ya construido, API completamente distinta). Se evaluó reconstruir contra Rapyd, pero el usuario comparó costos para su volumen real (~8 ventas/mes) y encontró que ePayco cobra menos en PSE. Se investigó ePayco en vivo: es 100% de Davivienda desde 2024, acepta liquidación a cualquier banco colombiano (no exige Bancolombia/Nequi como Wompi) y da tarifa preferencial a cuentas Davivienda -- el usuario ya tiene una. Mejor encaje de negocio y más barato que PayU/Rapyd. Confirmado como elección definitiva. - src/modules/epayco-pse/ reemplaza a src/modules/payu-pse/: mismo contrato AbstractPaymentProvider (initiatePayment/authorizePayment "pending_authorization"/getWebhookActionAndData con firma SHA256 verificada), pero envuelve el SDK oficial `epayco-sdk-node` en vez de llamar la API cruda -- leyendo el código fuente del SDK (no la documentación pública, que resultó desactualizada en más de un campo) se confirmó que el protocolo real de ePayco para PSE es bastante más particular de lo que sugieren los ejemplos: cada campo del payload se cifra con AES usando la private key del comercio, más un login previo que da un bearer token que también viaja en la misma request. Reimplementar esto a mano tenía mucho más riesgo de error que delegar en el SDK mantenido por el propio proveedor. - Corregido en el camino: el método real para listar bancos es `bank.getBanks()`, no `pseBank()` (así aparecía en un resumen de búsqueda, pero no existe en el SDK real -- confirmado leyendo node_modules/epayco-sdk-node/lib/resources/bank.js directamente). El campo real para la URL de confirmación es `method_confirmation` (con guión bajo en medio), no `metodoconfirmacion` como mostraba la documentación pública -- confirmado contra tests/bank.js del propio paquete. - retrievePayment sí se implementó esta vez (endpoint de validación de ePayco documentado); refundPayment sigue sin implementar -- ese endpoint no quedó verificado en vivo, tira error explícito en vez de fingir éxito. - Frontend: el step de pago recupera el selector de banco + tipo de persona + documento (a diferencia del diseño que se había planeado para Rapyd, donde esos datos se recolectaban en la página del propio Rapyd/SafetyPay) -- ePayco sí necesita esos campos de nuestro lado, como PayU. - Dos puntos quedan sin verificar contra un sandbox real (anotados en el código con qué hacer si fallan): el nombre exacto del campo con la URL del banco en la respuesta de bank.create() (initiatePayment tira un error con el payload completo si no matchea ningún candidato probado), y si p_cust_id_cliente/p_key del webhook son iguales a las API keys o valores aparte del dashboard de ePayco. Verificado: build real de Medusa con las 7 env vars EPAYCO_* dummy, smoke test del wrapper del SDK cargando y la verificación de firma dando el resultado esperado contra un caso conocido, typecheck+lint+ build completo del frontend. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01THZFwPzrwv6VAbuwmaf5vR
This commit is contained in:
1 parent
95cf791737
commit
12c4afa09c
17 files changed
+471
-509
No files matched your search
+2
-2
@@ -1,6 +1,6 @@
|
||||
import { ModuleProvider, Modules } from "@medusajs/framework/utils";
|
||||
import PayuPseProviderService from "./service";
|
||||
import EpaycoPseProviderService from "./service";
|
||||
|
||||
export default ModuleProvider(Modules.PAYMENT, {
|
||||
services: [PayuPseProviderService],
|
||||
services: [EpaycoPseProviderService],
|
||||
});
|
||||
@@ -0,0 +1,234 @@
|
||||
import { AbstractPaymentProvider, BigNumber } from "@medusajs/framework/utils";
|
||||
import type {
|
||||
AuthorizePaymentInput,
|
||||
AuthorizePaymentOutput,
|
||||
CancelPaymentInput,
|
||||
CancelPaymentOutput,
|
||||
CapturePaymentInput,
|
||||
CapturePaymentOutput,
|
||||
DeletePaymentInput,
|
||||
DeletePaymentOutput,
|
||||
GetPaymentStatusInput,
|
||||
GetPaymentStatusOutput,
|
||||
InitiatePaymentInput,
|
||||
InitiatePaymentOutput,
|
||||
Logger,
|
||||
ProviderWebhookPayload,
|
||||
RefundPaymentInput,
|
||||
RefundPaymentOutput,
|
||||
RetrievePaymentInput,
|
||||
RetrievePaymentOutput,
|
||||
UpdatePaymentInput,
|
||||
UpdatePaymentOutput,
|
||||
WebhookActionResult,
|
||||
} from "@medusajs/framework/types";
|
||||
import { createEpaycoClient, verifyWebhookSignature } from "../../lib/epayco";
|
||||
import type { EpaycoPseInputData, EpaycoPseOptions, EpaycoPseSessionData } from "./types";
|
||||
|
||||
type InjectedDependencies = {
|
||||
logger: Logger;
|
||||
};
|
||||
|
||||
// PSE via ePayco es asincrono igual que PayU/Rapyd: no hay confirmacion
|
||||
// real hasta que llega el webhook (url_confirmation), nunca en la
|
||||
// respuesta sincrona de bank.create(). authorizePayment/capturePayment
|
||||
// por eso son casi no-ops -- el estado real lo trae siempre el webhook.
|
||||
export default class EpaycoPseProviderService extends AbstractPaymentProvider<EpaycoPseOptions> {
|
||||
static identifier = "epayco";
|
||||
|
||||
protected logger_: Logger;
|
||||
protected options_: EpaycoPseOptions;
|
||||
|
||||
constructor(container: InjectedDependencies, options: EpaycoPseOptions) {
|
||||
super(container, options);
|
||||
this.logger_ = container.logger;
|
||||
this.options_ = options;
|
||||
}
|
||||
|
||||
static validateOptions(options: Record<string, unknown>): void {
|
||||
for (const key of ["publicKey", "privateKey", "pCustIdCliente", "pKey", "env", "responseUrl", "notifyUrl"]) {
|
||||
if (!options[key]) {
|
||||
throw new Error(`epayco-pse: falta la opción "${key}" en medusa-config.ts`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async initiatePayment(input: InitiatePaymentInput): Promise<InitiatePaymentOutput> {
|
||||
const data = input.data as unknown as EpaycoPseInputData & { session_id: string };
|
||||
const sessionId = data.session_id;
|
||||
const amount = new BigNumber(input.amount).numeric;
|
||||
const currency = input.currency_code.toUpperCase();
|
||||
|
||||
const client = createEpaycoClient(this.options_);
|
||||
|
||||
// Campos confirmados leyendo node_modules/epayco-sdk-node/lib/keylang.json
|
||||
// y tests/bank.js directamente (no la documentacion publica -- esa
|
||||
// mostraba "metodoconfirmacion", el codigo real que de verdad manda
|
||||
// el SDK usa "method_confirmation"). El SDK cifra estos valores y
|
||||
// agrega ip/test/auth por su cuenta, no hace falta hacerlo nosotros.
|
||||
const response = await client.bank.create({
|
||||
bank: data.bank_code,
|
||||
invoice: sessionId,
|
||||
description: `Orden ARI Shopping ${sessionId}`,
|
||||
value: String(amount),
|
||||
tax: "0",
|
||||
tax_base: "0",
|
||||
currency,
|
||||
type_person: data.user_type,
|
||||
doc_type: data.doc_type,
|
||||
doc_number: data.doc_number,
|
||||
name: data.first_name,
|
||||
last_name: data.last_name,
|
||||
email: data.email,
|
||||
country: "CO",
|
||||
city: data.city,
|
||||
cell_phone: data.phone,
|
||||
url_response: this.options_.responseUrl,
|
||||
url_confirmation: this.options_.notifyUrl,
|
||||
method_confirmation: "POST",
|
||||
});
|
||||
|
||||
if (response.error) {
|
||||
throw new Error(`ePayco rechazó la transacción para la sesión ${sessionId}: ${JSON.stringify(response).slice(0, 300)}`);
|
||||
}
|
||||
|
||||
// El nombre real del campo con la URL de redirección al banco NO
|
||||
// quedó verificado contra una respuesta real de sandbox en esta
|
||||
// sesión (ni la doc ni los tests del SDK lo muestran) -- se prueban
|
||||
// los candidatos más probables y, si ninguno aparece, se tira un
|
||||
// error con el payload completo para poder leerlo en los logs y
|
||||
// corregir esto en el primer intento real contra el sandbox.
|
||||
const flat = (response.data ?? response) as Record<string, unknown>;
|
||||
const bankUrlCandidates = ["urlbanco", "url_banco", "urlBanco", "url", "redirect_url"];
|
||||
const bankUrl = bankUrlCandidates.map((key) => flat[key]).find((value) => typeof value === "string" && value);
|
||||
|
||||
if (typeof bankUrl !== "string" || !bankUrl) {
|
||||
throw new Error(
|
||||
`ePayco no devolvió una URL de banco reconocible para la sesión ${sessionId}. Respuesta completa (revisar para corregir el nombre del campo): ${JSON.stringify(response).slice(0, 500)}`,
|
||||
);
|
||||
}
|
||||
|
||||
const sessionData: EpaycoPseSessionData = {
|
||||
...data,
|
||||
session_id: sessionId,
|
||||
ref_payco: String(flat.ref_payco ?? flat.refPayco ?? ""),
|
||||
transaction_id: String(flat.transactionID ?? flat.transaction_id ?? ""),
|
||||
bank_url: bankUrl,
|
||||
state: "PENDING",
|
||||
amount,
|
||||
currency,
|
||||
};
|
||||
|
||||
return { id: sessionId, data: sessionData as unknown as Record<string, unknown>, status: "pending" };
|
||||
}
|
||||
|
||||
// PSE es diferido: en el momento de completar el carrito, ePayco
|
||||
// todavia no confirmo nada -- "pending_authorization" es el mecanismo
|
||||
// documentado de Medusa para esto (la orden queda "awaiting" y el
|
||||
// navegador ya puede redirigirse al banco).
|
||||
async authorizePayment(input: AuthorizePaymentInput): Promise<AuthorizePaymentOutput> {
|
||||
return { data: input.data, status: "pending_authorization" };
|
||||
}
|
||||
|
||||
// Casi no-op: cuando el webhook reporta "captured", ePayco ya liquido
|
||||
// el dinero de su lado -- solo persiste el data actualizado.
|
||||
async capturePayment(input: CapturePaymentInput): Promise<CapturePaymentOutput> {
|
||||
return { data: input.data };
|
||||
}
|
||||
|
||||
async cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput> {
|
||||
this.logger_.warn("epayco-pse: cancelPayment llamado, pero PSE no soporta cancelar una transacción ya enviada");
|
||||
return { data: input.data };
|
||||
}
|
||||
|
||||
async deletePayment(input: DeletePaymentInput): Promise<DeletePaymentOutput> {
|
||||
return { data: input.data };
|
||||
}
|
||||
|
||||
// Endpoint de reembolso de ePayco no quedó verificado en vivo en esta
|
||||
// sesión -- con dinero real de por medio, mejor tirar un error
|
||||
// explícito que fingir un reembolso que nunca se procesó.
|
||||
async refundPayment(_input: RefundPaymentInput): Promise<RefundPaymentOutput> {
|
||||
throw new Error(
|
||||
"epayco-pse: refundPayment no está implementado todavía -- procesar el reembolso manualmente desde el panel de ePayco por ahora.",
|
||||
);
|
||||
}
|
||||
|
||||
// GET /validation/v1/reference/{ref_payco} -- endpoint de verificación
|
||||
// server-to-server documentado por ePayco para consultar el estado
|
||||
// real de una transacción.
|
||||
async retrievePayment(input: RetrievePaymentInput): Promise<RetrievePaymentOutput> {
|
||||
const data = input.data as unknown as EpaycoPseSessionData | undefined;
|
||||
const refPayco = data?.ref_payco;
|
||||
if (!refPayco) {
|
||||
throw new Error("epayco-pse: retrievePayment necesita ref_payco, no está guardado en esta sesión.");
|
||||
}
|
||||
const response = await fetch(`https://secure.epayco.co/validation/v1/reference/${refPayco}`);
|
||||
const json = await response.json();
|
||||
return { data: json as Record<string, unknown> };
|
||||
}
|
||||
|
||||
async updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput> {
|
||||
// ePayco no tiene concepto de "modificar" una transaccion ya
|
||||
// enviada. El checkout solo llega al step de pago despues de fijar
|
||||
// direccion y envio, asi que el total no deberia cambiar una vez que
|
||||
// existe una sesion PSE.
|
||||
return { data: input.data };
|
||||
}
|
||||
|
||||
async getPaymentStatus(input: GetPaymentStatusInput): Promise<GetPaymentStatusOutput> {
|
||||
const data = input.data as unknown as EpaycoPseSessionData | undefined;
|
||||
const state = data?.state;
|
||||
if (state === "1") return { status: "captured" };
|
||||
if (state === "3") return { status: "pending" };
|
||||
if (state === "PENDING") return { status: "pending" };
|
||||
return { status: "canceled" };
|
||||
}
|
||||
|
||||
// Unico punto real de confirmacion asincrona. Falla cerrado: si la
|
||||
// firma no matchea, no se hace nada -- nunca se confia en un webhook
|
||||
// sin firma valida.
|
||||
async getWebhookActionAndData(payload: ProviderWebhookPayload["payload"]): Promise<WebhookActionResult> {
|
||||
const body = payload.data as Record<string, unknown>;
|
||||
const refPayco = String(body.x_ref_payco ?? "");
|
||||
const transactionId = String(body.x_transaction_id ?? "");
|
||||
const amount = String(body.x_amount ?? "");
|
||||
const currencyCode = String(body.x_currency_code ?? "");
|
||||
const codState = String(body.x_cod_transaction_state ?? "");
|
||||
const signature = String(body.x_signature ?? "");
|
||||
// session_id sale del invoice que nosotros mandamos -- ePayco lo
|
||||
// devuelve como x_id_invoice (o x_extra1 si no viniera) segun el
|
||||
// webhook real; se intentan ambos.
|
||||
const sessionId = String(body.x_id_invoice ?? body.x_extra1 ?? refPayco);
|
||||
|
||||
const validSignature = verifyWebhookSignature({
|
||||
pCustIdCliente: this.options_.pCustIdCliente,
|
||||
pKey: this.options_.pKey,
|
||||
refPayco,
|
||||
transactionId,
|
||||
amount,
|
||||
currencyCode,
|
||||
signature,
|
||||
});
|
||||
|
||||
if (!validSignature) {
|
||||
this.logger_.warn(`epayco-pse: webhook con firma inválida para x_ref_payco=${refPayco}, ignorado`);
|
||||
return { action: "not_supported" };
|
||||
}
|
||||
|
||||
// Solo "1" (Aceptada) es exito -- cualquier otro codigo (2 Rechazada,
|
||||
// 4 Fallida, 6 Reversada, 9 caducada, 10 abandonada, 11 Cancelada,
|
||||
// 3 Pendiente) se trata como fallido en vez de asumir un exito
|
||||
// distinto. Un "failed" de mas es reintentable; un "captured" de mas
|
||||
// (dinero que nunca se aprobo) no lo es.
|
||||
const action = codState === "1" ? "captured" : "failed";
|
||||
|
||||
return {
|
||||
action,
|
||||
data: {
|
||||
session_id: sessionId,
|
||||
amount: new BigNumber(amount || 0),
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import type { EpaycoEnv } from "../../lib/epayco";
|
||||
|
||||
export interface EpaycoPseOptions {
|
||||
publicKey: string;
|
||||
privateKey: string;
|
||||
// p_cust_id_cliente / p_key -- valores propios del dashboard de ePayco
|
||||
// para verificar la firma del webhook, distintos de publicKey/privateKey.
|
||||
pCustIdCliente: string;
|
||||
pKey: string;
|
||||
env: EpaycoEnv;
|
||||
// A donde ePayco redirige al comprador de vuelta tras pagar en el
|
||||
// banco (pagina propia del storefront, no confirma nada por si sola).
|
||||
responseUrl: string;
|
||||
// URL publica de confirmacion -- por-transaccion (a diferencia de
|
||||
// Rapyd, que solo permite configurarlo una vez a nivel de cuenta),
|
||||
// apunta a la ruta nativa de Medusa /hooks/payment/:provider.
|
||||
notifyUrl: string;
|
||||
}
|
||||
|
||||
export type EpaycoDocType = "CC" | "CE" | "NIT" | "TI" | "PP";
|
||||
// "0" persona natural, "1" persona juridica -- valores reales que espera
|
||||
// el campo type_person de ePayco.
|
||||
export type EpaycoUserType = "0" | "1";
|
||||
|
||||
// Lo que el storefront manda como `data` al crear la sesion de pago PSE
|
||||
// (recolectado en el step "address" del checkout, mas el banco/tipo de
|
||||
// persona/documento del nuevo step "payment"). No viene de
|
||||
// `context.customer` porque el checkout es de invitado -- ver ADR 0009.
|
||||
export interface EpaycoPseInputData {
|
||||
bank_code: string;
|
||||
user_type: EpaycoUserType;
|
||||
doc_type: EpaycoDocType;
|
||||
doc_number: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
city: string;
|
||||
}
|
||||
|
||||
// Lo que initiatePayment devuelve y queda guardado en `data` de la
|
||||
// PaymentSession -- publico para el storefront, nunca poner credenciales
|
||||
// acá. `bank_url`: nombre de campo AUN NO VERIFICADO contra una
|
||||
// respuesta real de sandbox (ver comentario en service.ts) -- primer
|
||||
// punto a confirmar en la primera prueba end-to-end.
|
||||
export interface EpaycoPseSessionData extends EpaycoPseInputData {
|
||||
session_id: string;
|
||||
ref_payco: string;
|
||||
transaction_id: string;
|
||||
bank_url: string;
|
||||
state: string;
|
||||
amount: number;
|
||||
currency: string;
|
||||
}
|
||||
@@ -1,260 +0,0 @@
|
||||
import { AbstractPaymentProvider, BigNumber } from "@medusajs/framework/utils";
|
||||
import type {
|
||||
AuthorizePaymentInput,
|
||||
AuthorizePaymentOutput,
|
||||
CancelPaymentInput,
|
||||
CancelPaymentOutput,
|
||||
CapturePaymentInput,
|
||||
CapturePaymentOutput,
|
||||
DeletePaymentInput,
|
||||
DeletePaymentOutput,
|
||||
GetPaymentStatusInput,
|
||||
GetPaymentStatusOutput,
|
||||
InitiatePaymentInput,
|
||||
InitiatePaymentOutput,
|
||||
Logger,
|
||||
ProviderWebhookPayload,
|
||||
RefundPaymentInput,
|
||||
RefundPaymentOutput,
|
||||
RetrievePaymentInput,
|
||||
RetrievePaymentOutput,
|
||||
UpdatePaymentInput,
|
||||
UpdatePaymentOutput,
|
||||
WebhookActionResult,
|
||||
} from "@medusajs/framework/types";
|
||||
import { buildOrderSignature, formatPayuAmount, payuRequest, verifyWebhookSignature } from "../../lib/payu";
|
||||
import type { PayuPseInputData, PayuPseOptions, PayuPseSessionData } from "./types";
|
||||
|
||||
type InjectedDependencies = {
|
||||
logger: Logger;
|
||||
};
|
||||
|
||||
// PSE es AUTHORIZATION_AND_CAPTURE en un solo paso del lado de PayU, y
|
||||
// asincrono: no hay confirmacion real hasta que llega el webhook (ver
|
||||
// getWebhookActionAndData). authorizePayment/capturePayment por eso son
|
||||
// casi no-ops -- el estado real lo trae siempre el webhook, nunca una
|
||||
// llamada sincrona nuestra a PayU.
|
||||
export default class PayuPseProviderService extends AbstractPaymentProvider<PayuPseOptions> {
|
||||
static identifier = "payu";
|
||||
|
||||
protected logger_: Logger;
|
||||
protected options_: PayuPseOptions;
|
||||
|
||||
constructor(container: InjectedDependencies, options: PayuPseOptions) {
|
||||
super(container, options);
|
||||
this.logger_ = container.logger;
|
||||
this.options_ = options;
|
||||
}
|
||||
|
||||
static validateOptions(options: Record<string, unknown>): void {
|
||||
for (const key of ["apiLogin", "apiKey", "merchantId", "accountId", "env", "responseUrl", "notifyUrl"]) {
|
||||
if (!options[key]) {
|
||||
throw new Error(`payu-pse: falta la opción "${key}" en medusa-config.ts`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async initiatePayment(input: InitiatePaymentInput): Promise<InitiatePaymentOutput> {
|
||||
const data = input.data as unknown as PayuPseInputData & { session_id: string };
|
||||
const sessionId = data.session_id;
|
||||
const amount = new BigNumber(input.amount).numeric;
|
||||
const currency = input.currency_code.toUpperCase();
|
||||
const formattedAmount = formatPayuAmount(amount);
|
||||
|
||||
const signature = buildOrderSignature({
|
||||
apiKey: this.options_.apiKey,
|
||||
merchantId: this.options_.merchantId,
|
||||
referenceCode: sessionId,
|
||||
amount: formattedAmount,
|
||||
currency,
|
||||
});
|
||||
|
||||
// Shape del request confirmado contra la documentacion real de PayU
|
||||
// LatAm para command/paymentMethod/extraParameters -- la anidacion
|
||||
// exacta de buyer/payer (nombres de campo dentro de esos objetos) no
|
||||
// quedo verificada campo por campo contra una respuesta real de
|
||||
// sandbox en esta sesion. Primera prueba end-to-end contra el
|
||||
// sandbox real de PayU debe confirmar esto (ver plan de
|
||||
// implementacion, seccion Verificacion).
|
||||
const json = await payuRequest(
|
||||
{ apiLogin: this.options_.apiLogin, apiKey: this.options_.apiKey, merchantId: this.options_.merchantId, accountId: this.options_.accountId, env: this.options_.env },
|
||||
{
|
||||
command: "SUBMIT_TRANSACTION",
|
||||
merchant: {
|
||||
apiLogin: this.options_.apiLogin,
|
||||
apiKey: this.options_.apiKey,
|
||||
},
|
||||
transaction: {
|
||||
order: {
|
||||
accountId: this.options_.accountId,
|
||||
referenceCode: sessionId,
|
||||
description: `Orden ARI Shopping ${sessionId}`,
|
||||
language: "es",
|
||||
signature,
|
||||
notifyUrl: this.options_.notifyUrl,
|
||||
additionalValues: {
|
||||
TX_VALUE: { value: amount, currency },
|
||||
},
|
||||
buyer: {
|
||||
fullName: data.full_name,
|
||||
emailAddress: data.email,
|
||||
contactPhone: data.phone,
|
||||
dniNumber: data.doc_number,
|
||||
shippingAddress: {
|
||||
street1: data.address,
|
||||
city: data.city,
|
||||
state: data.state,
|
||||
country: "CO",
|
||||
},
|
||||
},
|
||||
},
|
||||
payer: {
|
||||
fullName: data.full_name,
|
||||
emailAddress: data.email,
|
||||
contactPhone: data.phone,
|
||||
dniNumber: data.doc_number,
|
||||
billingAddress: {
|
||||
street1: data.address,
|
||||
city: data.city,
|
||||
state: data.state,
|
||||
country: "CO",
|
||||
},
|
||||
},
|
||||
type: "AUTHORIZATION_AND_CAPTURE",
|
||||
paymentMethod: "PSE",
|
||||
paymentCountry: "CO",
|
||||
extraParameters: {
|
||||
RESPONSE_URL: this.options_.responseUrl,
|
||||
FINANCIAL_INSTITUTION_CODE: data.bank_code,
|
||||
USER_TYPE: data.user_type,
|
||||
PSE_REFERENCE2: data.doc_type,
|
||||
PSE_REFERENCE3: data.doc_number,
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const transactionResponse = (json.transactionResponse ?? {}) as Record<string, unknown>;
|
||||
const extraParameters = (transactionResponse.extraParameters ?? {}) as Record<string, unknown>;
|
||||
const bankUrl = extraParameters.BANK_URL;
|
||||
if (typeof bankUrl !== "string" || !bankUrl) {
|
||||
throw new Error(
|
||||
`PayU no devolvió BANK_URL para la sesión ${sessionId}: ${JSON.stringify(transactionResponse).slice(0, 300)}`,
|
||||
);
|
||||
}
|
||||
|
||||
const sessionData: PayuPseSessionData = {
|
||||
...data,
|
||||
session_id: sessionId,
|
||||
payu_order_id: String(transactionResponse.orderId ?? ""),
|
||||
payu_transaction_id: String(transactionResponse.transactionId ?? ""),
|
||||
reference_code: sessionId,
|
||||
bank_url: bankUrl,
|
||||
state: String(transactionResponse.state ?? "PENDING"),
|
||||
amount,
|
||||
currency,
|
||||
};
|
||||
|
||||
return { id: sessionId, data: sessionData as unknown as Record<string, unknown>, status: "pending" };
|
||||
}
|
||||
|
||||
// PSE es diferido: en el momento de completar el carrito, PayU todavia
|
||||
// no confirmo nada -- "pending_authorization" es el mecanismo
|
||||
// documentado de Medusa para esto (la orden queda "awaiting" y el
|
||||
// navegador ya puede redirigirse al banco).
|
||||
async authorizePayment(input: AuthorizePaymentInput): Promise<AuthorizePaymentOutput> {
|
||||
return { data: input.data, status: "pending_authorization" };
|
||||
}
|
||||
|
||||
// Casi no-op: cuando el webhook reporta "captured", PayU ya liquido el
|
||||
// dinero de su lado (un solo paso AUTHORIZATION_AND_CAPTURE) -- solo
|
||||
// persiste el data actualizado.
|
||||
async capturePayment(input: CapturePaymentInput): Promise<CapturePaymentOutput> {
|
||||
return { data: input.data };
|
||||
}
|
||||
|
||||
// PSE no tiene concepto de cancelar una transaccion ya enviada al banco.
|
||||
async cancelPayment(input: CancelPaymentInput): Promise<CancelPaymentOutput> {
|
||||
this.logger_.warn("payu-pse: cancelPayment llamado, pero PSE no soporta cancelar una transacción ya enviada");
|
||||
return { data: input.data };
|
||||
}
|
||||
|
||||
async deletePayment(input: DeletePaymentInput): Promise<DeletePaymentOutput> {
|
||||
return { data: input.data };
|
||||
}
|
||||
|
||||
// No verificado contra el endpoint real de PayU en esta sesión -- con
|
||||
// dinero real de por medio, mejor tirar un error explícito que fingir
|
||||
// un reembolso que nunca llamó a PayU.
|
||||
async refundPayment(_input: RefundPaymentInput): Promise<RefundPaymentOutput> {
|
||||
throw new Error(
|
||||
"payu-pse: refundPayment no está implementado todavía -- el endpoint de reembolso de PayU LatAm no quedó verificado en vivo. Procesar el reembolso manualmente desde el panel de PayU por ahora.",
|
||||
);
|
||||
}
|
||||
|
||||
// Idem refundPayment: no verificado en vivo, mejor fallar explícito.
|
||||
async retrievePayment(_input: RetrievePaymentInput): Promise<RetrievePaymentOutput> {
|
||||
throw new Error("payu-pse: retrievePayment no está implementado todavía -- usar el panel de PayU para consultar una transacción.");
|
||||
}
|
||||
|
||||
async updatePayment(input: UpdatePaymentInput): Promise<UpdatePaymentOutput> {
|
||||
// PayU no tiene concepto de "modificar" una transaccion ya enviada.
|
||||
// El checkout solo llega al step de pago despues de fijar direccion
|
||||
// y envio, asi que el total no deberia cambiar una vez que existe
|
||||
// una sesion PSE -- si esto se dispara, es una señal de que algo
|
||||
// cambió el carrito después de crear la sesión.
|
||||
return { data: input.data };
|
||||
}
|
||||
|
||||
async getPaymentStatus(input: GetPaymentStatusInput): Promise<GetPaymentStatusOutput> {
|
||||
const data = input.data as unknown as PayuPseSessionData | undefined;
|
||||
const state = data?.state;
|
||||
if (state === "APPROVED") return { status: "captured" };
|
||||
if (state === "DECLINED" || state === "EXPIRED" || state === "ERROR") return { status: "canceled" };
|
||||
return { status: "pending" };
|
||||
}
|
||||
|
||||
// Unico punto real de confirmacion asincrona. Falla cerrado: si la
|
||||
// firma no matchea, no se hace nada -- nunca se confia en un webhook
|
||||
// sin firma valida, es lo unico que impide que cualquiera falsifique
|
||||
// un pago aprobado.
|
||||
async getWebhookActionAndData(payload: ProviderWebhookPayload["payload"]): Promise<WebhookActionResult> {
|
||||
const body = payload.data as Record<string, unknown>;
|
||||
const merchantId = String(body.merchant_id ?? "");
|
||||
const referenceSale = String(body.reference_sale ?? "");
|
||||
const value = String(body.value ?? "");
|
||||
const currency = String(body.currency ?? "");
|
||||
const statePol = String(body.state_pol ?? "");
|
||||
const sign = String(body.sign ?? "");
|
||||
|
||||
const validSignature = verifyWebhookSignature({
|
||||
apiKey: this.options_.apiKey,
|
||||
merchantId,
|
||||
referenceSale,
|
||||
value,
|
||||
currency,
|
||||
statePol,
|
||||
sign,
|
||||
});
|
||||
|
||||
if (!validSignature) {
|
||||
this.logger_.warn(`payu-pse: webhook con firma inválida para reference_sale=${referenceSale}, ignorado`);
|
||||
return { action: "not_supported" };
|
||||
}
|
||||
|
||||
// Solo state_pol "4" (aprobado) es exito -- la tabla completa de
|
||||
// codigos no quedo 100% verificada, asi que cualquier otro valor se
|
||||
// trata como fallido en vez de asumir que es un exito distinto. Un
|
||||
// "failed" de mas es reintentable; un "captured" de mas (dinero que
|
||||
// nunca se aprobo) no lo es.
|
||||
const action = statePol === "4" ? "captured" : "failed";
|
||||
|
||||
return {
|
||||
action,
|
||||
data: {
|
||||
session_id: referenceSale,
|
||||
amount: new BigNumber(value || 0),
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
import type { PayuEnv } from "../../lib/payu";
|
||||
|
||||
export interface PayuPseOptions {
|
||||
apiLogin: string;
|
||||
apiKey: string;
|
||||
merchantId: string;
|
||||
accountId: string;
|
||||
env: PayuEnv;
|
||||
// A donde PayU redirige al comprador de vuelta tras pagar en el banco
|
||||
// (pagina propia del storefront, no confirma nada por si sola -- ver
|
||||
// src/modules/payu-pse/service.ts).
|
||||
responseUrl: string;
|
||||
// URL publica de notifyUrl -- la rellena Medusa (ruta nativa
|
||||
// /hooks/payment/:provider, no la escribimos nosotros), ver
|
||||
// medusa-config.ts.
|
||||
notifyUrl: string;
|
||||
}
|
||||
|
||||
export type PayuDocType = "CC" | "CE" | "NIT" | "TI" | "PP";
|
||||
export type PayuUserType = "N" | "J";
|
||||
|
||||
// Lo que el storefront manda como `data` al crear la sesion de pago PSE
|
||||
// (recolectado en el step "address" del checkout, mas el banco elegido en
|
||||
// el nuevo step "payment"). No viene de `context.customer` porque el
|
||||
// checkout es de invitado -- ver ADR 0009.
|
||||
export interface PayuPseInputData {
|
||||
bank_code: string;
|
||||
user_type: PayuUserType;
|
||||
doc_type: PayuDocType;
|
||||
doc_number: string;
|
||||
full_name: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
address: string;
|
||||
city: string;
|
||||
// Nombre del departamento (PayU pide "state" como texto libre, no un
|
||||
// codigo DIVIPOLA como el resto del checkout).
|
||||
state: string;
|
||||
}
|
||||
|
||||
// Lo que initiatePayment devuelve y queda guardado en `data` de la
|
||||
// PaymentSession -- publico para el storefront (ver nota en
|
||||
// AbstractPaymentProvider.initiatePayment), nunca poner credenciales acá.
|
||||
export interface PayuPseSessionData extends PayuPseInputData {
|
||||
session_id: string;
|
||||
payu_order_id: string;
|
||||
payu_transaction_id: string;
|
||||
reference_code: string;
|
||||
bank_url: string;
|
||||
state: string;
|
||||
amount: number;
|
||||
currency: string;
|
||||
}
|
||||
Reference in new issue
Block a user