| 1 |
(function ( $, undefined ) { |
| 2 |
var $form = $( 'form.checkout, form#order_review' ); |
| 3 |
|
| 4 |
function omiseFormHandler(){ |
| 5 |
function showError(message){ |
| 6 |
if(!message){ |
| 7 |
return; |
| 8 |
} |
| 9 |
$(".woocommerce-error, input.omise_token").remove(); |
| 10 |
|
| 11 |
$ulError = $("<ul>").addClass("woocommerce-error"); |
| 12 |
|
| 13 |
if($.isArray(message)){ |
| 14 |
$.each(message, function(i,v){ |
| 15 |
$ulError.append($("<li>" + v + "</li>")); |
| 16 |
}) |
| 17 |
}else{ |
| 18 |
$ulError.html("<li>" + message + "</li>"); |
| 19 |
} |
| 20 |
|
| 21 |
$form.prepend( $ulError ); |
| 22 |
$("html, body").animate({ |
| 23 |
scrollTop:0 |
| 24 |
},"slow"); |
| 25 |
} |
| 26 |
|
| 27 |
function hideError(){ |
| 28 |
$(".woocommerce-error").remove(); |
| 29 |
} |
| 30 |
|
| 31 |
function validSelection(){ |
| 32 |
$card_list = $("input[name='card_id']"); |
| 33 |
$selected_card_id = $("input[name='card_id']:checked"); |
| 34 |
// there is some existing cards but nothing selected then warning |
| 35 |
if($card_list.length > 0 && $selected_card_id.length === 0){ |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
function getSelectedCardId(){ |
| 43 |
$selected_card_id = $("input[name='card_id']:checked"); |
| 44 |
if($selected_card_id.length > 0){ |
| 45 |
return $selected_card_id.val(); |
| 46 |
} |
| 47 |
|
| 48 |
return ""; |
| 49 |
} |
| 50 |
|
| 51 |
if ( $( '#payment_method_omise' ).is( ':checked' ) ) { |
| 52 |
if( !validSelection() ){ |
| 53 |
showError("Please select a card or enter new payment information"); |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
if( getSelectedCardId() !== "" ) |
| 58 |
{ |
| 59 |
//submit the form right away if the card_id is not blank |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
if ( 0 === $( 'input.omise_token' ).length ) { |
| 64 |
$form.block({ |
| 65 |
message: null, |
| 66 |
overlayCSS: { |
| 67 |
background: '#fff url(' + wc_checkout_params.ajax_loader_url + ') no-repeat center', |
| 68 |
backgroundSize: '16px 16px', |
| 69 |
opacity: 0.6 |
| 70 |
} |
| 71 |
}); |
| 72 |
|
| 73 |
var omise_card_name = $( '#omise_card_name' ).val(), |
| 74 |
omise_card_number = $( '#omise_card_number' ).val(), |
| 75 |
omise_card_expiration_month = $( '#omise_card_expiration_month' ).val(), |
| 76 |
omise_card_expiration_year = $( '#omise_card_expiration_year' ).val(), |
| 77 |
omise_card_security_code = $( '#omise_card_security_code' ).val(); |
| 78 |
|
| 79 |
// Serialize the card into a valid card object. |
| 80 |
var card = { |
| 81 |
"name": omise_card_name, |
| 82 |
"number": omise_card_number, |
| 83 |
"expiration_month": omise_card_expiration_month, |
| 84 |
"expiration_year": omise_card_expiration_year, |
| 85 |
"security_code": omise_card_security_code |
| 86 |
}; |
| 87 |
|
| 88 |
var errors = OmiseUtil.validate_card(card); |
| 89 |
if(errors.length > 0){ |
| 90 |
showError(errors); |
| 91 |
$form.unblock(); |
| 92 |
return false; |
| 93 |
}else{ |
| 94 |
hideError(); |
| 95 |
if(Omise){ |
| 96 |
Omise.setPublicKey(omise_params.key); |
| 97 |
Omise.createToken("card", card, function (statusCode, response) { |
| 98 |
if (statusCode == 200) { |
| 99 |
$form.append( '<input type="hidden" class="omise_token" name="omise_token" value="' + response.id + '"/>' ); |
| 100 |
$( '#omise_card_name' ).val(""); |
| 101 |
$( '#omise_card_number' ).val(""); |
| 102 |
$( '#omise_card_expiration_month' ).val(""); |
| 103 |
$( '#omise_card_expiration_year' ).val(""); |
| 104 |
$( '#omise_card_security_code' ).val(""); |
| 105 |
$form.submit(); |
| 106 |
} else { |
| 107 |
if(response.message){ |
| 108 |
showError( "Unable to process payment with Omise. " + response.message ); |
| 109 |
}else if(response.responseJSON && response.responseJSON.message){ |
| 110 |
showError( "Unable to process payment with Omise. " + response.responseJSON.message ); |
| 111 |
}else if(response.status==0){ |
| 112 |
showError( "Unable to process payment with Omise. No response from Omise Api." ); |
| 113 |
}else { |
| 114 |
showError( "Unable to process payment with Omise [ status=" + response.status + " ]" ); |
| 115 |
} |
| 116 |
$form.unblock(); |
| 117 |
}; |
| 118 |
}); |
| 119 |
}else{ |
| 120 |
showError( 'Something wrong with connection to Omise.js. Please check your network connection' ); |
| 121 |
$form.unblock(); |
| 122 |
} |
| 123 |
|
| 124 |
return false; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
$(function(){ |
| 132 |
$( 'body' ).on( 'checkout_error', function () { |
| 133 |
$( '.omise_token' ).remove(); |
| 134 |
}); |
| 135 |
|
| 136 |
$( 'form.checkout' ).unbind('checkout_place_order_omise'); |
| 137 |
$( 'form.checkout' ).on( 'checkout_place_order_omise', function () { |
| 138 |
return omiseFormHandler(); |
| 139 |
}); |
| 140 |
|
| 141 |
/* Pay Page Form */ |
| 142 |
$( 'form#order_review' ).on( 'submit', function () { |
| 143 |
return omiseFormHandler(); |
| 144 |
}); |
| 145 |
|
| 146 |
/* Both Forms */ |
| 147 |
$( 'form.checkout, form#order_review' ).on( 'change', '#omise_cc_form input', function() { |
| 148 |
$( '.omise_token' ).remove(); |
| 149 |
}); |
| 150 |
}) |
| 151 |
})(jQuery) |
| 152 |
|