2015-04-13 15:37:22 +00:00
|
|
|
/*global Simplify_commerce_params, SimplifyCommerce */
|
2014-08-15 09:56:19 +00:00
|
|
|
(function ( $ ) {
|
|
|
|
|
|
|
|
// Form handler
|
|
|
|
function simplifyFormHandler() {
|
2016-03-01 17:09:29 +00:00
|
|
|
var $form = $( 'form.checkout, form#order_review, form#add_payment_method' );
|
2014-08-15 09:56:19 +00:00
|
|
|
|
2016-05-26 14:46:33 +00:00
|
|
|
if ( ( $( '#payment_method_simplify_commerce' ).is( ':checked' ) && 'new' === $( 'input[name="wc-simplify_commerce-payment-token"]:checked' ).val() ) || ( '1' === $( '#woocommerce_add_payment_method' ).val() ) ) {
|
2014-08-15 09:56:19 +00:00
|
|
|
|
2016-03-01 13:08:25 +00:00
|
|
|
if ( 0 === $( 'input.simplify-token' ).length ) {
|
2014-08-15 09:56:19 +00:00
|
|
|
|
|
|
|
$form.block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
2014-11-14 11:52:44 +00:00
|
|
|
background: '#fff',
|
2014-08-15 09:56:19 +00:00
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-09 23:36:52 +00:00
|
|
|
var card = $( '#simplify_commerce-card-number' ).val(),
|
|
|
|
cvc = $( '#simplify_commerce-card-cvc' ).val(),
|
|
|
|
expiry = $.payment.cardExpiryVal( $( '#simplify_commerce-card-expiry' ).val() ),
|
2016-05-26 14:46:33 +00:00
|
|
|
address1 = $form.find( '#billing_address_1' ).val() || '',
|
|
|
|
address2 = $form.find( '#billing_address_2' ).val() || '',
|
|
|
|
addressCountry = $form.find( '#billing_country' ).val() || '',
|
|
|
|
addressState = $form.find( '#billing_state' ).val() || '',
|
|
|
|
addressCity = $form.find( '#billing_city' ).val() || '',
|
|
|
|
addressZip = $form.find( '#billing_postcode' ).val() || '';
|
2014-08-15 09:56:19 +00:00
|
|
|
|
2016-03-31 11:27:34 +00:00
|
|
|
addressZip = addressZip.replace( /-/g, '' );
|
2014-08-15 09:56:19 +00:00
|
|
|
card = card.replace( /\s/g, '' );
|
|
|
|
|
|
|
|
SimplifyCommerce.generateToken({
|
2014-08-21 16:03:16 +00:00
|
|
|
key: Simplify_commerce_params.key,
|
2014-08-15 09:56:19 +00:00
|
|
|
card: {
|
|
|
|
number: card,
|
|
|
|
cvc: cvc,
|
|
|
|
expMonth: expiry.month,
|
2015-11-09 23:36:52 +00:00
|
|
|
expYear: ( expiry.year - 2000 ),
|
|
|
|
addressLine1: address1,
|
|
|
|
addressLine2: address2,
|
|
|
|
addressCountry: addressCountry,
|
|
|
|
addressState: addressState,
|
|
|
|
addressZip: addressZip,
|
2015-11-10 00:49:53 +00:00
|
|
|
addressCity: addressCity
|
2014-08-15 09:56:19 +00:00
|
|
|
}
|
|
|
|
}, simplifyResponseHandler );
|
|
|
|
|
|
|
|
// Prevent the form from submitting
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-21 16:03:16 +00:00
|
|
|
// Handle Simplify response
|
2014-08-15 09:56:19 +00:00
|
|
|
function simplifyResponseHandler( data ) {
|
2016-02-16 18:07:27 +00:00
|
|
|
|
2016-02-17 14:49:47 +00:00
|
|
|
var $form = $( 'form.checkout, form#order_review, form#add_payment_method' ),
|
2016-02-16 18:07:27 +00:00
|
|
|
ccForm = $( '#wc-simplify_commerce-cc-form' );
|
2014-08-15 09:56:19 +00:00
|
|
|
|
|
|
|
if ( data.error ) {
|
|
|
|
|
|
|
|
// Show the errors on the form
|
|
|
|
$( '.woocommerce-error, .simplify-token', ccForm ).remove();
|
|
|
|
$form.unblock();
|
|
|
|
|
|
|
|
// Show any validation errors
|
|
|
|
if ( 'validation' === data.error.code ) {
|
|
|
|
var fieldErrors = data.error.fieldErrors,
|
|
|
|
fieldErrorsLength = fieldErrors.length,
|
|
|
|
errorList = '';
|
|
|
|
|
|
|
|
for ( var i = 0; i < fieldErrorsLength; i++ ) {
|
2014-08-21 16:03:16 +00:00
|
|
|
errorList += '<li>' + Simplify_commerce_params[ fieldErrors[i].field ] + ' ' + Simplify_commerce_params.is_invalid + ' - ' + fieldErrors[i].message + '.</li>';
|
2014-08-15 09:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ccForm.prepend( '<ul class="woocommerce-error">' + errorList + '</ul>' );
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Insert the token into the form so it gets submitted to the server
|
|
|
|
ccForm.append( '<input type="hidden" class="simplify-token" name="simplify_token" value="' + data.id + '"/>' );
|
|
|
|
$form.submit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$( function () {
|
|
|
|
|
2015-04-13 15:37:22 +00:00
|
|
|
$( document.body ).on( 'checkout_error', function () {
|
2014-08-15 09:56:19 +00:00
|
|
|
$( '.simplify-token' ).remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Checkout Form */
|
|
|
|
$( 'form.checkout' ).on( 'checkout_place_order_simplify_commerce', function () {
|
|
|
|
return simplifyFormHandler();
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Pay Page Form */
|
|
|
|
$( 'form#order_review' ).on( 'submit', function () {
|
|
|
|
return simplifyFormHandler();
|
|
|
|
});
|
|
|
|
|
2016-02-17 14:49:47 +00:00
|
|
|
/* Pay Page Form */
|
|
|
|
$( 'form#add_payment_method' ).on( 'submit', function () {
|
|
|
|
return simplifyFormHandler();
|
|
|
|
});
|
|
|
|
|
2014-08-15 09:56:19 +00:00
|
|
|
/* Both Forms */
|
2016-02-17 14:49:47 +00:00
|
|
|
$( 'form.checkout, form#order_review, form#add_payment_method' ).on( 'change', '#wc-simplify_commerce-cc-form input', function() {
|
2014-08-15 09:56:19 +00:00
|
|
|
$( '.simplify-token' ).remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}( jQuery ) );
|