ive fixed a few phones with this issue. it is usually the following. (most times its not software, but sometimes it is, so i would suggest a factory reset before spending any kind of money)
Lithium batteries ARE Consumable. This means they will go bad over time. usually starts after about 300-400 charge cycles. Replacing the battery fixes the problem about 80% of the time for my store
the other 20% is usually that the Charge port is causing the issue. the charge port reads that the cord is present, but will not pull the current into the phone. In which case, replacing the port will fix the issue.
now a minimal% of the time, there canbe a board level failure. in which case, it would be more expensive to repair, than to buy a new phone.