mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 06:24:24 +01:00
Simplifying implementation of status switch
This commit is contained in:
@@ -465,48 +465,31 @@ namespace BTCPayServer.Controllers
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
[HttpPost("{payReqId}/changestate/{newState}")]
|
||||
[HttpPost("{payReqId}/complete")]
|
||||
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyPaymentRequests)]
|
||||
public async Task<IActionResult> ChangePaymentRequestState(string payReqId, string newState)
|
||||
public async Task<IActionResult> TogglePaymentRequestCompleted(string payReqId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(payReqId) || string.IsNullOrWhiteSpace(newState))
|
||||
if (string.IsNullOrWhiteSpace(payReqId))
|
||||
{
|
||||
return BadRequest("Invalid parameters");
|
||||
}
|
||||
|
||||
var paymentRequest = await _PaymentRequestRepository.FindPaymentRequest(payReqId, GetUserId());
|
||||
var model = new PaymentRequestStateChangeModel();
|
||||
if (paymentRequest == null)
|
||||
{
|
||||
model.NotFound = true;
|
||||
return NotFound(model);
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (newState == "completed")
|
||||
if (paymentRequest.Status != PaymentRequestStatus.Pending)
|
||||
{
|
||||
await _PaymentRequestRepository.UpdatePaymentRequestStatus(payReqId, PaymentRequestStatus.Completed);
|
||||
model.StatusString = "Settled";
|
||||
}
|
||||
else if (newState == "expired")
|
||||
{
|
||||
await _PaymentRequestRepository.UpdatePaymentRequestStatus(payReqId, PaymentRequestStatus.Expired);
|
||||
model.StatusString = "Expired";
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest($"Invalid state: {newState}");
|
||||
return BadRequest("Invalid payment request status. Only pending payment requests can be marked as completed.");
|
||||
}
|
||||
|
||||
return Json(model);
|
||||
await _PaymentRequestRepository.UpdatePaymentRequestStatus(payReqId, PaymentRequestStatus.Completed);
|
||||
|
||||
return RedirectToAction("GetPaymentRequests", new { storeId = paymentRequest.StoreDataId });
|
||||
}
|
||||
|
||||
public class PaymentRequestStateChangeModel
|
||||
{
|
||||
public bool NotFound { get; set; }
|
||||
public string? StatusString { get; set; }
|
||||
}
|
||||
|
||||
|
||||
private string GetUserId() => _UserManager.GetUserId(User);
|
||||
|
||||
private StoreData GetCurrentStore() => HttpContext.GetStoreData();
|
||||
|
||||
@@ -121,7 +121,9 @@
|
||||
@item.Status
|
||||
</span>
|
||||
<div class="dropdown-menu">
|
||||
<button type="button" class="dropdown-item lh-base" data-payment-request-id="@item.Id" data-new-state="completed">Mark as settled</button>
|
||||
<form asp-action="TogglePaymentRequestCompleted" asp-route-payReqId="@item.Id" method="post">
|
||||
<button type="submit" class="dropdown-item lh-base">Mark as settled</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
@@ -168,7 +170,3 @@ else
|
||||
There are no payment requests matching your criteria.
|
||||
</p>
|
||||
}
|
||||
|
||||
<script>
|
||||
window.storeId = '@storeId';
|
||||
</script>
|
||||
@@ -277,23 +277,6 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
}
|
||||
})
|
||||
|
||||
// Payment Request Status
|
||||
delegate('click', '[data-payment-request-state-badge] [data-payment-request-id][data-new-state]', async e => {
|
||||
const $button = e.target
|
||||
const $badge = $button.closest('[data-payment-request-state-badge]')
|
||||
const { paymentRequestId, newState } = $button.dataset
|
||||
|
||||
$badge.classList.add('pe-none'); // disable further interaction
|
||||
const response = await fetch(`${baseUrl}/payment-requests/${paymentRequestId}/changestate/${newState}`, { method: 'POST' })
|
||||
if (response.ok) {
|
||||
const { statusString } = await response.json()
|
||||
$badge.outerHTML = `<div class="badge badge-${newState}" data-payment-request-state-badge="${paymentRequestId}">${statusString}</div>`
|
||||
} else {
|
||||
$badge.classList.remove('pe-none');
|
||||
alert("Payment request state update failed");
|
||||
}
|
||||
})
|
||||
|
||||
// Time Format
|
||||
delegate('click', '.switch-time-format', switchTimeFormat);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user