There are two conditions for a fee payment to be fully valid:
If a payment does not relate to a pending European patent application (e.g. it relates to a patent application already deemed to have been withdrawn), there is no legal basis for the payment; the amount paid must be refunded.
If the payment is made before or on the due date and if, no later than that date, the legal basis ceases to exist (e.g. because the patent application is deemed to be withdrawn or is withdrawn), the amount paid is to be refunded. This also applies to renewal fees validly paid before the due date (Rule 51(1), second sentence).