Automattic\WooCommerce\Internal\RestApi\Routes\V4\Refunds

DataUtils::validate_refund_line_total │ public │ WC 11.3.0

Check a line refund against the original line and previous refunds.

Both refund paths use this limit. The computed path passes a tax-inclusive amount; the explicit wc/v3 path passes its tax-inclusive gross amount.

Method of the class: DataUtils{}

No Hooks.

Returns

true|WP_Error. Validation result.

Usage

$DataUtils = new DataUtils();
$DataUtils->validate_refund_line_total( $item, $refund_total, $already_refunded_total );
$item(WC_Order_Item_Product|WC_Order_Item_Fee|WC_Order_Item_Shipping) (required)
Original order item.
$refund_total(float) (required)
Requested line item refund amount.
$already_refunded_total(float) (required)
Prior refunds for this item, including tax.

Changelog

Since 11.3.0 Introduced.

DataUtils::validate_refund_line_total() code WC 11.3.0

public function validate_refund_line_total( $item, float $refund_total, float $already_refunded_total ) {
	$price_decimals      = wc_get_price_decimals();
	$signed_line_total   = (float) $item->get_total() + (float) $item->get_total_tax();
	$item_total_with_tax = abs( $signed_line_total );
	$abs_refund_total    = NumberUtil::round( abs( $refund_total ), $price_decimals );

	if ( $refund_total * $signed_line_total < 0 ) {
		return new WP_Error(
			'invalid_refund_total',
			__( 'Refund total has the wrong sign for this line item.', 'woocommerce' ),
			array( 'status' => WP_Http::BAD_REQUEST )
		);
	}

	// Mirror the preview path's three distinct over-refund errors (same
	// codes, messages, and 422 status) so create and preview reject the
	// same input identically. An over-refund is a well-formed but
	// unprocessable request, so 422 — not 400 — is the correct status,
	// matching the order-level cap the controller already returns.
	if ( $abs_refund_total > NumberUtil::round( $item_total_with_tax, $price_decimals ) ) {
		return new WP_Error(
			'refund_total_exceeds_line',
			sprintf(
				/* translators: %s: line item total including tax */
				__( 'refund_total cannot exceed the line item total including tax (%s).', 'woocommerce' ),
				wc_format_decimal( $item_total_with_tax, $price_decimals )
			),
			array( 'status' => WP_Http::UNPROCESSABLE_ENTITY )
		);
	}

	// Remaining is rounded to currency precision before both checks, so a
	// sub-cent residue left by rounding drift counts as fully refunded
	// rather than producing a "cannot exceed 0.00" rejection.
	$remaining_total = NumberUtil::round( $item_total_with_tax - abs( $already_refunded_total ), $price_decimals );
	if ( $remaining_total <= 0 ) {
		return new WP_Error(
			'line_item_already_refunded',
			__( 'This line item has already been fully refunded.', 'woocommerce' ),
			array( 'status' => WP_Http::UNPROCESSABLE_ENTITY )
		);
	}
	if ( $abs_refund_total > $remaining_total ) {
		return new WP_Error(
			'refund_total_exceeds_remaining',
			sprintf(
				/* translators: %s: remaining refundable amount */
				__( 'refund_total cannot exceed the remaining refundable amount for this line item (%s).', 'woocommerce' ),
				wc_format_decimal( $remaining_total, $price_decimals )
			),
			array( 'status' => WP_Http::UNPROCESSABLE_ENTITY )
		);
	}

	return true;
}