There is no way to prevent this. Accounting software is meant to record financial events. It’s not unheard of for customer to pay twice for the same invoice in error. So you would record money received against this invoice twice.
Any overpayment on invoice would be automatically transferred to general credit account for that customer and customer can be either paid back or leave it as a customer credit which can be applied to the next invoice.