redirect and post data to payment gateway after database insert.

I am submitting cart data to a database by using php/ajax/jQuery. If the database insert is successful, I then need to post data from the same page to the payment gateway. The instructions from the payment gateway are to post the form to their provided url with some hidden fields. But  I can't figure out how to store the data in the database at the same time or just before posting to that url. So far, in my jQuery I have posted to a method which inserts the data to the database. On my .done callback, I want to then after successful database entry post the data to the payment gateway url but not sure how to achieve this as I actually need the page to redirect to the url.

$(document).ready(function () {
	$('.placeOrder').on('click', function (e) {
		e.preventDefault();
		var form = $( '#checkoutForm' ).serialize();
		$.ajax({
				url: url + '/CartAjax/TempOrder',
				type: 'POST',
				dataType: 'json',
				data: form,
				beforeSend: function () {
					$('.placeOrder').prop('disabled', true);
					$('.spinner').show();
					$('.ordertext').hide();
				},
			})
			.done(function (data) {
				if (!data.success) {

					$( '.alert-danger' ).append(data.message).fadeIn();

				} else {

					// redirect to payment gateway and post the necessary hidden fields to that redirect page.	

				}
			})
			.fail(function (jqXHR, textStatus, errorThrown) {
				$('.alert-danger').append("Something went wrong. Try again later.");
				console.log(textStatus + ': ' + errorThrown);
				console.warn(jqXHR.responseText);
	  });
	});
});

Open in new window

LVL 1
Black SulfurAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ares KurkluSoftware EngineerCommented:
Can you maybe have 2 ajax calls one does the database insert then if that's successful you can proceed with the payment in the 2nd ajax call
like:
  $.ajax({ 
		//call the database save
                      success: function (data) {

                              //2nd ajax for payment
                         $.ajax({

                                 success: function (data) {

                                 },
                                 error: function (err) {
                                 }
                        });
                   },
                error: function (err) {
			//db save fail
                  }
     });

Open in new window

0
Chris StanyonWebDevCommented:
It depends on how you have your HTML setup and whether the POST to your payment gateway needs to be a 'normal' post or an AJAX Post.

In your code, it looks like you are serializing #checkoutForm to insert into the DB. If that's the form that you want to submit to your Payment gatway, then you should be able to just trigger the submit() method:

.done(function (data) {
    if (!data.success) {
        $( '.alert-danger' ).append(data.message).fadeIn();
    } else {
        $('#checkoutForm').submit();
    }
}

Open in new window

2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Black SulfurAuthor Commented:
Chris, that was surprisingly easy and seemed to work. I thought it was going to be more complicated than that! :)
0
Chris StanyonWebDevCommented:
Easy is good :)
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.