esnekpos
Version:
Node.js entegrasyon paketi - EsnekPOS ödeme sistemi için resmi olmayan istemci
436 lines (376 loc) • 12.4 kB
JavaScript
/**
* EsnekPOS API Kullanım Örnekleri - Ana Dosya
*
* Bu dosya, EsnekPOS entegrasyonunun tüm senaryolarını test etmek için kullanılabilir.
*/
const esnekpos = require('../src/index');
const readline = require('readline');
// Test için istemci oluştur
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
});
// Ana menü
function showMenu() {
console.log('\n===== EsnekPOS API Test Menüsü =====');
console.log('1. 3D Ödeme Başlat');
console.log('2. Ortak Ödeme Sayfası Başlat');
console.log('3. BKM Express Ödeme Başlat');
console.log('4. İşlem Sorgula');
console.log('5. İade/İptal İşlemi');
console.log('6. Taksit Seçeneklerini Listele');
console.log('7. Üye İşyeri Bakiyesi Sorgula');
console.log('8. BIN Sorgula');
console.log('9. Pazaryeri Alt Üye İşyeri Tanımla');
console.log('10. Tekrarlı Ödeme Başlat');
console.log('11. Fiziksel POS Listele');
console.log('0. Çıkış');
rl.question('\nLütfen bir seçenek girin: ', async (answer) => {
const option = parseInt(answer);
try {
switch(option) {
case 1:
await run3DPayment();
break;
case 2:
await runCommonPayment();
break;
case 3:
await runBkmPayment();
break;
case 4:
await runQueryTransaction();
break;
case 5:
await runRefundTransaction();
break;
case 6:
await runGetInstallments();
break;
case 7:
await runGetDealerBalance();
break;
case 8:
await runBinQuery();
break;
case 9:
await runSetSubMerchant();
break;
case 10:
await runRecurringPayment();
break;
case 11:
await runListPhysicalPos();
break;
case 0:
console.log('Çıkış yapılıyor...');
rl.close();
return;
default:
console.log('Geçersiz seçenek!');
}
} catch(err) {
console.error('Hata oluştu:', err.message);
}
// Ana menüye dön
showMenu();
});
}
// 3D Ödeme
async function run3DPayment() {
console.log('\n--- 3D Ödeme Başlatılıyor ---');
const orderRef = `ORDER_${Date.now()}`;
console.log(`Sipariş No: ${orderRef}`);
const result = await client.payment.create3DPayment({
Config: {
BACK_URL: 'https://example.com/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('3D Ödeme yanıtı:', JSON.stringify(result, null, 2));
console.log('\n3D URL:', result.URL_3DS);
console.log('\nBu URL\'e tarayıcıdan giderek ödeme işlemini tamamlayabilirsiniz.');
console.log('İşlem tamamlandıktan sonra BACK_URL adresine yönlendirileceksiniz.');
return result;
}
// Ortak Ödeme Sayfası
async function runCommonPayment() {
console.log('\n--- Ortak Ödeme Sayfası Başlatılıyor ---');
const orderRef = `ORDER_${Date.now()}`;
console.log(`Sipariş No: ${orderRef}`);
const result = await client.payment.createCommonPayment({
Config: {
ORDER_REF_NUMBER: orderRef,
ORDER_AMOUNT: '150.00',
PRICES_CURRENCY: 'TRY',
BACK_URL: 'https://example.com/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('Ortak Ödeme Sayfası yanıtı:', JSON.stringify(result, null, 2));
console.log('\nÖdeme URL:', result.URL_3DS);
console.log('\nBu URL\'e tarayıcıdan giderek ödeme sayfasına erişebilirsiniz.');
return result;
}
// BKM Express Ödeme
async function runBkmPayment() {
console.log('\n--- BKM Express Ödeme Başlatılıyor ---');
const orderRef = `ORDER_${Date.now()}`;
console.log(`Sipariş No: ${orderRef}`);
const result = await client.payment.createBkmPayment({
Config: {
ORDER_REF_NUMBER: orderRef,
ORDER_AMOUNT: '200.00',
PRICES_CURRENCY: 'TRY',
BACK_URL: 'https://example.com/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('BKM Express Ödeme yanıtı:', JSON.stringify(result, null, 2));
console.log('\nBKM Express URL:', result.URL_3DS);
console.log('\nBu URL\'e tarayıcıdan giderek BKM Express ödeme sayfasına erişebilirsiniz.');
return result;
}
// İşlem Sorgulama
async function runQueryTransaction() {
rl.question('\nSorgulamak istediğiniz sipariş referans numarasını girin: ', async (orderRef) => {
console.log(`\n--- Sipariş Sorgulanıyor: ${orderRef} ---`);
try {
const result = await client.query.queryTransaction(orderRef);
console.log('Sorgu sonucu:', JSON.stringify(result, null, 2));
} catch (error) {
console.error('Sorgulama hatası:', error.message);
}
showMenu();
});
// Bu fonksiyon zaten kendi içinde showMenu'yü çağırdığı için Promise döndürmeyeceğiz
return new Promise(resolve => {});
}
// İade/İptal İşlemi
async function runRefundTransaction() {
rl.question('\nİade/İptal yapmak istediğiniz sipariş referans numarasını girin: ', async (orderRef) => {
rl.question('İade/İptal tutarını girin: ', async (amount) => {
console.log(`\n--- İade/İptal İşlemi: ${orderRef}, Tutar: ${amount} ---`);
try {
const result = await client.refund.refundTransaction({
orderRefNumber: orderRef,
amount: parseFloat(amount),
syncWithPos: true
});
console.log('İade/İptal sonucu:', JSON.stringify(result, null, 2));
} catch (error) {
console.error('İade/İptal hatası:', error.message);
}
showMenu();
});
});
return new Promise(resolve => {});
}
// Taksit Seçenekleri Sorgulama
async function runGetInstallments() {
rl.question('\nTutar girin: ', async (amount) => {
rl.question('Kart BIN numarası (ilk 6 hane, opsiyonel): ', async (bin) => {
console.log(`\n--- Taksit Seçenekleri Sorgulanıyor ---`);
const options = {
amount: amount,
commissionForCustomer: 1
};
if (bin && bin.length === 6) {
options.bin = bin;
}
try {
const result = await client.query.getInstallmentOptions(options);
console.log('Taksit seçenekleri:', JSON.stringify(result, null, 2));
} catch (error) {
console.error('Taksit sorgulama hatası:', error.message);
}
showMenu();
});
});
return new Promise(resolve => {});
}
// Üye İşyeri Bakiyesi Sorgulama
async function runGetDealerBalance() {
console.log('\n--- Üye İşyeri Bakiyesi Sorgulanıyor ---');
try {
const result = await client.query.getDealerBalance({
currency: 'TRY'
});
console.log('Bakiye bilgisi:', JSON.stringify(result, null, 2));
} catch (error) {
console.error('Bakiye sorgulama hatası:', error.message);
}
return result;
}
// BIN Sorgulama
async function runBinQuery() {
rl.question('\nSorgulamak istediğiniz BIN numarasını girin (ilk 6 hane): ', async (bin) => {
console.log(`\n--- BIN Sorgulanıyor: ${bin} ---`);
try {
const result = await client.query.getBinInfo(bin);
console.log('BIN bilgisi:', JSON.stringify(result, null, 2));
} catch (error) {
console.error('BIN sorgulama hatası:', error.message);
}
showMenu();
});
return new Promise(resolve => {});
}
// Alt Üye İşyeri Tanımlama
async function runSetSubMerchant() {
console.log('\n--- Pazaryeri Alt Üye İşyeri Tanımlanıyor ---');
const externalId = `MERCHANT_${Date.now()}`;
try {
const result = await client.marketplace.setSubMerchant({
NAME: 'Test Mağaza',
OWNER_NAME: 'Test',
OWNER_SURNAME: 'User',
OWNER_IDENTITY_NUMBER: '12345678901',
EMAIL: 'teststore@example.com',
GSM: '5321234567',
COMPANY_NAME: 'Test Store Ltd',
TAX_OFFICE: 'Test Tax Office',
TAX_NUMBER: '1234567890',
BANK_NAME: 'Test Bank',
EXTERNAL_ID: externalId,
TYPE: 'PERSONAL_COMPANY',
BANK_ACCOUNTS: [
{
IBAN: 'TR330006100519786457841326',
CURRENCY: 'TRY'
}
]
});
console.log('Alt üye işyeri tanımlama sonucu:', JSON.stringify(result, null, 2));
console.log(`Oluşturulan External ID: ${externalId}`);
} catch (error) {
console.error('Alt üye işyeri tanımlama hatası:', error.message);
}
return result;
}
// Tekrarlı Ödeme
async function runRecurringPayment() {
console.log('\n--- Tekrarlı Ödeme Başlatılıyor ---');
const orderRef = `RECUR_${Date.now()}`;
console.log(`Sipariş No: ${orderRef}`);
try {
const result = await client.recurring.createRecurringPayment({
Config: {
ORDER_REF_NUMBER: orderRef,
BACK_URL: 'https://example.com/callback',
PRICES_CURRENCY: 'TRY',
ORDER_AMOUNT: '50.00',
REPEAT: '3',
TRIES_COUNT: '3',
START_DATE: new Date(Date.now() + 86400000).toISOString().split('T')[0] // Yarından itibaren
},
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('Tekrarlı ödeme sonucu:', JSON.stringify(result, null, 2));
} catch (error) {
console.error('Tekrarlı ödeme hatası:', error.message);
}
return result;
}
// Fiziksel POS Listele
async function runListPhysicalPos() {
console.log('\n--- Fiziksel POS Listesi Alınıyor ---');
try {
const result = await client.physicalPos.listPhysicalPos();
console.log('Fiziksel POS listesi:', JSON.stringify(result, null, 2));
} catch (error) {
console.error('Fiziksel POS listeleme hatası:', error.message);
}
return result;
}
// Programı başlat
console.log('EsnekPOS API Test Programı başlatılıyor...');
showMenu();