2018-08-30 18:34:39 +02:00
|
|
|
|
using System;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
2018-11-30 09:04:26 +01:00
|
|
|
|
using System.Net.Mime;
|
2017-12-17 11:58:55 +01:00
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
|
using System.Threading;
|
2018-08-30 18:34:39 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BTCPayServer.Data;
|
2017-12-17 11:58:55 +01:00
|
|
|
|
using BTCPayServer.Events;
|
2018-08-30 18:34:39 +02:00
|
|
|
|
using BTCPayServer.Filters;
|
2018-12-10 07:34:48 +01:00
|
|
|
|
using BTCPayServer.Models;
|
2018-08-30 18:34:39 +02:00
|
|
|
|
using BTCPayServer.Models.InvoicingModels;
|
2018-02-18 18:38:03 +01:00
|
|
|
|
using BTCPayServer.Payments;
|
2018-10-24 07:52:19 +02:00
|
|
|
|
using BTCPayServer.Payments.Changelly;
|
2018-12-11 12:47:38 +01:00
|
|
|
|
using BTCPayServer.Payments.CoinSwitch;
|
2018-03-30 07:34:14 +02:00
|
|
|
|
using BTCPayServer.Payments.Lightning;
|
2018-04-29 19:33:42 +02:00
|
|
|
|
using BTCPayServer.Security;
|
2018-08-30 18:34:39 +02:00
|
|
|
|
using BTCPayServer.Services.Invoices;
|
2018-11-30 09:04:26 +01:00
|
|
|
|
using BTCPayServer.Services.Invoices.Export;
|
2018-08-30 18:34:39 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
|
|
|
using NBitcoin;
|
|
|
|
|
using NBitpayClient;
|
|
|
|
|
using NBXplorer;
|
2018-11-27 07:13:09 +01:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public partial class InvoiceController
|
|
|
|
|
{
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("invoices/{invoiceId}")]
|
2018-12-06 08:58:04 +01:00
|
|
|
|
[Authorize(AuthenticationSchemes = Policies.CookieAuthentication)]
|
2018-01-08 12:06:16 +01:00
|
|
|
|
public async Task<IActionResult> Invoice(string invoiceId)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
|
|
|
|
|
{
|
2018-01-08 12:06:16 +01:00
|
|
|
|
InvoiceId = invoiceId,
|
2018-12-06 08:58:04 +01:00
|
|
|
|
UserId = GetUserId(),
|
2018-01-14 13:48:23 +01:00
|
|
|
|
IncludeAddresses = true,
|
|
|
|
|
IncludeEvents = true
|
2017-10-27 10:53:04 +02:00
|
|
|
|
})).FirstOrDefault();
|
|
|
|
|
if (invoice == null)
|
|
|
|
|
return NotFound();
|
2017-10-12 17:25:45 +02:00
|
|
|
|
|
2019-04-30 05:45:33 +02:00
|
|
|
|
var prodInfo = invoice.ProductInformation;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var store = await _StoreRepository.FindStore(invoice.StoreId);
|
2019-04-30 05:45:33 +02:00
|
|
|
|
var model = new InvoiceDetailsModel()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
StoreName = store.StoreName,
|
|
|
|
|
StoreLink = Url.Action(nameof(StoresController.UpdateStore), "Stores", new { storeId = store.Id }),
|
|
|
|
|
Id = invoice.Id,
|
2018-12-10 13:48:28 +01:00
|
|
|
|
State = invoice.GetInvoiceState().ToString(),
|
2018-05-13 08:09:17 +02:00
|
|
|
|
TransactionSpeed = invoice.SpeedPolicy == SpeedPolicy.HighSpeed ? "high" :
|
2018-05-11 15:12:45 +02:00
|
|
|
|
invoice.SpeedPolicy == SpeedPolicy.MediumSpeed ? "medium" :
|
|
|
|
|
invoice.SpeedPolicy == SpeedPolicy.LowMediumSpeed ? "low-medium" :
|
|
|
|
|
"low",
|
2017-10-27 10:53:04 +02:00
|
|
|
|
RefundEmail = invoice.RefundMail,
|
|
|
|
|
CreatedDate = invoice.InvoiceTime,
|
|
|
|
|
ExpirationDate = invoice.ExpirationTime,
|
2018-01-08 12:06:16 +01:00
|
|
|
|
MonitoringDate = invoice.MonitoringExpiration,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
OrderId = invoice.OrderId,
|
|
|
|
|
BuyerInformation = invoice.BuyerInformation,
|
2019-04-30 05:45:33 +02:00
|
|
|
|
Fiat = _CurrencyNameTable.DisplayFormatCurrency(prodInfo.Price, prodInfo.Currency),
|
|
|
|
|
TaxIncluded = _CurrencyNameTable.DisplayFormatCurrency(prodInfo.TaxIncluded, prodInfo.Currency),
|
2018-10-12 03:09:13 +02:00
|
|
|
|
NotificationEmail = invoice.NotificationEmail,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
NotificationUrl = invoice.NotificationURL,
|
2018-01-14 07:01:09 +01:00
|
|
|
|
RedirectUrl = invoice.RedirectURL,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
ProductInformation = invoice.ProductInformation,
|
2018-01-14 13:48:23 +01:00
|
|
|
|
StatusException = invoice.ExceptionStatus,
|
2018-11-27 07:13:09 +01:00
|
|
|
|
Events = invoice.Events,
|
2019-04-30 05:45:33 +02:00
|
|
|
|
PosData = PosDataParser.ParsePosData(invoice.PosData),
|
2019-08-24 16:10:13 +02:00
|
|
|
|
StatusMessage = StatusMessage,
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
};
|
2017-10-12 17:25:45 +02:00
|
|
|
|
|
2019-05-29 16:33:31 +02:00
|
|
|
|
model.Addresses = invoice.HistoricalAddresses.Select(h =>
|
|
|
|
|
new InvoiceDetailsModel.AddressModel
|
|
|
|
|
{
|
|
|
|
|
Destination = h.GetAddress(),
|
2019-06-03 18:06:03 +02:00
|
|
|
|
PaymentMethod = h.GetPaymentMethodId().ToPrettyString(),
|
2019-05-29 16:33:31 +02:00
|
|
|
|
Current = !h.UnAssigned.HasValue
|
|
|
|
|
}).ToArray();
|
2019-04-30 05:45:33 +02:00
|
|
|
|
|
2019-05-07 16:32:47 +02:00
|
|
|
|
var details = InvoicePopulatePayments(invoice);
|
2019-04-30 05:45:33 +02:00
|
|
|
|
model.CryptoPayments = details.CryptoPayments;
|
2019-08-24 16:10:13 +02:00
|
|
|
|
model.Payments = details.Payments;
|
2019-04-30 05:45:33 +02:00
|
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
2019-05-07 16:32:47 +02:00
|
|
|
|
private InvoiceDetailsModel InvoicePopulatePayments(InvoiceEntity invoice)
|
2019-04-30 05:45:33 +02:00
|
|
|
|
{
|
|
|
|
|
var model = new InvoiceDetailsModel();
|
2019-08-24 16:10:13 +02:00
|
|
|
|
model.Payments = invoice.GetPayments();
|
2019-05-24 15:22:38 +02:00
|
|
|
|
foreach (var data in invoice.GetPaymentMethods())
|
2018-01-08 12:06:16 +01:00
|
|
|
|
{
|
2018-02-18 18:38:03 +01:00
|
|
|
|
var accounting = data.Calculate();
|
2018-03-06 22:37:25 +01:00
|
|
|
|
var paymentMethodId = data.GetId();
|
2018-01-08 12:06:16 +01:00
|
|
|
|
var cryptoPayment = new InvoiceDetailsModel.CryptoPayment();
|
2019-08-24 16:10:13 +02:00
|
|
|
|
|
|
|
|
|
cryptoPayment.PaymentMethodId = paymentMethodId;
|
2019-06-03 18:06:03 +02:00
|
|
|
|
cryptoPayment.PaymentMethod = paymentMethodId.ToPrettyString();
|
2019-01-30 11:18:44 +01:00
|
|
|
|
cryptoPayment.Due = _CurrencyNameTable.DisplayFormatCurrency(accounting.Due.ToDecimal(MoneyUnit.BTC), paymentMethodId.CryptoCode);
|
|
|
|
|
cryptoPayment.Paid = _CurrencyNameTable.DisplayFormatCurrency(accounting.CryptoPaid.ToDecimal(MoneyUnit.BTC), paymentMethodId.CryptoCode);
|
|
|
|
|
cryptoPayment.Overpaid = _CurrencyNameTable.DisplayFormatCurrency(accounting.OverpaidHelper.ToDecimal(MoneyUnit.BTC), paymentMethodId.CryptoCode);
|
2019-05-30 09:02:52 +02:00
|
|
|
|
var paymentMethodDetails = data.GetPaymentMethodDetails();
|
|
|
|
|
cryptoPayment.Address = paymentMethodDetails.GetPaymentDestination();
|
2018-05-16 12:46:11 +02:00
|
|
|
|
cryptoPayment.Rate = ExchangeRate(data);
|
2018-01-08 12:06:16 +01:00
|
|
|
|
model.CryptoPayments.Add(cryptoPayment);
|
|
|
|
|
}
|
2019-04-30 05:45:33 +02:00
|
|
|
|
return model;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("i/{invoiceId}")]
|
2018-02-19 07:09:05 +01:00
|
|
|
|
[Route("i/{invoiceId}/{paymentMethodId}")]
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[Route("invoice")]
|
|
|
|
|
[AcceptMediaTypeConstraint("application/bitcoin-paymentrequest", false)]
|
|
|
|
|
[XFrameOptionsAttribute(null)]
|
2018-07-11 19:38:08 +02:00
|
|
|
|
[ReferrerPolicyAttribute("origin")]
|
2018-11-09 08:09:09 +01:00
|
|
|
|
public async Task<IActionResult> Checkout(string invoiceId, string id = null, string paymentMethodId = null,
|
|
|
|
|
[FromQuery]string view = null)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
//Keep compatibility with Bitpay
|
|
|
|
|
invoiceId = invoiceId ?? id;
|
|
|
|
|
id = invoiceId;
|
2019-03-31 20:46:38 +02:00
|
|
|
|
//
|
2017-10-13 09:07:57 +02:00
|
|
|
|
|
2019-01-31 11:07:38 +01:00
|
|
|
|
var model = await GetInvoiceModel(invoiceId, paymentMethodId == null ? null : PaymentMethodId.Parse(paymentMethodId));
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (model == null)
|
|
|
|
|
return NotFound();
|
2017-10-21 05:24:28 +02:00
|
|
|
|
|
2018-11-09 08:09:09 +01:00
|
|
|
|
if (view == "modal")
|
|
|
|
|
model.IsModal = true;
|
2018-07-12 10:38:21 +02:00
|
|
|
|
|
|
|
|
|
_CSP.Add(new ConsentSecurityPolicy("script-src", "'unsafe-eval'")); // Needed by Vue
|
2018-07-14 05:35:34 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(model.CustomCSSLink) &&
|
2018-07-12 10:38:21 +02:00
|
|
|
|
Uri.TryCreate(model.CustomCSSLink, UriKind.Absolute, out var uri))
|
|
|
|
|
{
|
|
|
|
|
_CSP.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.CustomLogoLink) &&
|
|
|
|
|
Uri.TryCreate(model.CustomLogoLink, UriKind.Absolute, out uri))
|
|
|
|
|
{
|
|
|
|
|
_CSP.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return View(nameof(Checkout), model);
|
|
|
|
|
}
|
2017-10-21 05:24:28 +02:00
|
|
|
|
|
2019-03-31 20:46:38 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("invoice-noscript")]
|
|
|
|
|
public async Task<IActionResult> CheckoutNoScript(string invoiceId, string id = null, string paymentMethodId = null)
|
|
|
|
|
{
|
|
|
|
|
//Keep compatibility with Bitpay
|
|
|
|
|
invoiceId = invoiceId ?? id;
|
|
|
|
|
id = invoiceId;
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
var model = await GetInvoiceModel(invoiceId, paymentMethodId == null ? null : PaymentMethodId.Parse(paymentMethodId));
|
|
|
|
|
if (model == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
2019-04-05 04:48:52 +02:00
|
|
|
|
return View(model);
|
2019-03-31 20:46:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 11:07:38 +01:00
|
|
|
|
private async Task<PaymentModel> GetInvoiceModel(string invoiceId, PaymentMethodId paymentMethodId)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-12-06 09:05:27 +01:00
|
|
|
|
var invoice = await _InvoiceRepository.GetInvoice(invoiceId);
|
2018-01-10 10:33:05 +01:00
|
|
|
|
if (invoice == null)
|
|
|
|
|
return null;
|
2018-01-09 03:41:07 +01:00
|
|
|
|
var store = await _StoreRepository.FindStore(invoice.StoreId);
|
2019-01-31 11:07:38 +01:00
|
|
|
|
bool isDefaultPaymentId = false;
|
|
|
|
|
if (paymentMethodId == null)
|
2018-02-18 18:38:03 +01:00
|
|
|
|
{
|
2019-01-31 11:07:38 +01:00
|
|
|
|
paymentMethodId = store.GetDefaultPaymentId(_NetworkProvider);
|
|
|
|
|
isDefaultPaymentId = true;
|
2018-01-12 08:30:34 +01:00
|
|
|
|
}
|
2019-05-29 11:43:50 +02:00
|
|
|
|
BTCPayNetworkBase network = _NetworkProvider.GetNetwork<BTCPayNetwork>(paymentMethodId.CryptoCode);
|
2019-01-31 11:07:38 +01:00
|
|
|
|
if (network == null && isDefaultPaymentId)
|
2018-05-07 05:25:50 +02:00
|
|
|
|
{
|
2019-05-29 16:33:31 +02:00
|
|
|
|
//TODO: need to look into a better way for this as it does not scale
|
2019-05-29 11:43:50 +02:00
|
|
|
|
network = _NetworkProvider.GetAll().OfType<BTCPayNetwork>().FirstOrDefault();
|
2018-05-07 05:25:50 +02:00
|
|
|
|
paymentMethodId = new PaymentMethodId(network.CryptoCode, PaymentTypes.BTCLike);
|
|
|
|
|
}
|
2018-01-12 08:30:34 +01:00
|
|
|
|
if (invoice == null || network == null)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return null;
|
2018-02-19 07:09:05 +01:00
|
|
|
|
if (!invoice.Support(paymentMethodId))
|
2018-01-12 08:30:34 +01:00
|
|
|
|
{
|
2019-01-31 11:07:38 +01:00
|
|
|
|
if (!isDefaultPaymentId)
|
2018-01-12 08:30:34 +01:00
|
|
|
|
return null;
|
2019-05-24 15:22:38 +02:00
|
|
|
|
var paymentMethodTemp = invoice.GetPaymentMethods()
|
2018-10-12 03:09:13 +02:00
|
|
|
|
.Where(c => paymentMethodId.CryptoCode == c.GetId().CryptoCode)
|
2018-08-08 07:45:46 +02:00
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
if (paymentMethodTemp == null)
|
2019-05-24 15:22:38 +02:00
|
|
|
|
paymentMethodTemp = invoice.GetPaymentMethods().First();
|
2018-02-19 07:09:05 +01:00
|
|
|
|
network = paymentMethodTemp.Network;
|
|
|
|
|
paymentMethodId = paymentMethodTemp.GetId();
|
2018-01-12 08:30:34 +01:00
|
|
|
|
}
|
2018-01-09 03:41:07 +01:00
|
|
|
|
|
2019-06-07 06:24:36 +02:00
|
|
|
|
var paymentMethod = invoice.GetPaymentMethod(paymentMethodId);
|
2018-02-19 07:09:05 +01:00
|
|
|
|
var paymentMethodDetails = paymentMethod.GetPaymentMethodDetails();
|
2019-05-24 15:22:38 +02:00
|
|
|
|
var dto = invoice.EntityToDTO();
|
2018-02-19 07:09:05 +01:00
|
|
|
|
var cryptoInfo = dto.CryptoInfo.First(o => o.GetpaymentMethodId() == paymentMethodId);
|
2018-03-23 09:27:48 +01:00
|
|
|
|
var storeBlob = store.GetStoreBlob();
|
2017-12-03 14:14:08 +01:00
|
|
|
|
var currency = invoice.ProductInformation.Currency;
|
2018-02-19 07:09:05 +01:00
|
|
|
|
var accounting = paymentMethod.Calculate();
|
2018-10-24 07:52:19 +02:00
|
|
|
|
|
|
|
|
|
ChangellySettings changelly = (storeBlob.ChangellySettings != null && storeBlob.ChangellySettings.Enabled &&
|
|
|
|
|
storeBlob.ChangellySettings.IsConfigured())
|
|
|
|
|
? storeBlob.ChangellySettings
|
|
|
|
|
: null;
|
2019-04-26 01:13:17 +02:00
|
|
|
|
|
2018-12-11 12:47:38 +01:00
|
|
|
|
CoinSwitchSettings coinswitch = (storeBlob.CoinSwitchSettings != null && storeBlob.CoinSwitchSettings.Enabled &&
|
|
|
|
|
storeBlob.CoinSwitchSettings.IsConfigured())
|
|
|
|
|
? storeBlob.CoinSwitchSettings
|
|
|
|
|
: null;
|
2018-10-24 07:52:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var changellyAmountDue = changelly != null
|
|
|
|
|
? (accounting.Due.ToDecimal(MoneyUnit.BTC) *
|
|
|
|
|
(1m + (changelly.AmountMarkupPercentage / 100m)))
|
|
|
|
|
: (decimal?)null;
|
|
|
|
|
|
2019-06-04 03:11:52 +02:00
|
|
|
|
|
|
|
|
|
var paymentMethodHandler = _paymentMethodHandlerDictionary[paymentMethodId];
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var model = new PaymentModel()
|
|
|
|
|
{
|
2018-01-09 03:41:07 +01:00
|
|
|
|
CryptoCode = network.CryptoCode,
|
2019-03-08 16:48:33 +01:00
|
|
|
|
RootPath = this.Request.PathBase.Value.WithTrailingSlash(),
|
2017-10-27 10:53:04 +02:00
|
|
|
|
OrderId = invoice.OrderId,
|
|
|
|
|
InvoiceId = invoice.Id,
|
2018-11-09 08:48:38 +01:00
|
|
|
|
DefaultLang = storeBlob.DefaultLang ?? "en",
|
2018-05-03 23:51:04 +02:00
|
|
|
|
HtmlTitle = storeBlob.HtmlTitle ?? "BTCPay Invoice",
|
2018-03-27 08:14:50 +02:00
|
|
|
|
CustomCSSLink = storeBlob.CustomCSS?.AbsoluteUri,
|
|
|
|
|
CustomLogoLink = storeBlob.CustomLogo?.AbsoluteUri,
|
2019-05-31 08:00:32 +02:00
|
|
|
|
CryptoImage = Request.GetRelativePathOrAbsolute(paymentMethodHandler.GetCryptoImage(paymentMethodId)),
|
2019-03-16 04:43:57 +01:00
|
|
|
|
LightningAmountInSatoshi = storeBlob.LightningAmountInSatoshi,
|
2018-02-19 07:09:05 +01:00
|
|
|
|
BtcAddress = paymentMethodDetails.GetPaymentDestination(),
|
2017-12-21 10:01:26 +01:00
|
|
|
|
BtcDue = accounting.Due.ToString(),
|
2018-05-11 19:15:26 +02:00
|
|
|
|
OrderAmount = (accounting.TotalDue - accounting.NetworkFee).ToString(),
|
2018-05-16 12:46:11 +02:00
|
|
|
|
OrderAmountFiat = OrderAmountFromInvoice(network.CryptoCode, invoice.ProductInformation),
|
2017-10-27 10:53:04 +02:00
|
|
|
|
CustomerEmail = invoice.RefundMail,
|
2018-04-26 04:13:44 +02:00
|
|
|
|
RequiresRefundEmail = storeBlob.RequiresRefundEmail,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
ExpirationSeconds = Math.Max(0, (int)(invoice.ExpirationTime - DateTimeOffset.UtcNow).TotalSeconds),
|
|
|
|
|
MaxTimeSeconds = (int)(invoice.ExpirationTime - invoice.InvoiceTime).TotalSeconds,
|
2018-01-19 16:33:37 +01:00
|
|
|
|
MaxTimeMinutes = (int)(invoice.ExpirationTime - invoice.InvoiceTime).TotalMinutes,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
ItemDesc = invoice.ProductInformation.ItemDesc,
|
2018-05-16 12:46:11 +02:00
|
|
|
|
Rate = ExchangeRate(paymentMethod),
|
2017-10-27 10:53:04 +02:00
|
|
|
|
MerchantRefLink = invoice.RedirectURL ?? "/",
|
2019-04-11 11:08:42 +02:00
|
|
|
|
RedirectAutomatically = invoice.RedirectAutomatically,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
StoreName = store.StoreName,
|
2018-03-30 07:34:14 +02:00
|
|
|
|
PeerInfo = (paymentMethodDetails as LightningLikePaymentMethodDetails)?.NodeInfo,
|
2018-02-25 16:48:12 +01:00
|
|
|
|
TxCount = accounting.TxRequired,
|
2017-12-21 10:01:26 +01:00
|
|
|
|
BtcPaid = accounting.Paid.ToString(),
|
2018-12-10 13:48:28 +01:00
|
|
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
|
|
|
|
Status = invoice.StatusString,
|
|
|
|
|
#pragma warning restore CS0618 // Type or member is obsolete
|
2019-01-07 07:35:18 +01:00
|
|
|
|
NetworkFee = paymentMethodDetails.GetNextNetworkFee(),
|
2018-04-13 21:10:06 +02:00
|
|
|
|
IsMultiCurrency = invoice.GetPayments().Select(p => p.GetPaymentMethodId()).Concat(new[] { paymentMethod.GetId() }).Distinct().Count() > 1,
|
2018-10-24 07:52:19 +02:00
|
|
|
|
ChangellyEnabled = changelly != null,
|
|
|
|
|
ChangellyMerchantId = changelly?.ChangellyMerchantId,
|
|
|
|
|
ChangellyAmountDue = changellyAmountDue,
|
2018-12-11 12:47:38 +01:00
|
|
|
|
CoinSwitchEnabled = coinswitch != null,
|
2019-04-26 01:13:17 +02:00
|
|
|
|
CoinSwitchAmountMarkupPercentage = coinswitch?.AmountMarkupPercentage ?? 0,
|
2018-12-11 12:47:38 +01:00
|
|
|
|
CoinSwitchMerchantId = coinswitch?.MerchantId,
|
2018-12-18 19:01:58 +01:00
|
|
|
|
CoinSwitchMode = coinswitch?.Mode,
|
2018-10-24 07:52:19 +02:00
|
|
|
|
StoreId = store.Id,
|
2019-05-24 15:22:38 +02:00
|
|
|
|
AvailableCryptos = invoice.GetPaymentMethods()
|
2018-02-18 18:38:03 +01:00
|
|
|
|
.Where(i => i.Network != null)
|
2019-05-29 16:33:31 +02:00
|
|
|
|
.Select(kv =>
|
2018-03-19 01:45:54 +01:00
|
|
|
|
{
|
2019-05-29 16:33:31 +02:00
|
|
|
|
var availableCryptoPaymentMethodId = kv.GetId();
|
2019-06-04 03:11:52 +02:00
|
|
|
|
var availableCryptoHandler = _paymentMethodHandlerDictionary[availableCryptoPaymentMethodId];
|
2019-05-29 16:33:31 +02:00
|
|
|
|
return new PaymentModel.AvailableCrypto()
|
|
|
|
|
{
|
|
|
|
|
PaymentMethodId = kv.GetId().ToString(),
|
|
|
|
|
CryptoCode = kv.GetId().CryptoCode,
|
|
|
|
|
PaymentMethodName = availableCryptoHandler.GetPaymentMethodName(availableCryptoPaymentMethodId),
|
|
|
|
|
IsLightning =
|
|
|
|
|
kv.GetId().PaymentType == PaymentTypes.LightningLike,
|
2019-05-31 08:00:32 +02:00
|
|
|
|
CryptoImage = Request.GetRelativePathOrAbsolute(availableCryptoHandler.GetCryptoImage(availableCryptoPaymentMethodId)),
|
2019-05-29 16:33:31 +02:00
|
|
|
|
Link = Url.Action(nameof(Checkout),
|
|
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
invoiceId = invoiceId,
|
|
|
|
|
paymentMethodId = kv.GetId().ToString()
|
|
|
|
|
})
|
|
|
|
|
};
|
2018-03-19 01:45:54 +01:00
|
|
|
|
}).Where(c => c.CryptoImage != "/")
|
2018-07-19 06:53:00 +02:00
|
|
|
|
.OrderByDescending(a => a.CryptoCode == "BTC").ThenBy(a => a.PaymentMethodName).ThenBy(a => a.IsLightning ? 1 : 0)
|
2018-07-14 05:35:34 +02:00
|
|
|
|
.ToList()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
};
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2019-05-31 08:00:32 +02:00
|
|
|
|
paymentMethodHandler.PreparePaymentModel(model, dto);
|
2019-08-25 15:50:11 +02:00
|
|
|
|
model.UISettings = paymentMethodHandler.GetCheckoutUISettings();
|
2019-05-29 16:33:31 +02:00
|
|
|
|
model.PaymentMethodId = paymentMethodId.ToString();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var expiration = TimeSpan.FromSeconds(model.ExpirationSeconds);
|
2018-04-18 11:23:39 +02:00
|
|
|
|
model.TimeLeft = expiration.PrettyPrint();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return model;
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2018-05-16 12:46:11 +02:00
|
|
|
|
private string OrderAmountFromInvoice(string cryptoCode, ProductInformation productInformation)
|
|
|
|
|
{
|
|
|
|
|
// if invoice source currency is the same as currently display currency, no need for "order amount from invoice"
|
|
|
|
|
if (cryptoCode == productInformation.Currency)
|
|
|
|
|
return null;
|
|
|
|
|
|
2018-10-09 16:30:06 +02:00
|
|
|
|
return _CurrencyNameTable.DisplayFormatCurrency(productInformation.Price, productInformation.Currency);
|
2018-05-16 12:46:11 +02:00
|
|
|
|
}
|
|
|
|
|
private string ExchangeRate(PaymentMethod paymentMethod)
|
2018-01-08 12:06:16 +01:00
|
|
|
|
{
|
2018-02-19 07:09:05 +01:00
|
|
|
|
string currency = paymentMethod.ParentEntity.ProductInformation.Currency;
|
2018-10-09 16:30:06 +02:00
|
|
|
|
return _CurrencyNameTable.DisplayFormatCurrency(paymentMethod.Rate, currency);
|
2018-05-10 05:39:13 +02:00
|
|
|
|
}
|
2018-01-08 12:06:16 +01:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("i/{invoiceId}/status")]
|
2018-02-19 07:09:05 +01:00
|
|
|
|
[Route("i/{invoiceId}/{paymentMethodId}/status")]
|
2019-01-28 08:24:11 +01:00
|
|
|
|
[Route("invoice/{invoiceId}/status")]
|
|
|
|
|
[Route("invoice/{invoiceId}/{paymentMethodId}/status")]
|
|
|
|
|
[Route("invoice/status")]
|
2018-02-19 07:09:05 +01:00
|
|
|
|
public async Task<IActionResult> GetStatus(string invoiceId, string paymentMethodId = null)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2019-01-31 11:07:38 +01:00
|
|
|
|
var model = await GetInvoiceModel(invoiceId, paymentMethodId == null ? null : PaymentMethodId.Parse(paymentMethodId));
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (model == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
return Json(model);
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-12-17 11:58:55 +01:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("i/{invoiceId}/status/ws")]
|
2019-01-28 08:24:11 +01:00
|
|
|
|
[Route("i/{invoiceId}/{paymentMethodId}/status/ws")]
|
|
|
|
|
[Route("invoice/{invoiceId}/status/ws")]
|
|
|
|
|
[Route("invoice/{invoiceId}/{paymentMethodId}/status")]
|
|
|
|
|
[Route("invoice/status/ws")]
|
2017-12-17 11:58:55 +01:00
|
|
|
|
public async Task<IActionResult> GetStatusWebSocket(string invoiceId)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.WebSockets.IsWebSocketRequest)
|
|
|
|
|
return NotFound();
|
2018-12-06 09:05:27 +01:00
|
|
|
|
var invoice = await _InvoiceRepository.GetInvoice(invoiceId);
|
2019-04-26 01:13:17 +02:00
|
|
|
|
if (invoice == null || invoice.Status == InvoiceStatus.Complete || invoice.Status == InvoiceStatus.Invalid || invoice.Status == InvoiceStatus.Expired)
|
2017-12-17 11:58:55 +01:00
|
|
|
|
return NotFound();
|
|
|
|
|
var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
|
|
|
|
|
CompositeDisposable leases = new CompositeDisposable();
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-01-07 13:07:06 +01:00
|
|
|
|
leases.Add(_EventAggregator.Subscribe<Events.InvoiceDataChangedEvent>(async o => await NotifySocket(webSocket, o.InvoiceId, invoiceId)));
|
2018-02-16 17:34:40 +01:00
|
|
|
|
leases.Add(_EventAggregator.Subscribe<Events.InvoiceNewAddressEvent>(async o => await NotifySocket(webSocket, o.InvoiceId, invoiceId)));
|
2018-06-21 07:15:36 +02:00
|
|
|
|
leases.Add(_EventAggregator.Subscribe<Events.InvoiceEvent>(async o => await NotifySocket(webSocket, o.Invoice.Id, invoiceId)));
|
2017-12-17 11:58:55 +01:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
var message = await webSocket.ReceiveAsync(DummyBuffer, default(CancellationToken));
|
|
|
|
|
if (message.MessageType == WebSocketMessageType.Close)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
leases.Dispose();
|
2018-02-12 19:27:36 +01:00
|
|
|
|
await webSocket.CloseSocket();
|
2017-12-17 11:58:55 +01:00
|
|
|
|
}
|
2017-12-25 13:52:27 +01:00
|
|
|
|
return new EmptyResult();
|
2017-12-17 11:58:55 +01:00
|
|
|
|
}
|
2018-01-08 12:06:16 +01:00
|
|
|
|
|
2017-12-17 11:58:55 +01:00
|
|
|
|
ArraySegment<Byte> DummyBuffer = new ArraySegment<Byte>(new Byte[1]);
|
|
|
|
|
private async Task NotifySocket(WebSocket webSocket, string invoiceId, string expectedId)
|
|
|
|
|
{
|
|
|
|
|
if (invoiceId != expectedId || webSocket.State != WebSocketState.Open)
|
|
|
|
|
return;
|
|
|
|
|
CancellationTokenSource cts = new CancellationTokenSource();
|
|
|
|
|
cts.CancelAfter(5000);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await webSocket.SendAsync(DummyBuffer, WebSocketMessageType.Binary, true, cts.Token);
|
|
|
|
|
}
|
2018-01-12 10:32:46 +01:00
|
|
|
|
catch { try { webSocket.Dispose(); } catch { } }
|
2017-12-17 11:58:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("i/{invoiceId}/UpdateCustomer")]
|
2019-01-28 08:24:11 +01:00
|
|
|
|
[Route("invoice/UpdateCustomer")]
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public async Task<IActionResult> UpdateCustomer(string invoiceId, [FromBody]UpdateCustomerModel data)
|
|
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ModelState);
|
|
|
|
|
}
|
|
|
|
|
await _InvoiceRepository.UpdateInvoice(invoiceId, data).ConfigureAwait(false);
|
2019-03-31 18:48:53 +02:00
|
|
|
|
return Ok("{}");
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("invoices")]
|
2018-04-30 15:00:43 +02:00
|
|
|
|
[Authorize(AuthenticationSchemes = Policies.CookieAuthentication)]
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[BitpayAPIConstraint(false)]
|
2019-04-26 01:13:17 +02:00
|
|
|
|
public async Task<IActionResult> ListInvoices(string searchTerm = null, int skip = 0, int count = 50, int timezoneOffset = 0)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-10-12 03:09:13 +02:00
|
|
|
|
var model = new InvoicesModel
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-10-12 03:09:13 +02:00
|
|
|
|
SearchTerm = searchTerm,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
Skip = skip,
|
2018-10-12 03:09:13 +02:00
|
|
|
|
Count = count,
|
2019-04-26 01:13:17 +02:00
|
|
|
|
StatusMessage = StatusMessage,
|
|
|
|
|
TimezoneOffset = timezoneOffset
|
2018-10-12 03:09:13 +02:00
|
|
|
|
};
|
2019-04-26 01:13:17 +02:00
|
|
|
|
InvoiceQuery invoiceQuery = GetInvoiceQuery(searchTerm, timezoneOffset);
|
2019-01-17 15:40:47 +01:00
|
|
|
|
var counting = _InvoiceRepository.GetInvoicesTotal(invoiceQuery);
|
2019-01-16 21:33:04 +01:00
|
|
|
|
invoiceQuery.Count = count;
|
|
|
|
|
invoiceQuery.Skip = skip;
|
|
|
|
|
var list = await _InvoiceRepository.GetInvoices(invoiceQuery);
|
2019-04-26 01:13:17 +02:00
|
|
|
|
|
2018-10-12 03:09:13 +02:00
|
|
|
|
foreach (var invoice in list)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-12-10 07:34:48 +01:00
|
|
|
|
var state = invoice.GetInvoiceState();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
model.Invoices.Add(new InvoiceModel()
|
|
|
|
|
{
|
2019-05-01 22:33:46 +02:00
|
|
|
|
Status = invoice.Status,
|
|
|
|
|
StatusString = state.ToString(),
|
2018-12-10 13:48:28 +01:00
|
|
|
|
ShowCheckout = invoice.Status == InvoiceStatus.New,
|
2018-05-26 16:32:20 +02:00
|
|
|
|
Date = invoice.InvoiceTime,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
InvoiceId = invoice.Id,
|
2018-02-28 11:03:23 +01:00
|
|
|
|
OrderId = invoice.OrderId ?? string.Empty,
|
2018-03-13 01:13:16 +01:00
|
|
|
|
RedirectUrl = invoice.RedirectURL ?? string.Empty,
|
2019-01-30 11:01:18 +01:00
|
|
|
|
AmountCurrency = _CurrencyNameTable.DisplayFormatCurrency(invoice.ProductInformation.Price, invoice.ProductInformation.Currency),
|
2018-12-10 07:34:48 +01:00
|
|
|
|
CanMarkInvalid = state.CanMarkInvalid(),
|
2019-04-30 05:45:33 +02:00
|
|
|
|
CanMarkComplete = state.CanMarkComplete(),
|
2019-05-07 16:32:47 +02:00
|
|
|
|
Details = InvoicePopulatePayments(invoice)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
2019-01-17 15:40:47 +01:00
|
|
|
|
model.Total = await counting;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return View(model);
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2019-04-26 01:13:17 +02:00
|
|
|
|
private InvoiceQuery GetInvoiceQuery(string searchTerm = null, int timezoneOffset = 0)
|
2018-10-12 03:09:13 +02:00
|
|
|
|
{
|
2019-04-26 01:13:17 +02:00
|
|
|
|
var fs = new SearchString(searchTerm);
|
2019-01-16 21:33:04 +01:00
|
|
|
|
var invoiceQuery = new InvoiceQuery()
|
2018-10-12 03:09:13 +02:00
|
|
|
|
{
|
2019-04-26 01:13:17 +02:00
|
|
|
|
TextSearch = fs.TextSearch,
|
2018-10-12 03:09:13 +02:00
|
|
|
|
UserId = GetUserId(),
|
2019-04-26 01:13:17 +02:00
|
|
|
|
Unusual = fs.GetFilterBool("unusual"),
|
|
|
|
|
Status = fs.GetFilterArray("status"),
|
|
|
|
|
ExceptionStatus = fs.GetFilterArray("exceptionstatus"),
|
|
|
|
|
StoreId = fs.GetFilterArray("storeid"),
|
|
|
|
|
ItemCode = fs.GetFilterArray("itemcode"),
|
|
|
|
|
OrderId = fs.GetFilterArray("orderid"),
|
|
|
|
|
StartDate = fs.GetFilterDate("startdate", timezoneOffset),
|
|
|
|
|
EndDate = fs.GetFilterDate("enddate", timezoneOffset)
|
2019-01-16 21:33:04 +01:00
|
|
|
|
};
|
|
|
|
|
return invoiceQuery;
|
2018-10-12 03:09:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 08:22:39 +01:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Authorize(AuthenticationSchemes = Policies.CookieAuthentication)]
|
|
|
|
|
[BitpayAPIConstraint(false)]
|
2019-04-26 01:13:17 +02:00
|
|
|
|
public async Task<IActionResult> Export(string format, string searchTerm = null, int timezoneOffset = 0)
|
2018-11-30 08:22:39 +01:00
|
|
|
|
{
|
2019-06-07 06:24:36 +02:00
|
|
|
|
var model = new InvoiceExport(_CurrencyNameTable);
|
2018-11-30 08:22:39 +01:00
|
|
|
|
|
2019-04-26 01:13:17 +02:00
|
|
|
|
InvoiceQuery invoiceQuery = GetInvoiceQuery(searchTerm, timezoneOffset);
|
2019-01-16 21:33:04 +01:00
|
|
|
|
invoiceQuery.Skip = 0;
|
2019-04-26 01:13:17 +02:00
|
|
|
|
invoiceQuery.Count = int.MaxValue;
|
2019-01-16 21:33:04 +01:00
|
|
|
|
var invoices = await _InvoiceRepository.GetInvoices(invoiceQuery);
|
2018-11-30 08:22:39 +01:00
|
|
|
|
var res = model.Process(invoices, format);
|
2018-11-30 09:04:26 +01:00
|
|
|
|
|
|
|
|
|
var cd = new ContentDisposition
|
|
|
|
|
{
|
2018-11-30 09:51:23 +01:00
|
|
|
|
FileName = $"btcpay-export-{DateTime.UtcNow.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture)}.{format}",
|
2018-11-30 09:04:26 +01:00
|
|
|
|
Inline = true
|
|
|
|
|
};
|
|
|
|
|
Response.Headers.Add("Content-Disposition", cd.ToString());
|
|
|
|
|
Response.Headers.Add("X-Content-Type-Options", "nosniff");
|
2018-11-30 08:22:39 +01:00
|
|
|
|
return Content(res, "application/" + format);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-05-24 08:38:47 +02:00
|
|
|
|
private SelectList GetPaymentMethodsSelectList()
|
|
|
|
|
{
|
2019-05-29 16:33:31 +02:00
|
|
|
|
return new SelectList(_paymentMethodHandlerDictionary.Distinct().SelectMany(handler =>
|
|
|
|
|
handler.GetSupportedPaymentMethods()
|
2019-06-03 18:06:03 +02:00
|
|
|
|
.Select(id => new SelectListItem(id.ToPrettyString(), id.ToString()))),
|
2019-05-24 08:38:47 +02:00
|
|
|
|
nameof(SelectListItem.Value),
|
|
|
|
|
nameof(SelectListItem.Text));
|
|
|
|
|
}
|
2019-05-29 16:33:31 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("invoices/create")]
|
2018-04-30 15:00:43 +02:00
|
|
|
|
[Authorize(AuthenticationSchemes = Policies.CookieAuthentication)]
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[BitpayAPIConstraint(false)]
|
|
|
|
|
public async Task<IActionResult> CreateInvoice()
|
|
|
|
|
{
|
2018-04-29 19:33:42 +02:00
|
|
|
|
var stores = new SelectList(await _StoreRepository.GetStoresByUserId(GetUserId()), nameof(StoreData.Id), nameof(StoreData.StoreName), null);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (stores.Count() == 0)
|
|
|
|
|
{
|
|
|
|
|
StatusMessage = "Error: You need to create at least one store before creating a transaction";
|
2018-03-23 08:24:57 +01:00
|
|
|
|
return RedirectToAction(nameof(UserStoresController.ListStores), "UserStores");
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2019-05-02 14:29:51 +02:00
|
|
|
|
|
2019-05-24 08:38:47 +02:00
|
|
|
|
return View(new CreateInvoiceModel() { Stores = stores, AvailablePaymentMethods = GetPaymentMethodsSelectList() });
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("invoices/create")]
|
2018-04-30 15:00:43 +02:00
|
|
|
|
[Authorize(AuthenticationSchemes = Policies.CookieAuthentication)]
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[BitpayAPIConstraint(false)]
|
2019-03-05 09:09:17 +01:00
|
|
|
|
public async Task<IActionResult> CreateInvoice(CreateInvoiceModel model, CancellationToken cancellationToken)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-04-29 19:33:42 +02:00
|
|
|
|
var stores = await _StoreRepository.GetStoresByUserId(GetUserId());
|
|
|
|
|
model.Stores = new SelectList(stores, nameof(StoreData.Id), nameof(StoreData.StoreName), model.StoreId);
|
2019-05-03 20:49:58 +02:00
|
|
|
|
|
2019-05-24 08:38:47 +02:00
|
|
|
|
model.AvailablePaymentMethods = GetPaymentMethodsSelectList();
|
2019-05-03 20:49:58 +02:00
|
|
|
|
|
2018-04-29 19:33:42 +02:00
|
|
|
|
var store = stores.FirstOrDefault(s => s.Id == model.StoreId);
|
2018-05-13 08:09:17 +02:00
|
|
|
|
if (store == null)
|
2018-04-29 19:33:42 +02:00
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError(nameof(model.StoreId), "Store not found");
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
2018-03-23 08:24:57 +01:00
|
|
|
|
StatusMessage = null;
|
2018-09-08 07:32:26 +02:00
|
|
|
|
if (!store.HasClaim(Policies.CanCreateInvoice.Key))
|
2018-03-23 08:24:57 +01:00
|
|
|
|
{
|
2018-04-05 08:44:27 +02:00
|
|
|
|
ModelState.AddModelError(nameof(model.StoreId), "You need to be owner of this store to create an invoice");
|
|
|
|
|
return View(model);
|
2018-03-23 08:24:57 +01:00
|
|
|
|
}
|
2018-04-05 08:44:27 +02:00
|
|
|
|
|
2018-02-20 04:45:04 +01:00
|
|
|
|
if (store.GetSupportedPaymentMethods(_NetworkProvider).Count() == 0)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-04-05 08:44:27 +02:00
|
|
|
|
ModelState.AddModelError(nameof(model.StoreId), "You need to configure the derivation scheme in order to create an invoice");
|
|
|
|
|
return View(model);
|
2018-03-23 08:24:57 +01:00
|
|
|
|
}
|
2019-05-03 20:49:58 +02:00
|
|
|
|
|
2018-03-23 08:24:57 +01:00
|
|
|
|
|
2018-04-18 15:27:01 +02:00
|
|
|
|
if (StatusMessage != null)
|
2018-03-23 08:24:57 +01:00
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return RedirectToAction(nameof(StoresController.UpdateStore), "Stores", new
|
|
|
|
|
{
|
|
|
|
|
storeId = store.Id
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-12-03 14:36:04 +01:00
|
|
|
|
|
|
|
|
|
try
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2019-02-21 10:40:27 +01:00
|
|
|
|
var result = await CreateInvoiceCore(new CreateInvoiceRequest()
|
2017-12-03 14:36:04 +01:00
|
|
|
|
{
|
|
|
|
|
Price = model.Amount.Value,
|
|
|
|
|
Currency = model.Currency,
|
|
|
|
|
PosData = model.PosData,
|
|
|
|
|
OrderId = model.OrderId,
|
|
|
|
|
//RedirectURL = redirect + "redirect",
|
2018-10-12 03:09:13 +02:00
|
|
|
|
NotificationEmail = model.NotificationEmail,
|
2017-12-03 14:36:04 +01:00
|
|
|
|
NotificationURL = model.NotificationUrl,
|
|
|
|
|
ItemDesc = model.ItemDesc,
|
|
|
|
|
FullNotifications = true,
|
|
|
|
|
BuyerEmail = model.BuyerEmail,
|
2019-05-02 14:29:51 +02:00
|
|
|
|
SupportedTransactionCurrencies = model.SupportedTransactionCurrencies?.ToDictionary(s => s, s => new InvoiceSupportedTransactionCurrency()
|
|
|
|
|
{
|
2019-05-03 20:49:58 +02:00
|
|
|
|
Enabled = true
|
2019-05-02 14:29:51 +02:00
|
|
|
|
})
|
2019-03-05 09:09:17 +01:00
|
|
|
|
}, store, HttpContext.Request.GetAbsoluteRoot(), cancellationToken: cancellationToken);
|
2017-10-12 17:25:45 +02:00
|
|
|
|
|
2017-12-03 14:36:04 +01:00
|
|
|
|
StatusMessage = $"Invoice {result.Data.Id} just created!";
|
|
|
|
|
return RedirectToAction(nameof(ListInvoices));
|
|
|
|
|
}
|
2018-05-03 18:46:52 +02:00
|
|
|
|
catch (BitpayHttpException ex)
|
2017-12-03 14:36:04 +01:00
|
|
|
|
{
|
2018-05-03 18:46:52 +02:00
|
|
|
|
ModelState.TryAddModelError(nameof(model.Currency), $"Error: {ex.Message}");
|
2017-12-03 14:36:04 +01:00
|
|
|
|
return View(model);
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-11-06 04:15:52 +01:00
|
|
|
|
[HttpPost]
|
2018-12-10 07:34:48 +01:00
|
|
|
|
[Route("invoices/{invoiceId}/changestate/{newState}")]
|
2018-04-30 15:00:43 +02:00
|
|
|
|
[Authorize(AuthenticationSchemes = Policies.CookieAuthentication)]
|
2017-11-06 04:15:52 +01:00
|
|
|
|
[BitpayAPIConstraint(false)]
|
2019-05-03 21:54:40 +02:00
|
|
|
|
public async Task<IActionResult> ChangeInvoiceState(string invoiceId, string newState)
|
2017-11-06 04:15:52 +01:00
|
|
|
|
{
|
2018-12-06 09:08:28 +01:00
|
|
|
|
var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
|
|
|
|
|
{
|
|
|
|
|
InvoiceId = invoiceId,
|
|
|
|
|
UserId = GetUserId()
|
|
|
|
|
})).FirstOrDefault();
|
2019-05-03 21:54:40 +02:00
|
|
|
|
|
|
|
|
|
var model = new InvoiceStateChangeModel();
|
2018-06-21 07:15:36 +02:00
|
|
|
|
if (invoice == null)
|
2019-05-03 21:54:40 +02:00
|
|
|
|
{
|
|
|
|
|
model.NotFound = true;
|
|
|
|
|
return NotFound(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-12-10 07:34:48 +01:00
|
|
|
|
if (newState == "invalid")
|
|
|
|
|
{
|
|
|
|
|
await _InvoiceRepository.UpdatePaidInvoiceToInvalid(invoiceId);
|
2019-02-19 04:06:13 +01:00
|
|
|
|
_EventAggregator.Publish(new InvoiceEvent(invoice, 1008, InvoiceEvent.MarkedInvalid));
|
2019-05-03 21:54:40 +02:00
|
|
|
|
model.StatusString = new InvoiceState("invalid", "marked").ToString();
|
2018-12-10 07:34:48 +01:00
|
|
|
|
}
|
2019-04-26 01:13:17 +02:00
|
|
|
|
else if (newState == "complete")
|
2018-12-10 07:34:48 +01:00
|
|
|
|
{
|
|
|
|
|
await _InvoiceRepository.UpdatePaidInvoiceToComplete(invoiceId);
|
2019-02-19 04:06:13 +01:00
|
|
|
|
_EventAggregator.Publish(new InvoiceEvent(invoice, 2008, InvoiceEvent.MarkedCompleted));
|
2019-05-03 21:54:40 +02:00
|
|
|
|
model.StatusString = new InvoiceState("complete", "marked").ToString();
|
2018-12-10 07:34:48 +01:00
|
|
|
|
}
|
2019-05-03 21:54:40 +02:00
|
|
|
|
|
|
|
|
|
return Json(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class InvoiceStateChangeModel
|
|
|
|
|
{
|
|
|
|
|
public bool NotFound { get; set; }
|
|
|
|
|
public string StatusString { get; set; }
|
2017-11-06 04:15:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[TempData]
|
|
|
|
|
public string StatusMessage
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
private string GetUserId()
|
|
|
|
|
{
|
|
|
|
|
return _UserManager.GetUserId(User);
|
|
|
|
|
}
|
2018-11-27 07:13:09 +01:00
|
|
|
|
|
|
|
|
|
public class PosDataParser
|
|
|
|
|
{
|
2019-01-26 05:26:49 +01:00
|
|
|
|
public static Dictionary<string, object> ParsePosData(string posData)
|
2018-11-27 07:13:09 +01:00
|
|
|
|
{
|
2019-04-26 01:13:17 +02:00
|
|
|
|
var result = new Dictionary<string, object>();
|
2018-11-27 07:13:09 +01:00
|
|
|
|
if (string.IsNullOrEmpty(posData))
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-04-26 01:13:17 +02:00
|
|
|
|
|
2018-11-27 07:13:09 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-04-26 01:13:17 +02:00
|
|
|
|
var jObject = JObject.Parse(posData);
|
2018-11-27 07:13:09 +01:00
|
|
|
|
foreach (var item in jObject)
|
|
|
|
|
{
|
2019-04-26 01:13:17 +02:00
|
|
|
|
|
2018-11-27 07:13:09 +01:00
|
|
|
|
switch (item.Value.Type)
|
|
|
|
|
{
|
|
|
|
|
case JTokenType.Array:
|
2019-01-26 05:26:49 +01:00
|
|
|
|
var items = item.Value.AsEnumerable().ToList();
|
|
|
|
|
for (var i = 0; i < items.Count(); i++)
|
|
|
|
|
{
|
|
|
|
|
result.Add($"{item.Key}[{i}]", ParsePosData(items[i].ToString()));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case JTokenType.Object:
|
|
|
|
|
result.Add(item.Key, ParsePosData(item.Value.ToString()));
|
2018-11-27 07:13:09 +01:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
result.Add(item.Key, item.Value.ToString());
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-26 01:13:17 +02:00
|
|
|
|
|
2018-11-27 07:13:09 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
result.Add(string.Empty, posData);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-26 01:13:17 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|