2017-09-13 08:47:34 +02:00
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
|
using BTCPayServer.Filters;
|
|
|
|
|
using BTCPayServer.Models.InvoicingModels;
|
2017-10-20 21:06:37 +02:00
|
|
|
|
using BTCPayServer.Services.Invoices;
|
2017-09-13 16:50:36 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2017-10-13 10:18:32 +02:00
|
|
|
|
using Microsoft.AspNetCore.Cors;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2017-09-13 17:13:22 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
using NBitcoin;
|
|
|
|
|
using NBitpayClient;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-12-03 14:36:04 +01:00
|
|
|
|
using BTCPayServer.Services.Rates;
|
2017-12-17 11:58:55 +01:00
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using BTCPayServer.Events;
|
2018-01-07 13:48:00 +01:00
|
|
|
|
using NBXplorer;
|
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-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()
|
|
|
|
|
{
|
|
|
|
|
UserId = GetUserId(),
|
2018-01-08 12:06:16 +01:00
|
|
|
|
InvoiceId = invoiceId,
|
|
|
|
|
IncludeAddresses = true
|
2017-10-27 10:53:04 +02:00
|
|
|
|
})).FirstOrDefault();
|
|
|
|
|
if (invoice == null)
|
|
|
|
|
return NotFound();
|
2017-10-12 17:25:45 +02:00
|
|
|
|
|
2017-12-21 07:52:04 +01:00
|
|
|
|
var dto = invoice.EntityToDTO(_NetworkProvider);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var store = await _StoreRepository.FindStore(invoice.StoreId);
|
2017-12-21 10:01:26 +01:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
InvoiceDetailsModel model = new InvoiceDetailsModel()
|
|
|
|
|
{
|
|
|
|
|
StoreName = store.StoreName,
|
|
|
|
|
StoreLink = Url.Action(nameof(StoresController.UpdateStore), "Stores", new { storeId = store.Id }),
|
|
|
|
|
Id = invoice.Id,
|
|
|
|
|
Status = invoice.Status,
|
|
|
|
|
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,
|
2018-01-08 12:06:16 +01:00
|
|
|
|
Fiat = FormatCurrency((decimal)dto.Price, dto.Currency),
|
2017-10-27 10:53:04 +02:00
|
|
|
|
NotificationUrl = invoice.NotificationURL,
|
|
|
|
|
ProductInformation = invoice.ProductInformation,
|
|
|
|
|
};
|
2017-10-12 17:25:45 +02:00
|
|
|
|
|
2018-01-08 12:06:16 +01:00
|
|
|
|
foreach (var data in invoice.GetCryptoData())
|
|
|
|
|
{
|
|
|
|
|
var cryptoInfo = dto.CryptoInfo.First(o => o.CryptoCode.Equals(data.Key, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
var accounting = data.Value.Calculate();
|
|
|
|
|
var paymentNetwork = _NetworkProvider.GetNetwork(data.Key);
|
|
|
|
|
var cryptoPayment = new InvoiceDetailsModel.CryptoPayment();
|
|
|
|
|
cryptoPayment.CryptoCode = paymentNetwork.CryptoCode;
|
|
|
|
|
cryptoPayment.Due = accounting.Due.ToString() + $" {paymentNetwork.CryptoCode}";
|
|
|
|
|
cryptoPayment.Paid = accounting.CryptoPaid.ToString() + $" {paymentNetwork.CryptoCode}";
|
|
|
|
|
cryptoPayment.Address = data.Value.DepositAddress.ToString();
|
|
|
|
|
cryptoPayment.Rate = FormatCurrency(data.Value);
|
|
|
|
|
cryptoPayment.PaymentUrl = cryptoInfo.PaymentUrls.BIP21;
|
|
|
|
|
model.CryptoPayments.Add(cryptoPayment);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var payments = invoice
|
|
|
|
|
.Payments
|
|
|
|
|
.Select(async payment =>
|
|
|
|
|
{
|
|
|
|
|
var m = new InvoiceDetailsModel.Payment();
|
2018-01-08 12:06:16 +01:00
|
|
|
|
var paymentNetwork = _NetworkProvider.GetNetwork(payment.GetCryptoCode());
|
|
|
|
|
m.CryptoCode = payment.GetCryptoCode();
|
|
|
|
|
m.DepositAddress = payment.GetScriptPubKey().GetDestinationAddress(paymentNetwork.NBitcoinNetwork);
|
2018-01-07 18:36:41 +01:00
|
|
|
|
m.Confirmations = (await _ExplorerClients.GetExplorerClient(payment.GetCryptoCode())?.GetTransactionAsync(payment.Outpoint.Hash))?.Confirmations ?? 0;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
m.TransactionId = payment.Outpoint.Hash.ToString();
|
|
|
|
|
m.ReceivedTime = payment.ReceivedTime;
|
2018-01-08 12:06:16 +01:00
|
|
|
|
m.TransactionLink = string.Format(paymentNetwork.BlockExplorerLink, m.TransactionId);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return m;
|
|
|
|
|
})
|
|
|
|
|
.ToArray();
|
|
|
|
|
await Task.WhenAll(payments);
|
2018-01-08 12:06:16 +01:00
|
|
|
|
model.Addresses = invoice.HistoricalAddresses;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
model.Payments = payments.Select(p => p.GetAwaiter().GetResult()).ToList();
|
|
|
|
|
model.StatusMessage = StatusMessage;
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("i/{invoiceId}")]
|
2018-01-09 03:41:07 +01:00
|
|
|
|
[Route("i/{invoiceId}/{cryptoCode}")]
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[Route("invoice")]
|
|
|
|
|
[AcceptMediaTypeConstraint("application/bitcoin-paymentrequest", false)]
|
|
|
|
|
[XFrameOptionsAttribute(null)]
|
2017-12-21 07:52:04 +01:00
|
|
|
|
public async Task<IActionResult> Checkout(string invoiceId, string id = null, string cryptoCode = null)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
//Keep compatibility with Bitpay
|
|
|
|
|
invoiceId = invoiceId ?? id;
|
|
|
|
|
id = invoiceId;
|
|
|
|
|
////
|
2017-10-13 09:07:57 +02:00
|
|
|
|
|
2017-12-21 07:52:04 +01:00
|
|
|
|
var model = await GetInvoiceModel(invoiceId, cryptoCode);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (model == null)
|
|
|
|
|
return NotFound();
|
2017-10-21 05:24:28 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return View(nameof(Checkout), model);
|
|
|
|
|
}
|
2017-10-21 05:24:28 +02:00
|
|
|
|
|
2017-12-21 07:52:04 +01:00
|
|
|
|
private async Task<PaymentModel> GetInvoiceModel(string invoiceId, string cryptoCode)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);
|
2018-01-09 03:41:07 +01:00
|
|
|
|
|
|
|
|
|
var store = await _StoreRepository.FindStore(invoice.StoreId);
|
|
|
|
|
if (cryptoCode == null)
|
|
|
|
|
cryptoCode = store.GetDefaultCrypto();
|
2017-12-21 07:52:04 +01:00
|
|
|
|
var network = _NetworkProvider.GetNetwork(cryptoCode);
|
|
|
|
|
if (invoice == null || network == null || !invoice.Support(network))
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return null;
|
2017-12-21 07:52:04 +01:00
|
|
|
|
var cryptoData = invoice.GetCryptoData(network);
|
2018-01-09 03:41:07 +01:00
|
|
|
|
|
2017-12-21 07:52:04 +01:00
|
|
|
|
var dto = invoice.EntityToDTO(_NetworkProvider);
|
|
|
|
|
var cryptoInfo = dto.CryptoInfo.First(o => o.CryptoCode == network.CryptoCode);
|
2018-01-09 03:41:07 +01:00
|
|
|
|
|
2017-12-03 14:14:08 +01:00
|
|
|
|
var currency = invoice.ProductInformation.Currency;
|
2017-12-21 10:01:26 +01:00
|
|
|
|
var accounting = cryptoData.Calculate();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var model = new PaymentModel()
|
|
|
|
|
{
|
2018-01-09 03:41:07 +01:00
|
|
|
|
CryptoCode = network.CryptoCode,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
ServerUrl = HttpContext.Request.GetAbsoluteRoot(),
|
|
|
|
|
OrderId = invoice.OrderId,
|
|
|
|
|
InvoiceId = invoice.Id,
|
2017-12-21 07:52:04 +01:00
|
|
|
|
BtcAddress = cryptoData.DepositAddress,
|
2018-01-09 09:27:26 +01:00
|
|
|
|
OrderAmount = (accounting.TotalDue - accounting.NetworkFee).ToString(),
|
2017-12-21 10:01:26 +01:00
|
|
|
|
BtcDue = accounting.Due.ToString(),
|
2017-10-27 10:53:04 +02:00
|
|
|
|
CustomerEmail = invoice.RefundMail,
|
|
|
|
|
ExpirationSeconds = Math.Max(0, (int)(invoice.ExpirationTime - DateTimeOffset.UtcNow).TotalSeconds),
|
|
|
|
|
MaxTimeSeconds = (int)(invoice.ExpirationTime - invoice.InvoiceTime).TotalSeconds,
|
|
|
|
|
ItemDesc = invoice.ProductInformation.ItemDesc,
|
2018-01-08 12:06:16 +01:00
|
|
|
|
Rate = FormatCurrency(cryptoData),
|
2017-10-27 10:53:04 +02:00
|
|
|
|
MerchantRefLink = invoice.RedirectURL ?? "/",
|
|
|
|
|
StoreName = store.StoreName,
|
2018-01-06 03:38:24 +01:00
|
|
|
|
InvoiceBitcoinUrl = cryptoInfo.PaymentUrls.BIP21,
|
2017-12-21 10:01:26 +01:00
|
|
|
|
TxCount = accounting.TxCount,
|
|
|
|
|
BtcPaid = accounting.Paid.ToString(),
|
2018-01-09 03:41:07 +01:00
|
|
|
|
Status = invoice.Status,
|
2018-01-09 09:27:26 +01:00
|
|
|
|
CryptoImage = "/" + Url.Content(network.CryptoImagePath),
|
2018-01-09 15:55:08 +01:00
|
|
|
|
NetworkFeeDescription = $"{accounting.TxCount} transaction{(accounting.TxCount > 1 ? "s" : "")} x {cryptoData.TxFee} {network.CryptoCode}",
|
|
|
|
|
AvailableCryptos = invoice.GetCryptoData().Select(kv=> new PaymentModel.AvailableCrypto()
|
|
|
|
|
{
|
|
|
|
|
CryptoCode = kv.Key,
|
|
|
|
|
CryptoImage = "/" + _NetworkProvider.GetNetwork(kv.Key).CryptoImagePath,
|
|
|
|
|
Link = Url.Action(nameof(Checkout), new { invoiceId = invoiceId, cryptoCode = kv.Key })
|
|
|
|
|
}).ToList()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
};
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2018-01-09 09:27:26 +01:00
|
|
|
|
var isMultiCurrency = invoice.Payments.Select(p=>p.GetCryptoCode()).Concat(new[] { network.CryptoCode }).Distinct().Count() > 1;
|
|
|
|
|
if (isMultiCurrency)
|
|
|
|
|
model.NetworkFeeDescription = $"{accounting.NetworkFee} {network.CryptoCode}";
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var expiration = TimeSpan.FromSeconds(model.ExpirationSeconds);
|
|
|
|
|
model.TimeLeft = PrettyPrint(expiration);
|
|
|
|
|
return model;
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2018-01-08 12:06:16 +01:00
|
|
|
|
private string FormatCurrency(CryptoData cryptoData)
|
|
|
|
|
{
|
|
|
|
|
string currency = cryptoData.ParentEntity.ProductInformation.Currency;
|
|
|
|
|
return FormatCurrency(cryptoData.Rate, currency);
|
|
|
|
|
}
|
|
|
|
|
public string FormatCurrency(decimal price, string currency)
|
|
|
|
|
{
|
|
|
|
|
return price.ToString("C", _CurrencyNameTable.GetCurrencyProvider(currency)) + $" ({currency})";
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
private string PrettyPrint(TimeSpan expiration)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
if (expiration.Days >= 1)
|
|
|
|
|
builder.Append(expiration.Days.ToString());
|
|
|
|
|
if (expiration.Hours >= 1)
|
|
|
|
|
builder.Append(expiration.Hours.ToString("00"));
|
|
|
|
|
builder.Append($"{expiration.Minutes.ToString("00")}:{expiration.Seconds.ToString("00")}");
|
|
|
|
|
return builder.ToString();
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("i/{invoiceId}/status")]
|
2018-01-09 03:41:07 +01:00
|
|
|
|
[Route("i/{invoiceId}/{cryptoCode}/status")]
|
2017-12-21 07:52:04 +01:00
|
|
|
|
public async Task<IActionResult> GetStatus(string invoiceId, string cryptoCode)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2017-12-21 07:52:04 +01:00
|
|
|
|
var model = await GetInvoiceModel(invoiceId, cryptoCode);
|
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")]
|
|
|
|
|
public async Task<IActionResult> GetStatusWebSocket(string invoiceId)
|
|
|
|
|
{
|
|
|
|
|
if (!HttpContext.WebSockets.IsWebSocketRequest)
|
|
|
|
|
return NotFound();
|
|
|
|
|
var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);
|
|
|
|
|
if (invoice == null || invoice.Status == "complete" || invoice.Status == "invalid" || invoice.Status == "expired")
|
|
|
|
|
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)));
|
|
|
|
|
leases.Add(_EventAggregator.Subscribe<Events.InvoicePaymentEvent>(async o => await NotifySocket(webSocket, o.InvoiceId, invoiceId)));
|
|
|
|
|
leases.Add(_EventAggregator.Subscribe<Events.InvoiceStatusChangedEvent>(async o => await NotifySocket(webSocket, o.InvoiceId, 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();
|
|
|
|
|
await CloseSocket(webSocket);
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
catch { await CloseSocket(webSocket); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task CloseSocket(WebSocket webSocket)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (webSocket.State == WebSocketState.Open)
|
|
|
|
|
{
|
|
|
|
|
CancellationTokenSource cts = new CancellationTokenSource();
|
|
|
|
|
cts.CancelAfter(5000);
|
|
|
|
|
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", cts.Token);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-17 14:57:27 +01:00
|
|
|
|
catch { }
|
2017-12-17 11:58:55 +01:00
|
|
|
|
finally { webSocket.Dispose(); }
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("i/{invoiceId}/UpdateCustomer")]
|
|
|
|
|
public async Task<IActionResult> UpdateCustomer(string invoiceId, [FromBody]UpdateCustomerModel data)
|
|
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ModelState);
|
|
|
|
|
}
|
|
|
|
|
await _InvoiceRepository.UpdateInvoice(invoiceId, data).ConfigureAwait(false);
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("invoices")]
|
|
|
|
|
[Authorize(AuthenticationSchemes = "Identity.Application")]
|
|
|
|
|
[BitpayAPIConstraint(false)]
|
|
|
|
|
public async Task<IActionResult> ListInvoices(string searchTerm = null, int skip = 0, int count = 20)
|
|
|
|
|
{
|
|
|
|
|
var model = new InvoicesModel();
|
2017-12-03 15:35:52 +01:00
|
|
|
|
var filterString = new SearchString(searchTerm);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
foreach (var invoice in await _InvoiceRepository.GetInvoices(new InvoiceQuery()
|
|
|
|
|
{
|
2017-12-03 15:35:52 +01:00
|
|
|
|
TextSearch = filterString.TextSearch,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
Count = count,
|
|
|
|
|
Skip = skip,
|
2017-12-03 15:35:52 +01:00
|
|
|
|
UserId = GetUserId(),
|
|
|
|
|
Status = filterString.Filters.TryGet("status"),
|
|
|
|
|
StoreId = filterString.Filters.TryGet("storeid")
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
model.SearchTerm = searchTerm;
|
|
|
|
|
model.Invoices.Add(new InvoiceModel()
|
|
|
|
|
{
|
|
|
|
|
Status = invoice.Status,
|
|
|
|
|
Date = invoice.InvoiceTime,
|
|
|
|
|
InvoiceId = invoice.Id,
|
|
|
|
|
AmountCurrency = $"{invoice.ProductInformation.Price.ToString(CultureInfo.InvariantCulture)} {invoice.ProductInformation.Currency}"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
model.Skip = skip;
|
|
|
|
|
model.Count = count;
|
|
|
|
|
model.StatusMessage = StatusMessage;
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("invoices/create")]
|
|
|
|
|
[Authorize(AuthenticationSchemes = "Identity.Application")]
|
|
|
|
|
[BitpayAPIConstraint(false)]
|
|
|
|
|
public async Task<IActionResult> CreateInvoice()
|
|
|
|
|
{
|
|
|
|
|
var stores = await GetStores(GetUserId());
|
|
|
|
|
if (stores.Count() == 0)
|
|
|
|
|
{
|
|
|
|
|
StatusMessage = "Error: You need to create at least one store before creating a transaction";
|
|
|
|
|
return RedirectToAction(nameof(StoresController.ListStores), "Stores");
|
|
|
|
|
}
|
|
|
|
|
return View(new CreateInvoiceModel() { Stores = stores });
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("invoices/create")]
|
|
|
|
|
[Authorize(AuthenticationSchemes = "Identity.Application")]
|
|
|
|
|
[BitpayAPIConstraint(false)]
|
|
|
|
|
public async Task<IActionResult> CreateInvoice(CreateInvoiceModel model)
|
|
|
|
|
{
|
2017-12-06 10:08:21 +01:00
|
|
|
|
model.Stores = await GetStores(GetUserId(), model.StoreId);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
var store = await _StoreRepository.FindStore(model.StoreId, GetUserId());
|
2018-01-06 10:57:56 +01:00
|
|
|
|
if (store.GetDerivationStrategies(_NetworkProvider).Count() == 0)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
StatusMessage = "Error: You need to configure the derivation scheme in order to create an invoice";
|
|
|
|
|
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
|
|
|
|
{
|
2017-12-03 14:36:04 +01:00
|
|
|
|
var result = await CreateInvoiceCore(new Invoice()
|
|
|
|
|
{
|
|
|
|
|
Price = model.Amount.Value,
|
|
|
|
|
Currency = model.Currency,
|
|
|
|
|
PosData = model.PosData,
|
|
|
|
|
OrderId = model.OrderId,
|
|
|
|
|
//RedirectURL = redirect + "redirect",
|
|
|
|
|
NotificationURL = model.NotificationUrl,
|
|
|
|
|
ItemDesc = model.ItemDesc,
|
|
|
|
|
FullNotifications = true,
|
|
|
|
|
BuyerEmail = model.BuyerEmail,
|
|
|
|
|
}, store, HttpContext.Request.GetAbsoluteRoot());
|
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));
|
|
|
|
|
}
|
|
|
|
|
catch (RateUnavailableException)
|
|
|
|
|
{
|
|
|
|
|
ModelState.TryAddModelError(nameof(model.Currency), "Unsupported currency");
|
|
|
|
|
return View(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
|
|
|
|
private async Task<SelectList> GetStores(string userId, string storeId = null)
|
|
|
|
|
{
|
|
|
|
|
return new SelectList(await _StoreRepository.GetStoresByUserId(userId), nameof(StoreData.Id), nameof(StoreData.StoreName), storeId);
|
|
|
|
|
}
|
2017-09-13 17:13:22 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Authorize(AuthenticationSchemes = "Identity.Application")]
|
|
|
|
|
[BitpayAPIConstraint(false)]
|
|
|
|
|
public IActionResult SearchInvoice(InvoicesModel invoices)
|
|
|
|
|
{
|
|
|
|
|
return RedirectToAction(nameof(ListInvoices), new
|
|
|
|
|
{
|
|
|
|
|
searchTerm = invoices.SearchTerm,
|
|
|
|
|
skip = invoices.Skip,
|
|
|
|
|
count = invoices.Count,
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-11-06 04:15:52 +01:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("invoices/invalidatepaid")]
|
|
|
|
|
[Authorize(AuthenticationSchemes = "Identity.Application")]
|
|
|
|
|
[BitpayAPIConstraint(false)]
|
|
|
|
|
public async Task<IActionResult> InvalidatePaidInvoice(string invoiceId)
|
|
|
|
|
{
|
|
|
|
|
await _InvoiceRepository.UpdatePaidInvoiceToInvalid(invoiceId);
|
|
|
|
|
return RedirectToAction(nameof(ListInvoices));
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|