Files
api/services/nginx/app/routes/moduleStripeRoute.php
T
Jepp9350 d28cb172a0 Add permission definitions to route handlers
This update introduces explicit permission definitions for various route handlers across multiple routes. These changes enhance clarity and allow for more granular control over route access based on defined permissions. The updates ensure better manageability and scalability of endpoint permissions.
2025-02-20 14:33:42 +01:00

143 lines
6.2 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use classes\email;
use classes\response;
use classes\router;
use classes\stripe;
use objects\logs_o;
use objects\orders_o;
use objects\stripe_module_customers_o;
use traits\route_t;
class moduleStripeRoute
{
use route_t;
public function run(): void
{
global /** @var response $response */
/** @var router $router */
$router, $response;
/** Modules > Stripe > Customers > List */
$this->get('/modules/stripe/customers', function () {
global $response;
self::requirePermission('modules_stripe_customers_list');
$user = (new authentication())->get_user();
if ($user) {
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the customers list');
$result = (new stripe())->customers->list();
$response->success((object)$result);
} else {
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the customers list without a valid session');
$response->error('Invalid session', 400);
}
},
[
'modules_stripe_customers_list' => 'List all customers'
]
);
/** Modules > Stripe > Products > List */
$this->get('/modules/stripe/products', function () {
global $response;
self::requirePermission('modules_stripe_products_list');
$user = (new authentication())->get_user();
if ($user) {
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the products list');
$result = (new stripe())->product->list();
$response->success((object)$result);
} else {
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the products list without a valid session');
$response->error('Invalid session', 400);
}
},
[
'modules_stripe_products_list' => 'List all products'
]
);
/** Modules > Stripe > Prices > List */
$this->get('/modules/stripe/prices', function () {
global $response;
self::requirePermission('modules_stripe_prices_list');
$user = (new authentication())->get_user();
if ($user) {
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the prices list');
$result = (new stripe())->prices->list();
$response->success((object)$result);
} else {
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the prices list without a valid session');
$response->error('Invalid session', 400);
}
},
[
'modules_stripe_prices_list' => 'List all prices'
]
);
/** Modules > Stripe > Send Invoice */
$this->post('/modules/stripe/invoice', function () {
global $response;
self::requirePermission('modules_stripe_invoice_send');
$user = (new authentication())->get_user();
if ($user) {
self::requireParameters(['email', 'order_id']);
self::requireType(self::fromRequest('email'), 'string');
self::requireType((int)self::fromRequest('order_id'), self::TYPE_INT());
self::requireMinLength('email', 4);
self::requireMaxLength('email', 255);
self::requireMinLength('order_id', 1);
self::requireMaxLength('order_id', 255);
// Check if the email is valid
if (!filter_var(self::fromRequest('email'), FILTER_VALIDATE_EMAIL)) {
$response->error('Invalid email', 400);
}
// Check if the order exists
$order = (new orders_o())->select((int)self::fromRequest('order_id'));
$order->requireSelected();
// Log the action
(new logs_o())->add('modules_stripe', 'global', 1, $user->id, 'MODULES_STRIPE', 'User sent an invoice');
// Create a customer account, if it doesn't exist
$hasCustomerAccount = (new stripe_module_customers_o())->doesCustomerHaveAccount((string)self::fromRequest('email'));
if (!$hasCustomerAccount) {
$customer = (new stripe())->customers->create((string)self::fromRequest('email'));
(new stripe_module_customers_o())->add((string)self::fromRequest('email'), $customer->id);
} else {
// Get the customer account by email
$customer = (new stripe())->customers->retrieve(
// Get the customer ID
(new stripe_module_customers_o())
->getCustomerId(
(string)self::fromRequest('email')
)
);
}
// Create an invoice
$invoice = (new stripe())->invoice->generate((int)self::fromRequest('order_id'), $customer->id);
// Set the order stripe invoice details
$order->setStripeInvoicing($invoice->id, $customer->id, $invoice->hosted_invoice_url);
$email = new email();
$email->sendStripeInvoiceEmail(
(string)self::fromRequest('email'),
null,
$invoice->hosted_invoice_url,
(int)self::fromRequest('order_id')
);
// Return the result
$response->success((object)$invoice);
} else {
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to send an invoice without a valid session');
$response->error('Invalid session', 400);
}
},
[
'modules_stripe_invoice_send' => 'Send invoice'
]
);
}
}