You cannot pre-empt the attack with Contingency
Contingency says:
choose a trigger under which the spell will be cast, using the same restrictions as for the trigger of a Ready action
And the Ready activity defines the following constraints for the trigger of the Ready action:
Notably, the trigger must be something that happens in the game world and is observable by the character rather than a rules concept that doesn’t exist in world. For instance, if a player says, “I Ready to shoot an arrow at her if she uses a concentrate action,” or “I Ready to attack him if he has fewer than 47 Hit Points,” find out what their character is trying to specifically observe. If they don’t have a clear answer for that, they need to adjust their action.
So, you could probably not set a trigger against an Attack of Opportunity, which like turns and hit points is a rules concept. But you could set a trigger against something like "If I am moving past an opponent and that opponent attacks me with his weapon".
However, you would not become invisible before the attack resolves, because Contingency does not say it disrupts whatever its trigger is. This is different from an Attack of Opportunity, which says it can:
If your attack is a critical hit and the trigger was a manipulate action, you disrupt that action.
The rule for Disrupting Actions says:
When an action is disrupted, you still use the actions or reactions you committed and you still expend any costs, but the action’s effects don’t occur.
This means, if you do not disrupt the action, its effects will occur. You will become invisible, but it will be too late to influence the attack roll and wether it hit.
Can Attack of Opportunity disrupt a Contingency'd spell?
Attack of Opportunity can only disrupt a manipulate action.
If your attack is a critical hit and the trigger was a manipulate action, you disrupt that action.
Your Contingency says
During the casting, choose a trigger under which the spell will be cast, [...] Once contingency is cast, you can cause the companion spell to come into effect as a reaction with that trigger.
This is not a manipulate action, so AoO cannot disrupt it.