While implementing a payment gateway, I ran into the situation where it is best for the user experience to use popups for the 3D Secure process.
The flow is like this:
- User is on the payment page and enters credit card data into hosted fields.
- On the "Buy" onclick event, I call the javascript snippet of the gateway to get the card token.
- I post that to the back-end.
- The back-end checks if 3DS is required.
- If yes, the front-end opens a new window with the 3DS form.
The code for the onclick is the following:
var win;
$("#buy").click(function() {
gateway.Tokenize(function(token) {
$.ajax({
url: 'backend.php',
type: "POST",
data: {
token: token
}
}).done(function(data) {
if (data.threeDRequired) {
win = window.open(data.url, "_blank");
win.focus();
}
});
});
});
Normally there is no problem with this and the pop up opens but if the Tokenize
method or the $.ajax
call takes too long to complete, the popup blocker gets triggered. Even adding async: false
to the $.ajax
call does not help.
Is there anything I can do to make that work in all cases? The only solution I can think of currently is to either open a popup right at the start of the click event and then load the URL when it is available or to let the user click an additional button if the popup got blocked. Both do not seem ideal.