Skip to content

Commit

Permalink
SP-1149: use resource token in webhook resend requests
Browse files Browse the repository at this point in the history
  • Loading branch information
swlodarski-sumoheavy committed Jan 24, 2025
1 parent c5909ca commit 78dbbf0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
16 changes: 12 additions & 4 deletions BitPay/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,15 @@ public async Task<Invoice> CancelInvoiceByGuid(string refundGuid)
/// Request a webhook to be resent.
/// </summary>
/// <param name="invoiceId">Invoice ID.</param>
/// <param name="invoiceToken">
/// The resource token for the invoiceId.
/// This token can be retrieved from the Bitpay's invoice object.
/// </param>
/// <returns>Status of request</returns>
/// <exception cref="ArgumentNullException"></exception>
public async Task<Boolean> RequestInvoiceWebhookToBeResent(string invoiceId)
public async Task<Boolean> RequestInvoiceWebhookToBeResent(string invoiceId, string invoiceToken)
{
return await CreateInvoiceClient().RequestInvoiceWebhookToBeResent(invoiceId).ConfigureAwait(false);
return await CreateInvoiceClient().RequestInvoiceWebhookToBeResent(invoiceId, invoiceToken).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -420,12 +424,16 @@ public async Task<Refund> UpdateRefundByGuid(string refundGuid, string status)
/// Send a refund notification
/// </summary>
/// <param name="refundId">A BitPay refundId </param>
/// <param name="refundToken">
/// The resource token for the refundId.
/// This token can be retrieved from the Bitpay's refund object.
/// </param>
/// <returns>An updated Refund Object </returns>
/// <throws>RefundCreationException RefundCreationException class </throws>
/// <throws>BitPayException BitPayException class </throws>
public async Task<Boolean> SendRefundNotification(string refundId)
public async Task<Boolean> SendRefundNotification(string refundId, string refundToken)
{
return await CreateRefundClient().SendRefundNotification(refundId).ConfigureAwait(false);
return await CreateRefundClient().SendRefundNotification(refundId, refundToken).ConfigureAwait(false);
}

/// <summary>
Expand Down
10 changes: 7 additions & 3 deletions BitPay/Clients/InvoiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,17 @@ public async Task<Invoice> CancelInvoiceByGuid(string guidId, bool forceCancel =
/// Request a webhook to be resent.
/// </summary>
/// <param name="invoiceId">Invoice ID.</param>
/// <param name="invoiceToken">
/// The resource token for the invoiceId.
/// This token can be retrieved from the Bitpay's invoice object.
/// </param>
/// <returns>Status of request</returns>
/// <exception cref="BitPayGenericException">BitPayGenericException class</exception>
/// <exception cref="BitPayApiException">BitPayApiException class</exception>
public async Task<bool> RequestInvoiceWebhookToBeResent(string invoiceId)
public async Task<bool> RequestInvoiceWebhookToBeResent(string invoiceId, string invoiceToken)
{
var parameters = ResourceClientUtil.InitParams();
parameters.Add("token", _accessTokens.GetAccessToken((Facade.Merchant)));
parameters.Add("token", invoiceToken);

string json = null!;

Expand Down Expand Up @@ -407,4 +411,4 @@ public async Task<Invoice> PayInvoice(string invoiceId, string status)
return invoice;
}
}
}
}
10 changes: 7 additions & 3 deletions BitPay/Clients/RefundClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,17 @@ public async Task<Refund> CancelByGuid(string refundGuid)
/// Send a refund notification
/// </summary>
/// <param name="refundId">A BitPay refundId </param>
/// <param name="refundToken">
/// The resource token for the refundId.
/// This token can be retrieved from the Bitpay's refund object.
/// </param>
/// <returns>An updated Refund Object </returns>
/// <exception cref="BitPayGenericException">BitPayGenericException class</exception>
/// <exception cref="BitPayApiException">BitPayApiException class</exception>
public async Task<bool> SendRefundNotification(string refundId)
public async Task<bool> SendRefundNotification(string refundId, string refundToken)
{
var parameters = ResourceClientUtil.InitParams();
parameters.Add("token", _accessTokens.GetAccessToken(Facade.Merchant));
parameters.Add("token", refundToken);

string json;

Expand Down Expand Up @@ -342,4 +346,4 @@ public async Task<bool> SendRefundNotification(string refundId)
}
}
}
}
}
3 changes: 3 additions & 0 deletions BitPay/Models/Invoice/Refund.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class Refund
[JsonProperty(PropertyName = "guid")]
public string? ResourceGuid { get; set; }

