Purchases in the app

Hi, I´m developing an app to do tests of an academy. I want to make the app free for the users of that academy. And I want to add the posibility to purchase the app, and when someone make a payment, I want to create a new account in the database.

I want to execute some code (to create a new user in the database) but only when a user makes a payment.

How can I do this?