2018-08-30 18:34:39 +02:00
|
|
|
|
using System;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
using System.Linq;
|
2019-03-05 09:09:17 +01:00
|
|
|
|
using System.Threading;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2018-08-30 18:34:39 +02:00
|
|
|
|
using BTCPayServer.Filters;
|
|
|
|
|
using BTCPayServer.Models;
|
|
|
|
|
using BTCPayServer.Security;
|
2017-10-20 21:06:37 +02:00
|
|
|
|
using BTCPayServer.Services.Invoices;
|
2018-05-11 10:16:18 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2018-08-30 18:34:39 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[BitpayAPIConstraint]
|
2018-09-08 07:32:26 +02:00
|
|
|
|
[Authorize(Policies.CanCreateInvoice.Key, AuthenticationSchemes = Policies.BitpayAuthentication)]
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public class InvoiceControllerAPI : Controller
|
2017-09-13 08:47:34 +02:00
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
private InvoiceController _InvoiceController;
|
|
|
|
|
private InvoiceRepository _InvoiceRepository;
|
2017-10-13 10:46:19 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public InvoiceControllerAPI(InvoiceController invoiceController,
|
2019-05-29 16:33:31 +02:00
|
|
|
|
InvoiceRepository invoceRepository)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2019-05-29 16:33:31 +02:00
|
|
|
|
_InvoiceController = invoiceController;
|
|
|
|
|
_InvoiceRepository = invoceRepository;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-10-13 10:46:19 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("invoices")]
|
|
|
|
|
[MediaTypeConstraint("application/json")]
|
2019-03-05 09:09:17 +01:00
|
|
|
|
public async Task<DataWrapper<InvoiceResponse>> CreateInvoice([FromBody] CreateInvoiceRequest invoice, CancellationToken cancellationToken)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2019-02-21 11:36:05 +01:00
|
|
|
|
if (invoice == null)
|
|
|
|
|
throw new BitpayHttpException(400, "Invalid invoice");
|
2019-03-05 09:09:17 +01:00
|
|
|
|
return await _InvoiceController.CreateInvoiceCore(invoice, HttpContext.GetStoreData(), HttpContext.Request.GetAbsoluteRoot(), cancellationToken: cancellationToken);
|
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/{id}")]
|
2018-12-06 08:58:04 +01:00
|
|
|
|
public async Task<DataWrapper<InvoiceResponse>> GetInvoice(string id)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-12-06 08:58:04 +01:00
|
|
|
|
var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
|
|
|
|
|
{
|
|
|
|
|
InvoiceId = id,
|
|
|
|
|
StoreId = new[] { HttpContext.GetStoreData().Id }
|
|
|
|
|
})).FirstOrDefault();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (invoice == null)
|
|
|
|
|
throw new BitpayHttpException(404, "Object not found");
|
2019-05-29 16:33:31 +02:00
|
|
|
|
return new DataWrapper<InvoiceResponse>(invoice.EntityToDTO());
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("invoices")]
|
|
|
|
|
public async Task<DataWrapper<InvoiceResponse[]>> GetInvoices(
|
|
|
|
|
string token,
|
|
|
|
|
DateTimeOffset? dateStart = null,
|
|
|
|
|
DateTimeOffset? dateEnd = null,
|
|
|
|
|
string orderId = null,
|
|
|
|
|
string itemCode = null,
|
|
|
|
|
string status = null,
|
|
|
|
|
int? limit = null,
|
|
|
|
|
int? offset = null)
|
|
|
|
|
{
|
|
|
|
|
if (dateEnd != null)
|
|
|
|
|
dateEnd = dateEnd.Value + TimeSpan.FromDays(1); //Should include the end day
|
2019-01-05 09:49:06 +01:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var query = new InvoiceQuery()
|
|
|
|
|
{
|
|
|
|
|
Count = limit,
|
|
|
|
|
Skip = offset,
|
|
|
|
|
EndDate = dateEnd,
|
|
|
|
|
StartDate = dateStart,
|
2019-01-06 10:00:55 +01:00
|
|
|
|
OrderId = orderId == null ? null : new[] { orderId },
|
|
|
|
|
ItemCode = itemCode == null ? null : new[] { itemCode },
|
2018-04-26 04:01:59 +02:00
|
|
|
|
Status = status == null ? null : new[] { status },
|
2018-04-29 13:32:43 +02:00
|
|
|
|
StoreId = new[] { this.HttpContext.GetStoreData().Id }
|
2017-10-27 10:53:04 +02:00
|
|
|
|
};
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2019-05-29 16:33:31 +02:00
|
|
|
|
var entities = (await _InvoiceRepository.GetInvoices(query))
|
2019-05-24 15:22:38 +02:00
|
|
|
|
.Select((o) => o.EntityToDTO()).ToArray();
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return DataWrapper.Create(entities);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|