$envelope RefundGuard::envelope(). * @return array|WP_Error */ public function replay( ActionDescriptor $action, array $envelope ): array|WP_Error { $order_id = absint( $envelope['order_id'] ?? 0 ); $order = wc_get_order( $order_id ); if ( ! $order instanceof \WC_Order ) { return new WP_Error( 'adafwc_refund_order_missing', __( 'The order for this refund no longer exists.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ); } if ( ! current_user_can( 'edit_shop_orders' ) ) { return new WP_Error( 'adafwc_refund_forbidden', __( 'The agent\'s WordPress user may no longer refund orders.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) ); } $decimals = ( new StoreCurrency() )->decimals(); $amount = Money::to_minor( $envelope['amount'] ?? null, $decimals, true ); $remainder = Orders::refundable( $order, 0, $decimals ); if ( null === $amount || null === $remainder || $amount > $remainder ) { return new WP_Error( 'adafwc_refund_exceeds_remainder', /* translators: %d: order id. */ sprintf( __( 'Order #%d no longer has this much left to refund. Nothing was refunded.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), $order_id ) ); } $refund = wc_create_refund( $envelope ); if ( is_wp_error( $refund ) ) { return $refund; } return array( 'refund_id' => $refund->get_id(), 'amount' => $refund->get_amount(), 'refunded_payment' => $refund->get_refunded_payment(), ); } }