At common law there is no prohibition such a term
Contract law is based on the premise that the contract is made by consenting people who want to make enforceable promises to each other and they know what’s best for themselves. At common law, you can’t agree to a contract and then avoid its consequences because you don’t like them or don’t think they are “fair”.
At common law, the only way to avoid a contractural obligation is to demonstrate that it is unconscionable, that is, the term is so onerous that no reasonable person would have agreed to it. A “pay when paid” clause is not unconscionable.
Contracts are about risk allocation - if a subcontractor agrees to assume the risk of the principal not paying the contractor there there is nothing to stop them doing so.
Statute law sometimes recognises that people need protection
The conceit of the common law is that contracts are formed by parties of relatively equal power who are able to negotiate and agree the terms of their contract. Since this is not necessarily the case, the legislature in various jurisdictions has stepped in to regulate some contracts.
Most commonly this occurs in the context of consumer protection law. Canada (and it’s provinces) have these laws so you need to check if your contract is covered - “consumer” can include businesses in some jurisdictions.
Further, in Canada (and it’s provinces), certain contracts in building and construction are covered by prompt payment laws which, if they apply, make “pay when paid” clauses void.