UNPKG

esnekpos

Version:

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

88 lines (72 loc) 2.2 kB
/** * API yanıt işleme yardımcı fonksiyonları */ const { STATUS_CODES } = require('../constants'); /** * API hatasını işler ve uygun hata nesnesi döndürür * @param {Error} error - Axios hata nesnesi * @returns {Error} - İşlenmiş hata */ function handleApiError(error) { // Axios yanıt hatası if (error.response) { const { data, status, statusText } = error.response; let errorMessage = `HTTP Hatası: ${status} ${statusText}`; if (data && data.RETURN_MESSAGE) { errorMessage = `${errorMessage} - ${data.RETURN_MESSAGE}`; } const customError = new Error(errorMessage); customError.statusCode = status; customError.apiResponse = data; return customError; } // İstek hatası (network hatası) if (error.request) { const networkError = new Error('Ağ hatası: Sunucudan yanıt alınamadı'); networkError.originalError = error; return networkError; } // Diğer hatalar return error; } /** * API yanıtının durumunu kontrol eder * @param {Object} response - API yanıtı * @returns {boolean} - Yanıt başarılı mı? */ function isSuccessfulResponse(response) { if (!response || typeof response !== 'object') { return false; } // Başarılı işlem durumları if (response.STATUS === 'SUCCESS' && parseInt(response.RETURN_CODE) === STATUS_CODES.SUCCESS) { return true; } // İptal işlemi başarılı durumu if (response.STATUS === 'ORDER_CANCEL' && parseInt(response.RETURN_CODE) === STATUS_CODES.ORDER_CANCEL) { return true; } return false; } /** * API yanıtından hata mesajı çıkarır * @param {Object} response - API yanıtı * @returns {string|null} - Hata mesajı (varsa) */ function getErrorMessage(response) { if (!response || typeof response !== 'object') { return null; } if (response.RETURN_MESSAGE_TR) { return response.RETURN_MESSAGE_TR; } if (response.RETURN_MESSAGE) { return response.RETURN_MESSAGE; } return null; } module.exports = { handleApiError, isSuccessfulResponse, getErrorMessage };