The charging port might just need cleaning. I’m still using an iPhone 11 and the charging port was getting super unreliable. I cleaned it a couple of weeks ago - gently, with a toothpick and a flashlight, there was some lint stuck inside - and it’s worked perfectly since then.
This - I have the iFixit cleaning kit which has a really tiny plastic pick / brush thing - I think it might be a dental crevice cleaning tool, like floss but for bigger gaps. I got a wad of lint out of my lightning port and all of a sudden CarPlay works perfectly. Same thing happens to USB-C ports.