btcpayserver/BTCPayServer/Controllers/InvoiceController.API.cs

89 lines
3.4 KiB
C#
Raw Normal View History

2020-06-28 21:44:35 -05:00
using System;
2017-09-13 15:47:34 +09:00
using System.Linq;
using System.Threading;
2017-09-13 15:47:34 +09:00
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
2020-03-19 19:11:15 +09:00
using BTCPayServer.Client;
using BTCPayServer.Filters;
using BTCPayServer.Models;
using BTCPayServer.Security;
2017-10-20 14:06:37 -05:00
using BTCPayServer.Services.Invoices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2017-09-13 15:47:34 +09:00
namespace BTCPayServer.Controllers
{
[BitpayAPIConstraint]
[Authorize(Policies.CanCreateInvoice, AuthenticationSchemes = AuthenticationSchemes.Bitpay)]
public class InvoiceControllerAPI : Controller
2017-09-13 15:47:34 +09:00
{
private readonly InvoiceController _InvoiceController;
private readonly InvoiceRepository _InvoiceRepository;
2017-10-13 17:46:19 +09:00
public InvoiceControllerAPI(InvoiceController invoiceController,
2019-12-27 14:32:43 -05:00
InvoiceRepository invoiceRepository)
{
_InvoiceController = invoiceController;
2019-12-27 14:32:43 -05:00
_InvoiceRepository = invoiceRepository;
}
2017-10-13 17:46:19 +09:00
[HttpPost]
[Route("invoices")]
[MediaTypeConstraint("application/json")]
public async Task<DataWrapper<InvoiceResponse>> CreateInvoice([FromBody] BitpayCreateInvoiceRequest invoice, CancellationToken cancellationToken)
{
if (invoice == null)
throw new BitpayHttpException(400, "Invalid invoice");
return await _InvoiceController.CreateInvoiceCore(invoice, HttpContext.GetStoreData(), HttpContext.Request.GetAbsoluteRoot(), cancellationToken: cancellationToken);
}
2017-09-13 15:47:34 +09:00
[HttpGet]
[Route("invoices/{id}")]
public async Task<DataWrapper<InvoiceResponse>> GetInvoice(string id)
{
var invoice = (await _InvoiceRepository.GetInvoices(new InvoiceQuery()
{
2020-06-28 17:55:27 +09:00
InvoiceId = new[] { id },
StoreId = new[] { HttpContext.GetStoreData().Id }
})).FirstOrDefault();
if (invoice == null)
throw new BitpayHttpException(404, "Object not found");
return new DataWrapper<InvoiceResponse>(invoice.EntityToDTO());
}
[HttpGet]
[Route("invoices")]
2020-11-13 16:28:15 +09:00
public async Task<IActionResult> GetInvoices(
string token,
DateTimeOffset? dateStart = null,
DateTimeOffset? dateEnd = null,
string orderId = null,
string itemCode = null,
string status = null,
int? limit = null,
int? offset = null)
{
2020-11-13 16:28:15 +09:00
if (User.Identity.AuthenticationType == Security.Bitpay.BitpayAuthenticationTypes.Anonymous)
return Forbid(Security.Bitpay.BitpayAuthenticationTypes.Anonymous);
if (dateEnd != null)
dateEnd = dateEnd.Value + TimeSpan.FromDays(1); //Should include the end day
2019-01-05 09:49:06 +01:00
var query = new InvoiceQuery()
{
Take = 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 },
Status = status == null ? null : new[] { status },
StoreId = new[] { this.HttpContext.GetStoreData().Id }
};
2017-09-13 15:47:34 +09:00
var entities = (await _InvoiceRepository.GetInvoices(query))
.Select((o) => o.EntityToDTO()).ToArray();
2017-09-13 15:47:34 +09:00
2020-11-13 16:28:15 +09:00
return Json(DataWrapper.Create(entities));
}
}
2017-09-13 15:47:34 +09:00
}