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, refresh_url: '/checkout/stripe/json', after_update_url: '/checkout/stripe-details', init: function(params) { var that = this; for (var i in params) that[i] = params[i]; }, refresh: function(after) { var that = this; $.ajax({ url: that.refresh_url, 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_or_edit_if_empty: function() { var that = this; that.refresh(function() { if (!that.card_brand || !that.card_last4) that.edit(); else that.print(); }); }, 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')); if (that.cc.invoice.total > 0.00) { var msg = that.card_brand && that.card_last4 ? that.card_brand + ' ending in ' + that.card_last4 : 'You have no card on file.'; div.append($('') .append(msg).append(' ') .append($('').attr('href', '#').html('Edit' ).click(function(e) { e.preventDefault(); that.edit(); })).append(' ') .append($('').attr('href', '#').html('Remove' ).click(function(e) { e.preventDefault(); that.delete(); })) .append($('').attr('id', 'payment_message')) ); } else { div.append($('') .append("No payment is required at this time. ") .append($('').attr('href', '#').html('Edit').click(function(e) { e.preventDefault(); that.edit(); }) )); } $('#'+that.container).empty().append(div); }, edit: function() { var that = this; if (that.cc.invoice.total <= 0.00) { that.print(); return; } 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: that.after_update_url, type: 'put', data: { token: resp.id, card: resp.card }, success: function(resp2) { if (resp2.success) { that.customer_id = resp2.customer_id; that.card_brand = resp2.card_brand; that.card_last4 = resp2.card_last4; that.print(); that.cc.print_ready_message(); } if (resp2.error) $('#payment_message').html("" + resp2.error + "
"); } }); } }); }, delete: function(confirm) { var that = this; if (!confirm) { var div = $('').addClass('note error') .append($('').append("Are you sure you want to remove your payment method on file?")) .append($('') .append($('').attr('type', 'button').val('Yes').click(function() { that.delete(true); })).append(' ') .append($('').attr('type', 'button').val('No' ).click(function() { $('#payment_message').empty(); })) ); $('#payment_message').empty().append(div); return; } $('#payment_message').html("Removing payment method...
"); $.ajax({ url: '/checkout/payment-method', type: 'delete', success: function(resp) { if (resp.error) $('#payment_message').html("" + resp.error + "
"); else { that.customer_id = null; that.card_brand = null; that.card_last4 = null; that.edit(); that.cc.print_ready_message(); } } }); }, ready: function() { var that = this; if (that.cc.invoice.total <= 0.00) return true; if (!that.customer_id ) return false; if (!that.card_brand ) return false; if (!that.card_last4 ) return false; return true; } };