Skip to content

Commit

Permalink
#v2.1 - Models refactoring (#51)
Browse files Browse the repository at this point in the history
* Structural updates
- Rate model
- Invoice model
- Refund model
- Settlements model
- Currency validation

* Functional
- Target framework 4.5.2
- Added netstandard 2.0 reference
  • Loading branch information
Antonio Buedo authored Jun 19, 2019
1 parent c0d26bf commit a8b9c78
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 38 deletions.
Binary file modified Assemblies/BitPay.Net.Lib/BitPay.dll
Binary file not shown.
18 changes: 18 additions & 0 deletions Assemblies/BitPay.Net.Lib/BitPay.dll.config
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="mscorlib" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.InteropServices" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
</assemblyBinding>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Text.RegularExpressions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
</assemblyBinding>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" culture="neutral" />
Expand Down
9 changes: 6 additions & 3 deletions BitPay/BitPay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
using BitPayAPI.Models;
using BitPayAPI.Models.Invoice;
using BitPayAPI.Models.Ledger;
using BitPayAPI.Models.Rate;
using BitPayAPI.Models.Settlement;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Configuration;

/**
* @author Antonio Buedo
* @date 4.15.2019
* @version 2.0.1904
* @date 6.19.2019
* @version 2.1.1904
*
* See bitpay.com/api for more information.
*/
Expand All @@ -33,7 +35,7 @@ public class BitPay
private static string _configFilePath;

private const string BitpayApiVersion = "2.0.0";
private const string BitpayPluginInfo = "BitPay_DotNet_Client_v2.0.1904";
private const string BitpayPluginInfo = "BitPay_DotNet_Client_v2.1.1906";

