import { MERCHANT_API_SHARED_KEY, MERCHANT_API_URL } from '../lib/constants' import { Request as OystRequest, makeError } from '@oyst/utils' import { isNil, merge, pick } from 'ramda' import Merchant from '../models/merchant' import RequestError from '../classes/request_error' import log from '../lib/log' const { ApiRequest, Request } = OystRequest export class MerchantAPI extends ApiRequest { constructor (init: any = {}) { super(merge({ api: 'merchant-api', endpoint: MERCHANT_API_URL, prefix: 'MERCH', req: {}, sharedKey: MERCHANT_API_SHARED_KEY, targetApi: 'merchant-api', type: 'http' }, init)) } public checkApiKey (key: string): Promise { return new Promise(async (resolve, reject) => { try { console.error() const result = await this.makeRequest({ url: `/api_keys/${key}` }) if (!isNil(result.data.error)) { return reject(this.genError(result)) } resolve(new Merchant({ id: Request.getAttribute([ 'data', 'api_key', 'merchant', 'id' ], result) as string })) } catch (err) { log({ errCheckKey: err }) reject(err) } }) } public show (id: string): Promise { return new Promise(async (resolve, reject) => { try { const result = await this.makeRequest({ url: `/merchants/${id}` }) if (result.data.error) { return reject(this.genError(result)) } resolve(Request.getAttribute(['data', 'merchant'], result)) } catch (err) { reject(makeError(err)) } }) } public updateUrl (id: string, notificationUrl: string): Promise { return new Promise(async (resolve, reject) => { try { const merchant = await this.show(id) const result = await this.makeRequest({ json: merge(pick([ 'refund_fees_rate', 'retainer_days', 'retainer_rate' ], merchant), { notification_url: notificationUrl }), method: 'PUT', url: `/merchants/${id}` }) if (result.data.error) { return reject(this.genError(result)) } resolve(Request.getAttribute(['data', 'merchant'], result)) } catch (err) { reject(makeError(err)) } }) } } export default new MerchantAPI()