@muhammedaksam/sipay-node
Version:
Node.js TypeScript SDK for Sipay payment gateway
126 lines • 4.43 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SipayHttpClient = void 0;
const axios_1 = __importDefault(require("axios"));
class SipayHttpClient {
constructor(config) {
this.config = {
baseUrl: 'https://provisioning.sipay.com.tr/ccpayment',
timeout: 80000,
...config,
};
this.client = axios_1.default.create({
baseURL: this.config.baseUrl,
timeout: this.config.timeout,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
// Request interceptor to add auth token
this.client.interceptors.request.use((config) => {
if (this.token && config.url !== '/api/token') {
config.headers.Authorization = `Bearer ${this.token}`;
}
return config;
});
// Response interceptor for error handling
this.client.interceptors.response.use((response) => response, (error) => {
throw this.createSipayError(error);
});
}
async authenticate() {
try {
const response = await this.client.post('/api/token', {
app_id: this.config.appId,
app_secret: this.config.appSecret,
});
if (response.data.status_code === 100 && response.data.data?.token) {
this.token = response.data.data.token;
}
else {
throw new Error(response.data.status_description || 'Authentication failed');
}
}
catch (error) {
throw this.createSipayError(error);
}
}
async request(method, url, data, options) {
// Ensure we have a valid token before making requests
if (!this.token && url !== '/api/token') {
await this.authenticate();
}
const config = {
method,
url,
...(data && { data }),
...options,
};
try {
const response = await this.client.request(config);
return response.data;
}
catch (error) {
throw this.createSipayError(error);
}
}
async get(url, params, options) {
return this.request('GET', url, params, options);
}
async post(url, data, options) {
return this.request('POST', url, data, options);
}
async postForm(url, data, options) {
const formOptions = {
...options,
headers: {
...options?.headers,
'Content-Type': 'application/x-www-form-urlencoded',
},
};
// Convert data to FormData for 3D payments
if (data && typeof data === 'object') {
const formData = new URLSearchParams();
for (const [key, value] of Object.entries(data)) {
if (Array.isArray(value) || typeof value === 'object') {
formData.append(key, JSON.stringify(value));
}
else {
formData.append(key, String(value));
}
}
return this.request('POST', url, formData.toString(), formOptions);
}
return this.request('POST', url, data, formOptions);
}
createSipayError(error) {
const sipayError = new Error();
sipayError.type = 'SipayError';
if (error.response?.data) {
const errorData = error.response.data;
sipayError.message =
errorData.status_description || errorData.message || 'Unknown Sipay error';
sipayError.status_code = errorData.status_code;
sipayError.status_description = errorData.status_description;
}
else if (error.request) {
sipayError.message = 'Network error: No response received from Sipay';
}
else {
sipayError.message = error.message || 'Unknown error occurred';
}
return sipayError;
}
getToken() {
return this.token;
}
setToken(token) {
this.token = token;
}
}
exports.SipayHttpClient = SipayHttpClient;
//# sourceMappingURL=http-client.js.map