Any way to run a block of code when a patron pledges/makes a payment?

I’m using the API in Python and was wondering if this was possible. It would make what I’m trying to do a lot easier.