Add Stripe terminal and department variables API endpoints

This update introduces new endpoints for managing Stripe terminal readers, locations, and department-specific configurations. It also adds support for creating, updating, and retrieving department variables along with enhanced validation, logging, and permission checks. These updates improve integration and expand functionality for Stripe and department-related operations.
This commit is contained in:
Jepp9350
2025-02-21 14:43:33 +01:00
parent ff12fbcbd0
commit d2c1cdda9a
9 changed files with 670 additions and 37 deletions
+135 -6
View File
@@ -7,6 +7,7 @@ use classes\email;
use classes\response;
use classes\router;
use classes\stripe;
use objects\departments_o;
use objects\logs_o;
use objects\orders_o;
use objects\stripe_module_customers_o;
@@ -139,22 +140,150 @@ class moduleStripeRoute
'modules_stripe_invoice_send' => 'Send invoice'
]
);
self::get('/modules/stripe/terminals', function () {
self::get('/modules/stripe/terminal/readers', function () {
global $response;
self::requirePermission('modules_stripe_terminals_list');
self::requirePermission('modules_stripe_terminal_readers_list');
$user = (new authentication())->get_user();
if ($user) {
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the terminals list');
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the readers list');
$result = (new stripe())->readers->list();
$response->success((object)$result);
} else {
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the terminals list without a valid session');
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the readers list without a valid session');
$response->error('Invalid session', 400);
}
},
[
'modules_stripe_terminals_list' => 'List all Stripe payment terminals'
'modules_stripe_terminal_readers_list' => 'List all Stripe terminal readers'
]
);
self::get('/modules/stripe/terminal/locations', function () {
global $response;
self::requirePermission('modules_stripe_terminal_locations_list');
$user = (new authentication())->get_user();
if ($user) {
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the terminal locations list');
$result = (new stripe())->readers->listLocations();
$response->success((object)$result);
} else {
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the terminal locations list without a valid session');
$response->error('Invalid session', 400);
}
},
[
'modules_stripe_terminal_locations_list' => 'List all Stripe terminal locations'
]
);
self::get('/modules/stripe/department/terminal/location',
function () {
global $response;
self::requirePermission('modules_stripe_department_terminal_location_list');
self::requireParameters(['id']);
self::requireType((int)self::fromRequest('id'), self::TYPE_INT());
// Require the id to be above 0
if ((int)self::fromRequest('id') < 1) {
$response->error('Invalid department ID', 400);
}
$user = (new authentication())->get_user();
if ($user) {
// Make sure the user has access to the department
self::requireDepartmentAccess((int)self::fromRequest('id'));
// Make sure the department exists
$department = (new departments_o())->select((int)self::fromRequest('id'));
$department->requireSelected();
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the department terminal location');
// Get the department terminal location
$isSet = $department->isStripeTerminalLocationSet();
// Return the result if the department terminal location is set, otherwise return null
$response->success([
'id' => (
$isSet
? $department->getStripeTerminalLocation()
: null
)
]);
} else {
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the department terminal location without a valid session');
$response->error('Invalid session', 400);
}
},
[
'modules_stripe_department_terminal_location_list' => 'List all department terminal location',
'department_access_:id' => 'Access department. - :id'
]
);
self::post('/modules/stripe/department/terminal/location',
function () {
global $response;
self::requirePermission('modules_stripe_department_terminal_location_set');
self::requireParameters(['id', 'location']);
self::requireType((int)self::fromRequest('id'), self::TYPE_INT());
self::requireType((string)self::fromRequest('location'), 'string');
self::requireMinLength('location', 1);
self::requireMaxLength('location', 255);
// Require the id to be above 0
if ((int)self::fromRequest('id') < 1) {
$response->error('Invalid department ID', 400);
}
$user = (new authentication())->get_user();
if ($user) {
// Make sure the user has access to the department
self::requireDepartmentAccess((int)self::fromRequest('id'));
// Make sure the department exists
$department = (new departments_o())->select((int)self::fromRequest('id'));
$department->requireSelected();
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User set the department terminal location');
// Set the department terminal location
$department->setStripeTerminalLocation((string)self::fromRequest('location'));
// Return the result
$response->success([
'id' => $department->getStripeTerminalLocation()
]);
} else {
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to set the department terminal location without a valid session');
$response->error('Invalid session', 400);
}
},
[
'modules_stripe_department_terminal_location_set' => 'Set department terminal location',
'department_access_:id' => 'Access department. - :id'
]
);
self::get('/modules/stripe/department/terminal/readers',
function () {
global $response;
self::requirePermission('modules_stripe_department_terminal_readers_list');
self::requireParameters(['id']);
self::requireType((int)self::fromRequest('id'), self::TYPE_INT());
// Require the id to be above 0
if ((int)self::fromRequest('id') < 1) {
$response->error('Invalid department ID', 400);
}
$user = (new authentication())->get_user();
if ($user) {
// Make sure the user has access to the department
self::requireDepartmentAccess((int)self::fromRequest('id'));
// Make sure the department exists
$department = (new departments_o())->select((int)self::fromRequest('id'));
$department->requireSelected();
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the department terminal readers');
// Get the department terminal readers
$readers = $department->getStripeTerminalReaders();
// Return the result
$response->success($readers);
} else {
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the department terminal readers without a valid session');
$response->error('Invalid session', 400);
}
},
[
'modules_stripe_department_terminal_readers_list' => 'List all department terminal readers',
'department_access_:id' => 'Access department. - :id'
]
);
}