checkout.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. function getPaymentPage(paymentMethod) {
  2. var check3ds = getUrlParameter('3ds');
  3. var url = 'route.php?r=getPaymentPage';
  4. if(check3ds == 'no') {
  5. url = url+'&3ds=no';
  6. }
  7. $.ajax({
  8. url: url,
  9. type: 'post',
  10. dataType: 'json',
  11. data: {paymentMethod: paymentMethod},
  12. success: function (response) {
  13. if (response.form) {
  14. $('body').append(response.form);
  15. if(response.paymentMethod == 'cc_merchantpage' || response.paymentMethod == 'installments_merchantpage') {
  16. showMerchantPage(response.url);
  17. }
  18. else if(response.paymentMethod == 'cc_merchantpage2') {
  19. var expDate = $('#payfort_fort_mp2_expiry_year').val()+''+$('#payfort_fort_mp2_expiry_month').val();
  20. var mp2_params = {};
  21. mp2_params.card_holder_name = $('#payfort_fort_mp2_card_holder_name').val();
  22. mp2_params.card_number = $('#payfort_fort_mp2_card_number').val();
  23. mp2_params.expiry_date = expDate;
  24. mp2_params.card_security_code = $('#payfort_fort_mp2_cvv').val();
  25. $.each(mp2_params, function(k, v){
  26. $('<input>').attr({
  27. type: 'hidden',
  28. id: k,
  29. name: k,
  30. value: v
  31. }).appendTo('#payfort_payment_form');
  32. });
  33. $('#payfort_payment_form input[type=submit]').click();
  34. }
  35. else{
  36. $('#payfort_payment_form input[type=submit]').click();
  37. }
  38. }
  39. }
  40. });
  41. }
  42. function showMerchantPage(merchantPageUrl) {
  43. if($("#payfort_merchant_page").size()) {
  44. $( "#payfort_merchant_page" ).remove();
  45. }
  46. $('<iframe name="payfort_merchant_page" id="payfort_merchant_page" height="630px" width="100%" frameborder="0" scrolling="no"></iframe>').appendTo('#pf_iframe_content');
  47. $( "#payfort_merchant_page" ).attr("src", merchantPageUrl);
  48. $( "#payfort_payment_form" ).attr("action", merchantPageUrl);
  49. $( "#payfort_payment_form" ).attr("target","payfort_merchant_page");
  50. $( "#payfort_payment_form" ).attr("method","POST");
  51. $('#payfort_payment_form input[type=submit]').click();
  52. //$( "#payfort_payment_form" ).submit();
  53. $( "#div-pf-iframe" ).show();
  54. }
  55. var getUrlParameter = function getUrlParameter(sParam) {
  56. var sPageURL = decodeURIComponent(window.location.search.substring(1)),
  57. sURLVariables = sPageURL.split('&'),
  58. sParameterName,
  59. i;
  60. for (i = 0; i < sURLVariables.length; i++) {
  61. sParameterName = sURLVariables[i].split('=');
  62. if (sParameterName[0] === sParam) {
  63. return sParameterName[1] === undefined ? true : sParameterName[1];
  64. }
  65. }
  66. };
  67. var payfortFort = (function () {
  68. return {
  69. validateCreditCard: function(element) {
  70. var isValid = false;
  71. var eleVal = $(element).val();
  72. eleVal = this.trimString(element.val());
  73. eleVal = eleVal.replace(/\s+/g, '');
  74. $(element).val(eleVal);
  75. $(element).validateCreditCard(function(result) {
  76. /*$('.log').html('Card type: ' + (result.card_type == null ? '-' : result.card_type.name)
  77. + '<br>Valid: ' + result.valid
  78. + '<br>Length valid: ' + result.length_valid
  79. + '<br>Luhn valid: ' + result.luhn_valid);*/
  80. isValid = result.valid;
  81. });
  82. return isValid;
  83. },
  84. validateCardHolderName: function(element) {
  85. $(element).val(this.trimString(element.val()));
  86. var cardHolderName = $(element).val();
  87. if(cardHolderName.length > 50) {
  88. return false;
  89. }
  90. return true;
  91. },
  92. validateCvc: function(element) {
  93. $(element).val(this.trimString(element.val()));
  94. var cvc = $(element).val();
  95. if(cvc.length > 4 || cvc.length == 0) {
  96. return false;
  97. }
  98. if(!this.isPosInteger(cvc)) {
  99. return false;
  100. }
  101. return true;
  102. },
  103. isDefined: function(variable) {
  104. if (typeof (variable) === 'undefined' || typeof (variable) === null) {
  105. return false;
  106. }
  107. return true;
  108. },
  109. trimString: function(str){
  110. return str.trim();
  111. },
  112. isPosInteger: function(data) {
  113. var objRegExp = /(^\d*$)/;
  114. return objRegExp.test( data );
  115. }
  116. };
  117. })();
  118. var payfortFortMerchantPage2 = (function () {
  119. return {
  120. validateCcForm: function () {
  121. this.hideError();
  122. var isValid = payfortFort.validateCardHolderName($('#payfort_fort_mp2_card_holder_name'));
  123. if(!isValid) {
  124. this.showError('Invalid Card Holder Name');
  125. return false;
  126. }
  127. isValid = payfortFort.validateCreditCard($('#payfort_fort_mp2_card_number'));
  128. if(!isValid) {
  129. this.showError('Invalid Credit Card Number');
  130. return false;
  131. }
  132. isValid = payfortFort.validateCvc($('#payfort_fort_mp2_cvv'));
  133. if(!isValid) {
  134. this.showError('Invalid Card CVV');
  135. return false;
  136. }
  137. return true;
  138. },
  139. showError: function(msg) {
  140. alert(msg);
  141. },
  142. hideError: function() {
  143. return;
  144. }
  145. };
  146. })();