We study a form of refined class number formula (resp. type number formula) for maximal orders in totally definite quaternion algebras over real quadratic fields, by taking into consideration the automorphism groups of right ideal classes (resp. unit groups of maximal orders). For each finite noncyclic group $G$, we give an explicit formula for the number of conjugacy classes of maximal orders whose unit groups modulo center are isomorphic to $G$, and write down a representative for each conjugacy class. This leads to a complete recipe (even explicit formulas in special cases) for the refined class number formula for all finite groups. As an application, we prove the existence of superspecial abelian surfaces whose endomorphism algebras coincide with $mathbb{Q}(sqrt{p})$ in all positive characteristic $p otequiv 1pmod{24}$.