Last active
December 19, 2015 08:59
-
-
Save canimus/5930272 to your computer and use it in GitHub Desktop.
Paymill Form
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
window.onload = -> | |
# Development | |
window.PAYMILL_PUBLIC_KEY = 'PUBLIC_TEST_KEY' | |
# Production | |
PaymillErrorHandler = (error_message) -> | |
# Add elements to container if don't exist | |
s = $('.container').children('#notification').size() | |
if s == 0 | |
$('.container').prepend('<div id="notification"><h2 id="notification-header">Payment Error<span class="close" onclick="$(this).parent().parent().remove()">×</span></h2><p id="notification-message"></p></div>') | |
$("#notification-message").text(error_message) | |
$(".submit-button").removeAttr("disabled") | |
return false | |
# Handling of payment form submission with Paymill API | |
# generated token. | |
PaymillResponseHandler = (error, result) -> | |
console.log("ErrorHandler: ") | |
console.log(error) | |
console.log("ResultHandler: ") | |
console.log(result) | |
if (error) | |
custom_message = switch | |
when error.apierror == "3ds_cancelled" then "User cancelled operation" | |
when error.apierror == "field_invalid_card_number" then "Invalid credit card number" | |
when error.apierror == "field_invalid_card_exp_year" then "Invalid year in your credit card" | |
when error.apierror == "field_invalid_card_exp_month" then "Invalid month in your credit card" | |
when error.apierror == "field_invalid_card_exp" then "Credit card expired" | |
when error.apierror == "field_invalid_card_cvc" then "Invalid checking number" | |
when error.apierror == "field_invalid_amount_int" then "Invalid amount for transaction" | |
return PaymillErrorHandler(custom_message) | |
else | |
form = $("#payment-form") | |
$("#notification").addClass("hidden").removeClass("alert alert-error") | |
$("#error-message").text("") | |
token_input = $("<input/>").attr("type", "hidden").attr("name", "paymillToken").val(result.token) | |
form.append(token_input) | |
form.get(0).submit() | |
# Validation of Credit Card information | |
# and token generation | |
PaymillFormSubmit = (event) -> | |
paymill.config('3ds_cancel_label', 'cancel'); | |
$('.submit-button').attr("disabled", "disabled") | |
# Validation of card number | |
if (false == paymill.validateCardNumber($('.card-number').val())) | |
return PaymillErrorHandler("Invalid credit card number") | |
# Validation of card expiration | |
if (false == paymill.validateExpiry($('.card-expiry-month').val(), $('.card-expiry-year').val())) | |
return PaymillErrorHandler("Credit card expired") | |
# Validation of card holder's name | |
if (true == ($('.card-holdername').val()=="")) | |
return PaymillErrorHandler("Card requires holder's name") | |
paymill.createToken({ | |
number:$('.card-number').val(), | |
exp_month:$('.card-expiry-month').val(), | |
exp_year:$('.card-expiry-year').val(), | |
cvc:$('.card-cvc').val(), | |
amount_int: 999, | |
currency: "EUR", | |
cardholder:$('.card-holdername').val() | |
}, PaymillResponseHandler) | |
return false | |
# Binding submit handler to payment form | |
$("#payment-form").submit(PaymillFormSubmit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Attention with the cardholder and cardholdername in first and second revision