It was supposed to be a $400 marijuana purchase that was part of a sting operation conducted by undercover police officers. But three alleged dealers, authorities said, tried to rob an undercover officer and triggered a shootout Tuesday afternoon in the parking lot of a Garfield shopping center filled with people, including teenagers on spring break.

When it was over, a Paramus police officer and one suspect were in the hospital with gun­shot wounds, and three suspects, including the injured shooter, were under arrest, Bergen County Prosecutor John L. Molinelli said in a news conference at Hackensack University Medical Center.

The officer, who was not identified, was assigned to the prosecutor’s narcotics task force and had been in the process of buying marijuana from a suspect inside his police-issued undercover vehicle, which was parked near a Marshalls store, when a second man walked up to the driver’s window, pointed a gun at him, and said, “Give me everything that you have,” Molinelli said.

The prosecutor said that undercover officers are taught to give in to such demands, but that this time the alleged gunman, Rafael Angel Vasquez, 25, of Passaic, became “aggressive,” leading the unarmed officer to grab the gun. Other police officers, who were nearby monitoring the sale, then descended on the location and shot the gunman after he shot the officer in the right hip and left lower leg, Molinelli said.

Vasquez faces an attempted murder charge and the other suspects, Melvin Guzman, 19, of Garfield and Patrick Morel, 21, of Clifton, face first-degree charges related to the shooting, Molinelli said. The prosecutor called it a “planned robbery,” adding that he does not believe the suspects ever intended to go through with the drug sale.

He said the officer made arrangements to complete a drug sale at the shopping center on Passaic Street. Guzman, he said, was sitting in the passenger seat of the officer’s car when the shooting occurred, and Morel fled in the dealers’ car, but was caught a short time later by state police on the Garden State Parkway near the Essex Toll Plaza.

The officer was in stable condition at Hackensack University Medical Center after undergoing surgery Tuesday afternoon. He suffered two fractures in his lower left leg, but his hip was not fractured, said Hackensack University Medical Center’s chairman of emergency services, Joseph Feldman. Molinelli said the officer was expected to make a full recovery.

Molinelli said the officer would not be identified because he is expected to continue working undercover. He has been a member of the Paramus Police Department for nine years and previously has worked undercover for the Prosecutor’s Office, Molinelli said. As is typical for such operations, he said, the officer was not carrying a gun or a badge and was not wearing a protective vest.

Vasquez underwent surgery at St. Joseph’s Regional Medical Center in Paterson after being shot in the left arm and the left leg, Molinelli said.

Patrons of the shopping center, which is anchored by a Wal­mart, said they heard up to five shots fired in rapid succession, then saw two men lying on the ground. One of the men was bleeding profusely from his right leg, they said. Police from Garfield, Wallington and the Bergen County Police responded.

Students on spring break were at the shopping center, and the shooting caused some panic in Marshalls, where shoppers pleaded with employees to lock the doors, fearing that the shooter would enter the store. A cashier, Carlos Loyola, 19, said he locked the doors.

Kaniya Beauvoir, 17, of Lodi said the man with the leg wound, who was wearing khaki pants and a blue-hooded sweat shirt, looked up at her and asked her for water as she stood on a hill above the parking lot. She said police officers were there to aid him within seconds. The other man, she said, was wearing a blue shirt.

“I think it’s crazy that of all places it happened here,” she said. “It sounded like he unloaded a full clip. Who does that? It’s the middle of the day and there are kids around.”

Other witnesses said they also saw two men lying on the ground shortly after they heard between three and five shots, which they said sounded like popping. Brianna Colon and Angie Salazar, both 17-year-olds from Garfield, said that one of the men was bleeding from the leg and had blood on his face.

“Yeah, I’m traumatized,” Colon said.

Paramus Mayor Richard LaBarbiera, addressing a council meeting Tuesday night, described the shooting as “scary.”

“Keep him, as well as all men of law enforcement, in your prayers,” LaBarbiera said of the injured officer.

Molinelli said it is not unusual for undercover officers to have weapons pulled on them, and that they typically turn over whatever cash they have on them, knowing that other detectives are standing by to make an arrest.

“But this time the man with the gun got aggressive,” Molinelli said. “The advance team standing by got distress signals from the officer and rushed the car. The gunman fired the first shot at the Paramus police officer and detectives returned fire.”

He said the gunman’s aggressive behavior led to a “scuffle,” but did not go into details about that behavior. He also did not say how the officer signaled the other detectives. But he said that at least seven plainclothes detectives were standing by in three groups during the operation, adding that they reacted to the situation “perfectly.”

He said such operations involving municipal police and county detectives happen “every day, every night, all the time.” He said that the location of drug deals in sting operations typically are dictated by drug dealers and often are in public places. Such deals are never done in a dealer’s car, he said, adding that law enforcement prefers not to be in a car at all.

“We prefer to do it out in the open,” he said. “They know that when it’s done in the parking lot like this there are civilians and they have to be careful.”

At the news conference, Molinelli was asked to explain how a shooting stemmed from what apparently began as a small drug deal, and whether the suspects attempted the robbery believing they might get only $400.

“That’s it,” he said. “People do amazing things today. This is the world we live in. … We all were lucky today.”