SAN RAMON, Calif. (AP) - A federal judge ordered Apple to dismantle a lucrative part of the competitive barricade guarding its closely run iPhone app store, but rejected allegations that the company has been running an illegal monopoly that stifles competition and innovation.

The ruling issued Friday continues to chip away at the so-called “walled garden” that Apple has built around its crown jewel, the iPhone, and its app store, without toppling it completely.

The 185-page decision from U.S. District Judge Yvonne Gonzalez Rogers also provided Apple with some vindication. The judge didn't brand Apple as a monopolist or require it to allow competing stores to offer apps for iPhones, iPads and iPods.

Those were two of the biggest objectives sought by Epic Games, the maker of the popular Fortnite video game that filed what it would hoped would be a landmark antitrust case last year after brazenly defying an exclusive payment system that funnels 15% to 30% of all in-app digital transactions on iPhones to Apple.

Such transactions can include everything from Netflix or Spotify subscriptions to the sale of digital item such as songs, movies or virtual tchotchkes for video games. Epic cast that highly lucrative fee as a price-gouging tactic that wouldn't be possible if competing stores were allowed to offer iPhone apps.

While parts of her decision raised questions about whether Apple's fees were driving up prices for consumers, Gonzalez Rogers left the fee structure intact and upheld the company's right to block other stores from offering apps for its iPhone. She sided with Apple on every other key point of the case.

But the judge did conclude Apple has been engaging in unfair competition under California law, prompting her to order the company to allow developers throughout the U.S. to insert links to other payment options besides its own within iPhone apps. That change would make it easier for app developers to avoid paying Apple's commissions, potentially affecting billions of dollars in revenue annually.

The prospect of Apple taking a hit to its lofty profit margins rattled investors, causing the company's stock price to fall by more than 3% in Friday's trading. That downturn delivered an $80 billion blow to Apple's market value.

Yet Apple did its best to frame the decision as a complete victory, even as it acknowledged it may appeal the portion of the ruling that will make it easier for app developers sidestep Apple's commissions.

“We are very pleased with the court's ruling and we consider this a huge win for Apple,” said Kate Adams, the company's general counsel. “This decision validates that Apple's `success is not illegal,' as the judge said.”

Gonzalez Rogers also dealt Epic a blow by ruling that the game maker breached its contract with Apple when Fortnite added a non-Apple payment system to its app. That defiance prompted Apple to oust Fortnite from its app store 13 months ago, triggering Epic's lawsuit. She ordered Epic to pay Apple nearly $3.7 million, or 30% of the revenue it collected while violating Apple's commissions.

Epic CEO Tim Sweeney denounced the ruling in a tweet, writing that it “isn't a win for developers or for consumers.”

He said Fortnite will return to Apple's app store once it can offer competitive in-app payments. “We will fight on,” he added in a subsequent tweet.

“It's a bit of a mixed bag,” said Stanford University law professor Mark Lemley. “I think on balance, it's a win for Apple. They dodged the biggest threat to them.”

The ruling caps a trial that spent the entire month of May focused on Apple's app store, one of the pillars holding up its $2 trillion empire.

Since that trial ended, Apple has taken two steps to loosen some of its app store rules - one to settle a lawsuit and another to appease Japanese regulators without altering its commissions. Those concessions make it easier for many apps to prod their users to pay for digital transactions in ways that avoid triggering Apple's fees.

As part of a deal with Japanese regulators announced last week, Apple had agreed to allow what it dubs “reader” apps - those that sell subscriptions to digital music, video and publications - to insert links steering users to other places to sign up for accounts and, in theory, pay for services.

Now Gonzalez Rogers is ordering Apple to go even further by allowing links for non-Apple payment options directly within all apps, something Apple has steadfastly resisted.

“Loosening the restrictions will increase competition,” Gonzalez Rogers wrote. For instance, it would expose Apple to rival payment services charging lower commissions.

Another Apple antagonist, Spotify, cheered the increased ease with which consumers could choose other payment options. The company, which has been among the most strident critics of Apple's commission system, called for lawmakers and regulators to do even more to break down the walls protecting Apple's app store.

A bill introduced in Congress earlier this year proposes to obliterate Apple's commission system and open up the market to more competition. “Much more must be done,” said Sen. Amy Klobuchar, a Democrat from Minnesota pushing for app store reforms.

Chris Hoofnagle, a law professor in residence at the University of California, Berkeley, views the decision as a major victory for app developers, although he is worried that the greater freedom to post in-app links will weaken privacy and security, as Apple has warned.

App developers are “going to have more opportunities to use different forms of payment and to contact people in different ways,” Hoofnagle said. “If developers choose bad payment mechanisms and if they use this decision as an opportunity to kind of open the spam floodgates, you might regret this outcome.”

In a research note, Cowen Washington Research Group analyst Doug Creutz said the inclusion of in-app links to other payment options could increase the pressure on Apple to lower its commissions.

“Apple will obviously have to respond to this to try to retain people in its payment system,” Creutz wrote.

Although the figures were kept confidential during the trial, analysts have estimated that Apple's app store division brings in $15 billion to $18 billion annually, helping to fuel the company's rapidly growing services division. That division's revenue has swelled from $24 billion in 2016 to $54 billion last year.

Apple CEO Tim Cook had warned that losing control over iPhone apps would create “a toxic kind of mess” during his testimony on the witness stand at the end of a four-week trial in Oakland, California.