If it runs OK off the charger then thats obviously giving the right amount of power out. One item eliminated. If the battery charge control in the laptop is misbehaving, that suddenly becomes a job for a man who knows. A new battery will probably be cheaper to try than a trip to a repair shop and the basic charge for applying a coat of looking at. A google for you model of computer plus something like "test battery charge" might reveal a forum where somebody has had the problem, and, more importantly, where somebody has given the answer.
If it is the battery that has gone past its sell by date, there are plenty of aftermarket suppliers. I have used such a couple of times, about half the original manufacturers price, and no problems, other than the replacements claiming higher capacity. One of the batteries involved was old in the first place, but the laptop had been set incorrectly for using battery power, left switched on, and the lithium battery died. The other was just old. That one charged, but flattened rapidly. The other just failed to show a charge.