The suspect has been identified as 26-year-old Jabree Johnson.
The incident was caught on surveillance video obtained by Action News.
It happened around 9 p.m. Monday in the parking lot of Krauszer's Food Store on the 1700 block of Greenwood Avenue in Hamilton Township, Mercer County.
"I hear the bullets, like five or six bullets," said Sara Batres, who lives nearby.
According to the Mercer County Prosecutor's Office, the federal investigation involved the FBI and ATF.
Officials said Johnson approached the undercover vehicle after one of the agents had exited.
Johnson entered the vehicle, pointed a gun at the second agent and demanded money.
The prosecutor's office said the second agent exited the vehicle and fired shots at Johnson.
As the suspect exited the vehicle, officials said he fired shots at the agent before fleeing down Atlantic Avenue.
"It's scary," said Batres.
A short time later, police were notified by Capital Health Regional Medical Center that a gunshot victim had been brought in by personal vehicle.
The gunshot victim was identified as Johnson.
Officials said Johnson told a detective with the Mercer County Shooting Response Team that he was shot in the area of Walnut Avenue and declined to provide further information.
Trenton police informed the Mercer County Homicide Task Force that no reports of shots fired in that area were received during that time.
Officials said Johnson had a large amount of cash with him.
The undercover vehicle recorded video of the exchange between the agent and Johnson.
The video, according to the prosecutor's office, depicts Johnson entering the vehicle with the second undercover agent, pointing a handgun at the agent, and telling him to hand over money.
Officials said the agent tossed an amount of bundled cash at Johnson, who then ordered the agent out of the vehicle.
The agent then fires at Johnson, officials said, striking him in the shoulder.
As Johnson exits the vehicle, officials said the video shows he fires multiple times at the agent before running off.
Johnson remains hospitalized in critical condition. He is charged with attempted murder, robbery, aggravated assault, resisting arrest and weapons offenses.
The owner of Krauszer's, Jay Patel, said he was behind the counter when he heard the shots outside. He said with customers inside the store, he immediately locked the doors.
"It was unusual to see 15, 20 cop cars roll in with all flashing lights at one time, so definitely scary," said Patel.
The undercover vehicle, a gray pickup truck, was towed early Tuesday morning, while investigators remained on scene close to 9 a.m. looking for clues.
Residents in the area said they come to the food store often and said the area is safe.
"I was shocked because I go to this store all the time, the family is nice," said Michele Gerald, a nearby resident.
No other injuries were reported.
The officer-involved shooting is being investigated by the Mercer County Homicide Task Force at the direction of the New Jersey Office of the Attorney General.