UNPKG

bitpay-sdk

Version:

Complete version of the NodeJS library for the new cryptographically secure BitPay API

209 lines 8.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RefundClient = void 0; const BitPayExceptionProvider_1 = require("../Exceptions/BitPayExceptionProvider"); const index_1 = require("../index"); const BitPayResponseParser_1 = require("../util/BitPayResponseParser"); const ParamsRemover_1 = require("../util/ParamsRemover"); class RefundClient { constructor(bitPayClient, tokenContainer, guidGenerator) { this.bitPayClient = bitPayClient; this.tokenContainer = tokenContainer; this.guidGenerator = guidGenerator; } /** * Create a refund for a BitPay invoice. * * @param refund RefundInterface * @returns Refund An updated Refund Object * @throws RefundCreationException */ async create(refund) { const params = { token: this.tokenContainer.getToken(index_1.Facade.Merchant), invoiceId: refund.invoice, amount: refund.amount, currency: refund.currency, preview: refund.preview, immediate: refund.immediate, buyerPaysRefundFee: refund.buyerPaysRefundFee }; params['guid'] = refund.guid ? refund.guid : this.guidGenerator.execute(); ParamsRemover_1.ParamsRemover.removeNullValuesFromObject(params); const result = await this.bitPayClient.post('refunds', params, true); try { return JSON.parse(result); } catch (e) { BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Refund', e.message); throw new Error(); } } /** * Retrieve a previously made refund request on a BitPay invoice. * * @param refundId The BitPay refund ID. * @returns Refund BitPay Refund object with the associated Refund object. * @throws BitPayApiException BitPayApiException class * @throws BitPayGenericException BitPayGenericException class */ async get(refundId) { const params = { token: this.tokenContainer.getToken(index_1.Facade.Merchant) }; const result = await this.bitPayClient.get('refunds/' + refundId, params, true); try { return JSON.parse(result); } catch (e) { BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Refund', e.message); throw new Error(); } } /** * Retrieve a previously made refund request on a BitPay invoice by Guid. * * @param guid The BitPay GUID. * @returns Refund BitPay Refund object with the associated Refund object. * @throws BitPayApiException BitPayApiException class * @throws BitPayGenericException BitPayGenericException class */ async getByGuid(guid) { const params = { token: this.tokenContainer.getToken(index_1.Facade.Merchant) }; const result = await this.bitPayClient.get('refunds/guid/' + guid, params, true); try { return JSON.parse(result); } catch (e) { BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Refund', e.message); throw new Error(); } } /** * Retrieve a previously made refund request on a BitPay invoice by guid. * * @param invoiceId The BitPay refund Guid. * @returns Refund BitPay Refund object with the associated Refund object. * @throws BitPayApiException BitPayApiException class * @throws BitPayGenericException BitPayGenericException class */ async getRefunds(invoiceId) { const params = { token: this.tokenContainer.getToken(index_1.Facade.Merchant), invoiceId: invoiceId }; const result = await this.bitPayClient.get('refunds', params, true); try { return JSON.parse(result); } catch (e) { BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Refund', e.message); throw new Error(); } } /** * Send a refund notification. * * @param refundId A BitPay refund ID. * @param refundToken The resource token for the refundId. * This token can be retrieved from the Bitpay's refund object. * @returns boolean An updated Refund Object * @throws BitPayApiException BitPayApiException class * @throws BitPayGenericException BitPayGenericException class */ async sendRefundNotification(refundId, refundToken) { const params = { token: refundToken }; const result = await this.bitPayClient.post('refunds/' + refundId + '/notifications', params, true); try { return BitPayResponseParser_1.BitPayResponseParser.jsonToBoolean(result); } catch (e) { BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Refund', e.message); throw new Error(); } } /** * Update the status of a BitPay invoice. * * @param refundId BitPay refund ID. * @param status The new status for the refund to be updated. * @returns Refund A BitPay generated Refund object. * @throws BitPayApiException BitPayApiException class * @throws BitPayGenericException BitPayGenericException class */ async update(refundId, status) { const params = { token: this.tokenContainer.getToken(index_1.Facade.Merchant), status: status }; const result = await this.bitPayClient.put('refunds/' + refundId, params, true); try { return JSON.parse(result); } catch (e) { BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Refund', e.message); throw new Error(); } } /** * Update the status of a BitPay invoice. * * @param guid BitPay refund Guid. * @param status The new status for the refund to be updated. * @returns Refund A BitPay generated Refund object. * @throws BitPayApiException BitPayApiException class * @throws BitPayGenericException BitPayGenericException class */ async updateByGuid(guid, status) { const params = { token: this.tokenContainer.getToken(index_1.Facade.Merchant), status: status }; const result = await this.bitPayClient.put('refunds/guid/' + guid, params, true); try { return JSON.parse(result); } catch (e) { BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Refund', e.message); throw new Error(); } } /** * Cancel a previously submitted refund request on a BitPay invoice. * * @param refundId The refund Id for the refund to be canceled. * @returns Cancelled refund Object. * @throws BitPayApiException BitPayApiException class * @throws BitPayGenericException BitPayGenericException class */ async cancel(refundId) { const params = { token: this.tokenContainer.getToken(index_1.Facade.Merchant) }; const result = await this.bitPayClient.delete('refunds/' + refundId, params, true); try { return JSON.parse(result); } catch (e) { BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Refund', e.message); throw new Error(); } } /** * Cancel a previously submitted refund request on a BitPay invoice * * @param guid The refund Guid for the refund to be canceled. * @returns Cancelled refund Object. * @throws BitPayApiException BitPayApiException class * @throws BitPayGenericException BitPayGenericException class */ async cancelByGuid(guid) { const params = { token: this.tokenContainer.getToken(index_1.Facade.Merchant) }; const result = await this.bitPayClient.delete('refunds/guid/' + guid, params, true); try { return JSON.parse(result); } catch (e) { BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Refund', e.message); throw new Error(); } } } exports.RefundClient = RefundClient; //# sourceMappingURL=RefundClient.js.map