UNPKG

gamemoney-commonjs

Version:
162 lines (161 loc) 7.12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GameMoneyError = exports.SignType = void 0; const axios_1 = __importDefault(require("axios")); const utils_1 = require("./utils"); var SignType; (function (SignType) { SignType["HMAC"] = "HMAC"; SignType["RSA"] = "RSA"; })(SignType || (exports.SignType = SignType = {})); class GameMoneyError extends Error { constructor(message) { super(message); this.name = 'GameMoneyError'; } } exports.GameMoneyError = GameMoneyError; class GameMoney { config; axios = axios_1.default.create({ baseURL: 'https://paygate.gamemoney.com/', responseType: 'json' }); constructor(config) { this.config = config; this.config = config; } async request(url, payload = {}, signType = SignType.HMAC) { const form = { rand: (0, utils_1.getRandomString)(20), project: this.config.project, ...payload }; if (signType === SignType.RSA) { if (!this.config.rsaPrivateKey) { throw new Error("To make requests with RSA sign type, 'rsaPrivateKey' must be provided"); } form.signature = (0, utils_1.generateRsaSignature)(form, this.config.rsaPrivateKey); } else { form.signature = (0, utils_1.generateHmacSignature)(form, this.config.hmacPrivateKey); } const { data: response } = await this.axios.postForm(url, form); if (!(0, utils_1.verifyRsaSignature)(response)) { throw new Error('Response signature mismatch'); } if (form.rand !== response.rand) { throw new Error(`Wrong rand parameter: ${response.rand}`); } if (response.state === 'error') { throw new GameMoneyError(response.error); } return response; } generateHmacSignature(body) { return (0, utils_1.generateHmacSignature)(body, this.config.hmacPrivateKey); } verifyRsaSignature(body) { return (0, utils_1.verifyRsaSignature)(body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#invoice_insert_api) */ async createInvoice(body) { return this.request('invoice/', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#invoice_status) */ async getInvoiceStatus(body) { return this.request('invoice/status', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#invoice_list) */ async getInvoiceList(body) { return this.request('invoice/list', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#card_session_status) */ async getCardSessionStatus(body) { return this.request('invoice/cardsession/status', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#checkout_check) */ async checkCheckout(body) { return this.request('checkout/check', body, SignType.RSA); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#checkout_insert) */ async createCheckout(body) { return this.request('checkout/insert', body, SignType.RSA); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#checkout_cancel) */ async cancelCheckout(body) { return this.request('checkout/cancel', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#checkout_status) */ async getCheckoutStatus(body) { return this.request('checkout/status', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#checkout_list) */ async getCheckoutList(body) { return this.request('checkout/list', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#card_add) */ async addCard(body) { return this.request('card/add', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#card_add_token) */ async addTokenCard(body) { return this.request('card/addtoken', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#card_list) */ async getCardList(body) { return this.request('card/list', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#card_fulllist) */ async getCardFullList(body) { return this.request('card/fulllist', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#card_list) */ async deleteCard(body) { return this.request('card/delete', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#exchange_prepare) */ async prepareExchange(body) { return this.request('exchange/prepare', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#exchange_convert) */ async convertExchange(body) { return this.request('exchange/convert', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#exchange_fastconvert) */ async fastConvertExchange(body) { return this.request('exchange/fastconvert', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#exchange_rate) */ async getExchangeRate(body) { return this.request('exchange/rate', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#exchange_info) */ async getExchangeInfo(body) { return this.request('exchange/info', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#exchange_status) */ async getExchangeStatus(body) { return this.request('exchange/status', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#stat_balance) */ async getBalanceStatistics(body) { return this.request('statistics/balance', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#stat_days_balance) */ async getDaysBalanceStatistics(body) { return this.request('statistics/days_balance_project', body); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#stat_paytypes) */ async getPayTypesStatistics() { return this.request('statistics/paytypes'); } /** For more details and usage information see [docs](https://cp.gmpays.com/apidoc#invoice_api_terminal) */ async createTerminal(body) { return this.request('terminal/create', body); } } exports.default = GameMoney;