Add ability to toggle payment methods
This commit is contained in:
parent
602387df58
commit
ca7e908af1
45
endpoints/payments/payment.php
Normal file
45
endpoints/payments/payment.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => "Your session expired. Please login again"
|
||||
]));
|
||||
}
|
||||
|
||||
if (!isset($_GET['paymentId']) || !isset($_GET['enabled'])) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => "Some fields are missing."
|
||||
]));
|
||||
}
|
||||
|
||||
$paymentId = $_GET['paymentId'];
|
||||
|
||||
$inUse = $db->querySingle('SELECT COUNT(*) as count FROM subscriptions WHERE payment_method_id=' . $paymentId) === 1;
|
||||
if ($inUse) {
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => "Can't delete used payment method"
|
||||
]));
|
||||
}
|
||||
|
||||
$enabled = $_GET['enabled'];
|
||||
|
||||
$sqlUpdate = 'UPDATE payment_methods SET enabled=:enabled WHERE id=:id';
|
||||
$stmtUpdate = $db->prepare($sqlUpdate);
|
||||
$stmtUpdate->bindParam(':enabled', $enabled);
|
||||
$stmtUpdate->bindParam(':id', $paymentId);
|
||||
$resultUpdate = $stmtUpdate->execute();
|
||||
|
||||
if ($resultUpdate) {
|
||||
die(json_encode([
|
||||
"success" => true
|
||||
]));
|
||||
}
|
||||
|
||||
die(json_encode([
|
||||
"success" => false,
|
||||
"message" => "Failed to update payment method in the database"
|
||||
]));
|
||||
@ -17,8 +17,9 @@
|
||||
}
|
||||
|
||||
$payment_methods = array();
|
||||
$query = "SELECT * FROM payment_methods";
|
||||
$result = $db->query($query);
|
||||
$query = $db->prepare("SELECT * FROM payment_methods WHERE enabled=:enabled");
|
||||
$query->bindValue(':enabled', 1, SQLITE3_INTEGER);
|
||||
$result = $query->execute();
|
||||
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$payment_methodId = $row['id'];
|
||||
$payment_methods[$payment_methodId] = $row;
|
||||
|
||||
7
migrations/000002.php
Normal file
7
migrations/000002.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
// This migration adds an "enabled" column to the payment_methods table and sets all values to 1.
|
||||
// It allows the user to disable payment methods without deleting them.
|
||||
|
||||
/** @noinspection PhpUndefinedVariableInspection */
|
||||
$db->exec('ALTER TABLE payment_methods ADD COLUMN enabled BOOLEAN DEFAULT 1');
|
||||
$db->exec('UPDATE payment_methods SET enabled = 1');
|
||||
@ -411,6 +411,35 @@ function editCurrency(currencyId) {
|
||||
}
|
||||
}
|
||||
|
||||
function togglePayment(paymentId) {
|
||||
const element = document.querySelector(`div[data-paymentid="${paymentId}"]`);
|
||||
|
||||
if (element.dataset.inUse === 'yes') {
|
||||
return showErrorMessage('Can\'t delete used payment method');
|
||||
}
|
||||
|
||||
const newEnabledState = element.dataset.enabled === '1' ? '0' : '1';
|
||||
const paymentMethodName = element.querySelector('.payment-name').innerText;
|
||||
|
||||
const url = `endpoints/payments/payment.php?action=toggle&paymentId=${paymentId}&enabled=${newEnabledState}`;
|
||||
|
||||
fetch(url).then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error("There was an error saving the payments method");
|
||||
}
|
||||
return response.json();
|
||||
}).then(data => {
|
||||
if (data.success) {
|
||||
element.dataset.enabled = newEnabledState;
|
||||
showSuccessMessage(`${paymentMethodName} was saved`);
|
||||
} else {
|
||||
showErrorMessage(data.message || "Failed to save payments method");
|
||||
}
|
||||
}).catch(error => {
|
||||
showErrorMessage(error.message || "There was an error saving the payments method");
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
|
||||
document.getElementById("userForm").addEventListener("submit", function(event) {
|
||||
|
||||
14
settings.php
14
settings.php
@ -486,9 +486,21 @@
|
||||
</header>
|
||||
<div class="payments-list">
|
||||
<?php
|
||||
$paymentsInUseQuery = $db->query('SELECT id FROM payment_methods WHERE id IN (SELECT DISTINCT payment_method_id FROM subscriptions)');
|
||||
$paymentsInUse = [];
|
||||
while ($row = $paymentsInUseQuery->fetchArray(SQLITE3_ASSOC)) {
|
||||
$paymentsInUse[] = $row['id'];
|
||||
}
|
||||
|
||||
foreach ($payments as $payment) {
|
||||
$inUse = in_array($payment['id'], $paymentsInUse);
|
||||
?>
|
||||
<div class="payments-payment">
|
||||
<div class="payments-payment"
|
||||
data-enabled="<?= $payment['enabled']; ?>"
|
||||
data-in-use="<?= $inUse ? 'yes' : 'no' ?>"
|
||||
data-paymentid="<?= $payment['id'] ?>"
|
||||
title="<?= $inUse ? 'Can\'t delete used payment method' : '' ?>"
|
||||
onClick="togglePayment(<?= $payment['id'] ?>)">
|
||||
<img src="images/uploads/icons/<?= $payment['icon'] ?>" alt="Logo" />
|
||||
<span class="payment-name">
|
||||
<?= $payment['name'] ?>
|
||||
|
||||
@ -478,6 +478,7 @@ main > .contain {
|
||||
}
|
||||
|
||||
.payments-list .payments-payment {
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
@ -485,6 +486,15 @@ main > .contain {
|
||||
background-color: #8FBFFA;
|
||||
padding: 6px 12px;
|
||||
border-radius: 8px;
|
||||
transition: filter 300ms;
|
||||
}
|
||||
|
||||
.payments-list .payments-payment[data-enabled="0"] {
|
||||
filter: grayscale(100%);
|
||||
}
|
||||
|
||||
.payments-list .payments-payment[data-in-use="yes"] {
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.payments-list .payments-payment > img {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user