OTTAWA -- Inside the Prime Minister's Office, they called it the "scenario for repayment" -- a plan that would make the Mike Duffy expenses scandal quietly go away, without anyone suggesting the senator had done wrong.

Nigel Wright, the marquee witness at Duffy's fraud, breach of trust and bribery trial on Wednesday, said he didn't tell former boss Stephen Harper that the deal involved someone else paying Duffy's contested expenses.

But several of Harper's closest aides were privy to the drawn-out, often tense negotiations that went on behind the scenes in 2013. Harper was briefed in general, Wright said.

"I told (Harper) that Sen. Duffy was agreeing to repay; I gave him -- in very broad terms, not in detail -- the media lines," Wright testified.

"I think what I stressed with the prime minister was that we believed and the government would be saying, Sen. Duffy had possibly made a mistake in his claims ... as opposed to wrongdoing and that he would repay them."

Duffy has pleaded not guilty to 31 charges in connection with his Senate office, living and travel expenses. Wright's testimony goes to his secret $90,000 repayment of Duffy's expenses, which has been framed by the Crown as being orchestrated by Duffy.

Wright, who flew in from London where he now works, answered Crown attorney Jason Neubauer's questions with long, detailed answers. He recalled dates and specific phone calls and admitted to feeling anger towards Duffy at different junctures -- and ultimately regret.

"If it became public, I thought it would be somewhat embarrassing," he said. "But there were a whole lot of connotations associated with it that I didn't really think through. If I had, I might not have done it."

Wright's highly anticipated testimony was accompanied by 426 pages of internal emails that were filed with the court.

From the moment the media began asking about the senator's residency issues in late 2012, Duffy dug in his heels.

The senator filed for expenses on the basis that he was travelling and living away from his "principal" residence in Prince Edward Island, even though he lived most of the time in suburban Ottawa.

He told Wright the Senate rules and forms were not clear and repaying the money would put him at risk of being declared not qualified to sit in the upper chamber.

While Wright said Duffy might have arguably been legally entitled to the expenses, he felt that common sense dictated that he shouldn't claim for expenses for somewhere he "hung his hat at night."

A drawn-out set of talks ensued involving Wright, Duffy, PMO lawyer Benjamin Perrin, Duffy's lawyer and strategists and communications staff.

The draft plan, circulated in February 2013, included the point that Duffy be extricated from an internal audit by the firm Deloitte.

"The purpose is to put an end to the ongoing questions about his expenses," reads the document, penned by Chris Woodcock, Harper's former director of issues management.

"A proactive repayment would allow Sen. Duffy to say he is doing the right thing without being found guilty of breaking the rules by Deloitte. The Senate committee would halt the audit provided that he acknowledges an error or wrongdoing."

Wright suggested that Duffy only "acknowledge an error and put it down to ambiguities in the rules and forms."

He told the court that Conservative Sen. David Tkachuk, once chairman of the powerful internal economy committee, was the first to suggest Duffy quietly repay the expenses and the committee would stop the audit.

The staffers would go to some lengths to find out if Deloitte would at least refrain from concluding whether Duffy's residency claims were legit. Wright was able to obtain the confidential work order given to Deloitte and Conservative Sen. Gerstein spoke to contacts within the firm.

Deloitte also did work for the party.

"I have been on the phone constantly with Gerstein who has been trying to arrange the necessary commitments from Deloitte but to date he hasn't been able to receive those assurances," wrote another aide, Patrick Rogers.

Ultimately, Duffy and the PMO reached an agreement that would see his expenses repaid by the party, the audit curtailed, the government defend Duffy's right to represent Prince Edward Island and Duffy keep his mouth shut about everything.

Detailed media plans were put in place, from the words Duffy would say to the TV outlets that would capture the moment.

But Gerstein, chairman of the party's financial wing, balked at the final price tag of $90,000 in expenses plus Duffy's legal costs. Wright said he made the quick decision to pay it himself.

"I had an obligation to fulfil my end of my arrangement with him and I couldn't think of another way of doing it," he said, noting it didn't impact his lifestyle. "I felt I would do it myself and could do it myself."

The Deloitte audit couldn't be stopped, but the PMO advised Duffy to stop co-operating. Its conclusions were vague and Harper's team ensured that a Senate committee report on Duffy was stripped of negative language.

Then Sen. Marjory LeBreton, the Government Senate leader, was coached on what to say if reporters asked why Duffy's report had been whitewashed.

"This repayment has been accepted and the Senate considers the matter closed," LeBreton was told to say.

Less than a week later, the matter would burst open with a leak to CTV News of Wright's $90,000 payment.