Hey Yair.
Your code runs on the front-end side. What happens is that after the payment succeeds, the user closes the site/redirect to another page/network fail before the code runs.
What you need to do is to move the function to the backend.
Use onPaymentUpdate() event on event.js and run the functionality there.
If you need data from the frontend, insert the data to a collection before the payment and update the collection (or another collection) after payment succeed