Question

I have a process that creates two database table entries, and sends two requests to the authorize.net api.

Now, if at any point in the process the program errors out for any reason, I'd like to 'rollback'. Any entry created in our system should be removed and the user should be notified that there was an error and should be given the option to retry.

A watered down version of the flow looks like this:

Create Account Entry -> Create User Entry -> Send one time payment request to Authorize.net -> Save result of one time payment request from Authorize.net -> Send recurring payment request to Authorize.net -> Save result of recurring payment request from Authorize.net

What's the best way to handle a situation like this?

Was it helpful?

Solution

You simply need to use an InnoDB table and you can then use the standard transaction processing statements. (i.e.: You'd use a begin, attempt to carry out the necessary processing and then either issue a commit or a rollback accordingly.)

OTHER TIPS

I would use regular InnoDB transactions to handle the database part. Rolling back the Authorize.NET calls would be trickier though and would just require some very careful exception handling.

You can wrap you queries in transaction please note the tables you want to rollback need to have their engine set to innodb. Heres an example of how the code might look:

//Wrap your queries in a transaction

    mysql_query("START TRANSACTION"); 

        try{ 
            //your stuff - if your stuff is successful commit, in the event of an error rollback

            if($success){
                mysql_query("COMMIT"); 
            } 
            else{ 
                mysql_query("ROLLBACK"); 
            } 
        }catch(Exception $e){ 
            //if something goes seriously wrong
            mysql_query("ROLLBACK"); 
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top