Controllers.prototype.editBilling = (function(){
return newEditBilling;
function newEditBilling(core, target, rcb){
return NewEditBilling(core.userID, core.notifications, target, rcb);
}
})();
var NewEditBilling = (function(){
var tmpl = newTemplate(),
ccRowTmpl = newCCRowTmpl(),
invalidHash = getInvalidHash();
var ErrNoCC = "no credit card available";
return newEditBilling;
function newEditBilling(userID, notifications, target, rcb, scb){
return new EditBilling(userID, notifications, target, rcb, scb);
}
function EditBilling(userID, notifications, target, returnCB, saveCB){
var events = new EventManager,
data = null,
element = null,
elements = null;
this.exit = exit;
apiRequest("GET", "billing", userID, null, init);
function init(body, sc){
element = getElementFromString(tmpl);
elements = new Elements(element, {
"cc" : {},
"footer" : { "selector" : "footer" },
"saveBtn" : { "selector" : ".save" },
"deleteBtn" : {}
})
refresh(body, sc);
events.add(elements.saveBtn, "click", save);
elements.deleteBtn = new ConfirmationButton(elements.footer, {
'color' : 'hot',
'text' : 'Delete',
'className' : 'auto',
'confirmColor' : 'mist',
'confirmText' : 'Are you sure?'
}, deleteAction);
if(!!returnCB){
returnCB();
returnCB = null;
}
target.appendChild(element);
}
function refresh(body, sc){
var d = !body.error ? body : {};
if(body.error && !!body.errors){
notifications.setMissingFields(body.errors);
return
}
if(d.isIO){
return
}
if(!!elements.cc){
removeChild(element, elements.cc);
elements.cc.exit();
}
elements.cc = new CC(d.cc);
element.insertBefore(elements.cc.getEle(), elements.footer);
}
function save(e){
preventDefault(e);
apiRequest("PUT", "payments/cc", userID, elements.cc, saveCb);
ga('send', 'event', 'Billing', 'Add credit card', userID);
}
function saveCb(body, sc){
if (sc !== 200) {
if(!!body && !!body.errors){
notifications.setMissingFields(body.errors.map(function(k){ return capitalizeN(0, k); }));
return;
}
notifications.setError("An error has occured while saving");
return;
}
notifications.setSuccess("Payment information successfully updated");
apiRequest("GET", "billing", userID, null, refresh);
if(!!saveCB){
saveCB();
}
}
function deleteAction(){
apiRequest("DELETE", "payments/cc", userID, null, saveCb);
ga('send', 'event', 'Billing', 'Delete credit card', userID);
}
function exit(){
if(!!element) removeChild(target, element);
if(!!elements) elements.exit();
if(!!events) events.reset();
housekeeping();
}
function housekeeping(){ events = data = element = elements = null; }
}
function Billing(events, obj){
var d = obj || {};
this.isIO = d.isIO === true;
this.cc = d.isIO !== true ? new CC(events, d.cc) : null;
}
function CC(obj){
var cc = obj || {},
ele = null,
events = new EventManager,
self = this;
this.firstName = cc.firstName || "";
this.lastName = cc.lastName || "";
this.address = cc.address || "";
this.city = cc.city || "";
this.state = cc.state || "";
this.zip = cc.zip || "";
this.cardNumber = cc.cardNumber || "";
this.cvv = cc.cvv || "";
this.expMonth = cc.expMonth || "";
this.expYear = cc.expYear || "";
this.exit = exit;
this.getEle = function(){ return ele; };
init();
function ccRow(key, value, size, placeholder){
this.key = key;
this.value = value;
this.size = size;
this.placeholder = placeholder;
}
function newCCRow(key, value, size, placeholder){
var ele = getElementFromString(render(ccRowTmpl, new ccRow(key, value, size, placeholder)));
events.add(ele, "keyup", function(k,sz, p){
return function(e){
if(e.keyCode > 36 && e.keyCode < 41){
return
}
if(key === "cardNumber"){
e.target.value = addDashes(removeDashes(e.target.value))
set(key, removeDashes(e.target.value));
return
}
set(key, e.target.value)
}
}(key, size, placeholder));
return ele;
}
function init(){
ele = document.createElement("cc");
ele.appendChild(newCCRow("firstName", self.firstName, "medium", "First name"));
ele.appendChild(newCCRow("lastName", self.lastName, "medium", "Last name"));
ele.appendChild(newCCRow("address", self.address, "wide", "Address"));
ele.appendChild(newCCRow("city", self.city, "medium", "City"));
ele.appendChild(newCCRow("state", self.state, "smallish", "State"));
ele.appendChild(newCCRow("zip", self.zip, "medium", "Zipcode"));
ele.appendChild(getElementFromString(""));
ele.appendChild(newCCRow("cardNumber", addDashes(self.cardNumber), "wide", "Card number"));
ele.appendChild(newCCRow("cvv", self.cvv, "tiny", "CVV"));
ele.appendChild(newCCRow("expMonth", self.expMonth, "tiny", "MM"));
ele.appendChild(newCCRow("expYear", self.expYear, "tiny", "YY"));
ele.appendChild(getElementFromString(""));
}
function addDashes(str){
var nstr = "",
addDash = false;
for(var i=0; i' + header + footer + '';
}
function newCCRowTmpl(){
return '';
}
})();