Skip to content

Commit 78dbbf0

Browse files
SP-1149: use resource token in webhook resend requests
1 parent c5909ca commit 78dbbf0

File tree

6 files changed

+48
-16
lines changed

6 files changed

+48
-16
lines changed

BitPay/Client.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,15 @@ public async Task<Invoice> CancelInvoiceByGuid(string refundGuid)
316316
/// Request a webhook to be resent.
317317
/// </summary>
318318
/// <param name="invoiceId">Invoice ID.</param>
319+
/// <param name="invoiceToken">
320+
/// The resource token for the invoiceId.
321+
/// This token can be retrieved from the Bitpay's invoice object.
322+
/// </param>
319323
/// <returns>Status of request</returns>
320324
/// <exception cref="ArgumentNullException"></exception>
321-
public async Task<Boolean> RequestInvoiceWebhookToBeResent(string invoiceId)
325+
public async Task<Boolean> RequestInvoiceWebhookToBeResent(string invoiceId, string invoiceToken)
322326
{
323-
return await CreateInvoiceClient().RequestInvoiceWebhookToBeResent(invoiceId).ConfigureAwait(false);
327+
return await CreateInvoiceClient().RequestInvoiceWebhookToBeResent(invoiceId, invoiceToken).ConfigureAwait(false);
324328
}
325329

326330
/// <summary>
@@ -420,12 +424,16 @@ public async Task<Refund> UpdateRefundByGuid(string refundGuid, string status)
420424
/// Send a refund notification
421425
/// </summary>
422426
/// <param name="refundId">A BitPay refundId </param>
427+
/// <param name="refundToken">
428+
/// The resource token for the refundId.
429+
/// This token can be retrieved from the Bitpay's refund object.
430+
/// </param>
423431
/// <returns>An updated Refund Object </returns>
424432
/// <throws>RefundCreationException RefundCreationException class </throws>
425433
/// <throws>BitPayException BitPayException class </throws>
426-
public async Task<Boolean> SendRefundNotification(string refundId)
434+
public async Task<Boolean> SendRefundNotification(string refundId, string refundToken)
427435
{
428-
return await CreateRefundClient().SendRefundNotification(refundId).ConfigureAwait(false);
436+
return await CreateRefundClient().SendRefundNotification(refundId, refundToken).ConfigureAwait(false);
429437
}
430438

431439
/// <summary>

BitPay/Clients/InvoiceClient.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,17 @@ public async Task<Invoice> CancelInvoiceByGuid(string guidId, bool forceCancel =
310310
/// Request a webhook to be resent.
311311
/// </summary>
312312
/// <param name="invoiceId">Invoice ID.</param>
313+
/// <param name="invoiceToken">
314+
/// The resource token for the invoiceId.
315+
/// This token can be retrieved from the Bitpay's invoice object.
316+
/// </param>
313317
/// <returns>Status of request</returns>
314318
/// <exception cref="BitPayGenericException">BitPayGenericException class</exception>
315319
/// <exception cref="BitPayApiException">BitPayApiException class</exception>
316-
public async Task<bool> RequestInvoiceWebhookToBeResent(string invoiceId)
320+
public async Task<bool> RequestInvoiceWebhookToBeResent(string invoiceId, string invoiceToken)
317321
{
318322
var parameters = ResourceClientUtil.InitParams();
319-
parameters.Add("token", _accessTokens.GetAccessToken((Facade.Merchant)));
323+
parameters.Add("token", invoiceToken);
320324

321325
string json = null!;
322326

@@ -407,4 +411,4 @@ public async Task<Invoice> PayInvoice(string invoiceId, string status)
407411
return invoice;
408412
}
409413
}
410-
}
414+
}

BitPay/Clients/RefundClient.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,17 @@ public async Task<Refund> CancelByGuid(string refundGuid)
302302
/// Send a refund notification
303303
/// </summary>
304304
/// <param name="refundId">A BitPay refundId </param>
305+
/// <param name="refundToken">
306+
/// The resource token for the refundId.
307+
/// This token can be retrieved from the Bitpay's refund object.
308+
/// </param>
305309
/// <returns>An updated Refund Object </returns>
306310
/// <exception cref="BitPayGenericException">BitPayGenericException class</exception>
307311
/// <exception cref="BitPayApiException">BitPayApiException class</exception>
308-
public async Task<bool> SendRefundNotification(string refundId)
312+
public async Task<bool> SendRefundNotification(string refundId, string refundToken)
309313
{
310314
var parameters = ResourceClientUtil.InitParams();
311-
parameters.Add("token", _accessTokens.GetAccessToken(Facade.Merchant));
315+
parameters.Add("token", refundToken);
312316

313317
string json;
314318

@@ -342,4 +346,4 @@ public async Task<bool> SendRefundNotification(string refundId)
342346
}
343347
}
344348
}
345-
}
349+
}

