Would you consider adding another filter hook which includes the $item_loop line counter, so something like:
===
$basket = apply_filters( 'ag_opayo_modify_basket', $basket, $order );
$basket_full_string = $item_loop . ':' . $basket;
return apply_filters( 'ag_opayo_modify_basket_full_string', $basket_full_string , $order );
===
That way we can construct our own basket string without having to worry about our line count not matching with the $item_loop variable in the main function.