The problem with my phone was solved by replacing the battery. As I explained above: I might have had a different problem all along. Still it might help to just unplug the battery for 30 minutes or to replace it (new ones cost around 15 € - it is not that difficult to replace).
Same problem on mine, shutting down randomly and hard to restart, sometimes doesn't start with the charger connected, I wait a few minutes and it works again, so annoying, my battery seems fine, it doesn't go out fast. And % are okay. .
The issues with my phone were fixed after replacing the charging connector inside the phone: https://www.mytrendyphone.eu/shop/samsung-galaxy-a40-charging-connector-flex-cable-248127p.html
(This is the first link that came up with search: do not know if it is a good place to buy one. I bought mine via eBay).
It is something possible to do your self, but there is a danger of breaking the phone electronics in the opening process.
Replacement batteries are sold together with the DIY "phone opening kits": it would make sense to replace the battery also once the phone is open.
Still the same problem with the new charging connector, direct shutting down randomly, hard to restart (need to wait a few minutes or it doesn't light on at all) and sometimes stop charging at about 80% (writted "charging" but % are not going up) and I need to reconnect the usb cable to continue chargiing. i'll try to replace the battery in a few days..