BitPay/Models/Invoice/Refund.cs

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public class Refund
2727
[JsonProperty(PropertyName = "guid")]
2828
public string? ResourceGuid { get; set; }
2929

30+
[JsonProperty(PropertyName = "token")]
31+
public string? Token { get; set; }
32+
3033
[JsonProperty(PropertyName = "refundAddress")]
3134
public string? RefundAddress { get; set; }
3235

BitPayFunctionalTest/BitPayFunctionalTest.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public async Task it_should_test_refunds_requests()
146146
var refundToCreateRequest = new Refund(invoiceId: invoiceId, amount: 10.0M);
147147
var refund = await _client.CreateRefund(refundToCreateRequest);
148148
var refundId = refund.Id!;
149+
var refundToken = refund.Token!;
149150

150151
var retrieveRefund = await _client.GetRefund(refundId);
151152
Assert.Equal(refundId, retrieveRefund.Id);
@@ -158,7 +159,7 @@ public async Task it_should_test_refunds_requests()
158159
Assert.NotEmpty(retrieveRefundByInvoiceId);
159160
retrieveRefundByInvoiceId.Exists(refundByInvoice => refundByInvoice.Invoice == invoiceId);
160161

161-
var refundNotification = await _client.SendRefundNotification(refundId);
162+
var refundNotification = await _client.SendRefundNotification(refundId, refundToken);
162163
Assert.True(refundNotification);
163164

164165
var cancelRefund = await _client.CancelRefund(refundId);
@@ -480,4 +481,4 @@ private static string GetBitPayUnitTestPath()
480481
return bitPayUnitTestPath;
481482
}
482483
}
483-
}
484+
}

BitPayUnitTest/ClientTest.cs

+16-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
using BitPay;
88
using BitPay.Clients;
99
using BitPay.Exceptions;
10+
using BitPay.Models;
1011
using BitPay.Models.Bill;
1112
using BitPay.Models.Invoice;
1213
using BitPay.Models.Payout;
1314
using BitPay.Utils;
1415

1516
using Moq;
1617

18+
using Newtonsoft.Json;
19+
1720
using Environment = BitPay.Environment;
1821
using SystemEnvironment = System.Environment;
1922

@@ -826,11 +829,15 @@ public void it_should_cancel_invoice_by_guid()
826829
[Fact]
827830
public void it_should_request_invoice_webhook_to_be_resent()
828831
{
832+
var invoiceToken = "cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT";var responseObject = new
833+
{
834+
token = invoiceToken
835+
};
829836
// given
830837
HttpContent response = new StringContent(File.ReadAllText(GetJsonResponsePath() + "invoiceWebhookResponse.json"));
831838
_bitPayClient.Setup(b => b.Post(
832839
"invoices/Hpqc63wvE1ZjzeeH4kEycF/notifications",
833-
"{\"token\":\"merchantToken\"}",
840+
JsonConvert.SerializeObject(responseObject),
834841
false
835842
)).ReturnsAsync(new HttpResponseMessage
836843
{
@@ -841,7 +848,7 @@ public void it_should_request_invoice_webhook_to_be_resent()
841848
});
842849

843850
// when
844-
var result = GetTestedClassAsMerchant().RequestInvoiceWebhookToBeResent("Hpqc63wvE1ZjzeeH4kEycF").Result;
851+
var result = GetTestedClassAsMerchant().RequestInvoiceWebhookToBeResent("Hpqc63wvE1ZjzeeH4kEycF", invoiceToken).Result;
845852

846853
// then
847854
Assert.True(result);
@@ -1665,11 +1672,16 @@ public void it_should_update_refund_by_guid()
16651672
[Fact]
16661673
public void it_should_send_refund_notification()
16671674
{
1675+
var refundToken = "cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT";
1676+
var responseObject = new
1677+
{
1678+
token = refundToken
1679+
};
16681680
// given
16691681
HttpContent response = new StringContent(File.ReadAllText(GetJsonResponsePath() + "sendRefundNotificationResponse.json"));
16701682
_bitPayClient.Setup(b => b.Post(
16711683
"refunds/WoE46gSLkJQS48RJEiNw3L/notifications",
1672-
"{\"token\":\"merchantToken\"}",
1684+
JsonConvert.SerializeObject(responseObject),
16731685
true
16741686
)).ReturnsAsync(new HttpResponseMessage
16751687
{
@@ -1679,7 +1691,7 @@ public void it_should_send_refund_notification()
16791691
});
16801692

16811693
// when
1682-
var result = GetTestedClassAsMerchant().SendRefundNotification("WoE46gSLkJQS48RJEiNw3L").Result;
1694+
var result = GetTestedClassAsMerchant().SendRefundNotification("WoE46gSLkJQS48RJEiNw3L", refundToken).Result;
16831695

16841696
// then
16851697
Assert.True(result);

0 commit comments

Comments
 (0)