UNPKG

simpay-typescript-api

Version:
164 lines 6.08 kB
import axios, {} from 'axios'; import { sha256 } from '../lib/hashing.js'; export class DirectBilling { key; password; client; constructor(key, password) { this.key = key; this.password = password; this.key = key; this.password = password; this.client = axios.create({ baseURL: 'https://api.simpay.pl/directbilling', headers: { 'X-SIM-KEY': this.key, 'X-SIM-PASSWORD': this.password, 'X-SIM-VERSION': '3.1.0', 'X-SIM-PLATFORM': 'TYPESCRIPT', }, }); } /* https://docs.simpay.pl/pl/typescript/?typescript#directbilling-pobieranie-listy-uslug */ async getServices() { const result = []; let response = await this.client.get('/'); result.push(...response.data.data); while (response.data.pagination.links.next_page !== null) { response = await this.client.get(`/?page=${+response.data.pagination.current_page + 1}`); result.push(...response.data.data); } return result.map((e) => { e.created_at = new Date(e.created_at.replace(' ', 'T')); return e; }); } async getServicesPaginated(page, pageSize) { const query = {}; if (page) query.page = `${page}`; if (pageSize) query.limit = `${pageSize}`; const url = `/?${new URLSearchParams(query).toString()}`; const response = (await this.client.get(url)).data; response.data = response.data.map((e) => { e.created_at = new Date(e.created_at.toString().replace(' ', 'T')); return e; }); return response; } /* https://docs.simpay.pl/pl/typescript/?typescript#directbilling-pobieranie-informacji-o-usludze */ async getService(id) { try { const service = (await this.client.get(`/${id}`)).data.data; service.created_at = new Date(service.created_at.replace(' ', 'T')); return service; } catch (e) { return undefined; } } /* https://docs.simpay.pl/pl/typescript/?typescript#directbilling-kalkulacja-prowizji */ async calculateCommission(serviceId, amount) { return (await this.client.get(`/${serviceId}/calculate?amount=${amount}`)).data.data; } /* https://docs.simpay.pl/pl/typescript/?typescript#directbilling-pobieranie-listy-transakcji */ async getTransactions(serviceId) { const result = []; let response = await this.client.get(`/${serviceId}/transactions`); result.push(...response.data.data); while (response.data.pagination.links.next_page !== null) { response = await this.client.get(`/${serviceId}/transactions?page=${response.data.pagination.current_page + 1}`); result.push(...response.data.data); } return result.map((e) => { e.created_at = new Date(e.created_at.replace(' ', 'T')); e.updated_at = new Date(e.updated_at.replace(' ', 'T')); return e; }); } async getTransactionsPaginated(serviceId, page, pageSize) { const query = {}; if (page) query.page = `${page}`; if (pageSize) query.limit = `${pageSize}`; const url = `/${serviceId}/transactions?${new URLSearchParams(query).toString()}`; const response = (await this.client.get(url)).data; response.data = response.data.map((e) => { e.created_at = new Date(e.created_at.replace(' ', 'T')); e.updated_at = new Date(e.updated_at.replace(' ', 'T')); return e; }); return response; } /* https://docs.simpay.pl/pl/typescript/?typescript#directbilling-pobieranie-informacji-o-transakcji */ async getTransaction(serviceId, transactionId) { const transaction = (await this.client.get(`/${serviceId}/transactions/${transactionId}`)) .data.data; transaction.created_at = new Date(transaction.created_at.replace(' ', 'T')); transaction.updated_at = new Date(transaction.updated_at.replace(' ', 'T')); return transaction; } /* https://docs.simpay.pl/pl/typescript/?typescript#directbilling-generowanie-transakcji */ async createTransaction(serviceId, key, request) { request.signature = this.generateSignature(key, request); return (await this.client.post(`/${serviceId}/transactions`, request)).data; } /* https://docs.simpay.pl/shell/?shell#directbilling-generowanie-transakcji */ checkNotification(key, body) { const signature = this.generateSignatureNotification(key, body); if (body.signature !== signature) return undefined; return body; } /* https://docs.simpay.pl/shell/?shell#directbilling-generowanie-transakcji */ generateSignature(key, request) { const elements = [ request.amount, request.amountType, request.description, request.control, request.returns?.success, request.returns?.failure, request.phoneNumber, request.steamid, key, ].filter((e) => e !== undefined && e !== null); return sha256(elements.join('|')); } generateSignatureNotification(key, request) { const elements = [ request.id, request.service_id, request.status, request.values?.net, request.values?.gross, request.values?.partner, request.returns?.complete, request.returns?.failure, request.control, request.number_from, request.provider, key, ].filter((e) => e !== undefined && e !== null); return sha256(elements.join('|')); } } //# sourceMappingURL=directbilling.js.map