var StripePaymentMethodController = function(params) { this.init(params); }; StripePaymentMethodController.prototype = { container: 'payment_method_container', stripe_key: false, customer_id: false, card_brand: false, card_last4: false, card_name: false, card_zip: false, init: function(params) { var that = this; for (var i in params) that[i] = params[i]; }, refresh: function(after) { var that = this; $.ajax({ url: '/checkout/stripe/json', type: 'get', success: function(resp) { that.stripe_key = resp.stripe_key; that.customer_id = resp.customer_id; that.card_brand = resp.card_brand; that.card_last4 = resp.card_last4; that.cc.print_ready_message(); if (after) after(); } }); }, print: function() { var that = this; if (!that.stripe_key) { that.refresh(function() { that.print(); }); return; } var msg = that.card_brand && that.card_last4 ? that.card_brand + ' ending in ' + that.card_last4 : 'You have no card on file.'; var div = $('
') .append($('').html('Payment Method')) .append($('') .append(msg).append(' ') .append($('').attr('href', '#').html('Edit').click(function(e) { e.preventDefault(); that.edit(); }) )); $('#'+that.container).empty().append(div); }, edit: function() { var that = this; var form = $('') .attr('action', '') .attr('method', 'post') .attr('id', 'stripe_form') .addClass('stripe_form') .submit(function(e) { e.preventDefault(); that.update(); return false; }); form.append($('').addClass('card_number_container') .append($('').attr('id', 'card_number').attr('type', 'tel').attr('autocomplete', 'off').attr('autocorrect', 'off').attr('spellcheck', 'off').attr('autocapitalize', 'off').attr('placeholder', 'Card number')) .append($('').addClass('svg icon').css('width', '30px').css('height', '30px').html(''))); form.append($('').addClass('card_exp_container') .append($('').attr('id', 'card_exp').attr('type', 'tel').attr('autocomplete', 'off').attr('autocorrect', 'off').attr('spellcheck', 'off').attr('autocapitalize', 'off').attr('placeholder', 'MM / YY').attr('x-autocompletetype', 'off').attr('autocompletetype', 'off')) .append($('').addClass('svg icon').css('width', '30px').css('height', '30px').html(''))); form.append($('').addClass('card_cvc_container') .append($('').attr('id', 'card_cvc').attr('type', 'tel').attr('autocomplete', 'off').attr('autocorrect', 'off').attr('spellcheck', 'off').attr('autocapitalize', 'off').attr('placeholder', 'CVC').attr('maxlength', '4')) .append($('" + error + "
"); return; } $('#save_payment_btn').attr('disabled', 'true').val('Saving card...'); Stripe.setPublishableKey(that.stripe_key); Stripe.card.createToken(info, function(status, resp) { if (resp.error) { $('#save_payment_btn').attr('disabled', 'false').val('Save Payment Method'); $('#payment_message').html("" + resp.error.message + "
"); } else { that.card_brand = resp.card.brand; that.card_last4 = resp.card.last4; $.ajax({ url: '/checkout/stripe-details', type: 'put', data: { token: resp.id, card: resp.card }, success: function(resp2) { if (resp2.success) { that.customer_id = resp.customer_id; that.print(); } if (resp2.error) $('#payment_message').html("" + resp2.error + "
"); } }); } }); }, ready: function() { var that = this; if (!that.customer_id ) return false; if (!that.card_brand ) return false; if (!that.card_last4 ) return false; return true; } };