Ending months of rumors, Walmart today announced a deal to buy ecommerce site Flipkart, giving the retailer a significant foothold in India while beating back a last-minute offer from Amazon.

Walmart said it would pay $16 billion for an initial stake of approximately 77 percent in Flipkart, with the balance of the company owned by China’s Tencent, investment firm Tiger Global, Microsoft, and Flipkart cofounder Binny Bansal. The deal gives Walmart a chance to compete in India, one of the world’s fastest-growing economies.

“India is one of the most attractive retail markets in the world, given its size and growth rate, and our investment is an opportunity to partner with the company that is leading transformation of ecommerce in the market,” said Doug McMillon, Walmart’s president and CEO, in a statement. “As a company, we are transforming globally to meet and exceed the needs of customers, and we look forward to working with Flipkart to grow in this critical market.”

Word of the pending deal has been a terribly kept secret, with Softbank’s Masayoshi Son, one of Flipkart’s largest investors, spilling the beans in a conference call earlier today. Still, the closing of the deal is a coup for Walmart, which has been fighting hard to keep up with Amazon in the ecommerce realm.

Indeed, there were reports that Amazon had made a last-minute offer to buy 60 percent of Flipkart. But Walmart appears to have carried the day, in part by promising that it would continue to support Flipkart’s ambition to eventually be listed as a separate, publicly traded subsidiary.

That kind of independence could be seen as critical to remaining a leader in India. Founded in 2007, Flipkart has built not just an ecommerce site, but a whole integrated supply chain that would be hard for outsiders like Amazon and Walmart to duplicate. The company had $4.6 billion in revenue for the fiscal year ending March 2018, an increase of 50 percent from the same period one year ago, according to the Walmart press release.

Walmart also pledged to continue supporting Flipkart’s efforts to expand its economic impact on the country by helping local farmers and merchants.

In addition to Walmart’s purchase, the companies said Flipkart may seek additional investors to continue expanding its ecommerce operations. The deal is expected to close by the end of 2018.

Separately, eBay announced that it would sell its stake in Flipkart for $1.1 billion and will end its strategic partnership that allows Flipkart to use the eBay brand in India. eBay said it would relaunch eBay India to “focus initially on the cross-border trade opportunity, which we believe is significant.”