Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

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

Avatar of Ares Kurklu
Ares Kurklu
Flag of Australia image

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

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Crazy Horse

ASKER

Chris, that was surprisingly easy and seemed to work. I thought it was going to be more complicated than that! :)
Easy is good :)