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($('
').addClass('svg icon').css('width', '30px').css('height', '30px').html(''))); form.append($('
').addClass('card_name_container') .append($('').attr('id', 'card_name').attr('type', 'text').attr('autocomplete', 'off').attr('autocorrect', 'off').attr('spellcheck', 'off').attr('autocapitalize', 'on').attr('placeholder', 'Name on card'))); form.append($('
').addClass('card_zip_container') .append($('').attr('id', 'card_zip').attr('type', 'tel').attr('autocomplete', 'off').attr('autocorrect', 'off').attr('spellcheck', 'off').attr('autocapitalize', 'on').attr('placeholder', 'Zip code'))); form.append($('
').attr('id', 'payment_message')) form.append($('

').addClass('payment_controls') .append($('').attr('type', 'button').attr('id', 'cancel_payment_btn').val('Cancel' ).click(function(e) { that.print(); })).append(' ') .append($('').attr('type', 'submit').attr('id', 'save_payment_btn').val('Save' )) ); $('#payment_method_container').empty() .append($('

').html('Payment Method')) .append(form); $('#stripe_form .card_number_container input').payment('formatCardNumber'); $('#stripe_form .card_exp_container input').payment('formatCardExpiry'); $('#stripe_form .card_cvc_container input').payment('formatCardCVC'); $('#checkout-continue').empty().append($('').attr('href', '/').html('return to the store')); }, update: function() { var that = this; var info = { number: $('#card_number').val(), exp: $('#card_exp').val(), cvc: $('#card_cvc').val(), name: $('card_name').val(), address_zip: $('card_zip').val() }; var exp = info.exp.split('/'); var m = exp.length > 0 ? exp[0] : ''; var y = exp.length > 1 ? exp[1] : ''; var error = false; if (!$.payment.validateCardNumber(info.number)) error = "Invalid card number."; if (!$.payment.validateCardExpiry(m, y)) error = "Invalid expiration date."; if (!$.payment.validateCardCVC(info.cvc)) error = "Invalid CVC."; if (error) { $('#payment_message').html("

" + 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; } };