Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SP-580 Node.js - Implement better refund exceptions #95

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 40 additions & 33 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ec } from 'elliptic';
import { BitPayExceptions as Exceptions, Env, Facade, KeyUtils } from './index';
import { Env, Facade, KeyUtils } from './index';
import {
BillInterface,
InvoiceInterface,
Expand All @@ -12,31 +12,34 @@ import {
RateInterface,
Rates
} from './Model';
import { BitPayClient } from './Client/BitPayClient';
import {
BitPayClient,
RateClient,
CurrencyClient,
InvoiceClient,
RefundClient,
PayoutClient,
PayoutGroupClient,
PayoutRecipientClient,
LedgerClient,
BillClient,
WalletClient,
SettlementClient
} from './Client/index';

import { TokenContainer } from './TokenContainer';
import { Environment } from './Environment';
import BitPayException from './Exceptions/BitPayException';
import { GuidGenerator } from './util/GuidGenerator';
import { RateClient } from './Client/RateClient';
import { CurrencyClient } from './Client/CurrencyClient';
import { InvoiceClient } from './Client/InvoiceClient';
import { InvoiceEventTokenInterface } from './Model/Invoice/InvoiceEventToken';
import { RefundInterface } from './Model/Invoice/Refund';
import { RefundClient } from './Client/RefundClient';
import { PayoutRecipientClient } from './Client/PayoutRecipientClient';
import { PayoutClient } from './Client/PayoutClient';
import { LedgerClient } from './Client/LedgerClient';
import { ParamsRemover } from './util/ParamsRemover';
import { BillClient } from './Client/BillClient';
import { WalletInterface } from './Model/Wallet/Wallet';
import { WalletClient } from './Client/WalletClient';
import { SettlementInterface } from './Model/Settlement/Settlement';
import { SettlementClient } from './Client/SettlementClient';
import { PosToken } from './PosToken';
import { PrivateKey } from './PrivateKey';
import { CurrencyInterface } from './Model/Currency/Currency';

import * as fs from 'fs';
import { BitPayExceptionProvider } from './Exceptions/BitPayExceptionProvider';

export class Client {
private bitPayClient: BitPayClient;
Expand All @@ -59,16 +62,16 @@ export class Client {
return;
}

if (tokenContainer === null) {
tokenContainer = new TokenContainer();
}

if (bitPayClient !== undefined && bitPayClient !== null) {
// using for tests
this.initForTests(bitPayClient, guidGenerator, tokenContainer);
return;
}

if (tokenContainer === null) {
tokenContainer = new TokenContainer();
}

if (environment === undefined) {
environment = Environment.Prod;
}
Expand Down Expand Up @@ -515,7 +518,7 @@ export class Client {
* @param payouts
*/
public async submitPayouts(payouts: PayoutInterface[]): Promise<PayoutGroupInterface> {
return this.createPayoutClient().submitPayouts(payouts);
return this.createPayoutGroupClient().submitPayouts(payouts);
}

/**
Expand All @@ -524,7 +527,7 @@ export class Client {
* @param payoutGroupId
*/
public async cancelPayouts(payoutGroupId: string): Promise<PayoutGroupInterface> {
return this.createPayoutClient().cancelPayouts(payoutGroupId);
return this.createPayoutGroupClient().cancelPayouts(payoutGroupId);
}

/**
Expand Down Expand Up @@ -731,7 +734,7 @@ export class Client {
return this.keyUtils.load_keypair(Buffer.from(keyHex).toString().trim());
}

throw new BitPayException(null, 'Missing ECKey');
BitPayExceptionProvider.throwGenericExceptionWithMessage('Missing ECKey');
}

private static getBaseUrl(environment: string) {
Expand All @@ -742,43 +745,47 @@ export class Client {
return this.keyUtils.getPublicKeyFromPrivateKey(ecKey);
}

private createRateClient() {
private createRateClient(): RateClient {
return new RateClient(this.bitPayClient);
}

private getCurrencyClient() {
private getCurrencyClient(): CurrencyClient {
return new CurrencyClient(this.bitPayClient);
}

private createInvoiceClient() {
private createInvoiceClient(): InvoiceClient {
return new InvoiceClient(this.bitPayClient, this.tokenContainer, this.guidGenerator);
}

private createRefundClient() {
private createRefundClient(): RefundClient {
return new RefundClient(this.bitPayClient, this.tokenContainer, this.guidGenerator);
}

private createPayoutRecipientClient() {
private createPayoutRecipientClient(): PayoutRecipientClient {
return new PayoutRecipientClient(this.bitPayClient, this.tokenContainer, this.guidGenerator);
}

private createPayoutClient() {
private createPayoutClient(): PayoutClient {
return new PayoutClient(this.bitPayClient, this.tokenContainer, this.guidGenerator);
}

private createLedgerClient() {
private createPayoutGroupClient(): PayoutGroupClient {
return new PayoutGroupClient(this.bitPayClient, this.tokenContainer, this.guidGenerator);
}

private createLedgerClient(): LedgerClient {
return new LedgerClient(this.bitPayClient, this.tokenContainer);
}

private createBillClient() {
private createBillClient(): BillClient {
return new BillClient(this.bitPayClient, this.tokenContainer);
}

private createWalletClient() {
private createWalletClient(): WalletClient {
return new WalletClient(this.bitPayClient);
}

private createSettlementClient() {
private createSettlementClient(): SettlementClient {
return new SettlementClient(this.bitPayClient, this.tokenContainer);
}

Expand All @@ -800,7 +807,7 @@ export class Client {

private initByConfigFilePath(configFilePath: string): void {
if (!fs.existsSync(configFilePath)) {
throw new Exceptions.Generic(null, 'Configuration file not found');
BitPayExceptionProvider.throwGenericExceptionWithMessage('Configuration file not found');
}

try {
Expand All @@ -813,7 +820,7 @@ export class Client {
this.bitPayClient = new BitPayClient(Client.getBaseUrl(environment), ecKey, this.getIdentity(ecKey));
this.guidGenerator = new GuidGenerator();
} catch (e) {
throw new Exceptions.Generic(null, 'Error when reading configuration file', null, e.apiCode);
BitPayExceptionProvider.throwGenericExceptionWithMessage('Error when reading configuration file');
}
}

Expand Down
45 changes: 23 additions & 22 deletions src/Client/BillClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BitPayClient } from './BitPayClient';
import { TokenContainer } from '../TokenContainer';
import { BillInterface } from '../Model';
import { Facade } from '../Facade';
import { BitPayExceptions as Exceptions } from '../index';
import { BitPayExceptionProvider } from '../Exceptions/BitPayExceptionProvider';

export class BillClient {
private bitPayClient: BitPayClient;
Expand All @@ -20,19 +20,17 @@ export class BillClient {
* @param facade The facade used to create it.
* @param signRequest Signed request.
* @returns Bill
* @throws BillCreationException
* @throws BitPayGenericException BitPayGenericException
* @throws BitPayApiException BitPayApiException
*/
public async create(bill: BillInterface, facade: string, signRequest: boolean): Promise<BillInterface> {
bill.token = this.tokenContainer.getToken(facade);
const result = await this.bitPayClient.post('bills', bill, signRequest);

try {
const result = await this.bitPayClient.post('bills', bill, signRequest);
return <BillInterface>JSON.parse(result);
} catch (e) {
throw new Exceptions.BillCreation(
'failed to deserialize BitPay server response (Bill) : ' + e.message,
e.apiCode
);
BitPayExceptionProvider.throwSerializeResourceException('Bill', e.message);
}
}

Expand All @@ -43,16 +41,17 @@ export class BillClient {
* @param facade The facade used to retrieve it.
* @param signRequest Signed request
* @returns Bill
* @throws BillQueryException
* @throws BitPayGenericException BitPayGenericException class
* @throws BitPayApiException BitPayApiException class
*/
public async get(billId: string, facade: string, signRequest: boolean): Promise<BillInterface> {
const params = { token: this.tokenContainer.getToken(facade) };
const result = await this.bitPayClient.get('bills/' + billId, params, signRequest);

try {
const result = await this.bitPayClient.get('bills/' + billId, params, signRequest);
return <BillInterface>JSON.parse(result);
} catch (e) {
throw new Exceptions.BillQuery('failed to deserialize BitPay server response (Bill) : ' + e.message, e.apiCode);
BitPayExceptionProvider.throwDeserializeResourceException('Bill', e.message);
}
}

Expand All @@ -61,19 +60,21 @@ export class BillClient {
*
* @param status
* @returns Bill[]
* @throws BillQueryException
* @throws BitPayGenericException BitPayGenericException
* @throws BitPayApiException BitPayApiException
*/
public async getBills(status: string | null): Promise<BillInterface> {
const params = { token: this.tokenContainer.getToken(Facade.Merchant) };
if (status) {
params['status'] = status;
}

const result = await this.bitPayClient.get('bills', params, true);

try {
const result = await this.bitPayClient.get('bills', params, true);
return <BillInterface>JSON.parse(result);
} catch (e) {
throw new Exceptions.BillQuery('failed to deserialize BitPay server response (Bill) : ' + e.message, e.apiCode);
BitPayExceptionProvider.throwDeserializeResourceException('Bill', e.message);
}
}

Expand All @@ -83,14 +84,16 @@ export class BillClient {
* @param bill
* @param billId
* @returns Bill
* @throws BillUpdateException
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
public async update(bill: BillInterface, billId: string): Promise<BillInterface> {
const result = await this.bitPayClient.put('bills/' + billId, bill);

try {
const result = await this.bitPayClient.put('bills/' + billId, bill);
return <BillInterface>JSON.parse(result);
} catch (e) {
throw new Exceptions.BillUpdate('failed to deserialize BitPay server response (Bill) : ' + e.message, e.apiCode);
BitPayExceptionProvider.throwDeserializeResourceException('Bill', e.message);
}
}

Expand All @@ -101,19 +104,17 @@ export class BillClient {
* @param billToken
* @param signRequest
* @returns string
* @throws BillDeliveryException
* @throws BitPayApiException BitPayApiException class
* @throws BitPayGenericException BitPayGenericException class
*/
public async deliver(billId: string, billToken: string, signRequest: boolean): Promise<boolean> {
const params = { token: billToken };
const result = await this.bitPayClient.post('bills/' + billId + '/deliveries', params, signRequest);

try {
const result = await this.bitPayClient.post('bills/' + billId + '/deliveries', params, signRequest);
return <string>JSON.parse(result) == 'Success';
} catch (e) {
throw new Exceptions.BillDelivery(
'failed to deserialize BitPay server response (Bill) : ' + e.message,
e.apiCode
);
BitPayExceptionProvider.throwDeserializeResourceException('Bill', e.message);
}
}
}
Loading