We derive optimal rules for paying hospitals for non-emergency care when providers choose quality and capacity, and patient demand is rationed by waiting time. Waiting for treatment is costly for patients, so that hospital payment rules should take account of their effect on waiting time as well as on quality. Since deterministic waiting time models imply that profit maximising hospitals will never choose to have both positive quality and positive waiting time, we develop a stochastic model of rationing by waiting in which both quality and expected waiting are positive in equilibrium. We use it to show that, although a prospective output price gives hospitals an incentive to attract patients by raising quality and reducing waiting times, it must be supplemented by a price attached to hospital decisions on quality or capacity or to a performance indicator which depends on those decisions (such as average waiting time, or average length of stay). A prospective output price by itself can support the optimal quality and waiting time distribution only if the welfare function respects patient preferences over quality and waiting time, if patients’ marginal rates of substitution between quality and waiting time are independent of income, and if waiting for treatment does not reduce the productivity of patients. If these conditions do not hold, supplementing the output price with a reward linked to the hospital's cost can increase welfare, though it is possible that costs should be taxed rather than subsidised.