Working on payment gateways

This commit is contained in:
Hillel Coren 2018-03-16 09:59:23 +02:00
parent 4cbed2e1c5
commit edda20fee7
2 changed files with 61 additions and 53 deletions

View File

@ -7,6 +7,7 @@
<script type="text/javascript" > <script type="text/javascript" >
$(function() { $(function() {
var $form = $('.payment-form'); var $form = $('.payment-form');
$form.unbind('submit');
braintree.setup("{{ $transactionToken }}", "custom", { braintree.setup("{{ $transactionToken }}", "custom", {
id: "payment-form", id: "payment-form",
hostedFields: { hostedFields: {
@ -36,7 +37,6 @@
}, },
onError: function(e) { onError: function(e) {
$form.find('button').prop('disabled', false); $form.find('button').prop('disabled', false);
NINJA.formIsSubmitted = false;
// Show the errors on the form // Show the errors on the form
if (e.details && e.details.invalidFieldKeys.length) { if (e.details && e.details.invalidFieldKeys.length) {
@ -56,13 +56,19 @@
$('#js-error-message').html(e.message).fadeIn(); $('#js-error-message').html(e.message).fadeIn();
} }
}, },
onPaymentMethodReceived: function(e) { onPaymentMethodReceived: function(event) {
if ($form.find('button').is(':disabled')) {
event.preventDefault();
return false;
}
// Disable the submit button to prevent repeated clicks // Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true); $form.find('button').prop('disabled', true);
$('#js-error-message').hide(); $('#js-error-message').hide();
// Insert the token into the form so it gets submitted to the server // Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="sourceToken"/>').val(e.nonce)); $form.append($('<input type="hidden" name="sourceToken"/>').val(event.nonce));
// and submit // and submit
$form.get(0).submit(); $form.get(0).submit();
} }

View File

@ -10,67 +10,69 @@
var countries = {!! Cache::get('countries')->pluck('iso_3166_2','id') !!}; var countries = {!! Cache::get('countries')->pluck('iso_3166_2','id') !!};
WePay.set_endpoint('{{ WEPAY_ENVIRONMENT }}'); WePay.set_endpoint('{{ WEPAY_ENVIRONMENT }}');
var $form = $('.payment-form'); var $form = $('.payment-form');
$('.payment-form').submit(function(event) { $('.payment-form').unbind('submit').submit(function(event) {
event.preventDefault(); if ($('#sourceToken').val()) {
// do nothing
var data = {
client_id: {{ WEPAY_CLIENT_ID }},
user_name: $('#first_name').val() + ' ' + $('#last_name').val(),
email: $('#email').val(),
cc_number: $('#card_number').val(),
cvv: $('#cvv').val(),
expiration_month: $('#expiration_month').val(),
expiration_year: $('#expiration_year').val(),
address: {
address1: $('#address1').val(),
address2: $('#address2').val(),
city: $('#city').val(),
country: countries[$("#country_id").val()]
}
};
if(data.address.country == 'US') {
data.address.zip = $('#postal_code').val();
} else { } else {
data.address.postcode = $('#postal_code').val(); event.preventDefault();
}
// Not including state/province, since WePay wants 2-letter codes and users enter the full name
// Disable the submit button to prevent repeated clicks if ($form.find('button').is(':disabled')) {
$form.find('button').prop('disabled', true); return false;
$('#js-error-message').hide(); }
var data = {
client_id: {{ WEPAY_CLIENT_ID }},
user_name: $('#first_name').val() + ' ' + $('#last_name').val(),
email: $('#email').val(),
cc_number: $('#card_number').val(),
cvv: $('#cvv').val(),
expiration_month: $('#expiration_month').val(),
expiration_year: $('#expiration_year').val(),
address: {
address1: $('#address1').val(),
address2: $('#address2').val(),
city: $('#city').val(),
country: countries[$("#country_id").val()]
}
};
if(data.address.country == 'US') {
data.address.zip = $('#postal_code').val();
} else {
data.address.postcode = $('#postal_code').val();
}
// Not including state/province, since WePay wants 2-letter codes and users enter the full name
// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);
$('#js-error-message').hide();
var response = WePay.credit_card.create(data, function(response) {
if (response.error) {
// Show the errors on the form
var error = response.error_description;
$form.find('button').prop('disabled', false);
$('#js-error-message').text(error).fadeIn();
} else {
// response contains id and card, which contains additional card details
var token = response.credit_card_id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="sourceToken"/>').val(token));
// and submit
$form.get(0).submit();
}
});
var response = WePay.credit_card.create(data, function(response) {
if (response.error) { if (response.error) {
// Show the errors on the form // Show the errors on the form
var error = response.error_description; var error = response.error_description;
$form.find('button').prop('disabled', false); $form.find('button').prop('disabled', false);
NINJA.formIsSubmitted = false;
$('#js-error-message').text(error).fadeIn(); $('#js-error-message').text(error).fadeIn();
} else {
if (NINJA.formIsSubmitted) {
return false;
}
NINJA.formIsSubmitted = true;
// response contains id and card, which contains additional card details
var token = response.credit_card_id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="sourceToken"/>').val(token));
// and submit
$form.get(0).submit();
} }
});
if (response.error) { // Prevent the form from submitting with the default action
// Show the errors on the form return false;
var error = response.error_description;
$form.find('button').prop('disabled', false);
NINJA.formIsSubmitted = false;
$('#js-error-message').text(error).fadeIn();
} }
// Prevent the form from submitting with the default action
return false;
}); });
}); });
</script> </script>