Add Stripe payment capture functionality
Introduced methods to handle Stripe payment capture and updated related classes and endpoints. Refactored constants for payment processors and integrated order collections to reflect Stripe payments. Ensured robust error handling and session validation for payment operations.
This commit is contained in:
@@ -403,7 +403,10 @@ class ordersRoute
|
||||
$stripe = new stripe();
|
||||
try {
|
||||
$payment_intent = $stripe->payment_intents->get(
|
||||
$stripe_payment_intents->payment_intent_id->value()
|
||||
$stripe_payment_intents->payment_intent_id->value(),
|
||||
[
|
||||
//'expand' => ['latest_charge'], // This is used to get the latest charge, that can be used to check if the payment has been refunded.
|
||||
]
|
||||
);
|
||||
} catch (\Stripe\Exception\InvalidRequestException $e) {
|
||||
$response->error('Payment intent not found', 400);
|
||||
@@ -465,6 +468,66 @@ class ordersRoute
|
||||
}
|
||||
});
|
||||
|
||||
$this->post('/orders/module/stripe/payment_intent/capture', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('confirm_payment_intent');
|
||||
// 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);
|
||||
}
|
||||
// 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);
|
||||
// Confirm the payment intent
|
||||
$stripe = new stripe();
|
||||
try {
|
||||
$paymentIntent = $stripe->payment_intents->capture(
|
||||
$stripe_payment_intents->payment_intent_id->value(),
|
||||
[] // Since we are capturing the payment, we don't need to pass any data
|
||||
);
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', $order->department_id->value(), 1, $user->id, 'CONFIRM_PAYMENT_INTENT', 'Successfully confirmed a payment intent (ID: ' . $data['id'] . ')');
|
||||
// Check if the payment intent was successful
|
||||
if ($paymentIntent->status !== 'succeeded') {
|
||||
$response->error('Payment intent not successful', 400);
|
||||
} else {
|
||||
// Update the order collection to reflect the payment
|
||||
$order_collection = $order->getOrderCollection();
|
||||
$order_collection->paidWithStripe($paymentIntent->id);
|
||||
}
|
||||
// Return a success message
|
||||
$response->success($paymentIntent->toJSON());
|
||||
} catch (\Stripe\Exception\InvalidRequestException $e) {
|
||||
$response->error('Payment intent not found', 400);
|
||||
}
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', 'global', 1, 0, 'CONFIRM_PAYMENT_INTENT', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'confirm_payment_intent' => 'Confirm a payment intent'
|
||||
]
|
||||
);
|
||||
|
||||
$this->post('/orders/module/stripe/debug/simulate_payment', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
|
||||
Reference in New Issue
Block a user