private string _baseUrl;
private string _clientName;
Expand Down Expand Up @@ -872,6 +874,7 @@ private void GetConfig()
{
try
{
string path = Directory.GetCurrentDirectory();
if (!File.Exists(_configFilePath))
{
throw new Exception("Configuration file not found");
Expand Down
21 changes: 12 additions & 9 deletions BitPay/BitPay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BitPayAPI</RootNamespace>
<AssemblyName>BitPay</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
Expand Down Expand Up @@ -80,6 +80,9 @@
<Reference Include="Multiformats.Base, Version=1.0.8.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Multiformats.Base.1.0.8\lib\net45\Multiformats.Base.dll</HintPath>
</Reference>
<Reference Include="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51">
<HintPath>..\..\..\..\..\..\Library\Frameworks\Mono.framework\Versions\5.18.1\lib\mono\4.7.2-api\Facades\netstandard.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -282,7 +285,6 @@
<Compile Include="Models\Invoice\ExchangeRateBch.cs" />
<Compile Include="Models\Invoice\ExchangeRateBtc.cs" />
<Compile Include="Models\Invoice\ExchangeRates.cs" />
<Compile Include="Models\InvoiceData.cs" />
<Compile Include="Models\Invoice\InvoiceBuyerProvidedInfo.cs" />
<Compile Include="Models\Invoice\InvoiceFlags.cs" />
<Compile Include="Models\Invoice\Status.cs" />
Expand All @@ -307,15 +309,16 @@
<Compile Include="Models\Invoice\SupportedTransactionCurrency.cs" />
<Compile Include="Models\Invoice\SupportedTransactionCurrencies.cs" />
<Compile Include="Models\Currency.cs" />
<Compile Include="Models\Rate\Rate.cs" />
<Compile Include="Models\Rate\Rates.cs" />
<Compile Include="Models\Settlement\InvoiceData.cs" />
<Compile Include="Models\Settlement\RefundAmount.cs" />
<Compile Include="Models\Settlement\RefundInfo.cs" />
<Compile Include="Models\Settlement\Settlement.cs" />
<Compile Include="Models\Settlement\SettlementLedgerEntry.cs" />
<Compile Include="Models\Settlement\WithHoldings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Models\Rate.cs" />
<Compile Include="Models\Rates.cs" />
<Compile Include="Models\RefundAmount.cs" />
<Compile Include="Models\RefundInfo.cs" />
<Compile Include="Models\Settlement.cs" />
<Compile Include="Models\SettlementLedgerEntry.cs" />
<Compile Include="Models\Token.cs" />
<Compile Include="Models\WithHoldings.cs" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
Expand Down
7 changes: 6 additions & 1 deletion BitPay/Models/Currency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Model provided to avoid mistyping. Check the allowed currencies at: https://bitpay.com/currencies
/// </summary>
public static class Currency
public class Currency
{
// Crypto
public const string BCH = "BCH";
Expand Down Expand Up @@ -179,5 +179,10 @@ public static class Currency
public const string ZAR = "ZAR";
public const string ZMW = "ZMW";
public const string ZWL = "ZWL";

public static bool isValid(string value)
{
return typeof(Currency).GetMember(value).Length > 0;
}
}
}
2 changes: 1 addition & 1 deletion BitPay/Models/Invoice/Invoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public string Currency
get => _currency;
set
{
if (typeof(Currency).GetMember(value).Length == 0)
if (!Models.Currency.isValid(value))
throw new BitPayException("Error: currency code must be a type of BitPayAPI.Models.Currency");

_currency = value;
Expand Down
2 changes: 1 addition & 1 deletion BitPay/Models/Rate.cs → BitPay/Models/Rate/Rate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Newtonsoft.Json;

namespace BitPayAPI.Models
namespace BitPayAPI.Models.Rate
{
/// <summary>
/// Provides an interface to a single exchange rate.
Expand Down
2 changes: 1 addition & 1 deletion BitPay/Models/Rates.cs → BitPay/Models/Rate/Rates.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BitPayAPI.Models
namespace BitPayAPI.Models.Rate
{
/// <summary>
/// Provides an interface to the BitPay server to obtain exchange rate information.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace BitPayAPI.Models
namespace BitPayAPI.Models.Settlement
{
public class InvoiceData
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BitPayAPI.Models
namespace BitPayAPI.Models.Settlement
{
public class RefundAmount
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BitPayAPI.Models
namespace BitPayAPI.Models.Settlement
{
public class RefundInfo
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace BitPayAPI.Models
namespace BitPayAPI.Models.Settlement
{
public class Settlement
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace BitPayAPI.Models
namespace BitPayAPI.Models.Settlement
{
public class SettlementLedgerEntry
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BitPayAPI.Models
namespace BitPayAPI.Models.Settlement
{
public class WithHoldings
{
Expand Down
4 changes: 2 additions & 2 deletions BitPay/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.1904")]
[assembly: AssemblyFileVersion("2.0.1904")]
[assembly: AssemblyVersion("2.1.1906")]
[assembly: AssemblyFileVersion("2.1.1906")]
2 changes: 1 addition & 1 deletion BitPaySetup/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion("2.0.1904")]
[assembly: AssemblyVersion("2.1.1906")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
Expand Down
35 changes: 28 additions & 7 deletions BitPayUnitTest/BitPayTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using BitPayAPI.Models;
using BitPayAPI.Models.Invoice;
using Microsoft.Extensions.Configuration;
using Buyer = BitPayAPI.Models.Invoice.Buyer;

namespace BitPayUnitTest {

Expand All @@ -21,7 +22,7 @@ public class BitPayTest {
private static readonly string PairingCode = "GHXnenG";

// Your favourite client name
private static readonly string ClientName = "BitPay .Net Client v2.0.1904 Tester on " + Environment.MachineName;
private static readonly string ClientName = "BitPay .Net Client v2.1.1906 Tester on " + Environment.MachineName;

// Define the date range for fetching results during the test
private static DateTime today = DateTime.Now;
Expand Down Expand Up @@ -147,7 +148,7 @@ public async Task TestShouldGetInvoiceNoSigned() {
// create an invoice without signature then retrieve it through the get method - they should match
var invoice = await _bitpay.CreateInvoice(new Invoice(100.0, Currency.EUR), signRequest: false);
var retrievedInvoice = await _bitpay.GetInvoice(invoice.Id, Facade.PointOfSale, false);
Assert.Equal(invoice.Id, retrievedInvoice.Id);
Assert.AreEqual(invoice.Id, retrievedInvoice.Id);
}

[TestMethod]
Expand All @@ -170,21 +171,21 @@ public async Task TestShouldCreateInvoiceWithAdditionalParams() {
Buyer = buyerData,
PosData = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
PaymentCurrencies = new List<string> {
Invoice.PaymentCurrencyBtc,
Invoice.PaymentCurrencyBch
Currency.BTC,
Currency.BCH
},
AcceptanceWindow = 480000,
FullNotifications = true,
// NotificationEmail = "",
// NotificationUrl = "",
NotificationUrl = "https://hookb.in/03EBRQJrzasGmGkNPNw9",
OrderId = "1234",
Physical = true,
// RedirectUrl = "",
TransactionSpeed = "medium",
TransactionSpeed = "high",
ItemCode = "bitcoindonation",
ItemDesc = "dhdhdfgh"
};
invoice = await _bitpay.CreateInvoice(invoice);
invoice = await _bitpay.CreateInvoice(invoice, Facade.Merchant);
Assert.AreEqual(Status.New, invoice.Status, "Status is incorrect");
Assert.AreEqual("Satoshi", invoice.Buyer.Name, "BuyerName is incorrect");
Assert.AreEqual("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", invoice.PosData, "PosData is incorrect");
Expand Down Expand Up @@ -326,5 +327,25 @@ so this test will always fail - since you can't approve the Payroll pairing code

}

[TestMethod]
public async Task TestGetSettlements() {

// make sure we get a ledger with a not null Entries property
var settlements = await _bitpay.GetSettlements(Currency.EUR, yesterday.AddMonths(-1).AddDays(3), tomorrow);
Assert.IsNotNull(settlements);

}

[TestMethod]
public async Task TestGetSettlement() {

// make sure we get a ledger with a not null Entries property
var settlements = await _bitpay.GetSettlements(Currency.EUR, yesterday.AddMonths(-1).AddDays(3), tomorrow);
var firstSettlement = settlements[0];
var settlement = await _bitpay.GetSettlement(firstSettlement.Id);
Assert.IsNotNull(settlement.Id);
Assert.AreEqual(firstSettlement.Id, settlement.Id);
}

}
}
33 changes: 27 additions & 6 deletions BitPayXUnitTest/BitPayTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Xunit;
using BitPayAPI;
using System.Collections.Generic;
Expand All @@ -22,7 +22,7 @@ public class Tests
private static readonly string PairingCode = "Bh3yy6r";

// Your favourite client name
private static readonly string ClientName = "BitPay .Net Client v2.0.1904 Tester on " + Environment.MachineName;
private static readonly string ClientName = "BitPay .Net Client v2.1.1906 Tester on " + Environment.MachineName;

// Define the date range for fetching results during the test
private static DateTime today = DateTime.Now;
Expand All @@ -36,7 +36,7 @@ public Tests()
{
// JSON minified with the BitPay configuration as in the required configuration file
// and parsed into a IConfiguration object
var json = "{\"BitPayConfiguration\":{\"Environment\":\"Test\",\"EnvConfig\":{\"Test\":{\"ClientDescription\":\"Net_test_140519\",\"ApiUrl\":\"https://test.bitpay.com/\",\"ApiVersion\":\"2.0.0\",\"PrivateKeyPath\":\"bitpay_private_test.key\",\"ApiTokens\":{\"pos\":\"GzkfCV5uiRPW6WE82iFWmGiVKSBw2Sjxe8pYq2DnD7Z7\",\"merchant\":\"NJ3nQUrmuH8RvBsKEGt7i1ocCFQfRo1qGSKUiUUPPbJ\",\"payroll\":\"8N17w4Aw3GXjykGyiqUzigswXf22MnfB7HP9vHqfDSHL\"}},\"Prod\":{\"ClientDescription\":\"\",\"ApiUrl\":\"https://bitpay.com/\",\"ApiVersion\":\"2.0.0\",\"PrivateKeyPath\":\"\",\"ApiTokens\":{\"pos\":\"\",\"merchant\":\"\",\"payroll\":\"\"}}}}}";
var json = "{\"BitPayConfiguration\":{\"Environment\":\"Test\",\"EnvConfig\":{\"Test\":{\"ClientDescription\":\"Net_test\",\"ApiUrl\":\"https://test.bitpay.com/\",\"ApiVersion\":\"2.0.0\",\"PrivateKeyPath\":\"bitpay_private_test.key\",\"ApiTokens\":{\"pos\":\"AvJdGrEqTW9HVsJit9zabAnrJabqaQDhWHRacHYgfgxK\",\"merchant\":\"CE2WRSEEt9FgXvXboxNFA4YdQyyDJmgVAo752TGA7eUj\",\"payroll\":\"9pJ7fzW1GGeuDQfj32aNATCDnyY6YAacVMcDrs7HHUNo\"}},\"Prod\":{\"ClientDescription\":\"\",\"ApiUrl\":\"https://bitpay.com/\",\"ApiVersion\":\"2.0.0\",\"PrivateKeyPath\":\"\",\"ApiTokens\":{\"pos\":\"\",\"merchant\":\"\",\"payroll\":\"\"}}}}}";
var memoryJsonFile = new MemoryFileInfo("config.json", Encoding.UTF8.GetBytes(json), DateTimeOffset.Now);
var memoryFileProvider = new MockFileProvider(memoryJsonFile);

Expand Down Expand Up @@ -86,7 +86,7 @@ again and they should pass.
public async Task TestShouldGetInvoiceId()
{
// create an invoice and make sure we receive an id - which means it has been successfully submitted
var invoice = new Invoice(50.0, Currency.USD);
var invoice = new Invoice(30.0, Currency.EUR);
var basicInvoice = await _bitpay.CreateInvoice(invoice);
Assert.NotNull(basicInvoice.Id);
}
Expand Down Expand Up @@ -176,11 +176,11 @@ public async Task TestShouldCreateInvoiceWithAdditionalParams() {
AcceptanceWindow = 480000,
FullNotifications = true,
// NotificationEmail = "",
// NotificationUrl = "",
NotificationUrl = "https://hookb.in/03EBRQJrzasGmGkNPNw9",
OrderId = "1234",
Physical = true,
// RedirectUrl = "",
TransactionSpeed = "medium",
TransactionSpeed = "high",
ItemCode = "bitcoindonation",
ItemDesc = "dhdhdfgh"
};
Expand Down Expand Up @@ -318,5 +318,26 @@ so this test will always fail - since you can't approve the Payroll pairing code
await _bitpay.CancelPayoutBatch(batch0.Id);

}

[Fact]
public async Task TestGetSettlements() {

// make sure we get a ledger with a not null Entries property
var settlements = await _bitpay.GetSettlements(Currency.EUR, yesterday.AddMonths(-1).AddDays(3), tomorrow);
Assert.NotNull(settlements);

}

[Fact]
public async Task TestGetSettlement() {

// make sure we get a ledger with a not null Entries property
var settlements = await _bitpay.GetSettlements(Currency.EUR, yesterday.AddMonths(-1).AddDays(3), tomorrow);
var firstSettlement = settlements[0];
var settlement = await _bitpay.GetSettlement(firstSettlement.Id);
Assert.NotNull(settlement.Id);
Assert.Equal(firstSettlement.Id, settlement.Id);
}

}
}

0 comments on commit a8b9c78

Please sign in to comment.