Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SP-1149: use resource token in webhook resend requests #166

Open
wants to merge 3 commits into
base: 6.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ jobs:
build-and-test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
dotnet-version: [ '6.0', '7.0', '8.0' ]

Expand All @@ -16,7 +17,7 @@ jobs:
with:
dotnet-version: ${{ matrix.dotnet-version }}
- name: Install dependencies
run: dotnet restore
run: dotnet restore -p:TargetFramework=net${{ matrix.dotnet-version }}
- name: Build
run: dotnet build --no-restore -f net${{ matrix.dotnet-version }}
- name: Test
Expand Down
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)
}
}
}
}
}
6 changes: 5 additions & 1 deletion BitPay/Exceptions/BitPayApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ public string? Code
return _code;
}
}

protected BitPayApiException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
}
6 changes: 5 additions & 1 deletion BitPay/Exceptions/BitPayException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@
public BitPayException(string message) : base(message)
{
}

protected BitPayException(SerializationInfo info, StreamingContext context) : base(info, context)

Check warning on line 19 in BitPay/Exceptions/BitPayException.cs

View workflow job for this annotation

GitHub Actions / build-and-test (8.0)

'Exception.Exception(SerializationInfo, StreamingContext)' is obsolete: 'This API supports obsolete formatter-based serialization. It should not be called or extended by application code.' (https://aka.ms/dotnet-warnings/SYSLIB0051)

Check warning on line 19 in BitPay/Exceptions/BitPayException.cs

View workflow job for this annotation

GitHub Actions / build-and-test (8.0)

'Exception.Exception(SerializationInfo, StreamingContext)' is obsolete: 'This API supports obsolete formatter-based serialization. It should not be called or extended by application code.' (https://aka.ms/dotnet-warnings/SYSLIB0051)
{
}
}
}
}
6 changes: 5 additions & 1 deletion BitPay/Exceptions/BitPayGenericException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ public class BitPayGenericException : BitPayException
public BitPayGenericException(string message) : base(message)
{
}

protected BitPayGenericException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
}
6 changes: 5 additions & 1 deletion BitPay/Exceptions/BitPayValidationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ public class BitPayValidationException : BitPayGenericException
public BitPayValidationException(string message) : base(message)
{
}

protected BitPayValidationException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
}
4 changes: 2 additions & 2 deletions BitPay/Models/Invoice/RecipientStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace BitPay.Models.Invoice
{
public class RecipientStatus
public static class RecipientStatus
{
public const string Active = "active";
public const string Invited = "invited";
Expand All @@ -12,4 +12,4 @@ public class RecipientStatus
public const string Unverified = "unverified";
public const string Verified = "verified";
}
}
}
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
Loading