-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexample-direct-debit.js
392 lines (339 loc) · 16.8 KB
/
example-direct-debit.js
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
// Let's be good developers and not pollute the global namespace
(function ($) {
// Let's make sure the DOM is ready
$(function () {
// Create an instance of the DonationService
var ds = new BLACKBAUD.api.DonationService(
$('.BBDonationApiContainer').data('partid'));
//Create the donation object we'll send
//In order to simplify our examples, some of this information is hard-coded.
var donation = {
MerchantAccountId: '00000000-0000-0000-0000-000000000000',
BBSPReturnUri: window.location.href,
BBSPTemplateSitePageId: 000,
Gift: {
PaymentMethod: 2,
Designations: []
}
};
// Create our success handler
var success = function (returnedDonation) {
console.log(returnedDonation);
if (returnedDonation.Donation.Gift.PaymentMethod === 2) {
window.location.href = window.location.href + "?t=" + d.Donation.Id;
}
};
// Create our error handler
var error = function (returnedErrors) {
console.log('Error!');
};
//Show and Hide directdebit div on the page
function showHideDirectDebitDiv() {
if ($("#paymentDirectDebit").prop("checked")) {
$("#directDebitInformation").show();
hideAndVisible_BankingTypeField();
addCountryHandler();
}
else {
$("#directDebitInformation").hide();
}
}
//Call the function when we have to hide and visible the banking type field
function hideAndVisible_BankingTypeField() {
if ($('[id="ddlBankingSystemType"] option').length == 1) {
$('#tr_BankingSystemType').css('display', 'none');
} else {
$('#tr_BankingSystemType').css('display', '');
}
}
//Use to bind the banking types that is configured in CRM
function bindDirectDebitData() {
donationService.getBankingTypeData(function (d) {
$.each(d, function () {
$("#ddlBankingSystemType").append($("<option></option>").val(this["Abbreviation"]).text(this["Description"]));
});
});
donationService.getAccountTypeData(function (d) {
$.each(d, function () {
$("#ddlAccountType").append($("<option></option>").val(this["Id"]).text(this["Description"]));
});
});
donationService.getOriginIdAndTime(function (d) {
$("#lbl_CurrentDate").text(d["Time"]);
$("#lbl_OrigNumber").text(d["OriginId"]);
});
}
//this function is used to select the banking type on the basis of country dropdown change
function addCountryHandler() {
var bankingSystemType = $('[id="ddlBankingSystemType"]'), addrCtl = $("[id='country'] :selected"), selectBankingOption, exists = false, bankingOptions;
if (bankingSystemType && bankingSystemType.length > 0) {
bankingOptions = $('[id="ddlBankingSystemType"] option');
if (bankingOptions.length > 0) {
if (!(bankingOptions.length === 1)) {
if (countryData && (validateCountry(addrCtl.val(), countryData.NZ) || validateCountry(addrCtl.val(), countryData.AUS))) {
selectBankingOption = "Aus_NZ";
}
else if (countryData && validateCountry(addrCtl.val(), countryData.UK)) {
selectBankingOption = "UK";
}
else if (countryData && validateCountry(addrCtl.val(), countryData.CAD) || validateCountry(addrCtl.val(), countryData.USA)) {
selectBankingOption = "USA";
}
exists = 0 !== bankingSystemType.find('option[value="' + selectBankingOption + '"]').length;
if (exists && exists === true) {
bankingSystemType.val(selectBankingOption);
} else {
bankingSystemType.val("Please Select");
}
}
}
hideAndVisible_DirectDebitFields();
}
}
//Hide and Visible direct debit fields on the basis of country dropdown and banking type dropdown change
function hideAndVisible_DirectDebitFields() {
var bankingType = "Please Select", htmlElement = "", routingNumberTextBoxLabel = "", selectedCountry = "", routingNumberTextBox = "", transitNumberBox = "",
routingNumber = $('[id="txtRoutingNumber"]'),
transitNumber = $('[id="txtCanTransitNumber"]'),
routingNumberLabel = $('[id="lblRoutNumLBL"]'),
addressCTL = $("[id='country'] :selected"),
bankingSystemLabel = $('[id="lblBankingSystemType"]'),
bankingSystemType = $('[id="ddlBankingSystemType"] :selected'),
accountType = $('[id="tr_AccountType"],[id*="DC_AccountType"]'),
accountNo = $('[id="spn_AccountNo"]'),
accountNoNZ = $('[id="spn_AccountNoNZ"]'),
financialInstitution = $('[id="lblFinancialLBL"]'),
accountNoLabel = $('[id="lblAccountNumber"]'),
routingNumberHelpSymbol = $('[id="lnk_BankHelp"]'),
requiredFieldAccountType = $('[id="RequiredFieldAccountType"]'),
ukFields = $('[id="tr_DDInstructions"], [id="tr_OrigNumber"], [id="tr_CurrentDate"], [id="tr_DDBankInstructions"], [id="tr_DDPDFInstructions"], [id="tr_DDPDFInstructions2"],[id="DC_DDInstructions"],[id="DC_OrigNumber"],[id="DC_CurrentDate"],[id="DC_DDBankInstructions"],[id="DC_DDPDFInstructions"],[id="DC_DDPDFInstructions2"]');
if (!routingNumber || routingNumber.length <= 0) {
return;
}
if (bankingSystemType && bankingSystemType.length > 0) {
bankingType = bankingSystemType.attr("Value");
}
if (routingNumber && routingNumber.length > 0) {
routingNumberTextBox = routingNumber;
}
if (addressCTL && addressCTL.length > 0) {
selectedCountry = addressCTL.val();
}
if (routingNumberLabel && routingNumberLabel.length > 0) {
routingNumberTextBoxLabel = routingNumberLabel;
}
if (transitNumber && transitNumber.length > 0) {
transitNumberBox = transitNumber;
$('[id="tr_CanadaTransitNum"]').css("display", "none");
}
accountNo.css("display", "");
accountNoNZ.css("display", "");
accountNoLabel.html("Account Number:");
routingNumberHelpSymbol.css("display", "");
if (ukFields && ukFields.length > 0) {
ukFields.css('display', 'none');
}
if (countryData && validateCountry(selectedCountry, countryData.CAD) && transitNumberBox && transitNumberBox.length > 0 && bankingType === "USA") {
routingNumberTextBoxLabel.html("Institution ID:");
routingNumberTextBox.attr("maxlength", "4");
routingNumberTextBox.val('');
accountType.css("display", "none");
accountNoNZ.css("display", "none");
$('[id="tr_CanadaTransitNum"]').css("display", "");
} else {
if (bankingType !== "Please Select") {
if (bankingType === "USA") {
routingNumberTextBoxLabel.html("Routing Number:");
routingNumberTextBox.attr("maxlength", "9");
routingNumberTextBox.val('');
accountType.css("display", "");
accountNoNZ.css("display", "none");
}
else if (bankingType === "UK") {
routingNumberTextBoxLabel.html("Sort Code:");
routingNumberTextBox.attr("maxlength", "6");
routingNumberTextBox.val('');
accountType.css("display", "none");
accountNoNZ.css("display", "none");
if (ukFields) {
ukFields.css('display', '');
}
}
else if (bankingType === "Europe") {
routingNumberTextBoxLabel.html("BIC:");
routingNumberTextBox.attr("maxlength", "11");
routingNumberTextBox.val('');
accountType.css("display", "");
accountNoNZ.css("display", "none");
routingNumberHelpSymbol.css("display", "none");
}
else if (bankingType === "Aus_NZ") {
routingNumberTextBoxLabel.html("BSB Number:");
routingNumberTextBox.attr("maxlength", "6");
routingNumberTextBox.val('');
accountType.css("display", "none");
accountNoNZ.css("display", "none");
if (countryData && validateCountry(selectedCountry, countryData.NZ)) {
accountNo.css("display", "none");
accountNoNZ.css("display", "");
routingNumberTextBoxLabel.html("Bank/Branch No.:");
accountNoLabel.html("Account No./Suffix:");
routingNumberTextBox.attr("maxlength", "7");
routingNumberTextBox.val('');
}
}
else if (bankingType === "Other") {
routingNumberTextBoxLabel.html("Bank Code:");
routingNumberTextBox.attr("maxlength", "25");
routingNumberTextBox.val('');
accountType.css("display", "");
accountNoNZ.css("display", "none");
routingNumberHelpSymbol.css("display", "none");
}
else {
routingNumberTextBoxLabel.html("Routing Number:");
routingNumberTextBox.attr("maxlength", "9");
routingNumberTextBox.val('');
accountType.css("display", "");
accountNoNZ.css("display", "none");
}
}
else {
routingNumberTextBoxLabel.html("Routing Number:");
routingNumberTextBox.attr("maxlength", "9");
accountType.css("display", "");
accountNoNZ.css("display", "none");
}
}
}
//this function is called when we click on help image of reoting number textbox(and it changes on the basis of banking type dropdown change)
function donationBankHelp() {
var sFeatures = 'WIDTH=392px,HEIGHT=400px,RESIZABLE=YES,SCROLLBARS=NO,TOOLBAR=NO,LEFT=5,TOP=20,location=No;status=No', oPopUp,
bankingType = $('[id="ddlBankingSystemType"] :selected'), addressCTL = $("[id='country'] :selected"), selectedValue = "", selectedCountry = "", qParam = "";
if (bankingType && bankingType.length > 0) {
selectedValue = bankingType.attr("Value");;
}
if (addressCTL && addressCTL.length > 0) {
selectedCountry = addressCTL.val();
}
if (countryData && validateCountry(selectedCountry, countryData.AUS) && selectedValue === 'Aus_NZ') {
qParam = 'AUS';
} else if (countryData && validateCountry(selectedCountry, countryData.NZ) && selectedValue === 'Aus_NZ') {
qParam = 'NZ';
} else if (countryData && validateCountry(selectedCountry, countryData.CAD) && selectedValue === 'USA' && $('[id="tr_CanadaTransitNum"]') && $('[id="tr_CanadaTransitNum"]').length > 0) {
qParam = 'CAD';
} else {
switch (selectedValue) {
case 'USA':
qParam = "USA";
break;
case "Aus_NZ":
qParam = "AUS";
break;
case "UK":
qParam = "UK";
break;
default:
qParam = "USA";
break;
}
}
oPopUp = new PopUpDialogBB('~/Admin/DonationHelp.ascx', '_blank', sFeatures, '&mode=2&displayfor=' + qParam);
oPopUp.Show();
}
function dDGuarantee() {
var sFeatures = 'WIDTH=650px,HEIGHT=415px,RESIZABLE=YES,SCROLLBARS=NO,TOOLBAR=NO,LEFT=5,TOP=20,location=No;status=No',
oPopUp = new PopUpDialogBB('~/Admin/DonationHelp.ascx', '_blank', sFeatures, '&mode=3');
oPopUp.Show();
}
//this is used to set country object with all possible value of the countries(like Abbreviation, Country Name, and Guid)
function getCountryMappings() {
//this is use for direct debit validations
countryData = {};
countryService.getCountries(function (d) {
$.each(d, function () {
var tmpData = {};
tmpData.Id = this["Id"];
tmpData.Abbreviation = this["Abbreviation"];
tmpData.Description = this["Description"];
switch (this["Id"].toUpperCase()) {
case 'D81CEF85-7569-4B2E-8F2E-F7CF998A3342':
countryData.USA = tmpData;
break;
case 'A2ECC209-3D20-4DDA-BC4C-7585C8E2E701':
countryData.UK = tmpData;
break;
case 'D9EE54CD-2183-490C-A3AD-11152B271335':
countryData.CAD = tmpData;
break;
case 'F189F24C-2538-46A1-8458-1E3F3967B843':
countryData.AUS = tmpData;
break;
case 'DA42443E-AB81-40EE-A239-91ED699C0801':
countryData.NZ = tmpData;
break;
}
});
});
}
//for validate the country thatused to select the banking type on the basis of country dropdown change
function validateCountry(country, ctryData) {
if (country.toUpperCase() === ctryData.Id.toUpperCase() || country.toUpperCase() === ctryData.Abbreviation.toUpperCase() || country.toUpperCase() === ctryData.Description.toUpperCase()) {
return true;
}
return false;
}
//Bind Direct Debit
getCountryMappings();
bindDirectDebitData();
//Attach Listeners
$("#recMonthly").click(function () {
$("#directDebit").css("display", "");
});
$("#recOneTime").click(function () {
$("#directDebit").css("display", "none");
});
$("#paymentDirectDebit").click(function () { showHideDirectDebitDiv(); })
$("#ddlBankingSystemType").change(function () { hideAndVisible_DirectDebitFields(); })
$("#lnk_BankHelp").click(function () { donationBankHelp(); });
$("#lnk_DDGuarantee").click(function () { dDGuarantee(); });
//Attach event listener on country change
$("#country").change(function () { addCountryHandler(); })
// Attach our event listener to the donate button
$('.btn-donate').click(function(e) {
// Stop the button from submitting the form
e.preventDefault();
//Payment Method(2) means direct debit and set all the properties of direct debit
if (paymentMethod === "2" && $("#paymentDirectDebit").prop("checked")) {
donation.Gift.DirectDebitInformation = {};
var selectedCountry = $("#country :selected").val(), bankingType = $("#ddlBankingSystemType :selected").val();
donation.Gift.DirectDebitInformation.FinancialInstitution = $("#txtFinancialInstitution").val();
donation.Gift.DirectDebitInformation.BranchName = $("#txtBranchName").val();
donation.Gift.DirectDebitInformation.AccountHolderName = $("#txtAccountHolder").val();
donation.Gift.DirectDebitInformation.BankingType = bankingType;
if (countryData && validateCountry(selectedCountry, countryData.CAD) && bankingType === "USA") {
donation.Gift.DirectDebitInformation.RoutingNumber = $("#txtRoutingNumber").val();
donation.Gift.DirectDebitInformation.TransitNumber = $("#txtCanTransitNumber").val();
}
else {
donation.Gift.DirectDebitInformation.RoutingNumber = $("#txtRoutingNumber").val();
}
if (countryData && validateCountry(selectedCountry, countryData.NZ) && bankingType === "Aus_NZ") {
donation.Gift.DirectDebitInformation.AccountNumber = $("#txtAccountNumberNZ").val();
donation.Gift.DirectDebitInformation.SuffixNo = $("#txtSuffixNZ").val();
}
else {
donation.Gift.DirectDebitInformation.AccountNumber = $("#txtAccountNumber").val();
}
if (bankingType === "Europe" || bankingType === "Other" || countryData && (!validateCountry(selectedCountry, countryData.CAD) && bankingType === "USA")) {
donation.Gift.DirectDebitInformation.AccountType = $("#ddlAccountType :selected").val();
}
else {
donation.Gift.DirectDebitInformation.AccountType = "";
}
}
// Submitting the donation
ds.createDonation(donation, success, error);
});
});
}(jQuery));