Customs and immigration – China transit visa: fly from DPS (Indonesia) to Shanghai with a stop (2 hours) in Beijing

Flight from DPS to Shanghai with a 2-hour stopover in Beijing. Do I have to go through immigration in Beijing or can I do it in Shanghai, where I would like to stay for 5 days with a transit visa?

I just do not want any problems when I land in Beijing. I will not leave the airport during my 2-hour stay.