Created
April 10, 2025 03:45
-
-
Save Giovasdf/483b1fc3f0c26bddad71109e978e54fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express') | |
| const cors = require('cors') | |
| const bodyParser = require('body-parser') | |
| const { Options, IntegrationApiKeys, Environment, WebpayPlus } = require('transbank-sdk') | |
| const app = express() | |
| const PORT = 3001 | |
| app.use(cors()) | |
| app.use(bodyParser.json()) | |
| // ✅ Configuración correcta de Webpay Plus | |
| const tx = new WebpayPlus.Transaction( | |
| new Options( | |
| '597055555532', // Código de comercio de integración | |
| '579B532A7440BB0C9079DED94D31EA1615BACEB56610332264630D42D0A36B1C', // Api Key de integración | |
| Environment.Integration | |
| ) | |
| ) | |
| // Endpoint para crear transacción | |
| app.post('/api/create-transaction', async (req, res) => { | |
| try { | |
| const { amount, sessionId, buyOrder, returnUrl } = req.body | |
| if (!amount || !sessionId || !buyOrder || !returnUrl) { | |
| return res.status(400).json({ error: 'Faltan datos requeridos' }) | |
| } | |
| console.log('🔁 Creando transacción con:', { amount, sessionId, buyOrder, returnUrl }) | |
| const response = await tx.create(buyOrder, sessionId, amount, returnUrl) | |
| console.log('✅ Transacción creada:', response) | |
| res.json(response) | |
| } catch (error) { | |
| console.error('❌ Error creando transacción:', error) | |
| res.status(500).json({ error: 'Error al crear la transacción', details: error.message }) | |
| } | |
| }) | |
| // Endpoint para confirmar transacción | |
| // Endpoint para confirmar transacción | |
| app.post('/api/commit-transaction', async (req, res) => { | |
| try { | |
| const { token_ws } = req.body; | |
| const response = await tx.commit(token_ws); | |
| console.log('✅ Transacción confirmada:', response); | |
| // Verificamos si es rechazada | |
| const isRejected = | |
| response.response_code < 0 || // códigos negativos son errores | |
| response.status !== 'AUTHORIZED'; // solo 'AUTHORIZED' es válida | |
| if (isRejected) { | |
| return res.json({ | |
| status: 'rejected', | |
| buyOrder: response.buy_order, | |
| responseCode: response.response_code, | |
| responseMessage: response.response_message || 'Transacción rechazada' | |
| }); | |
| } | |
| // ✅ OJO: No mezcles ...response para evitar sobrescribir `status` | |
| res.json({ | |
| status: 'approved', | |
| buyOrder: response.buy_order, | |
| amount: response.amount, | |
| authorizationCode: response.authorization_code, | |
| paymentTypeCode: response.payment_type_code, | |
| cardNumber: response.card_detail.card_number, | |
| transactionDate: response.transaction_date | |
| }); | |
| } catch (error) { | |
| console.error('❌ Error confirmando transacción:', error); | |
| res.status(500).json({ | |
| status: 'rejected', | |
| error: 'Error al confirmar transacción', | |
| details: error.message | |
| }); | |
| } | |
| }); | |
| app.listen(PORT, () => { | |
| console.log(`🚀 Servidor corriendo en http://localhost:${PORT}`) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment