its order. * * @var array */ private array $in_flight = array(); /** * Decides an effect: Api::hold_at_effect(). * * @var Closure(ActionDescriptor, array, string, bool): EffectOutcome */ private Closure $decide; /** * Constructor. * * @param (Closure(ActionDescriptor, array, string, bool): EffectOutcome)|null $decide Effect decision; defaults to the parent's API. */ public function __construct( ?Closure $decide = null ) { $this->decide = $decide ?? static fn( ActionDescriptor $action, array $envelope, string $provider, bool $independent ): EffectOutcome => Api::hold_at_effect( $action, $envelope, $provider, $independent ); } /** * Hook the guard. */ public function register(): void { add_action( 'woocommerce_create_refund', array( $this, 'guard' ), 10, 2 ); add_action( 'woocommerce_refund_created', array( $this, 'finished' ), 10, 1 ); // A failed refund - the gateway refused, a restock threw - is // deleted by wc_create_refund() before it returns its error. add_action( 'woocommerce_delete_order_refund', array( $this, 'finished' ), 10, 1 ); } /** * wc_create_refund() finished an allowed refund, made or failed: its * order's later saves are the agent's again. * * @param mixed $refund_id The refund. */ public function finished( $refund_id ): void { $refund_id = (int) $refund_id; if ( isset( $this->in_flight[ $refund_id ] ) ) { InFlight::order_done( $this->in_flight[ $refund_id ] ); unset( $this->in_flight[ $refund_id ] ); } } /** * Decide a refund about to be saved; veto it unless it may proceed. * * @param mixed $refund The unsaved WC_Order_Refund. * @param mixed $args wc_create_refund()'s arguments. * @throws \Exception When the firewall holds or denies the refund. */ public function guard( $refund, $args ): void { $envelope = self::envelope( is_array( $args ) ? $args : array() ); $order_id = $envelope['order_id']; $action = new ActionDescriptor( 'unclassified', 'create', 'refund', $order_id > 0 ? $order_id : null, null, array( 'amount' => $envelope['amount'], 'api_refund' => $envelope['refund_payment'], 'reason' => $envelope['reason'], // Already saved when this hook fires: the facts and the drift // fingerprint leave it out (Orders::refunded()). 'refund_id' => $refund instanceof \WC_Order_Refund ? $refund->get_id() : 0, ) ); try { $outcome = ( $this->decide )( $action, $envelope, self::PROVIDER_ID, true ); } catch ( \Throwable $failure ) { // Never an error WooCommerce's catch( Exception ) would miss, // leaving a refund row it had already saved. $outcome = new EffectOutcome( 'DENY', 'unavailable' ); } // A refund the door decided was measured there; one larger than // that is not the refund that was decided (DoorRefunds). $measured = DoorRefunds::measured( $order_id ); $amount = Money::to_minor( $envelope['amount'], ( new StoreCurrency() )->decimals(), true ); if ( null === $outcome->decision && null !== $measured && ( null === $amount || $amount > $measured ) ) { $outcome = new EffectOutcome( 'DENY', 'larger_than_decided' ); } if ( ! $outcome->vetoes() ) { $refund_id = $refund instanceof \WC_Order_Refund ? (int) $refund->get_id() : 0; if ( null !== $outcome->decision && $order_id > 0 && $refund_id > 0 && ! isset( $this->in_flight[ $refund_id ] ) ) { // Allowed here: the order saves wc_create_refund() makes from // now on are this refund's, not new agent writes. InFlight::order( $order_id ); $this->in_flight[ $refund_id ] = $order_id; } return; } $order = wc_get_order( $order_id ); if ( $order instanceof \WC_Order ) { $order->add_order_note( self::note( $outcome, $envelope['amount'], $order->get_currency() ) ); } throw new \Exception( esc_html( $outcome->held() ? __( 'Agent Firewall is holding this refund for approval. Nothing was refunded.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) : __( 'Agent Firewall denied this refund. Nothing was refunded.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ) ); } /** * What a held refund needs to run again on approval, and nothing else: * the same envelope comes out of the same arguments, so the replay * meets this hook as the write being replayed and is not held again. * * @param array $args wc_create_refund()'s arguments. * @return array{order_id: int, amount: string, reason: string|null, refund_payment: bool, restock_items: bool, line_items: array} */ public static function envelope( array $args ): array { $amount = $args['amount'] ?? 0; return array( 'order_id' => absint( $args['order_id'] ?? 0 ), 'amount' => is_scalar( $amount ) ? (string) $amount : '0', 'reason' => isset( $args['reason'] ) && is_scalar( $args['reason'] ) ? (string) $args['reason'] : null, 'refund_payment' => ! empty( $args['refund_payment'] ), 'restock_items' => ! empty( $args['restock_items'] ), 'line_items' => isset( $args['line_items'] ) && is_array( $args['line_items'] ) ? $args['line_items'] : array(), ); } /** * The order note naming what happened to the refund. * * @param EffectOutcome $outcome The decision. * @param string $amount Refund amount, major units. * @param string $currency Order currency. */ private static function note( EffectOutcome $outcome, string $amount, string $currency ): string { $price = html_entity_decode( wp_strip_all_tags( wc_price( (float) $amount, array( 'currency' => $currency ) ) ), ENT_QUOTES, 'UTF-8' ); return $outcome->held() /* translators: 1: refund amount, 2: hold id. */ ? sprintf( __( 'Agent Firewall held a refund of %1$s requested by an AI agent. Nothing was refunded; it awaits approval (hold #%2$d).', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), $price, (int) $outcome->pending_id ) /* translators: %s: refund amount. */ : sprintf( __( 'Agent Firewall denied a refund of %s requested by an AI agent. Nothing was refunded.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), $price ); } }