Skip to content

Commit d86743f

Browse files
committed
SP-1151 - use resource token in invoice and refund webhook resend requests
1 parent 6925c24 commit d86743f

7 files changed

+139
-113
lines changed

examples/Merchant/InvoiceRequests.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export class InvoiceRequests {
6767
public async requestInvoiceWebhookToBeResent() {
6868
const client = ClientProvider.create();
6969

70-
return await client.deliverBill('someBillId', 'myBillToken');
70+
const invoiceId = 'myInvoiceId';
71+
const invoice = await client.getInvoice(invoiceId);
72+
73+
return await client.requestInvoiceWebhookToBeResent(invoiceId, invoice.token);
7174
}
7275
}

examples/Merchant/RefundRequests.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ClientProvider } from '../ClientProvider';
21
import { Refund } from '../../src/Model/Invoice/Refund';
2+
import { ClientProvider } from '../ClientProvider';
33

44
export class RefundRequests {
55
public async createRefund() {
@@ -49,6 +49,12 @@ export class RefundRequests {
4949
public async requestRefundNotificationToBeResent() {
5050
const client = ClientProvider.create();
5151

52-
return await client.sendRefundNotification('someRefundId');
52+
const refundId = 'someRefundId';
53+
const refund = await client.getRefund(refundId);
54+
if (!refund.token) {
55+
throw new Error('Refund token is required to request a refund notification to be resent.');
56+
}
57+
58+
return await client.sendRefundNotification(refundId, refund.token);
5359
}
5460
}

src/Client.ts

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
/* eslint-disable @typescript-eslint/no-unused-vars*/
22

33
import { ec } from 'elliptic';
4+
import {
5+
BillClient,
6+
BitPayClient,
7+
CurrencyClient,
8+
InvoiceClient,
9+
LedgerClient,
10+
PayoutClient,
11+
PayoutGroupClient,
12+
PayoutRecipientClient,
13+
RateClient,
14+
RefundClient,
15+
SettlementClient,
16+
WalletClient
17+
} from './Client/index';
418
import { Env, Facade, KeyUtils } from './index';
519
import {
620
BillInterface,
721
InvoiceInterface,
822
LedgerEntryInterface,
923
LedgerInterface,
10-
PayoutInterface,
1124
PayoutGroupInterface,
25+
PayoutInterface,
1226
PayoutRecipientInterface,
1327
PayoutRecipients,
1428
RateInterface,
1529
Rates
1630
} from './Model';
17-
import {
18-
BitPayClient,
19-
RateClient,
20-
CurrencyClient,
21-
InvoiceClient,
22-
RefundClient,
23-
PayoutClient,
24-
PayoutGroupClient,
25-
PayoutRecipientClient,
26-
LedgerClient,
27-
BillClient,
28-
WalletClient,
29-
SettlementClient
30-
} from './Client/index';
3131

32-
import { TokenContainer } from './TokenContainer';
32+
import * as fs from 'fs';
3333
import { Environment } from './Environment';
34-
import { GuidGenerator } from './util/GuidGenerator';
34+
import { BitPayExceptionProvider } from './Exceptions/BitPayExceptionProvider';
35+
import { CurrencyInterface } from './Model/Currency/Currency';
3536
import { InvoiceEventTokenInterface } from './Model/Invoice/InvoiceEventToken';
3637
import { RefundInterface } from './Model/Invoice/Refund';
37-
import { ParamsRemover } from './util/ParamsRemover';
38-
import { WalletInterface } from './Model/Wallet/Wallet';
3938
import { SettlementInterface } from './Model/Settlement/Settlement';
39+
import { WalletInterface } from './Model/Wallet/Wallet';
4040
import { PosToken } from './PosToken';
4141
import { PrivateKey } from './PrivateKey';
42-
import { CurrencyInterface } from './Model/Currency/Currency';
43-
import * as fs from 'fs';
44-
import { BitPayExceptionProvider } from './Exceptions/BitPayExceptionProvider';
42+
import { TokenContainer } from './TokenContainer';
43+
import { GuidGenerator } from './util/GuidGenerator';
44+
import { ParamsRemover } from './util/ParamsRemover';
4545

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

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

398402
/**

src/Client/InvoiceClient.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { BitPayClient } from './BitPayClient';
1+
import { BitPayExceptionProvider } from '../Exceptions/BitPayExceptionProvider';
2+
import { Facade } from '../Facade';
23
import { Invoice, InvoiceInterface } from '../Model';
3-
import { TokenContainer } from '../TokenContainer';
4-
import { GuidGenerator } from '../util/GuidGenerator';
54
import { InvoiceEventTokenInterface } from '../Model/Invoice/InvoiceEventToken';
5+
import { TokenContainer } from '../TokenContainer';
66
import { BitPayResponseParser } from '../util/BitPayResponseParser';
7-
import { BitPayExceptionProvider } from '../Exceptions/BitPayExceptionProvider';
8-
import { Facade } from '../Facade';
7+
import { GuidGenerator } from '../util/GuidGenerator';
8+
import { BitPayClient } from './BitPayClient';
99

1010
export class InvoiceClient {
1111
private bitPayClient: BitPayClient;
@@ -225,12 +225,14 @@ export class InvoiceClient {
225225
* Request a BitPay Invoice Webhook.
226226
*
227227
* @param invoiceId A BitPay invoice ID.
228+
* @param invoiceToken The resource token for the invoiceId.
229+
* This token can be retrieved from the Bitpay's invoice object.
228230
* @returns boolean
229231
* @throws BitPayApiException BitPayApiException class
230232
* @throws BitPayGenericException BitPayGenericException class
231233
*/
232-
public async requestInvoiceWebhookToBeResent(invoiceId: string): Promise<boolean> {
233-
const params = { token: this.tokenContainer.getToken(Facade.Merchant) };
234+
public async requestInvoiceWebhookToBeResent(invoiceId: string, invoiceToken: string): Promise<boolean> {
235+
const params = { token: invoiceToken };
234236
const result = await this.bitPayClient.post('invoices/' + invoiceId + '/notifications', params);
235237

236238
try {

src/Client/RefundClient.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { BitPayClient } from './BitPayClient';
2-
import { TokenContainer } from '../TokenContainer';
3-
import { GuidGenerator } from '../util/GuidGenerator';
1+
import { BitPayExceptionProvider } from '../Exceptions/BitPayExceptionProvider';
42
import { Facade } from '../index';
53
import { RefundInterface } from '../Model/Invoice/Refund';
6-
import { ParamsRemover } from '../util/ParamsRemover';
4+
import { TokenContainer } from '../TokenContainer';
75
import { BitPayResponseParser } from '../util/BitPayResponseParser';
8-
import { BitPayExceptionProvider } from '../Exceptions/BitPayExceptionProvider';
6+
import { GuidGenerator } from '../util/GuidGenerator';
7+
import { ParamsRemover } from '../util/ParamsRemover';
8+
import { BitPayClient } from './BitPayClient';
99

1010
export class RefundClient {
1111
private bitPayClient: BitPayClient;
@@ -116,12 +116,14 @@ export class RefundClient {
116116
* Send a refund notification.
117117
*
118118
* @param refundId A BitPay refund ID.
119+
* @param refundToken The resource token for the invoirefundIdceId.
120+
* This token can be retrieved from the Bitpay's refund object.
119121
* @returns boolean An updated Refund Object
120122
* @throws BitPayApiException BitPayApiException class
121123
* @throws BitPayGenericException BitPayGenericException class
122124
*/
123-
public async sendRefundNotification(refundId: string): Promise<boolean> {
124-
const params = { token: this.tokenContainer.getToken(Facade.Merchant) };
125+
public async sendRefundNotification(refundId: string, refundToken: string): Promise<boolean> {
126+
const params = { token: refundToken };
125127
const result = await this.bitPayClient.post('refunds/' + refundId + '/notifications', params, true);
126128

127129
try {

test/clientFunctional.spec.ts

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
13
import { Client } from '../src';
2-
import { Buyer } from '../src/Model/Invoice/Buyer';
4+
import * as BitPaySDK from '../src/index';
35
import {
46
Bill,
5-
PayoutRecipient,
6-
Payout,
7+
BillInterface,
78
Invoice,
89
InvoiceInterface,
9-
PayoutRecipientInterface,
10-
PayoutInterface,
11-
LedgerInterface,
1210
LedgerEntryInterface,
13-
BillInterface,
14-
PayoutRecipients,
15-
PayoutGroupInterface
11+
LedgerInterface,
12+
Payout,
13+
PayoutGroupInterface,
14+
PayoutInterface,
15+
PayoutRecipient,
16+
PayoutRecipientInterface,
17+
PayoutRecipients
1618
} from '../src/Model';
17-
import * as fs from 'fs';
18-
import * as path from 'path';
19-
import { InvoiceEventTokenInterface } from '../src/Model/Invoice/InvoiceEventToken';
20-
import { Refund, RefundInterface } from '../src/Model/Invoice/Refund';
19+
import { billInterfaceSchema } from '../src/Model/Bill/Bill.zod';
2120
import { Item } from '../src/Model/Bill/Item';
22-
import { WalletInterface } from '../src/Model/Wallet/Wallet';
2321
import { CurrencyInterface } from '../src/Model/Currency/Currency';
24-
import * as BitPaySDK from '../src/index';
25-
import { rateInterfaceSchema } from '../src/Model/Rates/Rate.zod';
2622
import { currencyInterfaceSchema } from '../src/Model/Currency/Currency.zod';
23+
import { Buyer } from '../src/Model/Invoice/Buyer';
2724
import { invoiceSchema } from '../src/Model/Invoice/Invoice.zod';
25+
import { InvoiceEventTokenInterface } from '../src/Model/Invoice/InvoiceEventToken';
2826
import { invoiceEventTokenInterfaceSchema } from '../src/Model/Invoice/InvoiceEventToken.zod';
27+
import { Refund, RefundInterface } from '../src/Model/Invoice/Refund';
2928
import { refundInterfaceSchema } from '../src/Model/Invoice/Refund.zod';
30-
import { payoutRecipientInterfaceSchema } from '../src/Model/Payout/PayoutRecipient.zod';
31-
import { payoutInterfaceSchema } from '../src/Model/Payout/Payout.zod';
32-
import { payoutGroupInterfaceSchema } from '../src/Model/Payout/PayoutGroup.zod';
3329
import { ledgerInterfaceSchema } from '../src/Model/Ledger/Ledger.zod';
3430
import { ledgerEntryInterfaceSchema } from '../src/Model/Ledger/LedgerEntry.zod';
35-
import { billInterfaceSchema } from '../src/Model/Bill/Bill.zod';
31+
import { payoutInterfaceSchema } from '../src/Model/Payout/Payout.zod';
32+
import { payoutGroupInterfaceSchema } from '../src/Model/Payout/PayoutGroup.zod';
33+
import { payoutRecipientInterfaceSchema } from '../src/Model/Payout/PayoutRecipient.zod';
34+
import { rateInterfaceSchema } from '../src/Model/Rates/Rate.zod';
35+
import { WalletInterface } from '../src/Model/Wallet/Wallet';
3636
import { walletInterfaceSchema } from '../src/Model/Wallet/Wallet.zod';
3737
const Currencies = BitPaySDK.Currency;
3838
const PayoutStatus = BitPaySDK.PayoutStatus;
@@ -228,7 +228,7 @@ describe('BitPaySDK.Client', () => {
228228
* - GetRefund(string refundId)
229229
* - GetRefundByGuid(string guid)
230230
* - GetRefunds(string invoiceId)
231-
* - SendRefundNotification(string refundId)
231+
* - SendRefundNotification(string refundId, string refundToken)
232232
* - CancelRefund(string refundId)
233233
* - CancelRefundByGuid(string guid)
234234
* <p>
@@ -279,7 +279,9 @@ describe('BitPaySDK.Client', () => {
279279
});
280280

281281
it('should send refund notification', async () => {
282-
const result: boolean = await client.sendRefundNotification(refundId);
282+
const retrieveRefund: RefundInterface = await client.getRefund(refundId);
283+
284+
const result: boolean = await client.sendRefundNotification(refundId, retrieveRefund.token);
283285
expect(result).toBe(true);
284286
});
285287

0 commit comments

Comments
 (0)