Remove the keyboard and inspect the pen charging slot.
Look for dust, debris, or damage.
Clean the contacts gently with a dry microfiber cloth.
Make sure the pen snaps into place correctly.
The magnets should pull it into the correct charging position.
Check the keyboard connector.
Inspect the contacts where the keyboard attaches to the Surface for dirt or bent pins.
Install any pending Windows and Surface firmware updates.
Although this is unlikely to fix a charging circuit that has failed, it's worth ruling out firmware.
If the keyboard no longer charges any compatible Surface Slim Pen while other keyboards do, the keyboard's charging hardware has most likely developed a fault. Unfortunately, the charging components aren't user-repairable.
If the keyboard is under warranty
You can contact Microsoft Support to see if the keyboard is eligible for replacement. If it's out of warranty, replacement is usually the only practical solution.