Both words have two meanings. Occur can mean "to take place/happen" or it can mean "to suddenly be realized by someone (the direct object)".
Example: "The urge to learn another language occurred to me one month ago."
Happen can mean "to take place/occur". The second meaning is like "occur" but it is physical; it means "to be experienced by someone (the direct object)".
Example: It happened to you? The same thing happened to me.
If the direct object is a person, nonliving thing, or nonhuman living thing, then you use the second meaning. If the direct object is a time, date, place (sometimes), or there is no direct object, then you use the first meaning. When you use the first meaning, it does not matter which word you choose.
Example of meaning 1: "The attack happened on Friday." "The attack occurred on Friday."
The object is a day.
Example of meaning 2: "The attack happened to John on Friday."
The object is John, and something physical was experienced by to John.