Skip to content

Commit

Permalink
#v3.2.2001.1-rc - Refunds refactor (#69)
Browse files Browse the repository at this point in the history
* - Refunds refactored
* - Version upgrade
  • Loading branch information
Antonio Buedo committed Jan 10, 2020
1 parent bd279b2 commit c4b0e35
Show file tree
Hide file tree
Showing 28 changed files with 439 additions and 16 deletions.
Binary file modified Assemblies/BitPay.Net.Lib/net452/BitPay.dll
Binary file not shown.
Binary file modified Assemblies/BitPay.Net.Lib/net46/BitPay.dll
Binary file not shown.
Binary file modified Assemblies/BitPay.Net.Lib/net461/BitPay.dll
Binary file not shown.
Binary file modified Assemblies/BitPay.Net.Lib/net462/BitPay.dll
Binary file not shown.
Binary file modified Assemblies/BitPay.Net.Lib/net47/BitPay.dll
Binary file not shown.
Binary file modified Assemblies/BitPay.Net.Lib/net471/BitPay.dll
Binary file not shown.
Binary file modified Assemblies/BitPay.Net.Lib/net472/BitPay.dll
Binary file not shown.
4 changes: 2 additions & 2 deletions Assemblies/BitPay.Net.Lib/netcoreapp2.0/BitPay.deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v2.0": {
"BitPay/3.1.1912": {
"BitPay/3.2.2001": {
"dependencies": {
"BouncyCastle": "1.8.5",
"Microsoft.CSharp": "4.5.0",
Expand Down Expand Up @@ -147,7 +147,7 @@
}
},
"libraries": {
"BitPay/3.1.1912": {
"BitPay/3.2.2001": {
"type": "project",
"serviceable": false,
"sha512": ""
Expand Down
Binary file modified Assemblies/BitPay.Net.Lib/netcoreapp2.0/BitPay.dll
Binary file not shown.
4 changes: 2 additions & 2 deletions Assemblies/BitPay.Net.Lib/netcoreapp2.1/BitPay.deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v2.1": {
"BitPay/3.1.1912": {
"BitPay/3.2.2001": {
"dependencies": {
"BouncyCastle": "1.8.5",
"Microsoft.CSharp": "4.5.0",
Expand Down Expand Up @@ -137,7 +137,7 @@
}
},
"libraries": {
"BitPay/3.1.1912": {
"BitPay/3.2.2001": {
"type": "project",
"serviceable": false,
"sha512": ""
Expand Down
Binary file modified Assemblies/BitPay.Net.Lib/netcoreapp2.1/BitPay.dll
Binary file not shown.
4 changes: 2 additions & 2 deletions Assemblies/BitPay.Net.Lib/netcoreapp2.2/BitPay.deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v2.2": {
"BitPay/3.1.1912": {
"BitPay/3.2.2001": {
"dependencies": {
"BouncyCastle": "1.8.5",
"Microsoft.CSharp": "4.5.0",
Expand Down Expand Up @@ -137,7 +137,7 @@
}
},
"libraries": {
"BitPay/3.1.1912": {
"BitPay/3.2.2001": {
"type": "project",
"serviceable": false,
"sha512": ""
Expand Down
Binary file modified Assemblies/BitPay.Net.Lib/netcoreapp2.2/BitPay.dll
Binary file not shown.
4 changes: 2 additions & 2 deletions Assemblies/BitPay.Net.Lib/netstandard2.0/BitPay.deps.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"targets": {
".NETStandard,Version=v2.0": {},
".NETStandard,Version=v2.0/": {
"BitPay/3.1.1912": {
"BitPay/3.2.2001": {
"dependencies": {
"BouncyCastle": "1.8.5",
"Microsoft.CSharp": "4.5.0",
Expand Down Expand Up @@ -180,7 +180,7 @@
}
},
"libraries": {
"BitPay/3.1.1912": {
"BitPay/3.2.2001": {
"type": "project",
"serviceable": false,
"sha512": ""
Expand Down
Binary file modified Assemblies/BitPay.Net.Lib/netstandard2.0/BitPay.dll
Binary file not shown.
Binary file added Assemblies/BitPay.Net.Lib_3.2.2001.zip
Binary file not shown.
176 changes: 173 additions & 3 deletions BitPay/BitPay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

/**
* @author Antonio Buedo
* @date 20.12.2019
* @version 3.1.1912
* @date 10.01.2020
* @version 3.2.2001
*
* See bitpay.com/api for more information.
*/
Expand Down Expand Up @@ -173,7 +173,7 @@ public string GetTokenByFacade(string facade)
/// <param name="facade">The facade to create the invoice against</param>
/// <param name="signRequest">Allow unsigned request</param>
/// <returns>A new invoice object returned from the server.</returns>
public async Task<Invoice> CreateInvoice(Invoice invoice, string facade = Facade.PointOfSale, bool signRequest = true)
public async Task<Invoice> CreateInvoice(Invoice invoice, string facade = Facade.Merchant, bool signRequest = true)
{
try
{
Expand Down Expand Up @@ -277,6 +277,176 @@ public async Task<List<Invoice>> GetInvoices(DateTime dateStart, DateTime dateEn
}
}

/// <summary>
/// Create a refund for a BitPay invoice.
/// </summary>
/// <param name="invoice">A BitPay invoice object for which a refund request should be made. Must have been obtained using the merchant facade.</param>
/// <param name="refundEmail">The email of the buyer to which the refund email will be sent.</param>
/// <param name="amount">The amount of money to refund. If zero then a request for 100% of the invoice value is created.</param>
/// <param name="currency">The three digit currency code specifying the exchange rate to use when calculating the refund bitcoin amount. If this value is "BTC" then no exchange rate calculation is performed.</param>
/// <returns>ATrue if the refund was successfully created, false otherwise.</returns>
/// <throws>RefundCreationException RefundCreationException class</throws>
///
public async Task<bool> CreateRefund(Invoice invoice, string refundEmail, double amount, string currency) {

try
{
bool result;
Refund refund = new Refund();
refund.Token = invoice.Token;
refund.Guid = Guid.NewGuid().ToString();
refund.Amount = amount;
refund.RefundEmail = refundEmail;
refund.Currency = currency;
var json = JsonConvert.SerializeObject(refund);
var response = await Post("invoices/" + invoice.Id + "/refunds", json, true).ConfigureAwait(false);
var responseString = await ResponseToJsonString(response).ConfigureAwait(false);
JObject responseObject = JsonConvert.DeserializeObject<dynamic>(responseString);
bool.TryParse(responseObject.GetValue("success").ToString(), out result);

return result;
}
catch (Exception ex)
{
if (!(ex.GetType().IsSubclassOf(typeof(BitPayException)) || ex.GetType() == typeof(BitPayException)))
throw new RefundCreationException(ex);

throw;
}
}

/// <summary>
/// Retrieve all refund requests on a BitPay invoice.
/// </summary>
/// <param name="invoice">The BitPay invoice object having the associated refunds.</param>
/// <returns>A BitPay invoice object with the associated Refund objects updated.</returns>
/// <throws>RefundQueryException RefundQueryException class</throws>
///
public async Task<List<Refund>> GetRefunds(Invoice invoice)
{
try
{
var parameters = InitParams();
parameters.Add("token", invoice.Token);

var response = await Get("invoices/" + invoice.Id + "/refunds", parameters);
var responseString = await ResponseToJsonString(response);

return JsonConvert.DeserializeObject<List<Refund>>(responseString);
}
catch (Exception ex)
{
if (!(ex.GetType().IsSubclassOf(typeof(BitPayException)) || ex.GetType() == typeof(BitPayException)))
throw new RefundQueryException(ex);

throw;
}
}

/// <summary>
/// Retrieve a previously made refund request on a BitPay invoice.
/// </summary>
/// <param name="invoice">The BitPay invoice having the associated refund.</param>
/// <param name="refundId">The refund id for the refund to be updated with new status.</param>
/// <returns>TA BitPay invoice object with the associated Refund object updated.</returns>
/// <throws>RefundQueryException RefundQueryException class</throws>
public async Task<Refund> GetRefund(Invoice invoice, string refundId)
{
try
{
var parameters = InitParams();
parameters.Add("token", invoice.Token);

var response = await Get("invoices/" + invoice.Id + "/refunds/" + refundId, parameters);
var responseString = await ResponseToJsonString(response);

return JsonConvert.DeserializeObject<Refund>(responseString);
}
catch (Exception ex)
{
if (!(ex.GetType().IsSubclassOf(typeof(BitPayException)) || ex.GetType() == typeof(BitPayException)))
throw new RefundQueryException(ex);

throw;
}
}

/// <summary>
/// Implements the CancelRefund method bellow.
/// </summary>
/// <param name="invoiceId">The BitPay invoice Id having the associated refund to be canceled.</param>
/// <param name="refundId">The refund Id for the refund to be canceled.</param>
/// <returns> ATrue if the refund was successfully canceled, false otherwise.</returns>
/// <throws>RefundCancellationException RefundCancellationException class</throws>
public async Task<bool> CancelRefund(string invoiceId, string refundId)
{
try
{
var invoice = await GetInvoice(invoiceId);

return await CancelRefund(invoice, refundId);
}
catch (Exception ex)
{
if (!(ex.GetType().IsSubclassOf(typeof(BitPayException)) || ex.GetType() == typeof(BitPayException)))
throw new RefundCancellationException(ex);

throw;
}
}

/// <summary>
/// Implements the CancelRefund method bellow.
/// </summary>
/// <param name="invoice">The BitPay invoice having the associated refund to be canceled. Must have been obtained using the merchant facade.</param>
/// <param name="refundId">The refund objhect for the refund to be canceled.</param>
/// <returns> ATrue if the refund was successfully canceled, false otherwise.</returns>
/// <throws>RefundCancellationException RefundCancellationException class</throws>
public async Task<bool> CancelRefund(Invoice invoice, string refundId)
{
try
{
var refund = await GetRefund(invoice, refundId);

return await CancelRefund(invoice.Id, refund);
}
catch (Exception ex)
{
if (!(ex.GetType().IsSubclassOf(typeof(BitPayException)) || ex.GetType() == typeof(BitPayException)))
throw new RefundCancellationException(ex);

throw;
}
}

/// <summary>
/// Cancel a previously submitted refund request on a BitPay invoice.
/// </summary>
/// <param name="invoiceId">The BitPay invoiceId having the associated refund to be canceled.</param>
/// <param name="refund">The BitPay refund for the refund to be canceled.</param>
/// <returns> ATrue if the refund was successfully canceled, false otherwise.</returns>
/// <throws>RefundCancellationException RefundCancellationException class</throws>
public async Task<bool> CancelRefund(string invoiceId, Refund refund)
{
try
{
var parameters = InitParams();
parameters.Add("token", refund.Token);

var response = await Delete("invoices/" + invoiceId + "/refunds/" + refund.Id, parameters);
var responseString = await ResponseToJsonString(response).ConfigureAwait(false);
return responseString.Replace("\"", "").Equals("Success");
}
catch (Exception ex)
{
if (!(ex.GetType().IsSubclassOf(typeof(BitPayException)) || ex.GetType() == typeof(BitPayException)))
throw new RefundCancellationException(ex);

throw;
}

}

/// <summary>
/// Create a bill.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions BitPay/BitPay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<TargetFrameworks>net452;net46;net461;net462;net47;net471;net472;netstandard2.0;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2</TargetFrameworks>
<LangVersion>latest</LangVersion>
<!-- version numbers will be updated by build -->
<AssemblyVersion>3.1.1912.0</AssemblyVersion>
<FileVersion>3.1.1912</FileVersion>
<VersionPrefix>3.1.1912</VersionPrefix>
<Version>3.1.1912</Version>
<AssemblyVersion>3.2.2001.0</AssemblyVersion>
<FileVersion>3.2.2001</FileVersion>
<VersionPrefix>3.2.2001</VersionPrefix>
<Version>3.2.2001</Version>
<Authors>Antonio Buedo</Authors>
<Company>BitPay Inc.</Company>
<Owners>BitPay, Inc.</Owners>
Expand Down
2 changes: 1 addition & 1 deletion BitPay/Env.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Env
public const string TestUrl = "https://test.bitpay.com/";
public const string ProdUrl = "https://bitpay.com/";
public const string BitpayApiVersion = "2.0.0";
public const string BitpayPluginInfo = "BitPay_DotNet_Client_v3.1.1912";
public const string BitpayPluginInfo = "BitPay_DotNet_Client_v3.2.2001";
public class Tokens
{
public string POS { get; set; }
Expand Down
18 changes: 18 additions & 0 deletions BitPay/Exceptions/RefundCancellationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace BitPaySDK.Exceptions
{
public class RefundCancellationException : BitPayException
{
private const string BitPayCode = "BITPAY-REFUND-CANCELLATION";
private const string BitPayMessage = "Failed to cancel refund";

public RefundCancellationException() : base(BitPayCode, BitPayMessage)
{
}

public RefundCancellationException(Exception ex) : base(BitPayCode, BitPayMessage, ex)
{
}
}
}
18 changes: 18 additions & 0 deletions BitPay/Exceptions/RefundCreationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace BitPaySDK.Exceptions
{
public class RefundCreationException : BitPayException
{
private const string BitPayCode = "BITPAY-REFUND-CREATE";
private const string BitPayMessage = "Failed to create refund";

public RefundCreationException() : base(BitPayCode, BitPayMessage)
{
}

public RefundCreationException(Exception ex) : base(BitPayCode, BitPayMessage, ex)
{
}
}
}
28 changes: 28 additions & 0 deletions BitPay/Exceptions/RefundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace BitPaySDK.Exceptions
{
public class RefundException : BitPayException
{
private const string BitPayMessage = "An unexpected error occured while trying to manage the refund";
private readonly string _bitpayCode = "BITPAY-REFUND-GENERIC";

public RefundException() : base(BitPayMessage)
{
BitpayCode = _bitpayCode;
}

public RefundException(Exception ex) : base(BitPayMessage, ex)
{
BitpayCode = _bitpayCode;
}

public RefundException(string bitpayCode, string message) : base(bitpayCode, message)
{
}

public RefundException(string bitpayCode, string message, Exception cause) : base(bitpayCode, message, cause)
{
}
}
}
18 changes: 18 additions & 0 deletions BitPay/Exceptions/RefundQueryException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace BitPaySDK.Exceptions
{
public class RefundQueryException : RefundException
{
private const string BitPayCode = "BITPAY-REFUND-GET";
private const string BitPayMessage = "Failed to retrieve refund";

public RefundQueryException() : base(BitPayCode, BitPayMessage)
{
}

public RefundQueryException(Exception ex) : base(BitPayCode, BitPayMessage, ex)
{
}
}
}

0 comments on commit c4b0e35

Please sign in to comment.