Merge branch 'feature/editable-payment-methods' of https://github.com/ttam/Wallos into ttam-feature/editable-payment-methods

This commit is contained in:
ellite 2023-11-16 18:26:28 +01:00
commit c8918a9663
7 changed files with 113 additions and 4 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
/** @var \SQLite3 $db */ /** @var \SQLite3 $db */
require_once 'includes/connect_endpoint_crontabs.php'; require_once 'includes/connect.php';
$completedMigrations = []; $completedMigrations = [];

View 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"
]));

View File

@ -17,8 +17,9 @@
} }
$payment_methods = array(); $payment_methods = array();
$query = "SELECT * FROM payment_methods"; $query = $db->prepare("SELECT * FROM payment_methods WHERE enabled=:enabled");
$result = $db->query($query); $query->bindValue(':enabled', 1, SQLITE3_INTEGER);
$result = $query->execute();
while ($row = $result->fetchArray(SQLITE3_ASSOC)) { while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$payment_methodId = $row['id']; $payment_methodId = $row['id'];
$payment_methods[$payment_methodId] = $row; $payment_methods[$payment_methodId] = $row;

12
migrations/000002.php Normal file
View File

@ -0,0 +1,12 @@
<?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 */
$columnQuery = $db->query("SELECT * FROM pragma_table_info('payment_methods') where name='enabled'");
$columnRequired = $columnQuery->fetchArray(SQLITE3_ASSOC) === false;
if ($columnRequired) {
$db->exec('ALTER TABLE payment_methods ADD COLUMN enabled BOOLEAN DEFAULT 1');
$db->exec('UPDATE payment_methods SET enabled = 1');
}

View File

@ -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.addEventListener('DOMContentLoaded', function() {
document.getElementById("userForm").addEventListener("submit", function(event) { document.getElementById("userForm").addEventListener("submit", function(event) {

View File

@ -479,9 +479,21 @@
</header> </header>
<div class="payments-list"> <div class="payments-list">
<?php <?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) { 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" /> <img src="images/uploads/icons/<?= $payment['icon'] ?>" alt="Logo" />
<span class="payment-name"> <span class="payment-name">
<?= $payment['name'] ?> <?= $payment['name'] ?>

View File

@ -509,6 +509,7 @@ main > .contain {
} }
.payments-list .payments-payment { .payments-list .payments-payment {
cursor: pointer;
display: flex; display: flex;
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
@ -516,6 +517,15 @@ main > .contain {
background-color: #8FBFFA; background-color: #8FBFFA;
padding: 6px 12px; padding: 6px 12px;
border-radius: 8px; 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 { .payments-list .payments-payment > img {