UNPKG

gamemoney-fix

Version:
146 lines (145 loc) 6.19 kB
import got from 'got'; import { generateRsaSignature, generateHmacSignature, getRandomString, verifyRsaSignature, } from './utils.js'; export var SignType; (function (SignType) { SignType["HMAC"] = "HMAC"; SignType["RSA"] = "RSA"; })(SignType || (SignType = {})); export class GameMoneyError extends Error { constructor(message) { super(message); this.name = 'GameMoneyError'; } } export default class GameMoney { config; got = got.extend({ prefixUrl: 'https://paygate.gamemoney.com', responseType: 'json', }); constructor(config) { this.config = config; this.config = config; } async request(url, payload = {}, signType = SignType.HMAC) { const form = { rand: 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 = generateRsaSignature(form, this.config.rsaPrivateKey); } else { form.signature = generateHmacSignature(form, this.config.hmacPrivateKey); } const response = await this.got.post({ url, form, resolveBodyOnly: true, }); if (!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 generateHmacSignature(body, this.config.hmacPrivateKey); } verifyRsaSignature(body) { return 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.gamemoney.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#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.gamemoney.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_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.gamemoney.com/apidoc#invoice_api_terminal) */ async createTerminal(body) { return this.request('terminal/create', body); } }