(function($) {
$.fn.getBreadCheckout = function(options) {
var settings = $.extend({
buttonId: 'bread-checkout-btn',
actAsLabel: false,
asLowAs: true,
allowCheckout: true,
storeUrl:null,
storeId:null,
accountUrl:null,
cardSelectOption:'Bread Financing'
}, options);
function toCents(num) {
if(typeof(num) === 'undefined' || parseInt(num) <= 0){
return 0;
}else{
num = num.replace(/[,$]+/g, "").trim();
}
return Math.round(parseFloat(num) * 100);
}
function breadDisplay(){
var html = '
\
\
\
\
\
';
return html;
//onclick="setRadioHiddenFields(this, "paymentSelection");"
}
function populateItems(){
var items = [];
$.each(cart.items, function(i,obj){
var opts = "";
if(obj.options.length) {
$.each(obj.options, function(i,opt) {
opts += " " + opt.name + " " + opt.value + " Option Price:" + opt.priceExtra;
});
}
items.push({
name: obj.name + opts,
price: toCents(obj.price),
sku: obj.code,
imageUrl: obj.imageUrl,
detailUrl: settings.storeUrl + obj.id + ".html",
quantity: parseInt(obj.qty)
});
});
return items;
}
function initBread(){
var items = populateItems();
var shippingContact = {
fullName: $('#shipping-full-name').val(),
address: $('#shipping-address1').val(),
address2: $('#shipping-address2').val(),
city: $('#shipping-city').val(),
state: $('#shipping-state').val(),
zip: $('#shipping-zip').val(),
phone: $('#shipping-phone').val()
};
var billingContact = {
fullName: $('#shipping-full-name').val(),
address: $('#shipping-address1').val(),
address2: $('#shipping-address2').val(),
city: $('#shipping-city').val(),
state: $('#shipping-state').val(),
zip: $('#shipping-zip').val(),
phone: $('#shipping-phone').val(),
email: $('#billing-email').val()
};
var opts = {
buttonId: settings.buttonId,
actAsLabel: settings.actAsLabel,
asLowAs: settings.asLowAs,
allowCheckout : settings.allowCheckout,
items: items,
shippingOptions: [{
type: 'Shipping',
typeId: 'GS_001',
cost: parseInt((orderShipping * 100)),
}],
customTotal: parseInt((orderTotal * 100)),
tax: parseInt((orderTax * 100)),
shippingContact: shippingContact,
billingContact: billingContact
};
opts.done = function(err, tx_token) {
if (err) {
console.error("There was an error: " + err);
return;
}
if (tx_token !== undefined) {
$('#vwcf-'+settings.storeId+'-breadtoken').val(tx_token);
$('#card-type').val(settings.cardSelectOption);
localStorage.setItem("tld-breadToken", tx_token);
localStorage.setItem("tld-breadChosen", true);
localStorage.setItem("tld-breadTotal", orderTotal);
$('#ys_breadBilling').addClass('breadChosen');
$('input[name="eventName.sbContinueEvent"]').trigger('click');
}
return;
};
return opts;
}
function inValidateBread(cancelTransaction){
console.log(cancelTransaction);
cancelTransaction = (cancelTransaction == undefined) ? false : true;
$('#vwcf-'+settings.storeId+'-breadtoken').val('');
if(cancelTransaction && settings.accountUrl != null && !isNullOrEmpty(localStorage.getItem("tld-breadToken"))){
$.ajax({
dataType: "jsonp",
url: settings.accountUrl+'api/bread/invalidate',
data:{
transactionId: localStorage.getItem("tld-breadToken")
},
success:function(data){
//console.log(data);
},
error:function(jqXHR, textStatus, errorThrown){
console.log(textStatus);
}
});
}
localStorage.setItem("tld-breadToken", '');
localStorage.setItem("tld-breadChosen", false);
localStorage.setItem("tld-breadTotal", 0);
}
function validateFields(){
$('.breadErrorLabel').remove();
var showBread = true;
$.each(settings.requiredElem, function (i, obj) {
var elem = $(obj);
elem.removeClass('error');
if (elem.val() === '') {
elem.addClass('error');
elem.after('');
showBread = false;
}
switch(obj){
case '#shipping-full-name':
if(elem.val().split(' ').length < 2){
elem.addClass('error');
elem.after('');
showBread = false;
}
break;
case '#shipping-state':
if(elem.val().length > 2){
elem.addClass('error');
elem.after('');
showBread = false;
}
break;
case '#merchant-selected-shipping-methods':
if(orderShipping <= 0){
elem.addClass('error');
elem.after('');
showBread = false;
}
break;
}
});
if(!isNullOrEmpty($('#vwcf-'+settings.storeId+'-breadtoken').val()) && localStorage.getItem("tld-breadChosen") === "true" && localStorage.getItem("tld-breadTotal") == orderTotal){
return true;
}
if (showBread) {
var options = initBread();
bread.showCheckout(options);
}else{
alert("Please fix the issue in red")
}
}
function validateBread(){
if(settings.accountUrl != null && !isNullOrEmpty(localStorage.getItem("tld-breadToken"))){
$.ajax({
dataType: "jsonp",
url: settings.accountUrl+'api/bread/status',
data:{
transactionId: localStorage.getItem("tld-breadToken")
},
success:function(data){
if(data.status === 'CANCELED' || data.status === undefined){
inValidateBread(true);
}
},
error:function(jqXHR, textStatus, errorThrown){
console.log(textStatus);
}
});
}
return true;
}
function isNull(x) {
return x == undefined || x == null;
}
function isNullOrEmpty(x) {
return isNull(x) || x == "";
}
function setup(location) {
var pageType = $('.ys_wrapper').attr('id');
switch(pageType){
case 'ys_shipBillPage':
location.after(breadDisplay());
$('#vwcf-'+settings.storeId+'-breadtoken').hide();
$('#card-type').find('option[value="'+settings.cardSelectOption+'"]').hide();
$('body').on('click', '#ys_breadBilling', function () {
validateFields();
});
$.each(settings.requiredElem, function (i, obj) {
$('body').on('keyup', obj, function () {
if ($(this).val() !== '') {
$(this).removeClass('error').next('label.breadErrorLabel').remove();
}
});
});
var styles = '';
$('head').append(styles);
if(orderTotal != localStorage.getItem('tld-breadTotal')){
inValidateBread(true);
}
validateBread();
if(localStorage.getItem('tld-breadChosen') === "true" && !isNullOrEmpty(localStorage.getItem('tld-breadToken'))){
$('#ys_breadBilling').addClass('breadChosen');
$('#card-type').val(settings.cardSelectOption);
$('#vwcf-'+settings.storeId+'-breadtoken').val(localStorage.getItem('tld-breadToken'));
}
$('input[name="eventName.sbContinueEvent"]').click(function(e){
if($("#card-type").val() == settings.cardSelectOption){
if(!isNullOrEmpty($('#vwcf-'+settings.storeId+'-breadtoken').val()) && localStorage.getItem('tld-breadChosen') === "true"){
return true;
}else{
e.preventDefault();
validateFields();
return false;
}
}
});
break;
case 'ys_confirmationPage':
inValidateBread();
break;
}
};
function hideCardData(){
$('#labelcard-number, #labelcard-exp-month #labelcard-exp-year').hide();
};
function showCardData(){
$('#labelcard-number, #labelcard-exp-month #labelcard-exp-year').show();
}
setup($(this));
};
}( jQuery ));
jQuery(document).ready(function($){
$('.ys_paypalSection').getBreadCheckout({ //yhst-11234668104341 //deepbluewatches
storeId:'test-st30-truelight.stores',
storeUrl:'http://www.deepbluewatches.com/',
accountUrl:'https://account.truelightllc.com/',
requiredElem: ['#merchant-selected-shipping-methods','#shipping-full-name','#shipping-address1','#shipping-city','#shipping-state','#shipping-zip','#shipping-phone','#billing-email']
});
});