Skip to content

Instantly share code, notes, and snippets.

@canimus
Last active December 19, 2015 08:59
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save canimus/5930272 to your computer and use it in GitHub Desktop.
Paymill Form
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()">&times;</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)
@canimus
Copy link
Author

canimus commented Jul 4, 2013

Attention with the cardholder and cardholdername in first and second revision

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment