1
0
mirror of https://github.com/bitwarden/server.git synced 2025-02-22 02:51:33 +01:00

switch to official bitpay light library

This commit is contained in:
Kyle Spearrin 2019-12-19 10:27:06 -05:00
parent 665e78ec1c
commit e2d65e5b08
6 changed files with 27 additions and 33 deletions

View File

@ -50,7 +50,7 @@ namespace Bit.Api.Controllers
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<string> PostBitPayInvoice([FromBody]BitPayInvoiceRequestModel model)
{
var invoice = await _bitPayClient.CreateInvoiceAsync(model.ToBitpayClientInvoice(_globalSettings));
var invoice = await _bitPayClient.CreateInvoiceAsync(model.ToBitpayInvoice(_globalSettings));
return invoice.Url;
}

View File

@ -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<Guid?, Guid?> GetIdsFromPosData(NBitpayClient.Invoice invoice)
public Tuple<Guid?, Guid?> GetIdsFromPosData(BitPayLight.Models.Invoice.Invoice invoice)
{
Guid? orgId = null;
Guid? userId = null;

View File

@ -23,6 +23,7 @@
<ItemGroup>
<PackageReference Include="AWSSDK.SimpleEmail" Version="3.3.101.38" />
<PackageReference Include="AWSSDK.SQS" Version="3.3.102" />
<PackageReference Include="BitPay.Light" Version="1.0.1907" />
<PackageReference Include="Handlebars.Net" Version="1.10.1" />
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="2.7.0" />
<PackageReference Include="MailKit" Version="2.3.0" />
@ -37,7 +38,6 @@
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.2.0" />
<PackageReference Include="NBitpayClient" Version="1.0.0.34" />
<PackageReference Include="Npgsql" Version="4.0.9" />
<PackageReference Include="Quartz" Version="3.0.7" />
<PackageReference Include="Serilog.AspNetCore" Version="3.0.0" />

View File

@ -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; }
}

View File

@ -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
};

View File

@ -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<bool> TestAccessAsync()
public Task<BitPayLight.Models.Invoice.Invoice> GetInvoiceAsync(string id)
{
return _bpClient.TestAccessAsync(NBitpayClient.Facade.Merchant);
return _bpClient.GetInvoice(id);
}
public Task<NBitpayClient.Invoice> GetInvoiceAsync(string id)
public Task<BitPayLight.Models.Invoice.Invoice> CreateInvoiceAsync(BitPayLight.Models.Invoice.Invoice invoice)
{
return _bpClient.GetInvoiceAsync(id);
}
public Task<NBitpayClient.Invoice> CreateInvoiceAsync(NBitpayClient.Invoice invoice)
{
return _bpClient.CreateInvoiceAsync(invoice);
return _bpClient.CreateInvoice(invoice);
}
}
}