Skip to content

Commit

Permalink
SP-1151 - use resource token in invoice and refund webhook resend req…
Browse files Browse the repository at this point in the history
…uests
  • Loading branch information
swlodarski committed Jan 23, 2025
1 parent 6925c24 commit d86743f
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 113 deletions.
5 changes: 4 additions & 1 deletion examples/Merchant/InvoiceRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export class InvoiceRequests {
public async requestInvoiceWebhookToBeResent() {
const client = ClientProvider.create();

return await client.deliverBill('someBillId', 'myBillToken');
const invoiceId = 'myInvoiceId';
const invoice = await client.getInvoice(invoiceId);

return await client.requestInvoiceWebhookToBeResent(invoiceId, invoice.token);
}
}
10 changes: 8 additions & 2 deletions examples/Merchant/RefundRequests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClientProvider } from '../ClientProvider';
import { Refund } from '../../src/Model/Invoice/Refund';
import { ClientProvider } from '../ClientProvider';

export class RefundRequests {
public async createRefund() {
Expand Down Expand Up @@ -49,6 +49,12 @@ export class RefundRequests {
public async requestRefundNotificationToBeResent() {
const client = ClientProvider.create();

return await client.sendRefundNotification('someRefundId');
const refundId = 'someRefundId';
const refund = await client.getRefund(refundId);
if (!refund.token) {
throw new Error('Refund token is required to request a refund notification to be resent.');
}

return await client.sendRefundNotification(refundId, refund.token);
}
}
56 changes: 30 additions & 26 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
/* eslint-disable @typescript-eslint/no-unused-vars*/

import { ec } from 'elliptic';
import {
BillClient,
BitPayClient,
CurrencyClient,
InvoiceClient,
LedgerClient,
PayoutClient,
PayoutGroupClient,
PayoutRecipientClient,
RateClient,
RefundClient,
SettlementClient,
WalletClient
} from './Client/index';
import { Env, Facade, KeyUtils } from './index';
import {
BillInterface,
InvoiceInterface,
LedgerEntryInterface,
LedgerInterface,
PayoutInterface,
PayoutGroupInterface,
PayoutInterface,
PayoutRecipientInterface,
PayoutRecipients,
RateInterface,
Rates
} from './Model';
import {
BitPayClient,
RateClient,
CurrencyClient,
InvoiceClient,
RefundClient,
PayoutClient,
PayoutGroupClient,
PayoutRecipientClient,
LedgerClient,
BillClient,
WalletClient,
SettlementClient
} from './Client/index';

import { TokenContainer } from './TokenContainer';
import * as fs from 'fs';
import { Environment } from './Environment';
import { GuidGenerator } from './util/GuidGenerator';
import { BitPayExceptionProvider } from './Exceptions/BitPayExceptionProvider';
import { CurrencyInterface } from './Model/Currency/Currency';
import { InvoiceEventTokenInterface } from './Model/Invoice/InvoiceEventToken';
import { RefundInterface } from './Model/Invoice/Refund';
import { ParamsRemover } from './util/ParamsRemover';
import { WalletInterface } from './Model/Wallet/Wallet';
import { SettlementInterface } from './Model/Settlement/Settlement';
import { WalletInterface } from './Model/Wallet/Wallet';
import { PosToken } from './PosToken';
import { PrivateKey } from './PrivateKey';
import { CurrencyInterface } from './Model/Currency/Currency';
import * as fs from 'fs';
import { BitPayExceptionProvider } from './Exceptions/BitPayExceptionProvider';
import { TokenContainer } from './TokenContainer';
import { GuidGenerator } from './util/GuidGenerator';
import { ParamsRemover } from './util/ParamsRemover';

export class Client {
private bitPayClient: BitPayClient;
Expand Down Expand Up @@ -311,10 +311,12 @@ export class Client {
* The intent of this call is to address issues when BitPay sends a webhook but the client doesn't receive it,
* so the client can request that BitPay resend it.
* @param invoiceId The id of the invoice for which you want the last webhook to be resent.
* @param invoiceToken The resource token for the invoiceId.
* This token can be retrieved from the Bitpay's invoice object.
* @return Boolean status of request
*/
public async requestInvoiceWebhookToBeResent(invoiceId: string): Promise<boolean> {
return this.createInvoiceClient().requestInvoiceWebhookToBeResent(invoiceId);
public async requestInvoiceWebhookToBeResent(invoiceId: string, invoiceToken: string): Promise<boolean> {
return this.createInvoiceClient().requestInvoiceWebhookToBeResent(invoiceId, invoiceToken);
}

/**
Expand Down Expand Up @@ -389,10 +391,12 @@ export class Client {
* Send a refund notification.
*
* @param refundId A BitPay refund ID.
* @param refundToken The resource token for the invoirefundIdceId.
* This token can be retrieved from the Bitpay's refund object.
* @return An updated Refund Object
*/
public async sendRefundNotification(refundId: string): Promise<boolean> {
return this.createRefundClient().sendRefundNotification(refundId);
public async sendRefundNotification(refundId: string, refundToken: string): Promise<boolean> {
return this.createRefundClient().sendRefundNotification(refundId, refundToken);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions src/Client/InvoiceClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BitPayClient } from './BitPayClient';
import { BitPayExceptionProvider } from '../Exceptions/BitPayExceptionProvider';
import { Facade } from '../Facade';
import { Invoice, InvoiceInterface } from '../Model';
import { TokenContainer } from '../TokenContainer';
import { GuidGenerator } from '../util/GuidGenerator';
import { InvoiceEventTokenInterface } from '../Model/Invoice/InvoiceEventToken';
import { TokenContainer } from '../TokenContainer';
import { BitPayResponseParser } from '../util/BitPayResponseParser';
import { BitPayExceptionProvider } from '../Exceptions/BitPayExceptionProvider';
import { Facade } from '../Facade';
import { GuidGenerator } from '../util/GuidGenerator';
import { BitPayClient } from './BitPayClient';

export class InvoiceClient {
private bitPayClient: BitPayClient;
Expand Down Expand Up @@ -225,12 +225,14 @@ export class InvoiceClient {
* 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
*/
public async requestInvoiceWebhookToBeResent(invoiceId: string): Promise<boolean> {
const params = { token: this.tokenContainer.getToken(Facade.Merchant) };
public async requestInvoiceWebhookToBeResent(invoiceId: string, invoiceToken: string): Promise<boolean> {
const params = { token: invoiceToken };
const result = await this.bitPayClient.post('invoices/' + invoiceId + '/notifications', params);

try {
Expand Down
16 changes: 9 additions & 7 deletions src/Client/RefundClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BitPayClient } from './BitPayClient';
import { TokenContainer } from '../TokenContainer';
import { GuidGenerator } from '../util/GuidGenerator';
import { BitPayExceptionProvider } from '../Exceptions/BitPayExceptionProvider';
import { Facade } from '../index';
import { RefundInterface } from '../Model/Invoice/Refund';
import { ParamsRemover } from '../util/ParamsRemover';
import { TokenContainer } from '../TokenContainer';
import { BitPayResponseParser } from '../util/BitPayResponseParser';
import { BitPayExceptionProvider } from '../Exceptions/BitPayExceptionProvider';
import { GuidGenerator } from '../util/GuidGenerator';
import { ParamsRemover } from '../util/ParamsRemover';
import { BitPayClient } from './BitPayClient';

export class RefundClient {
private bitPayClient: BitPayClient;
Expand Down Expand Up @@ -116,12 +116,14 @@ export class RefundClient {
* Send a refund notification.
*
* @param refundId A BitPay refund ID.
* @param refundToken The resource token for the invoirefundIdceId.
* 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
*/
public async sendRefundNotification(refundId: string): Promise<boolean> {
const params = { token: this.tokenContainer.getToken(Facade.Merchant) };
public async sendRefundNotification(refundId: string, refundToken: string): Promise<boolean> {
const params = { token: refundToken };
const result = await this.bitPayClient.post('refunds/' + refundId + '/notifications', params, true);

try {
Expand Down
46 changes: 24 additions & 22 deletions test/clientFunctional.spec.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import * as fs from 'fs';
import * as path from 'path';
import { Client } from '../src';
import { Buyer } from '../src/Model/Invoice/Buyer';
import * as BitPaySDK from '../src/index';
import {
Bill,
PayoutRecipient,
Payout,
BillInterface,
Invoice,
InvoiceInterface,
PayoutRecipientInterface,
PayoutInterface,
LedgerInterface,
LedgerEntryInterface,
BillInterface,
PayoutRecipients,
PayoutGroupInterface
LedgerInterface,
Payout,
PayoutGroupInterface,
PayoutInterface,
PayoutRecipient,
PayoutRecipientInterface,
PayoutRecipients
} from '../src/Model';
import * as fs from 'fs';
import * as path from 'path';
import { InvoiceEventTokenInterface } from '../src/Model/Invoice/InvoiceEventToken';
import { Refund, RefundInterface } from '../src/Model/Invoice/Refund';
import { billInterfaceSchema } from '../src/Model/Bill/Bill.zod';
import { Item } from '../src/Model/Bill/Item';
import { WalletInterface } from '../src/Model/Wallet/Wallet';
import { CurrencyInterface } from '../src/Model/Currency/Currency';
import * as BitPaySDK from '../src/index';
import { rateInterfaceSchema } from '../src/Model/Rates/Rate.zod';
import { currencyInterfaceSchema } from '../src/Model/Currency/Currency.zod';
import { Buyer } from '../src/Model/Invoice/Buyer';
import { invoiceSchema } from '../src/Model/Invoice/Invoice.zod';
import { InvoiceEventTokenInterface } from '../src/Model/Invoice/InvoiceEventToken';
import { invoiceEventTokenInterfaceSchema } from '../src/Model/Invoice/InvoiceEventToken.zod';
import { Refund, RefundInterface } from '../src/Model/Invoice/Refund';
import { refundInterfaceSchema } from '../src/Model/Invoice/Refund.zod';
import { payoutRecipientInterfaceSchema } from '../src/Model/Payout/PayoutRecipient.zod';
import { payoutInterfaceSchema } from '../src/Model/Payout/Payout.zod';
import { payoutGroupInterfaceSchema } from '../src/Model/Payout/PayoutGroup.zod';
import { ledgerInterfaceSchema } from '../src/Model/Ledger/Ledger.zod';
import { ledgerEntryInterfaceSchema } from '../src/Model/Ledger/LedgerEntry.zod';
import { billInterfaceSchema } from '../src/Model/Bill/Bill.zod';
import { payoutInterfaceSchema } from '../src/Model/Payout/Payout.zod';
import { payoutGroupInterfaceSchema } from '../src/Model/Payout/PayoutGroup.zod';
import { payoutRecipientInterfaceSchema } from '../src/Model/Payout/PayoutRecipient.zod';
import { rateInterfaceSchema } from '../src/Model/Rates/Rate.zod';
import { WalletInterface } from '../src/Model/Wallet/Wallet';
import { walletInterfaceSchema } from '../src/Model/Wallet/Wallet.zod';
const Currencies = BitPaySDK.Currency;
const PayoutStatus = BitPaySDK.PayoutStatus;
Expand Down Expand Up @@ -228,7 +228,7 @@ describe('BitPaySDK.Client', () => {
* - GetRefund(string refundId)
* - GetRefundByGuid(string guid)
* - GetRefunds(string invoiceId)
* - SendRefundNotification(string refundId)
* - SendRefundNotification(string refundId, string refundToken)
* - CancelRefund(string refundId)
* - CancelRefundByGuid(string guid)
* <p>
Expand Down Expand Up @@ -279,7 +279,9 @@ describe('BitPaySDK.Client', () => {
});

it('should send refund notification', async () => {
const result: boolean = await client.sendRefundNotification(refundId);
const retrieveRefund: RefundInterface = await client.getRefund(refundId);

const result: boolean = await client.sendRefundNotification(refundId, retrieveRefund.token);
expect(result).toBe(true);
});

Expand Down
Loading

0 comments on commit d86743f

Please sign in to comment.