Add Stripe payment intents support and product subscription flag
Introduced functionality for handling Stripe payment intents, including creation, retrieval, and cancellation. Added a subscription_allowed flag to products for enabling subscription-specific operations. Integrated the necessary backend endpoints, object property updates, and route handling logic.
This commit is contained in:
@@ -4,12 +4,14 @@ namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use classes\response;
|
||||
use classes\stripe;
|
||||
use objects\collected_order_invoices_o;
|
||||
use objects\departments_o;
|
||||
use objects\economic_module_orders;
|
||||
use objects\logs_o;
|
||||
use objects\orders_o;
|
||||
use objects\stripe_module_orders_o;
|
||||
use objects\stripe_payment_intents_o;
|
||||
use objects\users_o;
|
||||
use traits\route_t;
|
||||
|
||||
@@ -261,6 +263,207 @@ class ordersRoute
|
||||
'mark_order_as_completed' => 'Mark an order as completed'
|
||||
]
|
||||
);
|
||||
|
||||
$this->post('/orders/module/stripe/payment_intent', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('charge_order_stripe');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Get the post data
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
// Check if the required fields are set
|
||||
if (!isset($data['id'])) {
|
||||
$response->error('ID is required', 400);
|
||||
}
|
||||
// Get the current order
|
||||
$order = (new orders_o())->getOrderById((int)$data['id']);
|
||||
// Check if the order exists
|
||||
if (!$order->exists()) {
|
||||
$response->error('Order not found', 400);
|
||||
}
|
||||
// Get the department
|
||||
$department = (new departments_o())->selectId((int)$order->department_id->value());
|
||||
// Check if the department is configured for Stripe payments
|
||||
if (!$department->isStripeConfigured()) {
|
||||
$response->error('Department is not configured for Stripe payments', 400);
|
||||
}
|
||||
// Check if the reader is set
|
||||
if (!isset($data['reader'])) {
|
||||
$response->error('Reader ID is required', 400);
|
||||
}
|
||||
// Get the tax percentage (if any)
|
||||
$tax_percentage = (isset($data['tax_percentage'])) ? (int)$data['tax_percentage'] : null;
|
||||
// Check if the tax percentage is valid
|
||||
if ($tax_percentage !== null && ($tax_percentage < 0 || $tax_percentage > 100)) {
|
||||
$response->error('Invalid tax percentage', 400);
|
||||
}
|
||||
function addTaxNetAmount($net_amount, $tax_percentage): float
|
||||
{
|
||||
// Check if the tax percentage is above 0
|
||||
if (empty($tax_percentage) || $tax_percentage <= 0) {
|
||||
return $net_amount;
|
||||
}
|
||||
return $net_amount + ($net_amount * ($tax_percentage / 100));
|
||||
}
|
||||
|
||||
// Get the Stripe payment intent
|
||||
$stripe = new stripe();
|
||||
$paymentIntent = $stripe->payment_intents->create(
|
||||
addTaxNetAmount(
|
||||
(float)$order->getNetAmount() * 100,
|
||||
$tax_percentage ?? 0
|
||||
),
|
||||
[
|
||||
'description' => 'Order ID: ' . $order->id,
|
||||
'metadata' => [
|
||||
'order_id' => $order->id,
|
||||
'customer_id' => $order->customer_id->value(),
|
||||
'department_id' => $order->department_id->value(),
|
||||
'tax_percentage' => $tax_percentage ?? 0,
|
||||
],
|
||||
'payment_method_types' => ['card_present'],
|
||||
'capture_method' => 'manual',
|
||||
]
|
||||
);
|
||||
// Validate the payment intent
|
||||
try {
|
||||
$stripe->payment_intents->get($paymentIntent->id);
|
||||
} catch (\Stripe\Exception\InvalidRequestException $e) {
|
||||
$response->error('Payment intent not found', 400);
|
||||
}
|
||||
// Set the payment intent ID in the order
|
||||
$stripe_payment_intents = new stripe_payment_intents_o();
|
||||
$stripe_payment_intents->add(
|
||||
(int)$order->id,
|
||||
$paymentIntent->id,
|
||||
$paymentIntent->client_secret,
|
||||
$paymentIntent->toJSON()
|
||||
);
|
||||
|
||||
// Send the payment intent to the reader
|
||||
$stripe->readers->sendPaymentIntent(
|
||||
$data['reader'],
|
||||
$paymentIntent->id,
|
||||
);
|
||||
// Set the reader on the stripe payment intent
|
||||
$stripe_payment_intents->reader_id->set($data['reader']);
|
||||
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', $order->department_id->value(), 1, $user->id, 'CHARGE_ORDER', 'Successfully charged an order (ID: ' . $data['id'] . ')');
|
||||
|
||||
$response->success([
|
||||
'payment_intent' => $paymentIntent->id,
|
||||
'client_secret' => $paymentIntent->client_secret,
|
||||
]);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', 'global', 1, 0, 'CHARGE_ORDER', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'charge_order' => 'Charge an order'
|
||||
]
|
||||
);
|
||||
|
||||
$this->get('/orders/module/stripe/payment_intent', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('get_payment_intent');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Get the post data
|
||||
self::requireParameters([
|
||||
'id'
|
||||
]);
|
||||
// Check if the required fields are set
|
||||
$id = self::getParameter('id');
|
||||
if (!isset($id)) {
|
||||
$response->error('ID is required', 400);
|
||||
}
|
||||
// Get the current order
|
||||
$order = (new orders_o())->getOrderById((int)$id);
|
||||
// Check if the order exists
|
||||
if (!$order->exists()) {
|
||||
$response->error('Order not found', 400);
|
||||
}
|
||||
// Check if the order has a payment intent
|
||||
$stripe_payment_intents = new stripe_payment_intents_o();
|
||||
if (!$stripe_payment_intents->doesOrderHavePaymentIntent((int)$order->id)) {
|
||||
$response->error('Order does not have a payment intent', 400);
|
||||
}
|
||||
$stripe_payment_intents->selectOrderPaymentIntent((int)$order->id);
|
||||
// Get the Stripe payment intent
|
||||
$stripe = new stripe();
|
||||
try {
|
||||
$payment_intent = $stripe->payment_intents->get(
|
||||
$stripe_payment_intents->payment_intent_id->value()
|
||||
);
|
||||
} catch (\Stripe\Exception\InvalidRequestException $e) {
|
||||
$response->error('Payment intent not found', 400);
|
||||
}
|
||||
$response->success(
|
||||
$payment_intent->toJSON()
|
||||
);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', 'global', 1, 0, 'GET_PAYMENT_INTENT', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'get_payment_intent' => 'Get a payment intent'
|
||||
]
|
||||
);
|
||||
|
||||
$this->delete('/orders/module/stripe/payment_intent', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('delete_payment_intent');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Get the data
|
||||
self::requireParameters([
|
||||
'id'
|
||||
]);
|
||||
$id = self::fromRequest('id');
|
||||
// Get the current order
|
||||
$order = (new orders_o())->getOrderById((int)$id);
|
||||
// Check if the order exists
|
||||
if (!$order->exists()) {
|
||||
$response->error('Order not found', 400);
|
||||
}
|
||||
// Check if the order has a payment intent
|
||||
$stripe_payment_intents = new stripe_payment_intents_o();
|
||||
if (!$stripe_payment_intents->doesOrderHavePaymentIntent((int)$order->id)) {
|
||||
$response->error('Order does not have a payment intent', 400);
|
||||
}
|
||||
$stripe_payment_intents->selectOrderPaymentIntent((int)$order->id);
|
||||
try {
|
||||
$stripe_payment_intents->delete();
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', $order->department_id->value(), 1, $user->id, 'DELETE_PAYMENT_INTENT', 'Successfully deleted a payment intent (ID: ' . $id . ')');
|
||||
// Return a success message
|
||||
$response->success(['message' => 'Payment intent deleted successfully']);
|
||||
} catch (\Stripe\Exception\InvalidRequestException $e) {
|
||||
$response->error('Payment intent not found', 400);
|
||||
}
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', 'global', 1, 0, 'DELETE_PAYMENT_INTENT', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user