reimplement payments system using stripe
[shapado:shapado.git] / app / assets / javascripts / payments / payments.js
1 var Payments = {
2   initialize: function() {
3     var form = $("#payment-form");
4     Stripe.setPublishableKey(form.attr("data-token"));
5     form.submit(function(event) {
6       // disable the submit button to prevent repeated clicks
7       $('.submit-button').attr("disabled", "disabled");
8
9       console.log(form.find('.card-number').val())
10       console.log(form.find('.card-cvc').val())
11       console.log(form.find('.card-expiry-month').val())
12       console.log(form.find('.card-expiry-year').val())
13       Stripe.createToken({
14           number: form.find('.card-number').val(),
15           cvc: form.find('.card-cvc').val(),
16           exp_month: form.find('.card-expiry-month').val(),
17           exp_year: form.find('.card-expiry-year').val()
18       }, Payments.handler);
19
20       // prevent the form from submitting with the default action
21       return false;
22     });
23   },
24   handler: function(status, response) {
25     if (response.error) {
26       $(".payment-errors").html(response.error.message);
27     } else {
28       var form$ = $("#payment-form");
29       var token = response['id'];
30
31       form$.append("<input type='hidden' name='stripeToken' value='" + token + "'/>");
32       form$.get(0).submit();
33     }
34   }
35 };
36
37 $(document).ready(function() {
38   Payments.initialize();
39 });