Walmart, the country’s largest retailer, is now using newly developed technology to thwart some gift card scammers — and to return money to thousands of people already ensnared in their ruthless deceptions.

Gift cards are one of the most important tools of the trade for scam artists who try to trick or scare people, often elders, into buying them at retail stores and reading the card numbers and codes to the scammers over the phone.

But, beginning in 2017, Walmart has frozen more than $4 million in gift card funds because it suspects those cards were purchased by customers who were in the process of being scammed.

And now, Walmart is attempting to return that money — in individual amounts averaging about $400 — to victims in Massachusetts and every other state, with the help of the Department of Justice, Walmart said.

Walmart declined to reveal full details of its anti-fraud program, saying to do so could tip off the scammers and encourage them to find new ways to elude detection.

But the giant retailer did confirm some basic aspects of its efforts on behalf of consumers after a 13-page affidavit recently surfaced in court filings in Arkansas.

The affidavit was filed in February in support of a legal action in which the Department of Justice is asking a judge to declare the money as “derived from’’ and “traceable’’ to fraud and therefore should be forfeited by the scammers who possess the numbers and codes to the gift cards.

If a judge consents, the money will be returned to scam victims, whose identities may be obtained from credit card and other records. Walmart is referring anyone who may have questions about getting a refund to the Department of Justice at https://www.justice.gov/uspc/victim-witness-program.

“This is a program to protect people who are vulnerable,’’ said Walmart spokesman Randy Hargrove. “These scams hurt our customers. That’s why we developed this program.’’

“The whole point is to shut down the scammers,’’ he said, adding, “No system is going to catch every bad actor.“

The $4 million Walmart hopes to return to victims is a tiny fraction of the total amount of scams committed with gift cards. In the first nine months of 2021, consumers reported losing almost $150 million in gift card fraud, according to the Federal Trade Commission.

The amount of reported fraud involving gift cards last year compares with about $115 million for all of 2020, the FTC says. And the estimate of ongoing actual fraud, as opposed to reported fraud, is believed to be many times higher since victims are often too embarrassed to report it.

Consumers can get educated on gift card scams at https://consumer.ftc.gov/articles/gift-card-scams.

Scammers like gift cards because they are the equivalent of cash, so long as they obtain the card number, which is printed on the back, and the code or PIN, which is also printed on the back, under a thin coating that can be scratched off after purchase.

Gift card numbers and codes can easily be sold online, for a slight discount, or used to make purchases online — no plastic card necessary.

A retired Episcopal priest from West Roxbury who was scammed out of $8,500 in gift cards was recently featured in the Globe. George Chapman, 80, purchased the cards at Macy’s, Lowe’s, and CVS after scammers hacked into his computer in ­February and scared him into thinking the only way he could “counteract’’ false charges on his credit card was to purchase gift cards.

Chapman, after realizing his mistake, offered himself as a public example of what not to do: “Let this be a lesson. Don’t let it happen to you.’’ If anyone asks you to buy gift cards and read them the numbers, don’t do it, he said.

Walmart said it has developed its “own proprietary, industry-leading technology designed to identify distinct red flags and freeze funds when possible … if consumer gift card fraud is suspected.’’

The Walmart spokesman, while declining to provide details of its technology advances, said the giant retailer is not aware of any other company that has identified such “red flags’’ for fraud or begun the process of returning money via the Department of Justice.

“We have implemented technology and algorithms that trigger fraud alerts,’’ he said.

One focus of Walmart’s efforts is on data maintained in its gift card center, which, among other things, allows anyone with a gift card number and code to instantly check the balance by going online or calling a 1-800 telephone number.

According to the affidavit, Walmart came to realize that an important “red flag’’ for possible fraud was how quickly card balances were checked after purchase. Sometimes those checks were made within seconds of the sale of the cards, and sometimes by someone accessing the information from an overseas location, according to the affidavit.

Walmart deemed that conduct suspicious enough to warrant further scrutiny.

“It’s one of several factors we look at,’’ Hargrove said of the balance checks.

Whether Walmart can put a serious dent in gift card fraud remains to be seen.

Lauren Saunders, associate director of the National Consumer Law Center, said, “Anything that helps get money back from scammers is a welcomed development, but we don’t know how path-breaking this is.’’

Representative Jake Auchincloss, vice chair of the House Financial Services Committee, said, “We’ve seen a surge in scammers defrauding older Americans, and we need both tech and policy solutions to prevent and redress this abuse.’’

Got a problem? Send your consumer issue to sean.murphy@globe.com. Follow him on Twitter @spmurphyboston.