-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.js
More file actions
556 lines (460 loc) · 17.9 KB
/
widget.js
File metadata and controls
556 lines (460 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
(function(window, document) {"use strict"; /* Wrap code in an IIFE */
var jQuery, $; // Localize jQuery variables
var HOST = 'http://widget.giv2giv.org'; // also set host in widget_example.html
var APIHOST = 'http://apitest.giv2giv.org/api';
var STRIPE_KEY = 'pk_test_d678rStKUyF2lNTZ3MfuOoHy';
function loadScript(url, callback) {
/* Load script from url and calls callback once it's loaded */
var scriptTag = document.createElement('script');
scriptTag.setAttribute("type", "text/javascript");
scriptTag.setAttribute("src", url);
if (typeof callback !== "undefined") {
if (scriptTag.readyState) {
/* For old versions of IE */
scriptTag.onreadystatechange = function () {
if (this.readyState === 'complete' || this.readyState === 'loaded') {
callback();
}
};
} else {
scriptTag.onload = callback;
}
}
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(scriptTag);
}
function main() {
// $(function() {
/* The main logic of our widget is here */
/* We should have fully loaded jquery, jquery-ui and all plugins */
var script = $('#giv2giv-script'),
charity_preferences = {
charity_id: script.data('charity-id'),
minamt: script.data('minimum-amount'),
maxamt: script.data('maximum-amount'),
minpct: script.data('minimum-passthru-percentage'),
maxpct: script.data('maximum-passthru-percentage'),
inc: script.data('incremenent'),
initial_amount: script.data('initial-amount'),
initial_passthru: script.data('initial-passthru'),
add_fees: script.data('donor-add-fees'),
return_url: script.data('return-url')
},
div = $('#giv2giv-button'),
frm = $('#giv2giv-form'),
dialog = $('#giv2giv-dialog'),
amountSlider = $('#giv2giv-amount-slider'),
passthruSlider = $('#giv2giv-passthru-slider'),
amount = $('#giv2giv-amount'),
passthru = $('#giv2giv-passthru-percent'),
donationDetails = $('#giv2giv-donation-details'),
addFeesLabel = $('#giv2giv-add-fees-label'),
addFees = $('#giv2giv-add-fees'),
charityPrefs = $.extend({
charity_id: null,
minamt: 5.00,
maxamt: 10000,
minpct: 0,
maxpct: 100,
inc: 1.00,
initial_amount: 25,
initial_passthru: 50,
add_fees: true,
return_url: "https://giv2giv.org/#charity/" + charity_preferences.charity_id
}, charity_preferences);
// Themes from jQueryUI http://jqueryui.com/themeroller/
// ui-lightness, ui-darkness, smoothness, start, redmond, sunny, overcast, le-frog,
// flick, pepper-grinder, eggplant, dark-hive, cupertino, south-street, blitzer, humanity
// hot-sneaks, excite-bike, vader, dot-luv, mint-choc, black-tie, trontastic, swanky-purse
var giv2givTheme = script.data('theme');
if ( giv2givTheme == '' ) giv2givTheme = 'flick';
var giv2givHead = document.getElementsByTagName('head')[0];
var giv2givLink = document.createElement('link');
giv2givLink.rel = 'stylesheet';
giv2givLink.type = 'text/css';
giv2givLink.href = 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/' + giv2givTheme + '/jquery-ui.css';
giv2givLink.media = 'all';
giv2givHead.appendChild(giv2givLink);
div.css(
{
'border':'3px solid black',
'height':50,
'width':125
}
);
addFees.prop("checked", charityPrefs.add_fees==true);
if (charityPrefs.charity_id == null) {
var div_html = "script tag missing data-charity-id=YOURCHARITYID";
div.html(div_html);
return;
}
// tabify bank account / credit card tabs
$( "#giv2giv-tabs" ).tabs({
activate: function() {
donationDetails.empty().append(returnFormattedDonationDetails(amount, passthru, addFees));
addFeesLabel.html(returnFormattedAmountDetails(amount));
},
create: function() {
donationDetails.empty().append(returnFormattedDonationDetails(amount, passthru, addFees));
}
});
var json_url = APIHOST + "/charity/"+charityPrefs.charity_id+"/widget_data.json";
$.getJSON(json_url, function(charity) {
var dialog = $( "#giv2giv-dialog" ).dialog({
autoOpen: false,
title: "Donate to " + charity.name + " through giv2giv.org",
height: 'auto',
width: '450px',
modal: true,
fluid: true,
buttons: {
Submit: function( event ){
event.preventDefault();
frm.find('button').prop('disabled', true);
// increase amount if donor assuming fees
charityPrefs.add_fees==true ? amount.val(parseStrToNum(amount.val())+calculateFee(amount)) : "";
if (whichProcessor()=='dwolla') {
frm
.attr('action', APIHOST + '/charity/' + charity.id + '/dwolla.json')
.submit();
}
else if (whichProcessor()=='stripe') { // is stripe
// Disable the submit button to prevent repeated clicks
Stripe.card.createToken(frm, function(status, response) {
if (response.error) {
// Show the errors on the form
$( "#giv2giv-results" ).text(response.error.message);
$( "#giv2giv-results" ).dialog( "open" );
frm.find('button').prop('disabled', false);
} else {
// charge success
// response contains id, token and card, which contains additional card details like last4
var token = response.id;
// Insert the token into the form so it gets submitted to the server
frm.append($('<input type="hidden" name="giv2giv-stripeToken" />').val(token));
//convert the donation string $52.34 to a number
amount.val(parseStrToNum(amount.val()));
$.ajax({
data: frm.serialize(),
url: APIHOST + '/charity/' + charity.id + '/' + whichProcessor() + '.json',
cache: false
}).done(function (response) {
console.log(response);
// Show the success on the form
$( "#giv2giv-results" ).dialog( "open" );
});
}
});
}
},
Cancel: function() {
dialog.dialog( "close" );
}
},
close: function() {
amount.removeClass( "ui-state-error");
}
});
// Show widget when button clicked
div.button().on( "click", function() {
dialog.dialog( "open" );
});
// Init the amount slider
amountSlider.slider({
animate: true,
value: charityPrefs.initial_amount,
min: charityPrefs.minamt,
max: charityPrefs.maxamt,
step: charityPrefs.inc,
slide: function(event, ui) {
amount
.val("$" + ui.value) // Update donation amount
.trigger('update'); // Parse, format, update
}
});
// Init the passthru slider
passthruSlider.slider({
animate: true,
value: charityPrefs.initial_passthru,
min: charityPrefs.minpct,
max: charityPrefs.maxpct,
step: charityPrefs.inc,
slide: function(event, ui) {
passthru
.val(ui.value+"%") // Update donation passthru
.trigger('update'); // Parse, format, update
}
});
// set Stripe key
$.getScript("https://js.stripe.com/v2/", function() {
Stripe.setPublishableKey(STRIPE_KEY);
});
// Attach listeners to the amount input fields to update the slider when amount is changed
amount
.on('keyup blur update', function(e) {
// Parse input field
var rawVal = parseStrToNum(amount.val());
// Update slider amount, but only
// if the update didn't originate
// from the manual slider change
if(e.type !== 'update') {
amountSlider.slider("value", rawVal);
}
// No need to format the amount
// on every keystroke
if(e.type !== 'keyup') {
// Parse and format amount
var rawVal = parseStrToNum(amount.val()) || charityPrefs.minamt,
val = rawVal.formatMoney(2, '.', ',');
// Update input field
amount.val('$' + val);
}
// Update details
donationDetails.html(returnFormattedDonationDetails(amount, passthru, addFees));
addFeesLabel.html(returnFormattedAmountDetails(amount));
})
.on('click', function() {
// Select all text in input field
// when clicking inside it
amount
.focus()
.select();
});
// Attach listeners to the amount input fields to update the slider when amount is changed
passthru
.on('keyup blur update', function(e) {
// Parse input field
var rawVal = parseStrToNum(passthru.val());
// Update slider amount, but only
// if the update didn't originate
// from the manual slider change
if(e.type !== 'update') {
passthruSlider.slider("value", rawVal);
}
// No need to format the amount
// on every keystroke
if(e.type !== 'keyup') {
// Parse and format amount
var rawVal = parseStrToNum(passthru.val()) || charityPrefs.minpct,
val = rawVal;//.formatPercent(2, '.', ',');
// Update input field
passthru.val(val+'%');
}
// Update details
donationDetails.html(returnFormattedDonationDetails(amount, passthru, addFees));
})
.on('click', function() {
// Select all text in input field
// when clicking inside it
passthru
.focus()
.select();
});
// Manually update the field amount
// to match the slider's initial amount
// and trigger a blur event to update
// the tooltips
amount
.val("$" + amountSlider.slider("value"))
.trigger('update');
passthru
.val(passthruSlider.slider("value")+'%')
.trigger('update');
// Fee assumption toggle button
addFees.change(function() {
var el = $(this);
charityPrefs.addFees = el.is(':checked');
donationDetails.empty().append(returnFormattedDonationDetails(amount, passthru, addFees));
addFeesLabel.html(returnFormattedAmountDetails(amount));
amount.trigger('update'); // Update tooltips
passthru.trigger('update'); // Update tooltips
});
// Validation
$('.validate_form').each(function() {
$(this).validate();
});
// Forms
$('input')
.on('focus', function() {
$(this)
.addClass('active')
.parents('.input_wrapper')
.addClass('active');
})
.on('blur', function() {
$(this)
.removeClass('active')
.parents('.input_wrapper')
.removeClass('active');
});
// Cool tooltips
//$(document).tooltip( {
// content:function(){
// return this.getAttribute("title");
// }
//});
$( "#giv2giv-accordion" ).accordion({
active: false,
collapsible: true,
heightStyle: "fill"
});
$( "#giv2giv-results" ).dialog({
autoOpen: false,
modal: true,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
$( "#giv2giv-dialog" ).dialog( "close" );
}
},
close: function() {
$( "#giv2giv-dialog" ).dialog( "close" );
}
});
// Load the FB.ui JS library
window.fbAsyncInit = function() {
FB.init({
appId : '668544863276117',
xfbml : true,
version : 'v2.3'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
// Share on facebook code
$('#fb-share').click(function() {
FB.ui({
method: 'feed',
link: charityPrefs.return_url,
caption: 'I just donated to ' + charity.name + ' using Giv2Giv!',
}, function(response){});
})
$('#twitter-share').attr("href", "https://twitter.com/intent/tweet?text=" + "I donated to a charity using Giv2Giv! Check it out here!&url=" + charityPrefs.return_url);
/*
// Bind form enter key
$("form").not('#frm-feedback').find("input").last().keydown(function(e) {
if(e.keyCode == 13) {
$(this).parents('form').submit();
}
});
// Generate and update tooltips
var rawVal = parseStrToNum(amount.val()) || gatewayOpts.min
var stripeMoneyLeft = gatewayOpts.addFees ? rawVal : (rawVal - ((rawVal * 0.029) + 0.30)),
dwollaMoneyLeft = gatewayOpts.addFees ? rawVal : (rawVal - (rawVal > 10 ? 0.25 : 0));
stripePayBtn
.data('tooltip', 'The nonprofit will receive <strong>$' + stripeMoneyLeft.formatMoney(2, '.', ',') + '</strong> (after fees) using this method');
.tooltip('refresh');
dwollaPayBtn
.data('tooltip', 'The nonprofit will receive <strong>$' + dwollaMoneyLeft.formatMoney(2, '.', ',') + '</strong> (after fees) using this method');
.tooltip('refresh');
*/
}); // getJSON end
} // end main()
/**
* Returns an HTML string with the donations details
*/
var returnFormattedDonationDetails = function (amount, passthru, addFees) {
var val, transactionAmount, amount_passthru, percent_passthru, amount_invested, net_amount=0, fee=0;
if (addFees.is(':checked')) {
transactionAmount = parseStrToNum(amount.val()) + calculateFee(amount);
}
else {
transactionAmount = parseStrToNum(amount.val());
fee = calculateFee(amount);
}
net_amount = transactionAmount - fee;
percent_passthru = parseStrToNum(passthru.val()) / 100; // convert int to percent e.g. 50 to .5
amount_passthru = net_amount * percent_passthru;
amount_invested = net_amount - amount_passthru;
val = "<h3>Summary:</h3>";
val += "<li>$" + amount_passthru.formatMoney(2, '.', ',') + " will be immediately sent to the charity</li>";
val += "<li>$" + amount_invested.formatMoney(2, '.', ',') + " will be invested to sustain the charity over time</li>";
val += "</ul>";
val += "<br />Your total donation today is: $" + transactionAmount.formatMoney(2, '.', ',');
return val;
}
var returnFormattedAmountDetails = function (amount) {
var fee = calculateFee(amount);
return "Add transaction fee of " + fee.formatMoney(2, '.', ',') +"?";
}
var whichProcessor = function() {
if ($('#giv2giv-tabs').tabs('option','active')==0)
return "dwolla";
else if ($('#giv2giv-tabs').tabs('option','active')==1)
return "stripe";
}
/**
* Returns fee (2-digit float) amount
*/
var calculateFee = function (amount) {
var fee;
var thisAmount = parseStrToNum(amount.val());
switch (whichProcessor()) {
case "stripe":
fee = 0.3 + parseFloat((.029 * thisAmount).toFixed(2));
break;
case "dwolla":
fee = 0.0
//if (thisAmount > 10.0) {
//fee = 0.25;
//}
break;
default:
fee = 0.0;
}
return fee;
}
/**
* Parses a string into a number
*
* parseStrToNum('$0.01aab');
* @desc removes all non-alpha numeric characters from the string
*
* @name parseStrToNum
* @param {string} string to parse
* @return {number} parsed number
*/
var parseStrToNum = function(str) {
var num = +str.replace(/[^0-9\.]+/g, '');
var dec = 2;
var d = 1;
for (var i=0; i<dec; i++){
d += "0";
}
return Math.round(num * d) / d;
//return val;
}
/**
* Formats a number into currency standards
*
* .formatNumber(2, '.', ',');
* @desc adds dots and commas to format a number into currency
* standards
*
* @name parseStrToNum
* @param {string} string to parse
* @return {number} parsed number
*/
Number.prototype.formatMoney = function(c, d, t){
var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
/* Load jQuery */
loadScript("//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js", function() {
/* Restore $ and window.jQuery to their previous values and store the
new jQuery in our local jQuery variables. */
$ = jQuery = window.jQuery.noConflict(true);
$('#giv2giv-button').load(HOST+'/button_contents.html');
loadScript("jquery-ui.min.js", function() { // load locally-modified JS
initjQueryUIPlugin(jQuery);
loadScript("jquery.validate.min.js", function() {
initjQueryValidatePlugin(jQuery);
main(); // call our main function
});
});
});
}(window, document)); /* end IIFE */