|null $input * @return array */ public static function cancel_order( $input = null ): array { if ( ! Agentmatica_Support_Settings::is_write_actions_enabled() ) { return self::refused( 'cancel', __( 'Order write actions are disabled on this store.', 'agentmatica-mcp-connector' ) ); } $verified = Agentmatica_WooCommerce_Order_Guards::require_verified_session( $input ); if ( isset( $verified['success'] ) && $verified['success'] === false ) { return self::normalize_failure( $verified ); } // Cancel and return need a logged-in store account (order number + email // is printed on packing slips and emails; app is sign-in only too). $login_required = Agentmatica_WooCommerce_Order_Guards::require_logged_in_session( $verified ); if ( $login_required !== null ) { return $login_required; } $input = is_array( $input ) ? $input : array(); if ( ! Agentmatica_WooCommerce_Order_Guards::confirmed( $input ) ) { return self::unconfirmed( 'cancel' ); } $order_number = sanitize_text_field( (string) ( $input['order_number'] ?? '' ) ); $reason = sanitize_text_field( (string) ( $input['reason'] ?? '' ) ); if ( $order_number === '' ) { return self::refused( 'cancel', __( 'order_number is required.', 'agentmatica-mcp-connector' ) ); } $order = Agentmatica_WooCommerce_Orders::find_order_by_number( $order_number ); if ( ! $order instanceof WC_Order ) { return self::refused( 'cancel', __( 'Order not found.', 'agentmatica-mcp-connector' ) ); } if ( ! Agentmatica_WooCommerce_Order_Guards::order_owned_by_session( $order, $verified ) ) { return self::refused( 'cancel', __( 'That order does not belong to this customer.', 'agentmatica-mcp-connector' ) ); } if ( (string) $order->get_status() === 'cancelled' ) { return self::success( 'cancel', __( 'This order was already cancelled.', 'agentmatica-mcp-connector' ), 'already_done', (int) $order->get_id() ); } $ineligible = Agentmatica_WooCommerce_Order_Guards::cancel_eligibility( $order ); if ( $ineligible !== null ) { return self::refused( 'cancel', $ineligible ); } $refund_mode = self::parse_refund_mode( $input ); $order->update_status( 'cancelled', $reason !== '' ? sprintf( /* translators: %s: visitor reason */ __( 'Cancelled via Agentmatica chat: %s', 'agentmatica-mcp-connector' ), $reason ) : __( 'Cancelled via Agentmatica chat.', 'agentmatica-mcp-connector' ) ); if ( 'original' === $refund_mode && function_exists( 'wc_create_refund' ) ) { $refund_amount = (float) $order->get_total(); if ( $refund_amount > 0 ) { // refund_payment sends the money back through the gateway. Without it // WooCommerce only records a refund and the shopper is never paid. $refund = wc_create_refund( array( 'amount' => $refund_amount, 'reason' => __( 'Order cancelled via Agentmatica chat.', 'agentmatica-mcp-connector' ), 'order_id' => (int) $order->get_id(), 'refund_payment' => true, ) ); if ( is_wp_error( $refund ) ) { $order->add_order_note( sprintf( /* translators: %s: gateway error message */ __( 'Agentmatica cancelled this order but the automatic refund failed (%s). Please refund the customer manually.', 'agentmatica-mcp-connector' ), $refund->get_error_message() ) ); } } } return self::success( 'cancel', __( 'Order cancelled successfully.', 'agentmatica-mcp-connector' ), 'success', (int) $order->get_id() ); } /** * @param array|null $input * @return array */ public static function update_shipping_address( $input = null ): array { if ( ! Agentmatica_Support_Settings::is_write_actions_enabled() ) { return self::refused( 'update_address', __( 'Order write actions are disabled on this store.', 'agentmatica-mcp-connector' ) ); } $verified = Agentmatica_WooCommerce_Order_Guards::require_verified_session( $input ); if ( isset( $verified['success'] ) && $verified['success'] === false ) { return self::normalize_failure( $verified ); } $login_required = Agentmatica_WooCommerce_Order_Guards::require_logged_in_session( $verified ); if ( $login_required !== null ) { return $login_required; } $input = is_array( $input ) ? $input : array(); if ( ! Agentmatica_WooCommerce_Order_Guards::confirmed( $input ) ) { return self::unconfirmed( 'update_address' ); } $order_number = sanitize_text_field( (string) ( $input['order_number'] ?? '' ) ); if ( $order_number === '' ) { return self::refused( 'update_address', __( 'order_number is required.', 'agentmatica-mcp-connector' ) ); } $order = Agentmatica_WooCommerce_Orders::find_order_by_number( $order_number ); if ( ! $order instanceof WC_Order ) { return self::refused( 'update_address', __( 'Order not found.', 'agentmatica-mcp-connector' ) ); } if ( ! Agentmatica_WooCommerce_Order_Guards::order_owned_by_session( $order, $verified ) ) { return self::refused( 'update_address', __( 'That order does not belong to this customer.', 'agentmatica-mcp-connector' ) ); } $ineligible = Agentmatica_WooCommerce_Order_Guards::address_update_eligibility( $order ); if ( $ineligible !== null ) { return self::refused( 'update_address', $ineligible ); } $address1 = sanitize_text_field( (string) ( $input['address1'] ?? '' ) ); $city = sanitize_text_field( (string) ( $input['city'] ?? '' ) ); $postcode = sanitize_text_field( (string) ( $input['zip'] ?? $input['postcode'] ?? '' ) ); if ( $address1 === '' || $city === '' || $postcode === '' ) { return self::refused( 'update_address', __( 'address1, city, and zip are required.', 'agentmatica-mcp-connector' ) ); } $new_address = array( 'first_name' => sanitize_text_field( (string) ( $input['first_name'] ?? $order->get_shipping_first_name() ) ), 'last_name' => sanitize_text_field( (string) ( $input['last_name'] ?? $order->get_shipping_last_name() ) ), 'address_1' => $address1, 'address_2' => sanitize_text_field( (string) ( $input['address2'] ?? '' ) ), 'city' => $city, 'postcode' => $postcode, 'state' => sanitize_text_field( (string) ( $input['province'] ?? $input['state'] ?? $order->get_shipping_state() ) ), 'country' => sanitize_text_field( (string) ( $input['country'] ?? $input['country_code'] ?? $order->get_shipping_country() ) ), ); $same_region_only = self::parse_same_region_only( $input ); $region_violation = Agentmatica_WooCommerce_Order_Guards::same_region_violation( $order, array( 'country' => $new_address['country'], 'state' => $new_address['state'], ), $same_region_only ); if ( $region_violation !== null ) { return self::refused( 'update_address', $region_violation ); } $order->set_address( $new_address, 'shipping' ); $order->save(); $order->add_order_note( __( 'Shipping address updated via Agentmatica chat.', 'agentmatica-mcp-connector' ) ); return self::success( 'update_address', __( 'Shipping address updated successfully.', 'agentmatica-mcp-connector' ), 'success', (int) $order->get_id() ); } /** * @param array|null $input * @return array */ public static function request_return( $input = null ): array { if ( ! Agentmatica_Support_Settings::is_write_actions_enabled() ) { return self::refused( 'create_return', __( 'Order write actions are disabled on this store.', 'agentmatica-mcp-connector' ) ); } $verified = Agentmatica_WooCommerce_Order_Guards::require_verified_session( $input ); if ( isset( $verified['success'] ) && $verified['success'] === false ) { return self::normalize_failure( $verified ); } // Cancel and return need a logged-in store account (order number + email // is printed on packing slips and emails; app is sign-in only too). $login_required = Agentmatica_WooCommerce_Order_Guards::require_logged_in_session( $verified ); if ( $login_required !== null ) { return $login_required; } $input = is_array( $input ) ? $input : array(); if ( ! Agentmatica_WooCommerce_Order_Guards::confirmed( $input ) ) { return self::unconfirmed( 'create_return' ); } $order_number = sanitize_text_field( (string) ( $input['order_number'] ?? '' ) ); $reason = sanitize_text_field( (string) ( $input['reason'] ?? '' ) ); if ( $order_number === '' ) { return self::refused( 'create_return', __( 'order_number is required.', 'agentmatica-mcp-connector' ) ); } $order = Agentmatica_WooCommerce_Orders::find_order_by_number( $order_number ); if ( ! $order instanceof WC_Order ) { return self::refused( 'create_return', __( 'Order not found.', 'agentmatica-mcp-connector' ) ); } if ( ! Agentmatica_WooCommerce_Order_Guards::order_owned_by_session( $order, $verified ) ) { return self::refused( 'create_return', __( 'That order does not belong to this customer.', 'agentmatica-mcp-connector' ) ); } $ineligible = Agentmatica_WooCommerce_Order_Guards::return_request_eligibility( $order ); if ( $ineligible !== null ) { return self::refused( 'create_return', $ineligible ); } if ( (string) $order->get_meta( '_agentmatica_return_request' ) !== '' ) { return self::success( 'create_return', __( 'A return has already been requested for this order.', 'agentmatica-mcp-connector' ), 'already_done', (int) $order->get_id() ); } $note = $reason !== '' ? sprintf( /* translators: 1: reason */ __( 'Return requested via Agentmatica chat. Reason: %1$s', 'agentmatica-mcp-connector' ), $reason ) : __( 'Return requested via Agentmatica chat.', 'agentmatica-mcp-connector' ); $order->add_order_note( $note, false, true ); $order->update_meta_data( '_agentmatica_return_request', wp_json_encode( array( 'requested_at' => gmdate( 'c' ), 'reason' => $reason, 'session_key' => Agentmatica_WooCommerce_Cart::sanitize_session_key( (string) ( $input['session_key'] ?? '' ) ), ) ) ); $order->save(); $admin_email = (string) get_option( 'admin_email' ); if ( $admin_email !== '' ) { wp_mail( $admin_email, sprintf( /* translators: %s: order number */ __( '[%s] Return request from chat', 'agentmatica-mcp-connector' ), $order->get_order_number() ), $note ); } return self::success( 'create_return', __( 'Return request submitted. The store team will follow up with next steps.', 'agentmatica-mcp-connector' ), 'success', (int) $order->get_id() ); } /** * @return array */ private static function success( string $action, string $message, string $result, int $order_id ): array { return array( 'success' => true, 'ok' => true, 'action' => $action, 'result' => $result, 'order_id' => $order_id, 'message' => $message, ); } /** * @return array */ private static function refused( string $action, string $message ): array { return array( 'success' => false, 'ok' => false, 'action' => $action, 'result' => 'refused', 'code' => 'refused', 'message' => $message, ); } /** * @return array */ private static function unconfirmed( string $action ): array { return array( 'success' => false, 'ok' => false, 'action' => $action, 'result' => 'refused', 'code' => 'unconfirmed', 'message' => __( 'Visitor confirmation is required before applying this change.', 'agentmatica-mcp-connector' ), ); } /** * @param array $payload * @return array */ private static function normalize_failure( array $payload ): array { if ( ! isset( $payload['ok'] ) ) { $payload['ok'] = false; } if ( ! isset( $payload['result'] ) ) { $payload['result'] = 'refused'; } return $payload; } /** * @param array $input */ private static function parse_refund_mode( array $input ): string { $mode = sanitize_key( (string) ( $input['cancel_refund_mode'] ?? 'original' ) ); return 'none' === $mode ? 'none' : 'original'; } /** * @param array $input */ private static function parse_same_region_only( array $input ): bool { if ( array_key_exists( 'address_same_region_only', $input ) ) { return filter_var( $input['address_same_region_only'], FILTER_VALIDATE_BOOLEAN ); } return true; } }