LONDON — Author J.K. Rowling accepted an apology and a charitable donation Wednesday from a law firm which revealed she wrote a crime novel under a pseudonym.

The Harry Potter author was exposed by a newspaper on July 14 as the author of The Cuckoo’s Calling, a thriller ostensibly written by former soldier and first-time novelist Robert Galbraith.

The book was published in April to good reviews but modest sales, and there was speculation that Rowling or her publisher had leaked the news to raise the book’s profile.

But the law firm Russells, which has done work for Rowling, acknowledged that one of its partners had let the information slip to his wife’s best friend, who tweeted it to a Sunday Times columnist.

Rowling sued the lawyer and the friend. Her attorney, Jenny Afia, told Britain’s High Court on Wednesday that Rowling had been left “angry and distressed that her confidences had been betrayed.

“As a reflection of their regret for breach of the claimant’s confidence, including frustrating the claimant’s ability to continue to write anonymously under the name Robert Galbraith, the defendants are here today to apologize publicly to the claimant,” Afia said.

Russells agreed to reimburse Rowling’s legal costs and to make a “substantial” donation to The Soldiers’ Charity, which helps former military personnel and their families.

Rowling also said she was donating all royalties from the book for the next three years to the charity.

The hero of her novel, Cormoran Strike, is a veteran who lost a leg in Afghanistan, and Rowling has said she drew on conversations with serving soldiers and veterans to create the character.

“This donation is being made to The Soldiers’ Charity partly as a thank you to the army people who helped me with research, but also because writing a hero who is a veteran has given me an even greater appreciation and understanding of exactly how much this charity does for ex-servicemen and their families, and how much that support is needed,” Rowling said in a statement.

The writer said she had always intended to give part of Galbraith’s royalties to the organization, “but I had not anticipated him making the bestseller list a mere three months after publication — indeed, I had not counted on him ever being there!”

Since Rowling was outed as the author, The Cuckoo’s Calling has topped bestseller lists.

A second Cormoran Strike novel is due for publication next year.