PHP header() gives headers already sent

I had to do it with WordPress’s inbuilt redirect function; wp_redirect();

wp_redirect( $payment->getPaymentUrl() );
exit;