add discount API and refactor some code.