From e2d65e5b0870d731caa96b34dc9448d2cbd43d31 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 19 Dec 2019 10:27:06 -0500 Subject: [PATCH] switch to official bitpay light library --- src/Api/Controllers/MiscController.cs | 2 +- src/Billing/Controllers/BitPayController.cs | 16 +++++++------ src/Core/Core.csproj | 2 +- src/Core/GlobalSettings.cs | 2 +- .../Api/Request/BitPayInvoiceRequestModel.cs | 15 ++++++------ src/Core/Utilities/BitPayClient.cs | 23 +++++++------------ 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/Api/Controllers/MiscController.cs b/src/Api/Controllers/MiscController.cs index 322eb134f8..64592b8397 100644 --- a/src/Api/Controllers/MiscController.cs +++ b/src/Api/Controllers/MiscController.cs @@ -50,7 +50,7 @@ namespace Bit.Api.Controllers [SelfHosted(NotSelfHostedOnly = true)] public async Task PostBitPayInvoice([FromBody]BitPayInvoiceRequestModel model) { - var invoice = await _bitPayClient.CreateInvoiceAsync(model.ToBitpayClientInvoice(_globalSettings)); + var invoice = await _bitPayClient.CreateInvoiceAsync(model.ToBitpayInvoice(_globalSettings)); return invoice.Url; } diff --git a/src/Billing/Controllers/BitPayController.cs b/src/Billing/Controllers/BitPayController.cs index f8a5fbafe2..54903cb813 100644 --- a/src/Billing/Controllers/BitPayController.cs +++ b/src/Billing/Controllers/BitPayController.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; using System.Data.SqlClient; +using System.Globalization; using System.Linq; using System.Threading.Tasks; @@ -104,7 +105,7 @@ namespace Bit.Billing.Controllers { var tx = new Core.Models.Table.Transaction { - Amount = invoice.Price, + Amount = Convert.ToDecimal(invoice.Price), CreationDate = GetTransactionDate(invoice), OrganizationId = ids.Item1, UserId = ids.Item2, @@ -112,7 +113,7 @@ namespace Bit.Billing.Controllers Gateway = GatewayType.BitPay, GatewayId = invoice.Id, PaymentMethodType = PaymentMethodType.BitPay, - Details = $"{invoice.TransactionCurrency}, BitPay {invoice.Id}" + Details = $"{invoice.Currency}, BitPay {invoice.Id}" }; await _transactionRepository.CreateAsync(tx); @@ -156,23 +157,24 @@ namespace Bit.Billing.Controllers return new OkResult(); } - private bool IsAccountCredit(NBitpayClient.Invoice invoice) + private bool IsAccountCredit(BitPayLight.Models.Invoice.Invoice invoice) { return invoice != null && invoice.PosData != null && invoice.PosData.Contains("accountCredit:1"); } - private DateTime GetTransactionDate(NBitpayClient.Invoice invoice) + private DateTime GetTransactionDate(BitPayLight.Models.Invoice.Invoice invoice) { var transactions = invoice.Transactions?.Where(t => t.Type == null && !string.IsNullOrWhiteSpace(t.Confirmations) && t.Confirmations != "0"); if(transactions != null && transactions.Count() == 1) { - return transactions.First().ReceivedTime.DateTime; + return DateTime.Parse(transactions.First().ReceivedTime, CultureInfo.InvariantCulture, + DateTimeStyles.RoundtripKind); } - return invoice.CurrentTime.DateTime; + return CoreHelpers.FromEpocMilliseconds(invoice.CurrentTime); } - public Tuple GetIdsFromPosData(NBitpayClient.Invoice invoice) + public Tuple GetIdsFromPosData(BitPayLight.Models.Invoice.Invoice invoice) { Guid? orgId = null; Guid? userId = null; diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index 78643f867a..a698e28754 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -23,6 +23,7 @@ + @@ -37,7 +38,6 @@ - diff --git a/src/Core/GlobalSettings.cs b/src/Core/GlobalSettings.cs index e375b617ca..e676d94205 100644 --- a/src/Core/GlobalSettings.cs +++ b/src/Core/GlobalSettings.cs @@ -197,7 +197,7 @@ namespace Bit.Core public class BitPaySettings { public bool Production { get; set; } - public string Base58Secret { get; set; } + public string Token { get; set; } public string NotificationUrl { get; set; } } diff --git a/src/Core/Models/Api/Request/BitPayInvoiceRequestModel.cs b/src/Core/Models/Api/Request/BitPayInvoiceRequestModel.cs index da5199d601..ec53ab6ef7 100644 --- a/src/Core/Models/Api/Request/BitPayInvoiceRequestModel.cs +++ b/src/Core/Models/Api/Request/BitPayInvoiceRequestModel.cs @@ -15,20 +15,19 @@ namespace Bit.Core.Models.Api public string Name { get; set; } public string Email { get; set; } - public NBitpayClient.Invoice ToBitpayClientInvoice(GlobalSettings globalSettings) + public BitPayLight.Models.Invoice.Invoice ToBitpayInvoice(GlobalSettings globalSettings) { - var inv = new NBitpayClient.Invoice + var inv = new BitPayLight.Models.Invoice.Invoice { - Price = Amount.Value, + Price = Convert.ToDouble(Amount.Value), Currency = "USD", - RedirectURL = ReturnUrl, - BuyerEmail = Email, - Buyer = new NBitpayClient.Buyer + RedirectUrl = ReturnUrl, + Buyer = new BitPayLight.Models.Invoice.Buyer { - email = Email, + Email = Email, Name = Name }, - NotificationURL = globalSettings.BitPay.NotificationUrl, + NotificationUrl = globalSettings.BitPay.NotificationUrl, FullNotifications = true, ExtendedNotifications = true }; diff --git a/src/Core/Utilities/BitPayClient.cs b/src/Core/Utilities/BitPayClient.cs index 4f8f59dac7..309eda1675 100644 --- a/src/Core/Utilities/BitPayClient.cs +++ b/src/Core/Utilities/BitPayClient.cs @@ -5,32 +5,25 @@ namespace Bit.Core.Utilities { public class BitPayClient { - private readonly NBitpayClient.Bitpay _bpClient; + private readonly BitPayLight.BitPay _bpClient; public BitPayClient(GlobalSettings globalSettings) { - if(CoreHelpers.SettingHasValue(globalSettings.BitPay.Base58Secret)) + if(CoreHelpers.SettingHasValue(globalSettings.BitPay.Token)) { - var btcSecret = new NBitcoin.BitcoinSecret(globalSettings.BitPay.Base58Secret, - globalSettings.BitPay.Production ? null : NBitcoin.Network.TestNet); - _bpClient = new NBitpayClient.Bitpay(btcSecret.PrivateKey, - new Uri(globalSettings.BitPay.Production ? "https://bitpay.com/" : "https://test.bitpay.com/")); + _bpClient = new BitPayLight.BitPay(globalSettings.BitPay.Token, + globalSettings.BitPay.Production ? BitPayLight.Env.Prod : BitPayLight.Env.Test); } } - public Task TestAccessAsync() + public Task GetInvoiceAsync(string id) { - return _bpClient.TestAccessAsync(NBitpayClient.Facade.Merchant); + return _bpClient.GetInvoice(id); } - public Task GetInvoiceAsync(string id) + public Task CreateInvoiceAsync(BitPayLight.Models.Invoice.Invoice invoice) { - return _bpClient.GetInvoiceAsync(id); - } - - public Task CreateInvoiceAsync(NBitpayClient.Invoice invoice) - { - return _bpClient.CreateInvoiceAsync(invoice); + return _bpClient.CreateInvoice(invoice); } } }