Add endpoint for booking collected invoices to Stripe

Introduce a new POST endpoint to handle adding collected order invoices to Stripe, with necessary validations and logging. Extend `collected_order_invoices_o` to include Stripe integration, fetching details of Stripe payment intents when applicable.
This commit is contained in:
Jepp9350
2025-04-22 12:01:55 +02:00
parent 60de6ae998
commit 26ccab3d3c
2 changed files with 58 additions and 2 deletions
@@ -5,7 +5,9 @@ namespace objects;
use classes\db;
use classes\economic;
use classes\object_property;
use classes\stripe;
use Exception;
use Stripe\Exception\ApiErrorException;
use traits\db_object_t;
const ECONOMIC_PROCESSOR = 1;
@@ -84,6 +86,9 @@ class collected_order_invoices_o extends db
$this->error_message = new object_property($this->table, $this->id, 'error_message', 'string', false);
}
/**
* @throws ApiErrorException If the payment method is Stripe and the request fails
*/
public function asArray(): array
{
$tmp = [
@@ -99,6 +104,7 @@ class collected_order_invoices_o extends db
'orders' => $this->getOrders(),
'economic_invoice_draft_id' => null, // This will be overwritten if the external id is set
'economic_invoice_booked_id' => null, // This will be overwritten if the external id is set
'stripe' => null, // This will be overwritten if the processor is Stripe
'total_net_amount' => self::getTotalAmount(),
'user' => (array)(new users_o())->getUserByCustomerNumber($this->customer_number->value())->asArray(),
//'debug' => (new economic())->invoices->draft->get((new economic())->invoices->draft->get_from_external_id($this->getExternalId())),
@@ -108,6 +114,17 @@ class collected_order_invoices_o extends db
$tmp['economic_invoice_draft_id'] = self::isDraftExisting() ? self::getInvoiceDraftId() : null;
$tmp['economic_invoice_booked_id'] = self::isBooked() ? self::getInvoiceBookedId() : null;
}
// If the processor is Stripe, get the stripe details
if ((int)$this->processor->value() === STRIPE_PROCESSOR || str_starts_with($this->external_id->value(), 'pi_')) {
// Get the stripe details
$stripe = new stripe();
$stripe_details = $stripe->payment_intents->get(
$this->external_id->value(),
[
'expand' => ['latest_charge', 'latest_charge.balance_transaction', 'latest_charge.payment_method_details.card.wallet']
]);
$tmp['stripe'] = $stripe_details;
}
return $tmp;
}
@@ -432,8 +449,8 @@ class collected_order_invoices_o extends db
if (empty($response->references->other)) {
throw new Exception('Invoice collection was not created successfully');
}
// Set the processor
$this->processor->set(1);
// Set the processor to E-conomic, if it's not already set to Stripe.
$this->processor->set(ECONOMIC_PROCESSOR);
// Object changed
self::objectChanged();
return $this;
@@ -226,6 +226,45 @@ class orderInvoicesRoute
]
);
/** Collected order invoices > Stripe > BOOK > POST */
$this->post('/collected-invoices/stripe/book', function () {
global $response;
self::requirePermission('add_collected_invoice_stripe');
$user = (new authentication())->get_user();
if ($user) {
(new logs_o())->add('orderInvoices', 'global', 1, $user->id, 'ADD_COLLECTED_INVOICE_STRIPE', 'User added a collected order invoice to Stripe');
// Require the ID, and validate its type and length
self::requireParameters(['id']);
self::requireType((int)self::getParameter('id'), self::type_int());
self::requireMinLength('id', 1);
self::requireMaxLength('id', 10);
// Require the ID to be above 0
self::requireMinValue((int)self::getParameter('id'), 1);
// Validate the ID against the database
$collected_order_invoices = (new collected_order_invoices_o())->select((int)self::getParameter('id'));
$collected_order_invoices->requireSelected();
// Require the external ID to be set
if ($collected_order_invoices->external_id->value() === null) {
$response->error('Transaction has not been created in Stripe', 400);
}
// Require the booked invoice ID to be not set
if ($collected_order_invoices->booked_invoice_id->value() !== null) {
$response->error('Invoice has already been booked', 400);
}
// Add the collected order invoice to Stripe
$collected_order_invoices->addToEconomic(true);
// Return the collected order invoice
$response->success($collected_order_invoices->asArray());
} else {
(new logs_o())->add('orderInvoices', 'global', 0, 0, 'ADD_COLLECTED_INVOICE_STRIPE', 'User tried to add a collected order invoice to Stripe without a valid session');
$response->error('Invalid session', 400);
}
},
[
'add_collected_invoice_stripe' => 'Add a collected order invoice to Stripe. This is a superuser-only route.'
]
);
/** Collected order invoices > Vehicle subscriptions > POST */
$this->post('/collected-invoices/vehicle-subscriptions', function () {
global $response;