summaryrefslogtreecommitdiff
path: root/payments/static/js/payments/stripe.js
blob: a17a8e4e72a98d3a5aa44daa2f38fdf47c739f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
document.addEventListener('DOMContentLoaded', function () {
    var stripeInput = document.getElementById('id_stripe_token');
    var form = stripeInput.form;
    var publishableKey = stripeInput.attributes['data-publishable-key'].value;
    Stripe.setPublishableKey(publishableKey);
    form.addEventListener('submit', function (e) {
        var button = this.querySelector('[type=submit]');
        button.disabled = true;
        Stripe.card.createToken({
            name: this.elements['name'].value,
            number: this.elements['number'].value,
            cvc: this.elements['cvv2'].value,
            exp_month: this.elements['expiration_0'].value,
            exp_year: this.elements['expiration_1'].value,
            address_line1: stripeInput.attributes['data-address-line1'].value,
            address_line2: stripeInput.attributes['data-address-line2'].value,
            address_city: stripeInput.attributes['data-address-city'].value,
            address_state: stripeInput.attributes['data-address-state'].value,
            address_zip: stripeInput.attributes['data-address-zip'].value,
            address_country: stripeInput.attributes['data-address-country'].value
        }, function (status, response) {
            if (400 <= status && status <= 500) {
                alert(response.error.message);
                button.disabled = false;
            } else {
                stripeInput.value = response.id;
                form.submit();
            }
        });
        e.preventDefault();
    }, false);
}, false);