UNPKG

esnekpos

Version:

Node.js entegrasyon paketi - EsnekPOS ödeme sistemi için resmi olmayan istemci

401 lines (354 loc) 11.8 kB
/** * EsnekPOS Otomatik Link Test Aracı * * Bu araç, 3D ödeme sayfası, ortak ödeme sayfası gibi EsnekPOS'tan * dönen URL'leri otomatik olarak açar ve sonuçları izler. */ const open = require('open'); const readline = require('readline'); const esnekpos = require('../src/index'); const express = require('express'); const bodyParser = require('body-parser'); const path = require('path'); const fs = require('fs'); // Test sonuçları için dizin const resultsDir = path.join(__dirname, 'test-results'); if (!fs.existsSync(resultsDir)) { fs.mkdirSync(resultsDir, { recursive: true }); } // Basit Express sunucusu oluştur (callback için) const app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); const port = 3001; // Test için EsnekPOS istemcisi const client = esnekpos.createClient({ merchant: 'TEST1234', merchantKey: '4oK26hK8MOXrIV1bzTRVPA==', testMode: true }); // Terminal arayüzü oluştur const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // Callback handler app.all('/callback', (req, res) => { console.log('\n✅ CALLBACK ALINDI'); console.log('-------------------------------'); const data = { timestamp: new Date().toISOString(), method: req.method, body: req.body, query: req.query, headers: req.headers }; console.log(JSON.stringify(data, null, 2)); console.log('-------------------------------'); // Sonucu kaydet const filename = `callback_result_${Date.now()}.json`; fs.writeFileSync(path.join(resultsDir, filename), JSON.stringify(data, null, 2)); // Kullanıcıya basit bir başarı sayfası göster res.send(` <html> <head> <title>Test Başarılı</title> <meta http-equiv="refresh" content="3;url=http://localhost:${port}"> <style> body { font-family: Arial; text-align: center; margin-top: 50px; } .success { color: green; font-size: 24px; } </style> </head> <body> <h1 class="success">İşlem Başarıyla Tamamlandı!</h1> <p>Bu pencere 3 saniye içinde kapanacak...</p> <script> setTimeout(() => { window.close(); }, 3000); </script> </body> </html> `); // Ana menüye dön setTimeout(() => { showMenu(); }, 1000); }); // Durum sayfası app.get('/', (req, res) => { res.send(` <html> <head> <title>EsnekPOS Link Test Aracı</title> <style> body { font-family: Arial; margin: 40px; line-height: 1.6; } h1 { color: #333; } .container { max-width: 800px; } .status { background: #f5f5f5; padding: 15px; border-radius: 5px; } .success { color: green; } </style> </head> <body> <div class="container"> <h1>EsnekPOS Link Test Aracı</h1> <div class="status"> <p class="success">Sunucu çalışıyor...</p> <p>Callback URL: <code>http://localhost:${port}/callback</code></p> <p>Bu sayfayı kapatabilirsiniz. Komut satırından test işlemlerine devam edin.</p> </div> </div> </body> </html> `); }); // Sunucuyu başlat const server = app.listen(port, () => { console.log(`Link Test Sunucusu başlatıldı: http://localhost:${port}`); console.log(`Callback URL: http://localhost:${port}/callback\n`); showMenu(); }); // Ana test menüsü function showMenu() { console.log('\n===== EsnekPOS Link Test Aracı ====='); console.log('1. 3D Ödeme Test Et'); console.log('2. Ortak Ödeme Sayfası Test Et'); console.log('3. BKM Express Test Et'); console.log('4. Tekrarlı Ödeme Test Et'); console.log('0. Çıkış'); rl.question('\nLütfen bir seçenek girin: ', async (answer) => { const option = parseInt(answer); try { switch(option) { case 1: await test3DPayment(); break; case 2: await testCommonPayment(); break; case 3: await testBkmPayment(); break; case 4: await testRecurringPayment(); break; case 0: console.log('Çıkış yapılıyor...'); rl.close(); server.close(() => { process.exit(0); }); return; default: console.log('Geçersiz seçenek!'); showMenu(); } } catch(err) { console.error('Hata oluştu:', err.message); showMenu(); } }); } // 3D Ödeme Test async function test3DPayment() { console.log('\n--- 3D Ödeme Testi Başlatılıyor ---'); const orderRef = `LINK_TEST_${Date.now()}`; console.log(`Sipariş No: ${orderRef}`); try { const result = await client.payment.create3DPayment({ Config: { BACK_URL: `http://localhost:${port}/callback`, PRICES_CURRENCY: 'TRY', ORDER_REF_NUMBER: orderRef, ORDER_AMOUNT: '100.00' }, CreditCard: { CC_NUMBER: '4159562885391991', EXP_MONTH: '12', EXP_YEAR: '2025', CC_CVV: '123', CC_OWNER: 'Test User', INSTALLMENT_NUMBER: '1' }, Customer: { FIRST_NAME: 'Test', LAST_NAME: 'User', MAIL: 'test@example.com', PHONE: '5321234567', CITY: 'Istanbul', STATE: 'Kadikoy', ADDRESS: 'Test Address', CLIENT_IP: '127.0.0.1' }, Product: [ { PRODUCT_ID: '1', PRODUCT_NAME: 'Test Product', PRODUCT_CATEGORY: 'Electronics', PRODUCT_DESCRIPTION: 'Test Description', PRODUCT_AMOUNT: '100.00' } ] }); console.log('\n✅ API yanıtı alındı'); console.log(`URL: ${result.URL_3DS}`); // Sonucu kaydet const filename = `3d_request_${Date.now()}.json`; fs.writeFileSync(path.join(resultsDir, filename), JSON.stringify(result, null, 2)); // Tarayıcıda aç console.log('\nTarayıcıda açılıyor...'); await open(result.URL_3DS); console.log('\nBaşarıyla açıldı. EsnekPOS sayfasını takip edin ve işlemi tamamlayın.'); console.log('İşlem sonucu callback URL\'e gönderilecektir.'); } catch (error) { console.error('3D ödeme hatası:', error.message); showMenu(); } } // Ortak Ödeme Sayfası Test async function testCommonPayment() { console.log('\n--- Ortak Ödeme Sayfası Testi Başlatılıyor ---'); const orderRef = `LINK_TEST_${Date.now()}`; console.log(`Sipariş No: ${orderRef}`); try { const result = await client.payment.createCommonPayment({ Config: { ORDER_REF_NUMBER: orderRef, ORDER_AMOUNT: '150.00', PRICES_CURRENCY: 'TRY', BACK_URL: `http://localhost:${port}/callback`, LOCALE: 'tr' }, Customer: { FIRST_NAME: 'Test', LAST_NAME: 'User', MAIL: 'test@example.com', PHONE: '5321234567', CITY: 'Istanbul', STATE: 'Kadikoy', ADDRESS: 'Test Address' }, Product: [ { PRODUCT_ID: '1', PRODUCT_NAME: 'Test Product', PRODUCT_CATEGORY: 'Electronics', PRODUCT_DESCRIPTION: 'Test Description', PRODUCT_AMOUNT: '150.00' } ] }); console.log('\n✅ API yanıtı alındı'); console.log(`URL: ${result.URL_3DS}`); // Sonucu kaydet const filename = `common_request_${Date.now()}.json`; fs.writeFileSync(path.join(resultsDir, filename), JSON.stringify(result, null, 2)); // Tarayıcıda aç console.log('\nTarayıcıda açılıyor...'); await open(result.URL_3DS); console.log('\nBaşarıyla açıldı. EsnekPOS sayfasını takip edin ve işlemi tamamlayın.'); console.log('İşlem sonucu callback URL\'e gönderilecektir.'); } catch (error) { console.error('Ortak ödeme sayfası hatası:', error.message); showMenu(); } } // BKM Express Test async function testBkmPayment() { console.log('\n--- BKM Express Testi Başlatılıyor ---'); const orderRef = `LINK_TEST_${Date.now()}`; console.log(`Sipariş No: ${orderRef}`); try { const result = await client.payment.createBkmPayment({ Config: { ORDER_REF_NUMBER: orderRef, ORDER_AMOUNT: '200.00', PRICES_CURRENCY: 'TRY', BACK_URL: `http://localhost:${port}/callback` }, Customer: { FIRST_NAME: 'Test', LAST_NAME: 'User', MAIL: 'test@example.com', PHONE: '5321234567', CITY: 'Istanbul', STATE: 'Kadikoy', ADDRESS: 'Test Address' }, Product: [ { PRODUCT_ID: '1', PRODUCT_NAME: 'Test Product', PRODUCT_CATEGORY: 'Electronics', PRODUCT_DESCRIPTION: 'Test Description', PRODUCT_AMOUNT: '200.00' } ] }); console.log('\n✅ API yanıtı alındı'); console.log(`URL: ${result.URL_3DS}`); // Sonucu kaydet const filename = `bkm_request_${Date.now()}.json`; fs.writeFileSync(path.join(resultsDir, filename), JSON.stringify(result, null, 2)); // Tarayıcıda aç console.log('\nTarayıcıda açılıyor...'); await open(result.URL_3DS); console.log('\nBaşarıyla açıldı. BKM Express sayfasını takip edin ve işlemi tamamlayın.'); console.log('İşlem sonucu callback URL\'e gönderilecektir.'); } catch (error) { console.error('BKM Express hatası:', error.message); showMenu(); } } // Tekrarlı Ödeme Test async function testRecurringPayment() { console.log('\n--- Tekrarlı Ödeme Testi Başlatılıyor ---'); const orderRef = `LINK_TEST_${Date.now()}`; console.log(`Sipariş No: ${orderRef}`); try { const tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1); const startDate = tomorrow.toISOString().split('T')[0]; const result = await client.recurring.createRecurringPayment({ Config: { ORDER_REF_NUMBER: orderRef, BACK_URL: `http://localhost:${port}/callback`, PRICES_CURRENCY: 'TRY', ORDER_AMOUNT: '50.00', REPEAT: '3', TRIES_COUNT: '3', START_DATE: startDate }, Cards: [ { CC_NUMBER: '4159562885391991', EXP_MONTH: '12', EXP_YEAR: '2025', CC_CVV: '123', CC_OWNER: 'Test User' } ], Customer: { FIRST_NAME: 'Test', LAST_NAME: 'User', MAIL: 'test@example.com', PHONE: '5321234567', CITY: 'Istanbul', STATE: 'Kadikoy', ADDRESS: 'Test Address', CLIENT_IP: '127.0.0.1' } }); console.log('\n✅ API yanıtı alındı'); console.log(JSON.stringify(result, null, 2)); // Sonucu kaydet const filename = `recurring_request_${Date.now()}.json`; fs.writeFileSync(path.join(resultsDir, filename), JSON.stringify(result, null, 2)); console.log('\nTekrarlı ödeme başarıyla oluşturuldu.'); console.log(`İlk ödeme ${startDate} tarihinde gerçekleşecek.`); showMenu(); } catch (error) { console.error('Tekrarlı ödeme hatası:', error.message); showMenu(); } } // Gerekli bağımlılıklar // npm install open express body-parser