NEW YORK — As Donald Trump was running for president in 2016, his old friend at the National Enquirer was scooping up potentially damaging stories about the candidate and paying out tens of thousands of dollars to keep them from the public eye.

But when it came to the seamy claims by porn performer Stormy Daniels, David Pecker said he put his foot down.

“I am not paying for this story,” he told jurors Thursday at Trump’s hush money trial, recounting his version of a conversation with Trump’s former lawyer Michael Cohen about the catch-and-kill scheme that prosecutors alleged amounted to interference in the race. Pecker was already $180,000 in the hole on other Trump-related stories by the time Daniels came along. “I didn’t want to be involved in this from the beginning.”

Pecker’s testimony was a critical building block for the prosecution’s theory that their partnership was a way to illegally influence the 2016 presidential election. The Manhattan district attorney is seeking to elevate the gravity of the history-making first trial of a former American president and the first of four criminal cases against Trump to reach a jury.

But the hush money trial was just one of the consequential legal matters facing the Republican presidential candidate Thursday.

The U.S. Supreme Court also heard arguments over whether Trump should be immune from prosecution in a federal case over his efforts to reverse his 2020 election loss to Joe Biden.

Trump’s many legal problems were colliding this week. The hush money case includes a looming decision on whether he violated a gag order and should be held in contempt. His former lawyers and associates were indicted in a 2020 election-related scheme in Arizona. And a New York judge rejected a request for a new trial in a defamation case that found Trump liable for $83.3 million in damages.

Trump has maintained he is not guilty of any of the charges, and as well that the stories that were bought and squelched were false.

Pecker recalled that the publication bought a sordid tale from a New York City doorman and purchased accusations of an extramarital affair with former Playboy model Karen McDougal to prevent the claims from getting out. There was some talk of reimbursement from Trump’s orbit, but Pecker was ultimately never paid.

The testimony about McDougal is important for the prosecution’s argument that Trump was seeking to win the election through illegal means

“We didn’t want the story to embarrass Mr. Trump or embarrass or hurt the campaign,” Pecker testified about that deal.

The breaking point for Pecker came, he said, with Daniels, who was eventually paid by Cohen to keep quiet over her claim of a 2006 sexual encounter with Trump. The ex-president denies it happened.

Pecker told the court that he was dining with his wife the night after the public learned of the infamous 2005 “Access Hollywood” tape in which Trump discussed grabbing women sexually without permission, when then-editor Dylan Howard called with an urgent matter.

Howard said he heard from Daniels’ representatives that she was trying to sell her story and that the tabloid could acquire it for $120,000, Pecker told jurors. Pecker was tapped out; he told Cohen as much.

At the same time, Pecker advised that someone — just not him — should do something to prevent the story from going public.

“I said to Michael, ‘My suggestion to you is that you should buy the story, and you should take it off the market because if you don’t and it gets out, I believe the boss will be very angry with you.’ ”

Cohen followed his advice, paying Daniels $130,000.

Pecker testified that Trump later invited him to a White House dinner in July 2017 to thank him for helping the campaign. The ex-publisher said Trump encouraged him to bring anyone he wanted, recounting that the then-president told him, “It’s your dinner.”

Pecker said that he and Howard, as well as some of his other business associates, posed for photos with Trump in the Oval Office.

At one point during the evening, Pecker said Trump asked him for an update on Karen McDougal.

“How’s Karen doing?” he recalled Trump asking as they walked past the Rose Garden from the Oval Office to the dining room.

“I said she’s doing well, she’s quiet, everything’s going good,” Pecker testified.

But months later, in March 2018, the president became furious when McDougal gave an interview to CNN’s Anderson Cooper, Pecker testified.

“I thought you had and we had an agreement with Karen McDougal that she can’t give any interviews or be on any TV channels,” Trump told Pecker by phone, the former National Enquirer publisher said.

He said he explained to the then-president that the agreement had been changed to allow her to speak to the press after a November 2016 article in The Wall Street Journal about the tabloid’s $150,000 payout to McDougal.

“Mr. Trump got very aggravated when he heard that I amended it, and he couldn’t understand why,” Pecker told jurors.

Prosecutors finished their questioning Thursday, and Trump defense attorney Emil Bove opened his cross-examination by grilling Pecker on his recollection of specific dates and meetings, an apparent effort to underscore the difficulty of recalling details relevant to the years-old allegations.