I'm wondering what the logic was behind Ming China banning foreign trade at several points in its history (for instance 1525-67), or restricting it to the tributary system. Wouldn't trying to attract foreign merchants to reside in Chinese ports increase customs revenue and strengthen the state?
I think you're referring to the Haijin, which was a policy first set forth and then canonized by Ming Taizu in his Ancestral Injunctions - the Huang Ming Zu Xun. The policy was first set forth by the Hongwu emperor - Ming Taizu - as a means to help combat the rampant Japanese piracy that plagued the Ming coastline, and to allow the Ming to devote their forces more fully towards the complete elimination of any Yuan remnants. Officially, the policy was just completely couterproductive and economically harmful as it resulted in sharp increases in instances of piracy and made lucrative trade for European silver difficult. However, the policy wasn't really that well enforced, and many countries, especially European powers like the Dutch, Spanish and Portuguese were able to conduct trade with the Ming dynasty by one way or another. Foreign trade during the Ming dynasty was never fully banned. By the mid-16th century, during the slow decline of the Ming dynasty, this policy was ultimately reversed due to Chinese officials and emperors recognizing the growing economic weakness of their country, but honestly by then, it was too little, far too late for the Ming economy.
The increases in revenue might have helped Ming China's economy somewhat, yes. But honestly the Ming dynasty had long shot off its own feet, economically speaking. Low tax rates, monetary contractions due to a restrictive silver market during the final decades of the Ming dynasty and even an Ice Age all spelled it out for the Ming dynasty. They were bound to fall into turmoil at one point or another and sure enough, they went into the pages of history as they once came - felled at the hands of a peasant revolt. If you really want to facepalm your head into oblivion, just read up on the Ming Dynasty's amazing management of the official salt economy... A real thriller.
One small note: There was also some degree of mistrust and disdain directed towards the merchant class, particularly by Confucian scholar bureaucrats as merchants were actually quite low on the official Confucian social hierarchy.