A sustain pedal can fit the socket and still behave incorrectly. Ask for its exact model and check your keyboard’s manual. If you need half-pedalling, check that the particular combination of keyboard and pedal supports it.
Inspect the cable and use the right socket
Look for crushed or split insulation where the cable enters the plug and pedal. The Yamaha P-225 and P-223 manual lists the FC4A switch and the half-pedal-capable FC3A as two different options for the SUSTAIN jack. On those keyboards, connect the pedal with the power off and keep your foot off it as you switch on, or the response can be reversed.
Listen to one note three ways
Play a note, let the key go and listen without the pedal. Repeat with the pedal pressed and listen for the longer tail. Then release the pedal and listen for the note to damp. Repeat a few times on a level floor to check that the lever doesn’t stick. If the response is backwards, reconnect as directed and check compatibility before assuming the pedal is faulty.