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