401 invalid_signature * replay -> 200 {status: duplicate} * safe mode check -> 409 site_identity_mismatch * payload validation -> 400 invalid_payload * order not found -> 202 {status: not_found} * note (+ complete) -> 200 {status: applied, woo_status} * * The event id is remembered only after the note is written, so a 202 for an * order that does not exist yet can succeed on a retry. * * @package Aha_Is_Woocommerce */ defined( 'ABSPATH' ) || exit; /** * Handles order status requests. */ final class Aha_Is_Woocommerce_Order_Status_Controller { const SCHEMA_FILE = 'order-status.request.schema.json'; /** Woo statuses that DELIVERED does not change. */ const FINAL_STATUSES = array( 'completed', 'cancelled', 'refunded', 'failed' ); /** * Handle the request. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function handle( $request ) { $verdict = Aha_Is_Woocommerce_Rest_Auth::verify_request( $request ); if ( ! $verdict['ok'] ) { return Aha_Is_Woocommerce_Rest_Response::invalid_signature(); } if ( Aha_Is_Woocommerce_Rest_Auth::is_replay( $verdict['event_id'] ) ) { return new WP_REST_Response( array( 'status' => 'duplicate' ), 200 ); } if ( Aha_Is_Woocommerce_Site_Identity::is_safe_mode() ) { $reason = Aha_Is_Woocommerce_Site_Identity::safe_mode_reason(); Aha_Is_Woocommerce_Logger::warning( 'order_status_rejected_safe_mode', array( 'reason' => $reason ) ); return Aha_Is_Woocommerce_Rest_Response::site_identity_mismatch( $reason ); } $raw_body = (string) $request->get_body(); $as_object = json_decode( $raw_body ); $schema = Aha_Is_Woocommerce_Contract::schema( self::SCHEMA_FILE ); if ( ! $schema ) { return Aha_Is_Woocommerce_Rest_Response::error( 'invalid_payload', 'The plugin could not load its contract schema.', 400 ); } if ( ! $schema->is_valid( $as_object ) ) { $error = ( null === $as_object && JSON_ERROR_NONE !== json_last_error() ) ? 'The body is not valid JSON.' : $schema->get_first_error(); return Aha_Is_Woocommerce_Rest_Response::error( 'invalid_payload', $error, 400 ); } $data = json_decode( $raw_body, true ); $order = $this->find_order( (string) $data['aha_order_id'] ); if ( ! $order ) { Aha_Is_Woocommerce_Logger::info( 'order_status_order_not_found', array( 'aha_order_id' => $data['aha_order_id'], 'status' => $data['status'] ) ); return new WP_REST_Response( array( 'status' => 'not_found' ), 202 ); } Aha_Is_Woocommerce_Echo_Guard::suppress(); try { /* translators: %s: Aha delivery status label. */ $order->add_order_note( sprintf( __( 'Aha delivery status: %s', 'aha-is-for-woocommerce' ), self::label( $data['status'] ) ), 0, false ); if ( 'DELIVERED' === $data['status'] && ! in_array( $order->get_status(), self::FINAL_STATUSES, true ) ) { $order->set_status( 'completed' ); $order->save(); } } finally { Aha_Is_Woocommerce_Echo_Guard::release(); } Aha_Is_Woocommerce_Rest_Auth::remember_event( $verdict['event_id'] ); Aha_Is_Woocommerce_Logger::info( 'order_status_applied', array( 'aha_order_id' => $data['aha_order_id'], 'status' => $data['status'], 'woo_status' => $order->get_status(), ) ); return new WP_REST_Response( array( 'status' => 'applied', 'woo_status' => $order->get_status() ), 200 ); } /** * Translated label of an Aha delivery status. * * @param string $status Status from the schema enum. * @return string */ public static function label( $status ) { switch ( $status ) { case 'RECEIVED': return __( 'Received', 'aha-is-for-woocommerce' ); case 'PREPARING': return __( 'Preparing', 'aha-is-for-woocommerce' ); case 'PICKUP_READY': return __( 'Ready for pickup', 'aha-is-for-woocommerce' ); case 'EN_ROUTE': return __( 'On the way', 'aha-is-for-woocommerce' ); case 'DELIVERED': return __( 'Delivered', 'aha-is-for-woocommerce' ); } return (string) $status; } /** * The Woo order for an aha_order_id: the order map first, then the order meta. * * @param string $aha_is_woocommerce_order_id Aha order id. * @return WC_Order|null */ private function find_order( $aha_is_woocommerce_order_id ) { $row = Aha_Is_Woocommerce_Order_Map::get( $aha_is_woocommerce_order_id ); $order_id = ( $row && $row['wc_order_id'] ) ? (int) $row['wc_order_id'] : Aha_Is_Woocommerce_Order_Map::find_order_id_by_meta( $aha_is_woocommerce_order_id ); $order = $order_id > 0 ? wc_get_order( $order_id ) : false; return $order ? $order : null; } }