UNPKG

unitpay-api

Version:
118 lines (117 loc) 5.31 kB
import got from 'got'; import { stringify } from 'qs'; import { base64Encode, getSignature } from './utils.js'; export default class Unitpay { config; got; constructor(config) { this.config = config; this.got = got.extend({ prefixUrl: `https://${this.config.domain}/api`, responseType: 'json', }); } async request(method, payload = {}) { if (!this.config.secretKey) { throw new Error('secretKey mismatch'); } const response = await this.got.get({ searchParams: stringify({ method, params: { ...payload, secretKey: this.config.secretKey, }, }), resolveBodyOnly: true, }); return response; } /** For more details and usage information see [docs](https://help.unitpay.ru/payments/create-payment) */ async initPayment(body) { if (body.cashItems && typeof body.cashItems !== 'string') { body.cashItems = base64Encode(body.cashItems); } if (!body.signature) { const signature = getSignature(body, this.config.secretKey); body.signature = signature; } return this.request('initPayment', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/payments/create-payment-easy) */ form(publicKey, parameters) { if (!publicKey) { throw new Error('publicKey mismatch'); } if (parameters.cashItems && typeof parameters.cashItems !== 'string') { parameters.cashItems = base64Encode(parameters.cashItems); } if (!parameters.signature) { const signature = getSignature(parameters, this.config.secretKey); parameters.signature = signature; } return `https://${this.config.domain}/pay/${publicKey}?${stringify(parameters)}`; } /** For more details and usage information see [docs](https://help.unitpay.ru/payments/payment-info) */ async getPayment(body) { return this.request('getPayment', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/payments/payment-refund) */ async refundPayment(body) { if (body.cashItems && typeof body.cashItems !== 'string') { body.cashItems = base64Encode(body.cashItems); } return this.request('refundPayment', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/payments/pre-authorization-payments) */ async confirmPayment(body) { return this.request('confirmPayment', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/payments/pre-authorization-payments) */ async cancelPayment(body) { return this.request('cancelPayment', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/payments/recurring-payments/subscription-list) */ async listSubscriptions(body) { return this.request('listSubscriptions', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/payments/recurring-payments/subscription-info) */ async getSubscription(body) { return this.request('getSubscription', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/payments/recurring-payments/close-subscription) */ async closeSubscription(body) { return this.request('closeSubscription', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/payouts/create_payout) */ async massPayment(body) { return this.request('massPayment', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/payouts/payout_info) */ async massPaymentStatus(body) { return this.request('massPaymentStatus', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/payouts/bin_info) */ async getBinInfo(body) { return this.request('getBinInfo', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/online-cash-register/advance_receipt) */ async offsetAdvance(body) { if (body.cashItems && typeof body.cashItems !== 'string') { body.cashItems = base64Encode(body.cashItems); } return this.request('offsetAdvance', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/unitpay-management/balance) */ async getPartner(body) { return this.request('getPartner', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/unitpay-management/commissions) */ async getCommissions(body) { return this.request('getCommissions', body); } /** For more details and usage information see [docs](https://help.unitpay.ru/unitpay-management/conversion-rates) */ async getCurrencyCourses(body) { return this.request('getCurrencyCourses', body); } }