From 64611456603b2169edbc5f6f0a1c01e50ef807e9 Mon Sep 17 00:00:00 2001 From: Szymon Wlodarski Date: Thu, 23 Jan 2025 14:32:19 +0100 Subject: [PATCH] SP-1151 - use resource token in webhook resend requests --- examples/Merchant/InvoiceRequests.ts | 5 +- examples/Merchant/RefundRequests.ts | 10 ++- package-lock.json | 4 +- package.json | 2 +- src/Client.ts | 56 ++++++++------- src/Client/InvoiceClient.ts | 16 +++-- src/Client/RefundClient.ts | 16 +++-- src/Env.ts | 2 +- test/clientFunctional.spec.ts | 46 ++++++------ test/clientUnit.spec.ts | 103 ++++++++++++++------------- 10 files changed, 143 insertions(+), 117 deletions(-) diff --git a/examples/Merchant/InvoiceRequests.ts b/examples/Merchant/InvoiceRequests.ts index dc9323f..41cba2b 100644 --- a/examples/Merchant/InvoiceRequests.ts +++ b/examples/Merchant/InvoiceRequests.ts @@ -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); } } diff --git a/examples/Merchant/RefundRequests.ts b/examples/Merchant/RefundRequests.ts index 385c923..0999b7e 100644 --- a/examples/Merchant/RefundRequests.ts +++ b/examples/Merchant/RefundRequests.ts @@ -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() { @@ -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); } } diff --git a/package-lock.json b/package-lock.json index d3ca292..47b3cdb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "bitpay-sdk", - "version": "6.3.0", + "version": "6.3.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "bitpay-sdk", - "version": "6.3.0", + "version": "6.3.1", "license": "MIT", "dependencies": { "bs58": "6.0.0", diff --git a/package.json b/package.json index 34ee451..c55867e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bitpay-sdk", - "version": "6.3.0", + "version": "6.3.1", "description": "Complete version of the NodeJS library for the new cryptographically secure BitPay API", "engines": { "node": ">=18.0.0 <23.0.0" diff --git a/src/Client.ts b/src/Client.ts index 7ecc583..20ba44f 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -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; @@ -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 { - return this.createInvoiceClient().requestInvoiceWebhookToBeResent(invoiceId); + public async requestInvoiceWebhookToBeResent(invoiceId: string, invoiceToken: string): Promise { + return this.createInvoiceClient().requestInvoiceWebhookToBeResent(invoiceId, invoiceToken); } /** @@ -389,10 +391,12 @@ export class Client { * 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. * @return An updated Refund Object */ - public async sendRefundNotification(refundId: string): Promise { - return this.createRefundClient().sendRefundNotification(refundId); + public async sendRefundNotification(refundId: string, refundToken: string): Promise { + return this.createRefundClient().sendRefundNotification(refundId, refundToken); } /** diff --git a/src/Client/InvoiceClient.ts b/src/Client/InvoiceClient.ts index fbee389..7cb14f1 100644 --- a/src/Client/InvoiceClient.ts +++ b/src/Client/InvoiceClient.ts @@ -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; @@ -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 { - const params = { token: this.tokenContainer.getToken(Facade.Merchant) }; + public async requestInvoiceWebhookToBeResent(invoiceId: string, invoiceToken: string): Promise { + const params = { token: invoiceToken }; const result = await this.bitPayClient.post('invoices/' + invoiceId + '/notifications', params); try { diff --git a/src/Client/RefundClient.ts b/src/Client/RefundClient.ts index 498dbe9..e08bd7e 100644 --- a/src/Client/RefundClient.ts +++ b/src/Client/RefundClient.ts @@ -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; @@ -116,12 +116,14 @@ export class RefundClient { * 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 */ - public async sendRefundNotification(refundId: string): Promise { - const params = { token: this.tokenContainer.getToken(Facade.Merchant) }; + public async sendRefundNotification(refundId: string, refundToken: string): Promise { + const params = { token: refundToken }; const result = await this.bitPayClient.post('refunds/' + refundId + '/notifications', params, true); try { diff --git a/src/Env.ts b/src/Env.ts index 5477dad..39de978 100644 --- a/src/Env.ts +++ b/src/Env.ts @@ -3,7 +3,7 @@ export const Prod = 'PROD'; export const TestUrl = 'https://test.bitpay.com/'; export const ProdUrl = 'https://bitpay.com/'; export const BitpayApiVersion = '2.0.0'; -export const BitpayPluginInfo = 'BitPay_NodeJs_Client_v6.3.0'; +export const BitpayPluginInfo = 'BitPay_NodeJs_Client_v6.3.1'; export const BitpayApiFrame = 'std'; export const BitpayApiFrameVersion = '1.0.0'; diff --git a/test/clientFunctional.spec.ts b/test/clientFunctional.spec.ts index ba4fc26..bc6a1e5 100644 --- a/test/clientFunctional.spec.ts +++ b/test/clientFunctional.spec.ts @@ -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; @@ -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) *

@@ -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); }); diff --git a/test/clientUnit.spec.ts b/test/clientUnit.spec.ts index 67c9ac2..be1ccf5 100644 --- a/test/clientUnit.spec.ts +++ b/test/clientUnit.spec.ts @@ -1,5 +1,7 @@ +import { DefaultBodyType, PathParams, http } from 'msw'; +import { setupServer } from 'msw/node'; import { Client, Facade } from '../src'; -import { Buyer } from '../src/Model/Invoice/Buyer'; +import { BitPayClient } from '../src/Client/BitPayClient'; import { Bill, BillInterface, @@ -11,82 +13,79 @@ import { RateInterface, Rates } from '../src/Model'; -import { Refund } from '../src/Model/Invoice/Refund'; import { Item } from '../src/Model/Bill/Item'; -import { BitPayClient } from '../src/Client/BitPayClient'; -import { DefaultBodyType, PathParams, http } from 'msw'; -import { setupServer } from 'msw/node'; +import { Buyer } from '../src/Model/Invoice/Buyer'; +import { InvoiceBuyerProvidedInfo } from '../src/Model/Invoice/InvoiceBuyerProvidedInfo'; +import { Refund } from '../src/Model/Invoice/Refund'; import { TokenContainer } from '../src/TokenContainer'; import { GuidGenerator } from '../src/util/GuidGenerator'; -import { InvoiceBuyerProvidedInfo } from '../src/Model/Invoice/InvoiceBuyerProvidedInfo'; +import * as cancelInvoiceSuccessResponseMock from './json/cancelInvoiceSuccessResponse.json'; +import * as cancelPayoutGroupResponseMock from './json/cancelPayoutGroupResponse.json'; +import * as cancelPayoutResponseMock from './json/cancelPayoutResponse.json'; +import * as cancelRefundResponseMock from './json/cancelRefundResponse.json'; import * as createBillRequestMock from './json/createBillRequest.json'; import * as createBillResponseMock from './json/createBillResponse.json'; -import * as getBillsResponseMock from './json/getBillsResponse.json'; -import * as updateBillRequestMock from './json/updateBillRequest.json'; -import * as updateBillResponseMock from './json/updateBillResponse.json'; +import * as createInvoiceRequestMock from './json/createInvoiceRequest.json'; +import * as createInvoiceResponseMcok from './json/createInvoiceResponse.json'; +import * as createPayoutGroupRequestMock from './json/createPayoutGroupRequest.json'; +import * as createPayoutGroupResponseMock from './json/createPayoutGroupResponse.json'; +import * as createPayoutRequestMock from './json/createPayoutRequest.json'; +import * as createPayoutResponseMock from './json/createPayoutResponse.json'; +import * as createRefundResponseMock from './json/createRefundResponse.json'; +import * as deletePayoutRecipientResponseMock from './json/deletePayoutRecipientResponse.json'; import * as deliverBillRequestMock from './json/deliverBillRequest.json'; import * as deliverBillResponseMock from './json/deliverBillResponse.json'; import * as errorResponse from './json/errorResponse.json'; +import * as getBillsResponseMock from './json/getBillsResponse.json'; import * as getCurrenciesResponseMock from './json/getCurrenciesResponse.json'; -import * as createInvoiceRequestMock from './json/createInvoiceRequest.json'; -import * as createInvoiceResponseMcok from './json/createInvoiceResponse.json'; +import * as getInvoiceEventTokenMock from './json/getInvoiceEventToken.json'; import * as getInvoiceResponseMock from './json/getInvoiceResponse.json'; import * as getInvoicesResponseMock from './json/getInvoicesResponse.json'; -import * as payInvoiceRequestMock from './json/payInvoiceRequest.json'; -import * as payInvoiceResponseMock from './json/payInvoiceResponse.json'; -import * as cancelInvoiceSuccessResponseMock from './json/cancelInvoiceSuccessResponse.json'; -import * as invoiceWebhookResponseMock from './json/invoiceWebhookResponse.json'; -import * as getInvoiceEventTokenMock from './json/getInvoiceEventToken.json'; import * as getLedgerEntriesResponseMock from './json/getLedgerEntriesResponse.json'; import * as getLedgersResponseMock from './json/getLedgersResponse.json'; -import * as createPayoutRequestMock from './json/createPayoutRequest.json'; -import * as createPayoutResponseMock from './json/createPayoutResponse.json'; -import * as createPayoutGroupRequestMock from './json/createPayoutGroupRequest.json'; -import * as createPayoutGroupResponseMock from './json/createPayoutGroupResponse.json'; +import * as getPayoutRecipientResponseMock from './json/getPayoutRecipientResponse.json'; +import * as getPayoutRecipientsResponseMock from './json/getPayoutRecipientsResponse.json'; import * as getPayoutResponseMock from './json/getPayoutResponse.json'; import * as getPayoutsResponseMock from './json/getPayoutsResponse.json'; -import * as cancelPayoutResponseMock from './json/cancelPayoutResponse.json'; -import * as cancelPayoutGroupResponseMock from './json/cancelPayoutGroupResponse.json'; +import * as getRateResponseMock from './json/getRateResponse.json'; +import * as getRatesResponseMock from './json/getRatesResponse.json'; +import * as getSettlementReconciliationReportResponseMock from './json/getSettlementReconciliationReportResponse.json'; +import * as getSettlementResponseMock from './json/getSettlementResponse.json'; +import * as getSettlementsResponseMock from './json/getSettlementsResponse.json'; +import * as getSupportedWalletsMock from './json/getSupportedWallets.json'; +import * as invalidSignature from './json/invalidSignature.json'; +import * as invoiceWebhookResponseMock from './json/invoiceWebhookResponse.json'; +import * as payInvoiceRequestMock from './json/payInvoiceRequest.json'; +import * as payInvoiceResponseMock from './json/payInvoiceResponse.json'; import * as sendPayoutNotificationRequestMock from './json/sendPayoutNotificationRequest.json'; import * as sendPayoutNotificationResponseMock from './json/sendPayoutNotificationResponse.json'; +import * as sendRefundNotificationResponseMock from './json/sendRefundNotificationResponse.json'; import * as submitPayoutRecipientsRequestMock from './json/submitPayoutRecipientsRequest.json'; import * as submitPayoutRecipientsResponseMock from './json/submitPayoutRecipientsResponse.json'; -import * as getPayoutRecipientsResponseMock from './json/getPayoutRecipientsResponse.json'; -import * as getPayoutRecipientResponseMock from './json/getPayoutRecipientResponse.json'; +import * as updateBillRequestMock from './json/updateBillRequest.json'; +import * as updateBillResponseMock from './json/updateBillResponse.json'; import * as updatePayoutRecipientResponseMock from './json/updatePayoutRecipientResponse.json'; -import * as deletePayoutRecipientResponseMock from './json/deletePayoutRecipientResponse.json'; -import * as getRateResponseMock from './json/getRateResponse.json'; -import * as getRatesResponseMock from './json/getRatesResponse.json'; -import * as createRefundResponseMock from './json/createRefundResponse.json'; import * as updateRefundRequestMock from './json/updateRefundRequest.json'; import * as updateRefundResponseMock from './json/updateRefundResponse.json'; -import * as sendRefundNotificationRequestMock from './json/sendRefundNotificationRequest.json'; -import * as sendRefundNotificationResponseMock from './json/sendRefundNotificationResponse.json'; -import * as cancelRefundResponseMock from './json/cancelRefundResponse.json'; -import * as getSettlementsResponseMock from './json/getSettlementsResponse.json'; -import * as getSettlementResponseMock from './json/getSettlementResponse.json'; -import * as getSettlementReconciliationReportResponseMock from './json/getSettlementReconciliationReportResponse.json'; -import * as getSupportedWalletsMock from './json/getSupportedWallets.json'; -import * as invalidSignature from './json/invalidSignature.json'; import { isEqual } from 'lodash'; -import * as BitPaySDK from '../src/index'; +import { HttpRequestResolverExtras } from 'msw/lib/core/handlers/HttpHandler'; +import { ResponseResolverInfo } from 'msw/lib/core/handlers/RequestHandler'; import BitPayApiException from '../src/Exceptions/BitPayApiException'; -import { invoiceSchema } from '../src/Model/Invoice/Invoice.zod'; +import * as BitPaySDK from '../src/index'; import { billInterfaceSchema } from '../src/Model/Bill/Bill.zod'; import { currencyInterfaceSchema } from '../src/Model/Currency/Currency.zod'; -import { ledgerEntryInterfaceSchema } from '../src/Model/Ledger/LedgerEntry.zod'; +import { invoiceSchema } from '../src/Model/Invoice/Invoice.zod'; +import { refundInterfaceSchema } from '../src/Model/Invoice/Refund.zod'; import { ledgerInterfaceSchema } from '../src/Model/Ledger/Ledger.zod'; +import { ledgerEntryInterfaceSchema } from '../src/Model/Ledger/LedgerEntry.zod'; import { payoutInterfaceSchema } from '../src/Model/Payout/Payout.zod'; import { payoutGroupInterfaceSchema } from '../src/Model/Payout/PayoutGroup.zod'; import { payoutGroupFailedInterfaceSchema } from '../src/Model/Payout/PayoutGroupFailed.zod'; +import { payoutRecipientInterfaceSchema } from '../src/Model/Payout/PayoutRecipient.zod'; import { rateInterfaceSchema } from '../src/Model/Rates/Rate.zod'; -import { refundInterfaceSchema } from '../src/Model/Invoice/Refund.zod'; import { settlementInterfaceSchema } from '../src/Model/Settlement/Settlement.zod'; import { walletInterfaceSchema } from '../src/Model/Wallet/Wallet.zod'; -import { payoutRecipientInterfaceSchema } from '../src/Model/Payout/PayoutRecipient.zod'; -import { HttpRequestResolverExtras } from 'msw/lib/core/handlers/HttpHandler'; -import { ResponseResolverInfo } from 'msw/lib/core/handlers/RequestHandler'; let client; let oneMonthAgo; @@ -169,6 +168,13 @@ describe('BitPaySDK.Client', () => { } } + function validatResourceTokenInFormData(resourceToken: string, json: { token?: string }) { + const token = json.token; + if (token !== resourceToken) { + throw new Error('Missing/wrong token'); + } + } + function validatePayoutTokenInFormData(json: { token?: string }) { const token = json.token; if (token !== payoutToken) { @@ -694,12 +700,13 @@ describe('BitPaySDK.Client', () => { }); it('should send invoice webhook to be resent', async () => { + const invoiceToken = 'cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT'; server.use( http.post( host + '/invoices/Hpqc63wvE1ZjzeeH4kEycF/notifications', async (responseResolver: ResponseResolverInfo, DefaultBodyType>) => { const json = (await responseResolver.request.json()) as object; - validateMerchantTokenInFormData(json); + validatResourceTokenInFormData(invoiceToken, json); validateSignatureRequest(responseResolver.request); return new Response(JSON.stringify(invoiceWebhookResponseMock)); @@ -707,7 +714,7 @@ describe('BitPaySDK.Client', () => { ) ); - const result = await client.requestInvoiceWebhookToBeResent('Hpqc63wvE1ZjzeeH4kEycF'); + const result = await client.requestInvoiceWebhookToBeResent('Hpqc63wvE1ZjzeeH4kEycF', invoiceToken); expect(result).toBe(true); }); @@ -1299,21 +1306,21 @@ describe('BitPaySDK.Client', () => { }); it('should send refund notification', async () => { + const refundToken = 'cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT'; server.use( http.post( host + '/refunds/WoE46gSLkJQS48RJEiNw3L/notifications', async (responseResolver: ResponseResolverInfo, DefaultBodyType>) => { const json = (await responseResolver.request.json()) as object; validateSignatureRequest(responseResolver.request); - validateMerchantTokenInFormData(json); - validateRequest(json, sendRefundNotificationRequestMock); + validatResourceTokenInFormData(refundToken, json); return new Response(JSON.stringify(sendRefundNotificationResponseMock)); } ) ); - const result = await client.sendRefundNotification('WoE46gSLkJQS48RJEiNw3L', 'created'); + const result = await client.sendRefundNotification('WoE46gSLkJQS48RJEiNw3L', refundToken); expect(result).toBe(true); });