bitpay-sdk
Version:
Complete version of the NodeJS library for the new cryptographically secure BitPay API
224 lines • 8.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InvoiceClient = void 0;
const BitPayExceptionProvider_1 = require("../Exceptions/BitPayExceptionProvider");
const Facade_1 = require("../Facade");
const BitPayResponseParser_1 = require("../util/BitPayResponseParser");
class InvoiceClient {
constructor(bitPayClient, tokenContainer, guidGenerator) {
this.bitPayClient = bitPayClient;
this.tokenContainer = tokenContainer;
this.guidGenerator = guidGenerator;
}
/**
* Create a BitPay invoice.
*
* @param invoice An Invoice object with request parameters defined
* @param facade The facade used to create it.
* @param signRequest Signed request.
* @returns Invoice
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
async create(invoice, facade, signRequest) {
invoice.guid = invoice.guid ? invoice.guid : this.guidGenerator.execute();
invoice.token = this.tokenContainer.getToken(facade);
const result = await this.bitPayClient.post('invoices', invoice, signRequest);
try {
return JSON.parse(result);
}
catch (e) {
BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Invoice', e.message);
throw new Error();
}
}
/**
* Retrieve a BitPay invoice by invoice id using the specified facade. The client must have been previously
* authorized for the specified facade (the public facade requires no authorization).
*
* @param invoiceId The id of the invoice to retrieve.
* @param facade The facade used to create it.
* @param signRequest Signed request.
* @returns Invoice
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
async get(invoiceId, facade, signRequest) {
const params = { token: this.tokenContainer.getToken(facade) };
const result = await this.bitPayClient.get('invoices/' + invoiceId, params, signRequest);
try {
return JSON.parse(result);
}
catch (e) {
BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Invoice', e.message);
throw new Error();
}
}
/**
* @param guid
* @param facade
* @param signRequest
* @returns Invoice
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
async getByGuid(guid, facade, signRequest) {
const params = { token: this.tokenContainer.getToken(facade) };
const result = await this.bitPayClient.get('invoices/guid/' + guid, params, signRequest);
try {
return JSON.parse(result);
}
catch (e) {
BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Invoice', e.message);
throw new Error();
}
}
/**
* Retrieve a collection of BitPay invoices.
*
* @param params
* @returns Invoice[]
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
async getInvoices(params) {
params['token'] = this.tokenContainer.getToken(Facade_1.Facade.Merchant);
const result = await this.bitPayClient.get('invoices', params, true);
try {
return JSON.parse(result);
}
catch (e) {
BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Invoice', e.message);
throw new Error();
}
}
/**
*
* @param invoiceId
* @returns
*/
async getInvoiceEventToken(invoiceId) {
const params = {};
params['token'] = this.tokenContainer.getToken(Facade_1.Facade.Merchant);
const result = await this.bitPayClient.get('invoices/' + invoiceId + '/events', params, true);
try {
return JSON.parse(result);
}
catch (e) {
BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Invoice', e.message);
throw new Error();
}
}
/**
* Update a BitPay invoice.
*
* @param invoiceId The id of the invoice to updated.
* @param params
* @returns Invoice
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
async update(invoiceId, params) {
params['token'] = this.tokenContainer.getToken(Facade_1.Facade.Merchant);
const result = await this.bitPayClient.put('invoices/' + invoiceId, params);
try {
return JSON.parse(result);
}
catch (e) {
BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Invoice', e.message);
throw new Error();
}
}
/**
* Pay an invoice with a mock transaction
*
* @param invoiceId The id of the invoice.
* @param status Status the invoice will become. Acceptable values are confirmed (default) and complete.
* @returns Invoice Invoice object.
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
async pay(invoiceId, status) {
const params = {
token: this.tokenContainer.getToken(Facade_1.Facade.Merchant),
status: status
};
const result = await this.bitPayClient.put('invoices/pay/' + invoiceId, params);
try {
return JSON.parse(result);
}
catch (e) {
BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Invoice', e.message);
throw new Error();
}
}
/**
* Cancel a BitPay invoice.
*
* @param invoiceId The id of the invoice to updated.
* @param forceCancel
* @returns Invoice Cancelled invoice object.
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
async cancel(invoiceId, forceCancel) {
const params = {
token: this.tokenContainer.getToken(Facade_1.Facade.Merchant),
forceCancel: forceCancel
};
const result = await this.bitPayClient.delete('invoices/' + invoiceId, params);
try {
return JSON.parse(result);
}
catch (e) {
BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Invoice', e.message);
throw new Error();
}
}
/**
* Cancel a BitPay invoice.
*
* @param guid The guid of the invoice to cancel.
* @param forceCancel
* @returns Invoice Cancelled invoice object.
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
async cancelByGuid(guid, forceCancel) {
const params = {
token: this.tokenContainer.getToken(Facade_1.Facade.Merchant),
forceCancel: forceCancel
};
const result = await this.bitPayClient.delete('invoices/guid/' + guid, params);
try {
return JSON.parse(result);
}
catch (e) {
BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Invoice', e.message);
throw new Error();
}
}
/**
* Request a BitPay Invoice Webhook.
*
* @param invoiceId A BitPay invoice ID.
* @param invoiceToken The resource token for the invoiceId.
* This token can be retrieved from the Bitpay's invoice object.
* @returns boolean
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
async requestInvoiceWebhookToBeResent(invoiceId, invoiceToken) {
const params = { token: invoiceToken };
const result = await this.bitPayClient.post('invoices/' + invoiceId + '/notifications', params);
try {
return BitPayResponseParser_1.BitPayResponseParser.jsonToBoolean(result);
}
catch (e) {
BitPayExceptionProvider_1.BitPayExceptionProvider.throwDeserializeResourceException('Invoice', e.message);
throw new Error();
}
}
}
exports.InvoiceClient = InvoiceClient;
//# sourceMappingURL=InvoiceClient.js.map