2017-09-13 08:47:34 +02:00
|
|
|
|
using BTCPayServer.Authentication;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using BTCPayServer.Logging;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using NBitpayClient;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BTCPayServer.Models;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using NBitcoin;
|
|
|
|
|
using NBitcoin.DataEncoders;
|
|
|
|
|
using BTCPayServer.Filters;
|
|
|
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
|
using NBitcoin.Payment;
|
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
|
using BTCPayServer.Models.InvoicingModels;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using BTCPayServer.Services;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2017-09-15 09:06:57 +02:00
|
|
|
|
using BTCPayServer.Services.Stores;
|
2017-10-20 21:06:37 +02:00
|
|
|
|
using BTCPayServer.Services.Invoices;
|
2017-09-15 09:06:57 +02:00
|
|
|
|
using BTCPayServer.Services.Rates;
|
|
|
|
|
using BTCPayServer.Services.Wallets;
|
2017-09-27 07:18:09 +02:00
|
|
|
|
using BTCPayServer.Validations;
|
2017-10-11 05:20:44 +02:00
|
|
|
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-09-27 08:16:30 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Routing;
|
2017-10-12 09:33:53 +02:00
|
|
|
|
using NBXplorer.DerivationStrategy;
|
2017-10-12 17:25:45 +02:00
|
|
|
|
using NBXplorer;
|
2018-01-07 18:36:41 +01:00
|
|
|
|
using BTCPayServer.HostedServices;
|
2018-02-18 18:38:03 +01:00
|
|
|
|
using BTCPayServer.Payments;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public partial class InvoiceController : Controller
|
|
|
|
|
{
|
|
|
|
|
InvoiceRepository _InvoiceRepository;
|
2018-01-08 18:57:06 +01:00
|
|
|
|
IRateProviderFactory _RateProviders;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
StoreRepository _StoreRepository;
|
|
|
|
|
UserManager<ApplicationUser> _UserManager;
|
2017-10-27 11:58:43 +02:00
|
|
|
|
private CurrencyNameTable _CurrencyNameTable;
|
2017-12-17 11:58:55 +01:00
|
|
|
|
EventAggregator _EventAggregator;
|
2017-12-21 07:52:04 +01:00
|
|
|
|
BTCPayNetworkProvider _NetworkProvider;
|
2018-02-20 04:45:04 +01:00
|
|
|
|
private readonly BTCPayWalletProvider _WalletProvider;
|
|
|
|
|
IServiceProvider _ServiceProvider;
|
|
|
|
|
public InvoiceController(
|
|
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
|
InvoiceRepository invoiceRepository,
|
2017-10-27 11:58:43 +02:00
|
|
|
|
CurrencyNameTable currencyNameTable,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
UserManager<ApplicationUser> userManager,
|
2018-01-08 18:57:06 +01:00
|
|
|
|
IRateProviderFactory rateProviders,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
StoreRepository storeRepository,
|
2017-12-17 11:58:55 +01:00
|
|
|
|
EventAggregator eventAggregator,
|
2018-02-20 04:45:04 +01:00
|
|
|
|
BTCPayWalletProvider walletProvider,
|
|
|
|
|
BTCPayNetworkProvider networkProvider)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-02-20 04:45:04 +01:00
|
|
|
|
_ServiceProvider = serviceProvider;
|
2017-10-27 11:58:43 +02:00
|
|
|
|
_CurrencyNameTable = currencyNameTable ?? throw new ArgumentNullException(nameof(currencyNameTable));
|
2017-10-27 10:53:04 +02:00
|
|
|
|
_StoreRepository = storeRepository ?? throw new ArgumentNullException(nameof(storeRepository));
|
|
|
|
|
_InvoiceRepository = invoiceRepository ?? throw new ArgumentNullException(nameof(invoiceRepository));
|
2018-01-08 18:57:06 +01:00
|
|
|
|
_RateProviders = rateProviders ?? throw new ArgumentNullException(nameof(rateProviders));
|
2017-10-27 10:53:04 +02:00
|
|
|
|
_UserManager = userManager;
|
2017-12-17 11:58:55 +01:00
|
|
|
|
_EventAggregator = eventAggregator;
|
2017-12-21 07:52:04 +01:00
|
|
|
|
_NetworkProvider = networkProvider;
|
2018-02-20 04:45:04 +01:00
|
|
|
|
_WalletProvider = walletProvider;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-12-21 07:52:04 +01:00
|
|
|
|
|
2018-01-17 07:11:05 +01:00
|
|
|
|
internal async Task<DataWrapper<InvoiceResponse>> CreateInvoiceCore(Invoice invoice, StoreData store, string serverUrl)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
var entity = new InvoiceEntity
|
|
|
|
|
{
|
2018-01-06 10:57:56 +01:00
|
|
|
|
InvoiceTime = DateTimeOffset.UtcNow
|
2017-10-27 10:53:04 +02:00
|
|
|
|
};
|
2018-01-06 10:57:56 +01:00
|
|
|
|
|
2017-12-21 07:52:04 +01:00
|
|
|
|
var storeBlob = store.GetStoreBlob();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
Uri notificationUri = Uri.IsWellFormedUriString(invoice.NotificationURL, UriKind.Absolute) ? new Uri(invoice.NotificationURL, UriKind.Absolute) : null;
|
|
|
|
|
if (notificationUri == null || (notificationUri.Scheme != "http" && notificationUri.Scheme != "https")) //TODO: Filer non routable addresses ?
|
|
|
|
|
notificationUri = null;
|
|
|
|
|
EmailAddressAttribute emailValidator = new EmailAddressAttribute();
|
2018-01-17 07:11:05 +01:00
|
|
|
|
entity.ExpirationTime = entity.InvoiceTime.AddMinutes(storeBlob.InvoiceExpiration);
|
2017-12-03 06:43:52 +01:00
|
|
|
|
entity.MonitoringExpiration = entity.ExpirationTime + TimeSpan.FromMinutes(storeBlob.MonitoringExpiration);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
entity.OrderId = invoice.OrderId;
|
|
|
|
|
entity.ServerUrl = serverUrl;
|
2018-01-07 20:14:35 +01:00
|
|
|
|
entity.FullNotifications = invoice.FullNotifications || invoice.ExtendedNotifications;
|
|
|
|
|
entity.ExtendedNotifications = invoice.ExtendedNotifications;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
entity.NotificationURL = notificationUri?.AbsoluteUri;
|
|
|
|
|
entity.BuyerInformation = Map<Invoice, BuyerInformation>(invoice);
|
|
|
|
|
//Another way of passing buyer info to support
|
|
|
|
|
FillBuyerInfo(invoice.Buyer, entity.BuyerInformation);
|
|
|
|
|
if (entity?.BuyerInformation?.BuyerEmail != null)
|
|
|
|
|
{
|
|
|
|
|
if (!EmailValidator.IsEmail(entity.BuyerInformation.BuyerEmail))
|
|
|
|
|
throw new BitpayHttpException(400, "Invalid email");
|
|
|
|
|
entity.RefundMail = entity.BuyerInformation.BuyerEmail;
|
|
|
|
|
}
|
|
|
|
|
entity.ProductInformation = Map<Invoice, ProductInformation>(invoice);
|
|
|
|
|
entity.RedirectURL = invoice.RedirectURL ?? store.StoreWebsite;
|
|
|
|
|
entity.Status = "new";
|
|
|
|
|
entity.SpeedPolicy = ParseSpeedPolicy(invoice.TransactionSpeed, store.SpeedPolicy);
|
2017-11-12 15:23:21 +01:00
|
|
|
|
|
2018-03-28 15:37:01 +02:00
|
|
|
|
|
|
|
|
|
var supportedPaymentMethods = store.GetSupportedPaymentMethods(_NetworkProvider)
|
|
|
|
|
.Select(c =>
|
|
|
|
|
(Handler: (IPaymentMethodHandler)_ServiceProvider.GetService(typeof(IPaymentMethodHandler<>).MakeGenericType(c.GetType())),
|
|
|
|
|
SupportedPaymentMethod: c,
|
|
|
|
|
Network: _NetworkProvider.GetNetwork(c.PaymentId.CryptoCode)))
|
|
|
|
|
.Where(c => c.Network != null)
|
|
|
|
|
.Select(o =>
|
|
|
|
|
(SupportedPaymentMethod: o.SupportedPaymentMethod,
|
|
|
|
|
PaymentMethod: CreatePaymentMethodAsync(o.Handler, o.SupportedPaymentMethod, o.Network, entity, storeBlob)))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
List<string> paymentMethodErrors = new List<string>();
|
2018-03-25 18:57:44 +02:00
|
|
|
|
List<ISupportedPaymentMethod> supported = new List<ISupportedPaymentMethod>();
|
2018-03-28 15:37:01 +02:00
|
|
|
|
var paymentMethods = new PaymentMethodDictionary();
|
|
|
|
|
foreach (var o in supportedPaymentMethods)
|
2018-02-20 04:45:04 +01:00
|
|
|
|
{
|
2018-03-28 15:37:01 +02:00
|
|
|
|
try
|
2018-03-25 18:57:44 +02:00
|
|
|
|
{
|
2018-03-28 15:37:01 +02:00
|
|
|
|
var paymentMethod = await o.PaymentMethod;
|
|
|
|
|
if (paymentMethod == null)
|
|
|
|
|
throw new PaymentMethodUnavailableException("Payment method unavailable (The handler returned null)");
|
|
|
|
|
supported.Add(o.SupportedPaymentMethod);
|
|
|
|
|
paymentMethods.Add(paymentMethod);
|
|
|
|
|
}
|
|
|
|
|
catch (PaymentMethodUnavailableException ex)
|
|
|
|
|
{
|
|
|
|
|
paymentMethodErrors.Add($"{o.SupportedPaymentMethod.PaymentId.CryptoCode} ({o.SupportedPaymentMethod.PaymentId.PaymentType}): Payment method unavailable ({ex.Message})");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
paymentMethodErrors.Add($"{o.SupportedPaymentMethod.PaymentId.CryptoCode} ({o.SupportedPaymentMethod.PaymentId.PaymentType}): Unexpected exception ({ex.ToString()})");
|
2018-03-25 18:57:44 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 15:37:01 +02:00
|
|
|
|
if (supported.Count == 0)
|
2018-03-25 18:57:44 +02:00
|
|
|
|
{
|
2018-03-28 15:37:01 +02:00
|
|
|
|
StringBuilder errors = new StringBuilder();
|
|
|
|
|
errors.AppendLine("No payment method available for this store");
|
|
|
|
|
foreach(var error in paymentMethodErrors)
|
|
|
|
|
{
|
|
|
|
|
errors.AppendLine(error);
|
|
|
|
|
}
|
|
|
|
|
throw new BitpayHttpException(400, errors.ToString());
|
2017-12-21 07:52:04 +01:00
|
|
|
|
}
|
2018-03-25 18:57:44 +02:00
|
|
|
|
|
|
|
|
|
entity.SetSupportedPaymentMethods(supported);
|
|
|
|
|
entity.SetPaymentMethods(paymentMethods);
|
2018-02-20 04:45:04 +01:00
|
|
|
|
#pragma warning disable CS0618
|
|
|
|
|
// Legacy Bitpay clients expect information for BTC information, even if the store do not support it
|
|
|
|
|
var legacyBTCisSet = paymentMethods.Any(p => p.GetId().IsBTCOnChain);
|
2018-02-25 16:48:12 +01:00
|
|
|
|
if (!legacyBTCisSet && _NetworkProvider.BTC != null)
|
2018-01-12 08:30:34 +01:00
|
|
|
|
{
|
|
|
|
|
var btc = _NetworkProvider.BTC;
|
2018-02-20 04:45:04 +01:00
|
|
|
|
var feeProvider = ((IFeeProviderFactory)_ServiceProvider.GetService(typeof(IFeeProviderFactory))).CreateFeeProvider(btc);
|
2018-02-01 21:22:22 +01:00
|
|
|
|
var rateProvider = storeBlob.ApplyRateRules(btc, _RateProviders.GetRateProvider(btc, false));
|
2018-01-12 08:30:34 +01:00
|
|
|
|
if (feeProvider != null && rateProvider != null)
|
|
|
|
|
{
|
|
|
|
|
var gettingFee = feeProvider.GetFeeRateAsync();
|
2018-01-14 07:26:14 +01:00
|
|
|
|
var gettingRate = rateProvider.GetRateAsync(invoice.Currency);
|
2018-01-12 08:30:34 +01:00
|
|
|
|
entity.TxFee = GetTxFee(storeBlob, await gettingFee);
|
|
|
|
|
entity.Rate = await gettingRate;
|
|
|
|
|
}
|
|
|
|
|
#pragma warning restore CS0618
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
entity.PosData = invoice.PosData;
|
2018-03-28 15:37:01 +02:00
|
|
|
|
entity = await _InvoiceRepository.CreateInvoiceAsync(store.Id, entity, paymentMethodErrors, _NetworkProvider);
|
|
|
|
|
|
2018-01-18 12:56:55 +01:00
|
|
|
|
_EventAggregator.Publish(new Events.InvoiceEvent(entity, 1001, "invoice_created"));
|
2017-12-21 07:52:04 +01:00
|
|
|
|
var resp = entity.EntityToDTO(_NetworkProvider);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return new DataWrapper<InvoiceResponse>(resp) { Facade = "pos/invoice" };
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2018-03-28 15:37:01 +02:00
|
|
|
|
private async Task<PaymentMethod> CreatePaymentMethodAsync(IPaymentMethodHandler handler, ISupportedPaymentMethod supportedPaymentMethod, BTCPayNetwork network, InvoiceEntity entity, StoreBlob storeBlob)
|
|
|
|
|
{
|
|
|
|
|
var rate = await storeBlob.ApplyRateRules(network, _RateProviders.GetRateProvider(network, false)).GetRateAsync(entity.ProductInformation.Currency);
|
|
|
|
|
PaymentMethod paymentMethod = new PaymentMethod();
|
|
|
|
|
paymentMethod.ParentEntity = entity;
|
|
|
|
|
paymentMethod.Network = network;
|
|
|
|
|
paymentMethod.SetId(supportedPaymentMethod.PaymentId);
|
|
|
|
|
paymentMethod.Rate = rate;
|
|
|
|
|
var paymentDetails = await handler.CreatePaymentMethodDetails(supportedPaymentMethod, paymentMethod, network);
|
|
|
|
|
if (storeBlob.NetworkFeeDisabled)
|
|
|
|
|
paymentDetails.SetNoTxFee();
|
|
|
|
|
paymentMethod.SetPaymentMethodDetails(paymentDetails);
|
2018-03-28 16:15:10 +02:00
|
|
|
|
|
|
|
|
|
// Check if Lightning Max value is exceeded
|
|
|
|
|
if (supportedPaymentMethod.PaymentId.PaymentType == PaymentTypes.LightningLike &&
|
|
|
|
|
storeBlob.LightningMaxValue != null)
|
|
|
|
|
{
|
|
|
|
|
var lightningMaxValue = storeBlob.LightningMaxValue;
|
|
|
|
|
var lightningMaxValueRate = 0.0m;
|
|
|
|
|
if (lightningMaxValue.Currency == entity.ProductInformation.Currency)
|
|
|
|
|
lightningMaxValueRate = paymentMethod.Rate;
|
|
|
|
|
else
|
|
|
|
|
lightningMaxValueRate = await storeBlob.ApplyRateRules(network, _RateProviders.GetRateProvider(network, false)).GetRateAsync(lightningMaxValue.Currency);
|
|
|
|
|
|
|
|
|
|
var lightningMaxValueCrypto = Money.Coins(lightningMaxValue.Value / lightningMaxValueRate);
|
|
|
|
|
if (paymentMethod.Calculate().Due > lightningMaxValueCrypto)
|
|
|
|
|
{
|
|
|
|
|
throw new PaymentMethodUnavailableException("Lightning max value exceeded");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
///////////////
|
|
|
|
|
|
|
|
|
|
|
2018-03-28 15:37:01 +02:00
|
|
|
|
#pragma warning disable CS0618
|
|
|
|
|
if (paymentMethod.GetId().IsBTCOnChain)
|
|
|
|
|
{
|
|
|
|
|
entity.TxFee = paymentMethod.TxFee;
|
|
|
|
|
entity.Rate = paymentMethod.Rate;
|
|
|
|
|
entity.DepositAddress = paymentMethod.DepositAddress;
|
|
|
|
|
}
|
|
|
|
|
#pragma warning restore CS0618
|
|
|
|
|
return paymentMethod;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 04:45:04 +01:00
|
|
|
|
#pragma warning disable CS0618
|
2018-01-12 08:30:34 +01:00
|
|
|
|
private static Money GetTxFee(StoreBlob storeBlob, FeeRate feeRate)
|
|
|
|
|
{
|
|
|
|
|
return storeBlob.NetworkFeeDisabled ? Money.Zero : feeRate.GetFee(100);
|
|
|
|
|
}
|
2018-02-20 04:45:04 +01:00
|
|
|
|
#pragma warning restore CS0618
|
2018-01-12 08:30:34 +01:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
private SpeedPolicy ParseSpeedPolicy(string transactionSpeed, SpeedPolicy defaultPolicy)
|
|
|
|
|
{
|
|
|
|
|
if (transactionSpeed == null)
|
|
|
|
|
return defaultPolicy;
|
|
|
|
|
var mappings = new Dictionary<string, SpeedPolicy>();
|
|
|
|
|
mappings.Add("low", SpeedPolicy.LowSpeed);
|
|
|
|
|
mappings.Add("medium", SpeedPolicy.MediumSpeed);
|
|
|
|
|
mappings.Add("high", SpeedPolicy.HighSpeed);
|
|
|
|
|
if (!mappings.TryGetValue(transactionSpeed, out SpeedPolicy policy))
|
|
|
|
|
policy = defaultPolicy;
|
|
|
|
|
return policy;
|
|
|
|
|
}
|
2017-10-23 07:51:21 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
private void FillBuyerInfo(Buyer buyer, BuyerInformation buyerInformation)
|
|
|
|
|
{
|
|
|
|
|
if (buyer == null)
|
|
|
|
|
return;
|
|
|
|
|
buyerInformation.BuyerAddress1 = buyerInformation.BuyerAddress1 ?? buyer.Address1;
|
|
|
|
|
buyerInformation.BuyerAddress2 = buyerInformation.BuyerAddress2 ?? buyer.Address2;
|
|
|
|
|
buyerInformation.BuyerCity = buyerInformation.BuyerCity ?? buyer.City;
|
|
|
|
|
buyerInformation.BuyerCountry = buyerInformation.BuyerCountry ?? buyer.country;
|
|
|
|
|
buyerInformation.BuyerEmail = buyerInformation.BuyerEmail ?? buyer.email;
|
|
|
|
|
buyerInformation.BuyerName = buyerInformation.BuyerName ?? buyer.Name;
|
|
|
|
|
buyerInformation.BuyerPhone = buyerInformation.BuyerPhone ?? buyer.phone;
|
|
|
|
|
buyerInformation.BuyerState = buyerInformation.BuyerState ?? buyer.State;
|
|
|
|
|
buyerInformation.BuyerZip = buyerInformation.BuyerZip ?? buyer.zip;
|
|
|
|
|
}
|
2017-10-13 09:44:55 +02:00
|
|
|
|
|
2017-12-21 07:52:04 +01:00
|
|
|
|
private DerivationStrategyBase ParseDerivationStrategy(string derivationStrategy, BTCPayNetwork network)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2017-12-21 07:52:04 +01:00
|
|
|
|
return new DerivationStrategyFactory(network.NBitcoinNetwork).Parse(derivationStrategy);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-10-12 09:33:53 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
private TDest Map<TFrom, TDest>(TFrom data)
|
|
|
|
|
{
|
|
|
|
|
return JsonConvert.DeserializeObject<TDest>(JsonConvert.SerializeObject(data));
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|