[JsonProperty(PropertyName = "token")]
public string? Token { get; set; }

[JsonProperty(PropertyName = "refundAddress")]
public string? RefundAddress { get; set; }

Expand Down
5 changes: 3 additions & 2 deletions BitPayFunctionalTest/BitPayFunctionalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public async Task it_should_test_refunds_requests()
var refundToCreateRequest = new Refund(invoiceId: invoiceId, amount: 10.0M);
var refund = await _client.CreateRefund(refundToCreateRequest);
var refundId = refund.Id!;
var refundToken = refund.Token!;

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

var refundNotification = await _client.SendRefundNotification(refundId);
var refundNotification = await _client.SendRefundNotification(refundId, refundToken);
Assert.True(refundNotification);

var cancelRefund = await _client.CancelRefund(refundId);
Expand Down Expand Up @@ -480,4 +481,4 @@ private static string GetBitPayUnitTestPath()
return bitPayUnitTestPath;
}
}
}
}
20 changes: 16 additions & 4 deletions BitPayUnitTest/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
using BitPay;
using BitPay.Clients;
using BitPay.Exceptions;
using BitPay.Models;
using BitPay.Models.Bill;
using BitPay.Models.Invoice;
using BitPay.Models.Payout;
using BitPay.Utils;

using Moq;

using Newtonsoft.Json;

using Environment = BitPay.Environment;
using SystemEnvironment = System.Environment;

Expand Down Expand Up @@ -826,11 +829,15 @@ public void it_should_cancel_invoice_by_guid()
[Fact]
public void it_should_request_invoice_webhook_to_be_resent()
{
var invoiceToken = "cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT";var responseObject = new
{
token = invoiceToken
};
// given
HttpContent response = new StringContent(File.ReadAllText(GetJsonResponsePath() + "invoiceWebhookResponse.json"));
_bitPayClient.Setup(b => b.Post(
"invoices/Hpqc63wvE1ZjzeeH4kEycF/notifications",
"{\"token\":\"merchantToken\"}",
JsonConvert.SerializeObject(responseObject),
false
)).ReturnsAsync(new HttpResponseMessage
{
Expand All @@ -841,7 +848,7 @@ public void it_should_request_invoice_webhook_to_be_resent()
});

// when
var result = GetTestedClassAsMerchant().RequestInvoiceWebhookToBeResent("Hpqc63wvE1ZjzeeH4kEycF").Result;
var result = GetTestedClassAsMerchant().RequestInvoiceWebhookToBeResent("Hpqc63wvE1ZjzeeH4kEycF", invoiceToken).Result;

// then
Assert.True(result);
Expand Down Expand Up @@ -1665,11 +1672,16 @@ public void it_should_update_refund_by_guid()
[Fact]
public void it_should_send_refund_notification()
{
var refundToken = "cM78LHk17Q8fktDE6QLBBFfvH1QKBhRkHibTLcxhgzsu3VDRvSyu3CGi17DuwYxhT";
var responseObject = new
{
token = refundToken
};
// given
HttpContent response = new StringContent(File.ReadAllText(GetJsonResponsePath() + "sendRefundNotificationResponse.json"));
_bitPayClient.Setup(b => b.Post(
"refunds/WoE46gSLkJQS48RJEiNw3L/notifications",
"{\"token\":\"merchantToken\"}",
JsonConvert.SerializeObject(responseObject),
true
)).ReturnsAsync(new HttpResponseMessage
{
Expand All @@ -1679,7 +1691,7 @@ public void it_should_send_refund_notification()
});

// when
var result = GetTestedClassAsMerchant().SendRefundNotification("WoE46gSLkJQS48RJEiNw3L").Result;
var result = GetTestedClassAsMerchant().SendRefundNotification("WoE46gSLkJQS48RJEiNw3L", refundToken).Result;

// then
Assert.True(result);
Expand Down

0 comments on commit 78dbbf0

Please sign in to comment.