Skip to content

Commit

Permalink
Introduce lock order mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
dpanta94 committed Jan 9, 2025
1 parent ba40bca commit f1f85d0
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 11 deletions.
34 changes: 24 additions & 10 deletions src/Tickets/Commerce/Gateways/Stripe/REST/Order_Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,16 @@ public function handle_create_order( WP_REST_Request $request ) {
'success' => false,
];

$orders = tribe( Order::class );
$messages = $this->get_error_messages();
$data = $request->get_json_params();
$purchaser = tribe( Order::class )->get_purchaser_data( $data );
$purchaser = $orders->get_purchaser_data( $data );

if ( is_wp_error( $purchaser ) ) {
return $purchaser;
}

$order = tribe( Order::class )->create_from_cart( tribe( Gateway::class ), $purchaser );
$order = $orders->create_from_cart( tribe( Gateway::class ), $purchaser );

$payment_intent = tribe( Payment_Intent_Handler::class )->update_payment_intent( $data, $order );

Expand All @@ -129,10 +130,14 @@ public function handle_create_order( WP_REST_Request $request ) {
}

// Orders need to pass the Pending status always.
$updated = tribe( Order::class )->modify_status( $order->ID, Pending::SLUG, [
'gateway_payload' => $payment_intent,
'gateway_order_id' => $payment_intent['id'],
] );
$updated = $orders->modify_status(
$order->ID,
Pending::SLUG,
[
'gateway_payload' => $payment_intent,
'gateway_order_id' => $payment_intent['id'],
]
);

if ( is_wp_error( $updated ) ) {
return $updated;
Expand Down Expand Up @@ -233,12 +238,19 @@ public function handle_update_order( WP_REST_Request $request ) {
return new WP_Error( 'tec-tc-gateway-stripe-invalid-payment-intent-status', $messages['invalid-payment-intent-status'], $payment_intent_status );
}

$updated = tribe( Order::class )->modify_status( $order->ID, $status->get_slug(), [
'gateway_payload' => $payment_intent,
'gateway_order_id' => $payment_intent['id'],
] );
$orders = tribe( Order::class );

$updated = $orders->modify_status(
$order->ID,
$status->get_slug(),
[
'gateway_payload' => $payment_intent,
'gateway_order_id' => $payment_intent['id'],
]
);

if ( is_wp_error( $updated ) ) {
$orders->checkout_completed( $order->ID );
return $updated;
}

Expand All @@ -253,6 +265,8 @@ public function handle_update_order( WP_REST_Request $request ) {

$response['redirect_url'] = add_query_arg( [ 'tc-order-id' => $gateway_order_id ], tribe( Success::class )->get_url() );

$orders->checkout_completed( $order->ID );

return new WP_REST_Response( $response );
}

Expand Down
3 changes: 2 additions & 1 deletion src/Tickets/Commerce/Gateways/Stripe/Webhooks/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public static function get_handler_method_for_event( $type ) {
* @return bool|WP_Error|null
*/
public static function update_order_status( \WP_Post $order, Commerce_Status\Status_Interface $status, array $metadata = [] ) {
// Lock checks and if locked => reschedule.
return tribe( Order::class )->modify_status( $order->ID, $status->get_slug(), $metadata );
}
}
}
Loading

0 comments on commit f1f85d0

Please sign in to comment.