Created
May 28, 2018 06:47
-
-
Save osc2nuke/92348c5126a880195459a055d940cd58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getSubmitCardDetailsJavascript() { | |
$stripe_publishable_key = MODULE_PAYMENT_STRIPE_PUBLISHABLE_KEY; | |
$js = <<<EOD | |
<script src="https://js.stripe.com/v2/"></script> | |
<script> | |
$(function() { | |
Stripe.setPublishableKey('{$stripe_publishable_key}'); | |
$('form[name="checkout_confirmation"]').attr('id', 'payment-form'); | |
$('#payment-form').submit(function(event) { | |
var \$form = $(this); | |
if ( ($('#stripe_table').length < 1) || ($('form[name="checkout_confirmation"] input[name="stripe_card"]:radio:checked').val() == '0') ) { | |
// Disable the submit button to prevent repeated clicks | |
\$form.find('button').prop('disabled', true); | |
try { | |
Stripe.card.createToken(\$form, stripeResponseHandler); | |
} catch ( error ) { | |
\$form.find('.payment-errors').text(error); | |
} | |
// Prevent the form from submitting with the default action | |
return false; | |
} | |
}); | |
var stripeResponseHandler = function(status, response) { | |
var \$form = $('#payment-form'); | |
if (response.error) { | |
// Show the errors on the form | |
\$form.find('.payment-errors').text(response.error.message); | |
\$form.find('button').prop('disabled', false); | |
} else { | |
// token contains id, last4, and card type | |
var token = response.id; | |
// Insert the token into the form so it gets submitted to the server | |
\$form.append($('<input type="hidden" name="stripeToken" />').val(token)); | |
// and submit | |
\$form.get(0).submit(); | |
} | |
}; | |
if ( $('#stripe_table').length > 0 ) { | |
if ( typeof($('#stripe_table').parent().closest('table').attr('width')) == 'undefined' ) { | |
$('#stripe_table').parent().closest('table').attr('width', '100%'); | |
} | |
$('#stripe_table .moduleRowExtra').hide(); | |
$('#stripe_table_new_card').hide(); | |
$('form[name="checkout_confirmation"] input[name="stripe_card"]').change(function() { | |
var selected = $(this).val(); | |
if ( selected == '0' ) { | |
stripeShowNewCardFields(); | |
} else { | |
$('#stripe_table_new_card').hide(); | |
} | |
$('tr[id^="stripe_card_"]').removeClass('moduleRowSelected'); | |
$('#stripe_card_' + selected).addClass('moduleRowSelected'); | |
}); | |
$('form[name="checkout_confirmation"] input[name="stripe_card"]:first').prop('checked', true).trigger('change'); | |
$('#stripe_table .moduleRow').hover(function() { | |
$(this).addClass('moduleRowOver'); | |
}, function() { | |
$(this).removeClass('moduleRowOver'); | |
}).click(function(event) { | |
var target = $(event.target); | |
if ( !target.is('input:radio') ) { | |
$(this).find('input:radio').each(function() { | |
if ( $(this).prop('checked') == false ) { | |
$(this).prop('checked', true).trigger('change'); | |
} | |
}); | |
} | |
}); | |
} else { | |
if ( typeof($('#stripe_table_new_card').parent().closest('table').attr('width')) == 'undefined' ) { | |
$('#stripe_table_new_card').parent().closest('table').attr('width', '100%'); | |
} | |
} | |
}); | |
function stripeShowNewCardFields() { | |
$('#stripe_table_new_card').show(); | |
} | |
</script> | |
EOD; | |
return $js; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment