Order_Endpoint::handle_recheck_order( string $order_id, WP_Post $order )

Gets the Order object again, in another request, to check for purchases possibly denied after creation.


Parameters

$order_id

(string) (Required) The PayPal order ID.

$order

(WP_Post) (Required) The TC Order object.


Top ↑

Return

(bool|WP_Error|WP_REST_Response)


Top ↑

Source

File: src/Tickets/Commerce/Gateways/PayPal/REST/Order_Endpoint.php

	public function handle_recheck_order( $order_id, $order ) {

		$paypal_order_response       = tribe( Client::class )->get_order( $order_id );
		$paypal_order_status         = Arr::get( $paypal_order_response, [ 'status' ] );
		$paypal_order_purchase_units = Arr::get( $paypal_order_response, [ 'purchase_units' ], [] );
		$paypal_order_captures       = [];
		$messages                    = $this->get_error_messages();

		foreach( $paypal_order_purchase_units as $unit ) {
			if ( ! empty( $unit['payments']['captures'] ) ) {
				$paypal_order_captures[] = $unit['payments']['captures'];
			}
		}

		if ( Status::CREATED === $paypal_order_status && ! empty( $paypal_order_captures ) ) {
			$paypal_order_captures = array_shift( $paypal_order_captures );
			if ( count( $paypal_order_captures ) > 1 ) {
				// Sort the captures array by the update timestamp
				usort( $paypal_order_captures, function( $a, $b ) {
					return strtotime( $a['update_time'] ) <=> strtotime( $b['update_time'] );
				} );
			}

			foreach( $paypal_order_captures as $capture ) {
				$paypal_order_status = $capture['status'];
				$final = $capture['final_capture'] ?? false;

				if ( $final ) {
					break;
				}
			}
		}

		$status = tribe( Status::class )->convert_to_commerce_status( $paypal_order_status );

		if ( ! $status ) {
			return new WP_Error( 'tec-tc-gateway-paypal-invalid-capture-status', $messages['invalid-capture-status'], $paypal_order_response );
		}

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

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

		if ( in_array( $paypal_order_status, [ Status::FAILED, Status::DECLINED ], true ) ) {
			return new WP_Error( 'tec-tc-gateway-paypal-capture-declined', $messages['capture-declined'], $paypal_order_response );
		}

		$response['success']  = true;
		$response['status']   = $status->get_slug();
		$response['order_id'] = $order->ID;

		// When we have success we clear the cart.
		tribe( Cart::class )->clear_cart();

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

		return new WP_REST_Response( $response );
	}

Top ↑

Changelog

Changelog
Version Description
5.4.0.2 Introduced.