Add Stripe integration for customers, payments, and products

This commit introduces a comprehensive Stripe module with support for managing customers, products, prices, and payment links. It also includes endpoint routes, helpers, and database updates to support Stripe operations, such as creating and retrieving entities or handling payment workflows. Additionally, a stripe module was integrated into existing routes and objects to enable seamless interaction with Stripe APIs.
This commit is contained in:
Jepp9350
2025-02-18 18:31:23 +01:00
parent 20841858ac
commit 410ca84bf1
20 changed files with 1114 additions and 55 deletions
@@ -0,0 +1,105 @@
<?php
namespace routes;
use classes\authentication;
use classes\response;
use classes\router;
use classes\stripe;
use objects\logs_o;
use objects\orders_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 > 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 > 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 > 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');
$result = (new stripe())->payment_link->generate(
(int)self::fromRequest('order_id'),
(string)self::fromRequest('email')
);
// Set the order stripe invoice details
$order->setStripeInvoicing($result->id, (string)self::fromRequest('email'), $result->url);
// Return the result
$response->success((object)$result);
} 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);
}
});
}
}