BERLIN—Angela Merkel had just returned to her apartment here after meeting critics of her policy of welcoming Middle East refugees, when aides phoned her with news of terrorist attacks in Paris.

The German chancellor’s open door for people fleeing war in Syria, Iraq and elsewhere had already weakened her once-unassailable popularity. She knew, says a person familiar with her thinking, that immigration opponents in Germany and Europe would want to link the Islamist terrorist threat with refugees trekking to Europe and would...