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.Cors;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using NBitpayClient;
|
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-12-21 07:52:04 +01:00
|
|
|
|
private BTCPayNetworkProvider _NetworkProvider;
|
2017-10-13 10:46:19 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public InvoiceControllerAPI(InvoiceController invoiceController,
|
|
|
|
|
InvoiceRepository invoceRepository,
|
2017-12-21 07:52:04 +01:00
|
|
|
|
BTCPayNetworkProvider networkProvider)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
this._InvoiceController = invoiceController;
|
|
|
|
|
this._InvoiceRepository = invoceRepository;
|
2017-12-21 07:52:04 +01:00
|
|
|
|
this._NetworkProvider = networkProvider;
|
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");
|
2017-12-21 07:52:04 +01:00
|
|
|
|
var resp = invoice.EntityToDTO(_NetworkProvider);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return new DataWrapper<InvoiceResponse>(resp);
|
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var entities = (await _InvoiceRepository.GetInvoices(query))
|
2017-12-21 07:52:04 +01:00
|
|
|
|
.Select((o) => o.EntityToDTO(_NetworkProvider)).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
|
|
|
|
}
|