btcpayserver/BTCPayServer/Controllers/InvoiceController.UI.cs

201 lines
6.0 KiB
C#
Raw Normal View History

2017-09-13 08:47:34 +02:00
using BTCPayServer.Data;
using BTCPayServer.Filters;
using BTCPayServer.Invoicing;
using BTCPayServer.Models.InvoicingModels;
2017-09-13 16:50:36 +02:00
using Microsoft.AspNetCore.Authorization;
2017-09-13 08:47:34 +02:00
using Microsoft.AspNetCore.Mvc;
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;
namespace BTCPayServer.Controllers
{
public partial class InvoiceController
{
[HttpGet]
[Route("i/{invoiceId}")]
[AcceptMediaTypeConstraint("application/bitcoin-paymentrequest", false)]
2017-09-13 09:56:33 +02:00
public async Task<IActionResult> Checkout(string invoiceId)
2017-09-13 08:47:34 +02:00
{
var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);
if(invoice == null)
return NotFound();
var store = await _StoreRepository.FindStore(invoice.StoreId);
var dto = EntityToDTO(invoice);
var model = new PaymentModel()
{
OrderId = invoice.OrderId,
InvoiceId = invoice.Id,
BTCAddress = invoice.DepositAddress.ToString(),
BTCAmount = (invoice.GetTotalCryptoDue() - invoice.TxFee).ToString(),
BTCTotalDue = invoice.GetTotalCryptoDue().ToString(),
BTCDue = invoice.GetCryptoDue().ToString(),
CustomerEmail = invoice.RefundMail,
ExpirationSeconds = Math.Max(0, (int)(invoice.ExpirationTime - DateTimeOffset.UtcNow).TotalSeconds),
MaxTimeSeconds = (int)(invoice.ExpirationTime - invoice.InvoiceTime).TotalSeconds,
ItemDesc = invoice.ProductInformation.ItemDesc,
Rate = invoice.Rate.ToString(),
RedirectUrl = invoice.RedirectURL,
StoreName = store.StoreName,
TxFees = invoice.TxFee.ToString(),
InvoiceBitcoinUrl = dto.PaymentUrls.BIP72,
2017-09-13 18:06:11 +02:00
TxCount = invoice.GetTxCount(),
BTCPaid = invoice.GetTotalPaid().ToString(),
2017-09-13 08:47:34 +02:00
Status = invoice.Status
};
var expiration = TimeSpan.FromSeconds((double)model.ExpirationSeconds);
model.TimeLeft = PrettyPrint(expiration);
return View(model);
}
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();
}
[HttpGet]
[Route("i/{invoiceId}/status")]
public async Task<IActionResult> GetStatus(string invoiceId)
{
var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);
if(invoice == null)
return NotFound();
return Content(invoice.Status);
}
[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();
}
[HttpGet]
2017-09-13 16:50:36 +02:00
[Route("invoices")]
[Authorize(AuthenticationSchemes = "Identity.Application")]
2017-09-13 08:47:34 +02:00
[BitpayAPIConstraint(false)]
2017-09-13 09:56:33 +02:00
public async Task<IActionResult> ListInvoices(string searchTerm = null, int skip = 0, int count = 20)
2017-09-13 08:47:34 +02:00
{
var model = new InvoicesModel();
foreach(var invoice in await _InvoiceRepository.GetInvoices(new InvoiceQuery()
{
TextSearch = searchTerm,
Count = count,
Skip = skip,
2017-09-13 16:50:36 +02:00
UserId = GetUserId()
2017-09-13 08:47:34 +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);
}
[HttpGet]
2017-09-13 16:50:36 +02:00
[Route("invoices/create")]
[Authorize(AuthenticationSchemes = "Identity.Application")]
2017-09-13 08:47:34 +02:00
[BitpayAPIConstraint(false)]
public async Task<IActionResult> CreateInvoice()
2017-09-13 08:47:34 +02:00
{
return View(new CreateInvoiceModel() { Stores = await GetStores(GetUserId()) });
2017-09-13 08:47:34 +02:00
}
[HttpPost]
2017-09-13 16:50:36 +02:00
[Route("invoices/create")]
[Authorize(AuthenticationSchemes = "Identity.Application")]
2017-09-13 08:47:34 +02:00
[BitpayAPIConstraint(false)]
public async Task<IActionResult> CreateInvoice(CreateInvoiceModel model)
{
if(!ModelState.IsValid)
{
model.Stores = await GetStores(GetUserId(), model.StoreId);
2017-09-13 08:47:34 +02:00
return View(model);
}
2017-09-13 16:50:36 +02:00
var store = await _StoreRepository.FindStore(model.StoreId, GetUserId());
if(string.IsNullOrEmpty(store.DerivationStrategy))
{
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-09-13 08:47:34 +02:00
var result = await CreateInvoiceCore(new Invoice()
{
Price = model.Amount.Value,
Currency = "USD",
PosData = model.PosData,
OrderId = model.OrderId,
//RedirectURL = redirect + "redirect",
//NotificationURL = CallbackUri + "/notification",
ItemDesc = model.ItemDesc,
FullNotifications = true,
BuyerEmail = model.BuyerEmail,
2017-09-13 08:47:34 +02:00
}, store);
StatusMessage = $"Invoice {result.Data.Id} just created!";
2017-09-13 10:46:51 +02:00
return RedirectToAction(nameof(ListInvoices));
2017-09-13 08:47:34 +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 08:47:34 +02:00
[HttpPost]
2017-09-13 16:50:36 +02:00
[Authorize(AuthenticationSchemes = "Identity.Application")]
2017-09-13 08:47:34 +02:00
[BitpayAPIConstraint(false)]
public IActionResult SearchInvoice(InvoicesModel invoices)
{
2017-09-13 10:46:51 +02:00
return RedirectToAction(nameof(ListInvoices), new
2017-09-13 08:47:34 +02:00
{
searchTerm = invoices.SearchTerm,
skip = invoices.Skip,
count = invoices.Count,
});
}
[TempData]
public string StatusMessage
{
get;
set;
}
2017-09-13 16:50:36 +02:00
private string GetUserId()
2017-09-13 08:47:34 +02:00
{
2017-09-13 16:50:36 +02:00
return _UserManager.GetUserId(User);
2017-09-13 08:47:34 +02:00
}
}
}