Did Job end up suffering because he brought it upon himself by doing wrong or sinning? Or was it God who allowed Job to suffer for His purpose?

The Bible gives the following answer:

Then the Lord asked Satan, “Have you noticed my servant Job? He is the finest man in all the earth. He is blameless—a man of complete integrity. He fears God and stays away from evil.”

Satan replied to the Lord, “Yes, but Job has good reason to fear God. You have always put a wall of protection around him and his home and his property. You have made him prosper in everything he does. Look how rich he is! But reach out and take away everything he has, and he will surely curse you to your face!”

 “All right, you may test him,” the Lord said to Satan. “Do whatever you want with everything he possesses, but don’t harm him physically.” So Satan left the Lord’s presence. Job 1:8-12 NLT

Then the Lord asked Satan, “Have you noticed my servant Job? He is the finest man in all the earth. He is blameless—a man of complete integrity. He fears God and stays away from evil. And he has maintained his integrity, even though you urged me to harm him without cause.”

Satan replied to the Lord, “Skin for skin! A man will give up everything he has to save his life. But reach out and take away his health, and he will surely curse you to your face!”

“All right, do with him as you please,” the Lord said to Satan. “But spare his life.”  So Satan left the Lord’s presence, and he struck Job with terrible boils from head to foot. Job 2:3-7 NLT

So, it is God who allows what will happen to people, whether good or bad!

That is why the Scripture says;

“So it is God who decides to show mercy. We can neither choose it nor work for it. For the Scriptures say that God told Pharaoh, “I have appointed you for the very purpose of displaying my power in you and to spread my fame throughout the earth.” So you see, God chooses to show mercy to some, and he chooses to harden the hearts of others so they refuse to listen.” Romans 9:16-18 NLT

Therefore, it says that, “God is working in you, giving you the desire and the power to do what pleases Him!” Philippians 2:13 NLT