It was mentioned that the Companions of the Prophet (peace be upon him) refused to take the ransom, therefore the following Ayah was revealed that you may take it.
The general opinion of jurists has been settled that an Imam is given choice concerning captives: He may kill them or take ransom according to the general benefits of Muslims. It was a benefit for Muslim at that time that disbelievers must have been killed, but Allah forgave that action.
﴿فَكُلُوا مِمَّا غَنِمْتُمْ حَلاَلاً طَيِّباً وَاتَّقُوا اللّهَ إِنَّ اللّهَ غَفُورٌ رَّحِيمٌ﴾ [الأنفال: 69]
69 - So, eat from the booty which you gained from your enemy because it is lawful and has no sin. Fear Allah and do not disobey His Orders, for He is the Forgiving who forgives your past sins and All-Merciful who permitted you to take the booty as well as the ransom of freeing captives.
﴿يَا أَيُّهَا النَّبِيُّ قُل لِّمَن فِي أَيْدِيكُم مِّنَ الأَسْرَى إِن يَعْلَمِ اللّهُ فِي قُلُوبِكُمْ خَيْراً يُؤْتِكُمْ خَيْراً مِّمَّا أُخِذَ مِنكُمْ وَيَغْفِرْ لَكُمْ وَاللّهُ غَفُورٌ رَّحِيمٌ﴾ [الأنفال: 70]
70 - O Prophet, say to those whom you captured in the battle of Badr from among those who ransomed themselves: When Allah finds faith and truthfulness in your heart, He shall grant you money more than the amounted you paid to ransom yourselves, and shall forgive your sins and polytheism, for Allah is Oft-Forgiving for His Believing Servants and Merciful with them.
﴿وَإِن يُرِيدُوا خِيَانَتَكَ فَقَدْ خَانُوا اللّهَ مِن قَبْلُ فَأَمْكَنَ مِنْهُمْ وَاللّهُ عَلِيمٌ حَكِيمٌ﴾ [الأنفال: 71]
71 - If the captives want to break the covenant by not giving you the ransom, by fighting you again, or by helping the polytheists, they did so before the battle of Badr, but Allah gave you power over them and you killed and captured a number of them in Badr. So, let them expect the same destination because Allah is All-Knowing with His Creation and All-Wise in things which He ordains.
﴿إِنَّ الَّذِينَ آمَنُوا وَهَاجَرُوا وَجَاهَدُوا بِأَمْوَالِهِمْ وَأَنفُسِهِمْ فِي سَبِيلِ اللّهِ وَالَّذِينَ آوَوا وَّنَصَرُوا أُوْلَئِكَ بَعْضُهُمْ أَوْلِيَاء بَعْضٍ وَالَّذِينَ آمَنُوا وَلَمْ يُهَاجِرُوا مَا لَكُم مِّن وَلاَيَتِهِم مِّن شَيْءٍ حَتَّى يُهَاجِرُوا وَإِنِ اسْتَنصَرُوكُمْ فِي الدِّينِ فَعَلَيْكُمُ النَّصْرُ إِلاَّ عَلَى قَوْمٍ بَيْنَكُمْ وَبَيْنَهُم مِّيثَاقٌ وَاللّهُ بِمَا تَعْمَلُونَ بَصِيرٌ﴾ [الأنفال: 72]
72 - Indeed, those who believed, left their people and homes, and spent their monies in things that please Allah such as: Giving in charity to the needy or buying weapons and horses for the sake of Allah and exerted efforts in the way of Allah, they are the true believers. They are the Supporters who provided shelter to their Muslim brothers who migrated and dwelled them in their houses, preferred them to themselves, and aided them against their enemy by fighting with them, those are guardians to one another and may inherit from one another.
As for the believers who remained in their homes without migration, they shall not inherit from those who migrated even if they are their relatives until they migrate like them. If the believers who did not migrate seek your help in fighting against polytheists, you have to aid them because they are your Muslim brothers. So, you have to help them unless you have a covenant between you and polytheists, in this case, it is not permissible for you to break it. Verily, Allah is All-Acquainted with your actions, so do not disobey His Commands and do not violate His Limits.