1/** 2 * Give - Currency Switcher Frontend JS script. 3 * 4 * @package Give_Currency_Switcher 5 */ 6var give_global_vars, Give_Currency_Switcher, give_cs_json_obj; 7 8jQuery.noConflict(); 9 10(function ($) { 11 /** 12 * Currency switcher admin js script. 13 * @since 1.0 14 * 15 * @type {{init: init, init_chosen: init_chosen}} 16 */ 17 Give_Currency_Switcher = { 18 isMultiStepForm: false, 19 20 /** 21 * List of Give forms. 22 */ 23 forms: false, 24 25 /** 26 * Current variable price id. 27 */ 28 price_id: '', 29 30 /** 31 * Initialization frontend js. 32 * 33 * @since 1.0 34 */ 35 init: function () { 36 forms = $('form.give-form'); 37 38 this.init_switcher(); 39 40 // Remove sanitize amount when donation value is updated. 41 jQuery(document).on( 42 'give_donation_value_updated blur', 43 '.give-donation-amount', 44 this.remove_sanitize_amount 45 ); 46 47 // When gateway is changed. 48 jQuery(document).on('give_gateway_loaded', this.changed_payment_gateway); 49 50 // Validate minimum price. 51 jQuery(document).find('.give-donation-amount .give-text-input').on('blur', this.update_donation_amount); 52 53 // Bind event to trigger the donation form. 54 $('.give-btn-reveal').on('click', this.handle_reveal_btn); 55 56 // Switch donation form's currency. 57 $('body').on('change', '.give-cs-select-currency', this.switch_currency); 58
59 // Do not show drop-down if currencies are not activated. 60 // $( 'body' ).on( 'mousedown', 'select.give-cs-select-currency', this.chosenCurrency ); 61 62 // Fix Currency selector drop-down width in model view. 63 jQuery(document).on('mfpOpen', this.adjust_dropdown_width); 64 jQuery(document).on('click', '.give-card', this.adjust_dropdown_width); 65 jQuery(document).on('mfpClose', this.adjust_dropdown_width_after_close); 66 }, 67 68 /** 69 * Adjust width when button view modal close. 70 * 71 * @since 1.0.4 72 */ 73 adjust_dropdown_width_after_close: function () { 74 setTimeout(function () { 75 Give_Currency_Switcher.init_switcher(); 76 }, 10); 77 }, 78 79 /** 80 * Move the Currency switcher drop-down and set it with amount input field. 81 * for all forms. 82 * 83 * @since 1.0 84 */ 85 init_switcher: function () { 86 forms.each(function () { 87 // Get the form. 88 var form = $(this); 89 90 // If using a multi-step form, store goal and income information in form attributes 91 if (form.parent().hasClass('give-embed-form')) { 92 Give_Currency_Switcher.isMultiStepForm = true; 93 form.attr( 94 'data-goal-amount', 95 Number( 96 form 97 .find('.goal .number') 98 .text() 99 .replace(/[^0-9.-]+/g, '') 100 ) 101 ); 102 form.attr( 103 'data-income-amount', 104 Number( 105 form 106 .find('.raised .number') 107 .text() 108 .replace(/[^0-9]+/g, '') 109 ) 110 ); 111 } 112 113 // Check if currency switcher is enabled. 114 if (!Give_Currency_Switcher.module.isCurrencySwitcherEnable(form)) { 115 return; 116 } 117 118 var cs_switcher = form.find('select.give-cs-select-currency'), // Get the currency drop-down. 119 currencies = Give_Currency_Switcher.module.getSupportedCurrencies(form), 120 currency_symbol = form.find('.give-currency-symbol').text(); 121 122 // If acronyms is enabled then show currency code with symbol. 123 if ('enabled' === Give_Currency_Switcher.module.getValueOf('acronyms', form)) { 124 var currency_code = Give.form.fn.getInfo('currency_code', form); 125 126 // If acronyms is already there. 127 currency_symbol = 128 -1 === currency_symbol.indexOf(currency_code) 129 ? Give.form.fn.getInfo('currency_code', form) + ' ' + currency_symbol 130 : currency_symbol; 131 } 132 133 // If Currency switcher is activated. 134 if ( 135 Give_Currency_Switcher.module.isCurrencySwitcherEnable(form) && 136 1 < Object.keys(currencies).length 137 ) { 138 // Get the clone of the currency switcher drop-down. 139 var switcher_clone = cs_switcher 140 .removeClass('give-hidden') 141 .addClass('give-cs-mini-dropdown') 142 .css({ 143 width: form 144 .find('.give-currency-symbol') 145 .addClass('give-cs-mini-dropdown-icon') 146 .outerWidth(), 147 }) 148 .clone(); 149 150 // Add currency switcher drop-down accordingly. 151 if ('after' === give_global_vars.currency_pos) { 152 switcher_clone.insertAfter( 153 form.find( 154 '.give-donation-amount input[name="give-amount"], .give-donation-amount span.give-amount-top' 155 ) 156 ); 157 } else { 158 switcher_clone.prependTo(form.find('.give-donation-amount')); 159 } 160 161 // Change the currency symbol and code. 162 form.find('.give-currency-symbol').html(currency_symbol); 163 164 // Update currency selector drop-down width. 165 Give_Currency_Switcher.adjust_dropdown_width(); 166 167 // Remove switcher drop-down. 168 cs_switcher.remove(); 169 } else if ( 170 Give_Currency_Switcher.module.isCurrencySwitcherEnable(form) && 171 1 === Object.keys(currencies).length 172 ) { 173 // If only one currency is supported. 174 form.find('.give-currency-symbol').html(currency_symbol); 175 } 176 177 if (!Give_Currency_Switcher.module.isGatewaySupportCurrency(form, currency_code)) { 178 Give_Currency_Switcher.module.showCurrencyNotSupportGatewayNotice(form, currency_code); 179 180 // Select switched currency. 181 form.find('.give-cs-select-currency').val(currency_code).trigger('change'); 182 } 183 }); 184 }, 185 186 /** 187 * Adjust currency switcher selector width when opening model 188 * 189 * @since 1.0.1 190 */ 191 adjust_dropdown_width: function () { 192 $('form.give-form').each(function () { 193 // Get the form. 194 var form = $(this); 195 196 if (Give_Currency_Switcher.module.isCurrencySwitcherEnable(form)) { 197 // Get the current drop-down width. 198 var dropdown_width = form.find('.give-currency-symbol.give-cs-mini-dropdown-icon').outerWidth(); 199 200 // Modify the width of the element. 201 form.find('.give-cs-select-currency').css({width: dropdown_width}); 202 203 // Get the outer width of drop-down. 204 var width = form.find('.give-cs-select-currency').outerWidth(); 205 206 // Set margin to the currency selector drop-down. 207 form.find('.give-cs-mini-dropdown').css('marginRight', -Math.abs(width) + 'px'); 208 } 209 }); 210 }, 211 212 /** 213 * Handle reveal and modal view. 214 * @since 1.0 215 */ 216 handle_reveal_btn: function () { 217 $(this).parents('form').find('.give-currency-switcher-wrap').slideDown(); 218 }, 219 220 /** 221 * Disable switch currency drop-down if the donation amount is lower than minimum amount. 222 * 223 * @since 1.0 224 * @param e 225 */ 226 update_donation_amount: function (e) { 227 // Get the donation form . 228 var form = jQuery(e.currentTarget).closest('form'); 229 230 if ( 231 !Give_Currency_Switcher.module.isCurrencySwitcherEnable(form) || 232 0 >= form.find('.give-cs-select-currency').length 233 ) { 234 return; 235 } 236 237 // If there is any custom amount already set, remove it. 238 form.find('input[name="give-cs-custom-amount"]').remove(); 239 240 // Change the donation form's amount. 241 Give_Currency_Switcher.module.setValueOf('give-amount', $(this).val(), form); 242 243 // Change the donation form's amount. 244 form.find('.give-final-total-amount').attr('data-total', $(this).val()); 245 246 var formPriceID = Give.form.fn.getPriceID(form), 247 formCurrency = Give_Currency_Switcher.module.getValueOf('give-cs-currency', form), 248 formBaseAmounts = JSON.parse(Give.form.fn.getInfo('data-give_cs_base_amounts', form)), 249 donationAmount = Give_Currency_Switcher.module.getDonationAmount( 250 form, 251 Give.form.fn.getInfo('decimal_separator', form) 252 ); 253 254 // Get the exchange rates. 255 var currencyRates = Give_Currency_Switcher.module.getExchangeRates(form); 256 257 // Convert the currency amount in base currency. 258 var amountInBase = 259 0 !== currencyRates[formCurrency] ? donationAmount / currencyRates[formCurrency] : donationAmount; 260 261 // Set custom amount as 0; 262 formBaseAmounts.custom = 0; 263 264 // If the price id -1 then it is "Set donation" otherwise check for variable custom amount. 265 if ( 266 (-1 === formPriceID && parseFloat(formBaseAmounts[0]) !== parseFloat(amountInBase)) || 267 ('custom' === formPriceID && parseFloat(amountInBase) !== parseFloat(formBaseAmounts[formPriceID])
267) 268 ) { 269 // Update form's custom amount. 270 formBaseAmounts.custom = amountInBase; 271 } 272 273 // Update base amount. 274 form.attr('data-give_cs_base_amounts', JSON.stringify(formBaseAmounts)); 275 276 // Unblock currency switcher drop-down. 277 setTimeout(function () { 278 0 < form.find('div.give-invalid-minimum').length 279 ? form.find('.give-cs-select-currency').attr('disabled', 'disabled') 280 : form.find('.give-cs-select-currency').removeAttr('disabled'); 281 }, 400); 282 283 // Update message. 284 Give_Currency_Switcher.module.updateCurrencyMessage(form, formPriceID); 285 286 // Change price ID. 287 if (formPriceID !== Give_Currency_Switcher.price_id) { 288 Give_Currency_Switcher.price_id = Give.form.fn.getPriceID(form); 289 } 290 291 // Update minimum amount. 292 Give_Currency_Switcher.updateMinimumAmount(form, formPriceID); 293 }, 294 295 /** 296 * When Donor Switch Currency from the donation form. 297 * 298 * @since 1.0 299 * @param e 300 */ 301 switch_currency: function (e) { 302 // Get the donation form. 303 var $form = $(this).closest('form.give-form'); 304 305 // Return, if currency switcher isn't activate. 306 if (!Give_Currency_Switcher.module.isCurrencySwitcherEnable($form)) { 307 return; 308 } 309 310 var new_currency = $(this).val(), // Current . 311 old_currency = Give_Currency_Switcher.module.getValueOf('give-cs-form-currency', $form), // Previous. 312 supported_currencies = Give_Currency_Switcher.module.getSupportedCurrencies($form), // Supported currencies. 313 variable_prices = Give.form.fn.getVariablePrices($form); // Get the variable IDs. 314 315 // Bail out if new_currency not set. 316 if (null === new_currency) { 317 return; 318 } 319 320 // Get the symbol of the selected currency. 321 var new_currency_sign = supported_currencies[new_currency].currencySetting.symbol || ''; // New currency symbol. 322 323 if (!Give_Currency_Switcher.module.isGatewaySupportCurrency($form, new_currency)) { 324 Give_Currency_Switcher.module.showCurrencyNotSupportGatewayNotice($form, new_currency); 325 326 // Select switched currency. 327 $form.find('.give-donation-amount .give-text-input').trigger('blur'); 328 $form.find('#give-purchase-button').attr('disabled', true); 329 return; 330 } 331 332 // Change currency symbol with currency code based on 'acronyms' option in Give's setting. 333 $form.find('.give-currency-symbol').html( 334 'enabled' === Give_Currency_Switcher.module.getValueOf('acronyms', $form) 335 ? new_currency + ' ' + new_currency_sign // eg. [ USD $ ] 336 : new_currency_sign 337 ); 338 339 var currencySettings = supported_currencies[new_currency].currencySetting.setting, 340 thousandSeparator = 341 '' !== currencySettings['thousands_separator'] ? currencySettings['thousands_separator'] : ' ', 342 decimalSeparator = 343 '' !== currencySettings['decimal_separator'] ? currencySettings['decimal_separator'] : '.'; 344 345 // Update form currency formatting options. 346 Give.form.fn.setInfo('attr', new_currency_sign, $form, 'data-currency_symbol'); 347 Give.form.fn.setInfo('attr', new_currency, $form, 'data-currency_code'); 348 Give.form.fn.setInfo('attr', thousandSeparator, $form, 'data-thousands_separator'); 349 Give.form.fn.setInfo('attr', decimalSeparator, $form, 'data-decimal_separator'); 350 Give.form.fn.setInfo('attr', currencySettings['number_decimals'], $form, 'data-number_decimals'); 351 352 // Set new currency and exchange rate. 353 Give_Currency_Switcher.module.setValueOf( 354 'give-cs-exchange-rate', 355 Give_Currency_Switcher.module.getExchangeRates($form)[new_currency], 356 $form 357 ); 358 Give_Currency_Switcher.module.setValueOf('give-cs-form-currency', new_currency, $form); 359 360 // Remove currency notices if exists. 361 $(this).closest('.give-display-onpage').find('.give-cs-notices').remove(); 362 363 // Bail out if, new currency and old currency same. 364 if (new_currency === old_currency) { 365 return; 366 } 367 368 var allSupportedCurrencies = Give_Currency_Switcher.module.getAllSupportedCurrencies(), 369 decimal_separator = allSupportedCurrencies[old_currency].setting['decimal_separator']; 370 371 // Convert single amount. 372 Give_Currency_Switcher.updateSingleAmount($form, old_currency, decimal_separator); 373 374 // Form Price ID. 375 var form_price_id = Give.form.fn.getPriceID($form, false); 376 377 // If this donation form has variable prices. 378 if (-1 < form_price_id || 'custom' === form_price_id) { 379 Give_Currency_Switcher.updateVariablePrices($form, form_price_id, old_currency, variable_prices); 380 } 381 382 // Trigger Give amount field. 383 $form.find('.give-donation-amount .give-text-input').trigger('blur'); 384 385 // Convert Goal amounts. 386 Give_Currency_Switcher.updateGoalAmount($form, new_currency, decimal_separator); 387 388 // Update currency switched message. 389 Give_Currency_Switcher.module.updateCurrencyMessage($form, form_price_id); 390 391 // Update FeeRecovery base amount. 392 Give_Currency_Switcher.module.updateFeeAmount(old_currency, new_currency, $form, decimal_separator); 393 394 // Unset form cache. 395 Give.cache['form_' + Give.form.fn.getInfo('form-id', $form)] = []; 396 397 /** 398 * Allow developer to add custom JS when currency switched. 399 * 400 * @since 1.0 401 */ 402 $(document).trigger('give_cs_switched', $form); 403 }, 404 405 /** 406 * Convert / Update single amount. 407 * 408 * @param form 409 * @param currency_old 410 * @param decimal_separator 411 */
412 updateSingleAmount: function (form, currency_old, decimal_separator) { 413 var give_total = Give_Currency_Switcher.module.getDonationAmount(form, decimal_separator), // Get the give form amount. 414 currency_chosen = Give_Currency_Switcher.module.getValueOf('give-cs-currency', form), // Get the selected currency. 415 give_custom_amounts = Give_Currency_Switcher.module.getCustomAmounts(form), // Get all of the custom amounts. 416 converted_amount = give_custom_amounts[currency_chosen]; // Get the custom amount. 417 418 // If custom amount isn't exists. 419 if (!converted_amount) { 420 // Convert amount to the selected currency. 421 converted_amount = Give_Currency_Switcher.module.convertCurrency( 422 give_total, 423 form, 424 currency_old, 425 null, 426 true 427 ); 428 } 429 430 // Update currency selector drop-down width. 431 Give_Currency_Switcher.adjust_dropdown_width(); 432 433 // Format the converted amount. 434 var amount_formatted = Give.fn.formatCurrency(converted_amount, {}, form); 435 436 // Update custom amount field 437 form.find('.give-amount-top').val(amount_formatted); 438 form.find('span.give-amount-top').text(amount_formatted); 439 440 // Remove custom amount field, if exists. 441 form.find('span.give-cs-amount-totals').remove(); 442 443 /** 444 * Temporarily Store form's amount. 445 * 446 * data-total => Converted amount. 447 * data-sanitize-amount => Un-formatted amount without rounding off the decimal amounts. 448 * data-custom_amount => When chosen currency has custom amount then store the calculated amount to it. 449 */ 450 $('<span/>', { 451 class: 'give-cs-amount-totals', 452 'data-total': amount_formatted, 453 'data-sanitize-amount': converted_amount, 454 'data-custom_amount': give_custom_amounts[currency_chosen] 455 ? Give_Currency_Switcher.module.convertCurrency(give_total, form, currency_old) 456 : '', 457 }).appendTo(form); 458 }, 459 460 /** 461 * Convert or Update the variable prices. 462 * 463 * @param form 464 * @param form_price_id 465 * @param currency_old 466 * @param variable_prices 467 */ 468 updateVariablePrices: function (form, form_price_id, currency_old, variable_prices) { 469 var current_currency = Give_Currency_Switcher.module.getValueOf('give-cs-currency', form), 470 variable_amounts = []; // Store new converted amounts, 471 472 /** 473 * While rendering the donation form we are storing variable's custom and converted amount 474 * to "span.give-cs-variable-amount" DOM selector. It will needs to update according to the 475 * chosen currency. 476 */ 477 var variable_old_amounts = form.find('span.give-cs-variable-amount'); 478 479 // Update each variable price. 480 $.each(variable_prices, function (index, variable) { 481 var price_id = variable.price_id, // Get the price id. 482 variable_custom_prices = Give_Currency_Switcher.module.getCustomAmounts(form, price_id), // Get the custom prices. 483 variable_amount = variable.amount, // Get the variable amount. 484 variable_selector = form.find('.give-donation-levels-wrap [data-price-id=' + price_id + ']'); 485 486 var previous_variable_amounts = variable_old_amounts.length 487 ? JSON.parse(decodeURIComponent(variable_old_amounts.attr('data-custom_amount'))) 488 : []; // Get the temporarily stored variable amounts. 489 490 // Go through all old values. 491 $.each(previous_variable_amounts, function (index, price_data) { 492 if (price_data[price_id]) { 493 variable_amount = price_data[price_id]['converted-amount']; 494 } 495 }); 496 497 // If this variable has custom price instead of the calculation through rates. 498 var converted_var_amount = !variable_custom_prices[current_currency] 499 ? Give_Currency_Switcher.module.convertCurrency( 500 variable_amount, 501 form, 502 currency_old, 503 null, 504 true, 505 price_id 506 ) // Convert the variable amount if it has no custom amount. 507 : variable_custom_prices[current_currency]; 508 509 // Update variable price. 510 form.find('.give-donation-levels-wrap [data-price-id=' + price_id + ']').val( 511 Give.fn.formatCurrency(converted_var_amount, {}, form) 512 ); 513 514 // If display style is 'radio'. 515 var label_text = variable_selector.hasClass('give-radio-input') 516 ? form
517 .find('.give-donation-levels-wrap [data-price-id=' + price_id + ']') 518 .closest('li') 519 .find('label') 520 .html() 521 : form.find('.give-donation-levels-wrap [data-price-id=' + price_id + ']').html(); 522 523 // Change label of the variable element. 524 if ('custom' !== price_id) { 525 // Check if price has label. 526 var parsedJSON = jQuery.parseJSON(give_cs_json_obj), 527 form_id = Give.form.fn.getInfo('form-id', form), 528 price_has_label = parsedJSON['form_' + form_id]['variable_prices'][price_id]['has_label'], 529 decimals_enabled = parsedJSON['form_' + form_id]['decimals_enabled']; 530 531 if (Give_Currency_Switcher.isMultiStepForm) { 532 var symbol = Give.form.fn.getInfo('currency_symbol', form), 533 position = Give.form.fn.getInfo('currency_position', form), 534 precision = Give.form.fn.getInfo('number_decimals', form), 535 amount; 536 537 if ('enabled' === decimals_enabled && Give.fn.numberHasDecimal(converted_var_amount)) { 538 amount = Give.fn.formatCurrency( 539 converted_var_amount, 540 { 541 symbol: symbol, 542 position: position, 543 precision: precision, 544 }, 545 form 546 ); 547 } else { 548 amount = Math.round(converted_var_amount); 549 } 550 551 var label_string = 552 '<div class="currency currency--' + position + '">' + symbol + '</div>' + amount; 553 554 form.find('.give-donation-levels-wrap') 555 .find( 556 '.give-btn-level-' + 557 price_id + 558 ', *[for="give-radio-level-' + 559 price_id + 560 '"], .give-donation-level-' + 561 price_id 562 ) 563 .html(label_string); 564 } else { 565 var amount = Give.fn.formatCurrency( 566 converted_var_amount, 567 { 568 symbol: Give.form.fn.getInfo('currency_symbol', form), 569 position: Give.form.fn.getInfo('currency_position', form), 570 precision: Give.form.fn.getInfo('number_decimals', form), 571 }, 572 form 573 ); 574 575 var label_array = label_text.split(', '), 576 label_length = label_array.length; 577 578 var label_string = amount; 579 if (2 === label_length) { 580 label_string = amount + ', ' + label_array[1]; 581 } 582 583 if (!price_has_label) { 584 form.find('.give-donation-levels-wrap') 585 .find( 586 '.give-btn-level-' + 587 price_id + 588 ', *[for="give-radio-level-' + 589 price_id + 590 '"], .give-donation-level-' + 591 price_id 592 ) 593 .html(label_string); 594 } 595 } 596 } 597 598 // Trigger variable price. 599 if ('custom' !== form_price_id) { 600 Give.form.fn.autoSetMultiLevel( 601 form.find('.give-donation-levels-wrap').find('*[data-price-id="' + form_price_id + '"]') 602 ); 603 } 604 605 // Create array of the new converted variable's amount. 606 var amount_obj = {}; 607 608 amount_obj[price_id] = { 609 'converted-amount': Give_Currency_Switcher.module.convertCurrency( 610 variable_amount, 611 form, 612 currency_old 613 ), // Converted amount for this price id. 614 'custom-amount': variable_custom_prices[current_currency], // Custom amount for this price id, if exists. 615 }; 616 617 // Store custom amount. 618 variable_amounts.push(amount_obj); 619 }); 620 621 if (0 < variable_old_amounts.length) { 622 // Update variable amounts. 623 variable_old_amounts.attr('data-custom_amount', JSON.stringify(variable_amounts)); 624 } else { 625 // Store all the custom amount. 626 $('<span/>', { 627 class: 'give-cs-variable-amount give-hidden', 628 'data-custom_amount': JSON.stringify(variable_amounts), 629 }).appendTo(form); 630 } 631 }, 632 633 /** 634 * Convert/Update Goal amount When switch currency. 635 * 636 * @param {object} form 637 * @param {string} newCurrency 638 * @param decimal_separator 639 */ 640 updateGoalAmount: function (form, newCurrency, decimal_separator) { 641 const $goalType = Give.form.fn.getInfo('goal_format', form), 642 $goalWrapper = Give_Currency_Switcher.isMultiStepForm 643 ? form.parent().find('.income-stats') 644 : form.parent().find('.give-goal-progress'); 645 646 // If Goal amount is disabled or goal type isn't amount. 647 if ('undefined' === $goalType || 'amount' !== $goalType) { 648 return; 649 } 650 651 if (Give_Currency_Switcher.isMultiStepForm) { 652 const exchangeRates = Give_Currency_Switcher.module.getExchangeRates(form),
653 exchangeRate = exchangeRates[newCurrency] !== 0 ? exchangeRates[newCurrency] : 1, 654 goalAmount = Give.fn.formatCurrency( 655 form.attr('data-goal-amount') * exchangeRate, 656 { 657 symbol: Give.form.fn.getInfo('currency_symbol', form), 658 position: Give.form.fn.getInfo('currency_position', form), 659 precision: 0, 660 }, 661 form 662 ), 663 incomeAmount = Give.fn.formatCurrency( 664 form.attr('data-income-amount') * exchangeRate, 665 { 666 symbol: Give.form.fn.getInfo('currency_symbol', form), 667 position: Give.form.fn.getInfo('currency_position', form), 668 precision: 0, 669 }, 670 form 671 ); 672 673 // Convert goal amount to selected currency. 674 $goalWrapper.find('.goal .number').text(goalAmount); 675 // Convert income to selected currency. 676 $goalWrapper.find('.raised .number').text(incomeAmount); 677 } else { 678 goalAmounts = JSON.parse($goalWrapper.find('span.goal-text').attr('data-amounts')); 679 incomeAmounts = JSON.parse($goalWrapper.find('span.income').attr('data-amounts')); 680 681 // Convert goal amount to selected currency. 682 $goalWrapper.find('span.goal-text').text(goalAmounts[newCurrency]); 683 // Convert income to selected currency. 684 $goalWrapper.find('span.income').text(incomeAmounts[newCurrency]); 685 } 686 }, 687 688 /** 689 * Convert Minimum amount 690 * 691 * @since 1.1 692 * 693 * @param form 694 * @param price_id 695 */ 696 updateMinimumAmount: function (form, price_id) { 697 var selected_currency = Give_Currency_Switcher.module.getValueOf('give-cs-currency', form), // Get the selected currency. 698 give_custom_amounts = Give_Currency_Switcher.module.getCustomAmounts(form, price_id), // Get all of the custom amounts. 699 give_custom_price_amount = give_custom_amounts[selected_currency] || false; // Get the custom amount. 700 701 // Convert the Minimum amount. 702 var converted_give_minimum_amount = Give_Currency_Switcher.module.convertCurrency( 703 Give_Currency_Switcher.module.getDefaultMinimumAmount(form), 704 form, 705 give_global_vars.currency 706 ); 707 708 // If custom price amount is less the minimum amount. 709 if (give_custom_price_amount && give_custom_price_amount < converted_give_minimum_amount) { 710 converted_give_minimum_amount = give_custom_price_amount; 711 } 712 713 var format_args = { 714 symbol: '', 715 decimal: '.', 716 thousand: '', 717 precision: Give.form.fn.getInfo('number_decimals', form), 718 }; 719 720 // Update donation minimum amount. 721 Give_Currency_Switcher.module.setValueOf( 722 'give-form-minimum', 723 Give.fn.formatCurrency(converted_give_minimum_amount, format_args, form), 724 form 725 ); 726 727 // Get the maximum amount range value. 728 if ((form_maximum_amount = Give_Currency_Switcher.module.getDefaultMinimumAmount(form, 'max'))) { 729 // Convert the amount. 730 form_maximum_amount = Give_Currency_Switcher.module.convertCurrency( 731 form_maximum_amount, 732 form, 733 give_global_vars.currency 734 ); 735 736 if (give_custom_price_amount && give_custom_price_amount > form_maximum_amount) { 737 form_maximum_amount = give_custom_price_amount; 738 } 739 740 // Update donation maximum amount. 741 Give_Currency_Switcher.module.setValueOf( 742 'give-form-maximum', 743 Give.fn.formatCurrency(form_maximum_amount, format_args, form), 744 form 745 ); 746 } 747 }, 748 749 /** 750 * Payment Gateway changed. 751 * 752 * @since 1.0 753 * 754 * @param ev 755 * @param response HTML Content. 756 * @param formId Form ID 757 */ 758 changed_payment_gateway: function (ev, response, formId) { 759 // Declare required form details. 760 var formObject = jQuery(document).find('form#' + formId), 761 supportedGateways = Give_Currency_Switcher.module.getSupportedCurrencies(formObject), 762 selectedCurrency = formObject.find('input[name="give-cs-form-currency"]').val(), 763 currencyDropdown = formObject.find('.give-cs-select-currency'), 764 $totalAmountField = formObject.find('span.give-final-total-amount'), // Final donation amount container selector. 765 currentAmount = Give.form.fn.getAmount(formObject); // Selected donation amount value.; 766 767 // Bailout, if currency switcher is not enabled. 768 if (!Give_Currency_Switcher.module.isCurrencySwitcherEnable(formOb
768ject)) { 769 return; 770 } 771 772 // Remove currency not supported old notices. 773 formObject.find('#give_currency_not_support.give_notice').parent().remove(); 774 775 // Remove all of the currencies. 776 formObject.find('.give-cs-select-currency').empty(); 777 778 if (1 === Object.keys(supportedGateways).length) { 779 formObject.find('span.give-currency-symbol').addClass('give-cs-dropdown-hidden'); 780 formObject.find('select[name="give-cs-currency"]').addClass('give-cs-remove-cursor').hide(); 781 formObject.find('.give-cs-mini-dropdown-icon').addClass('give-cs-reset-width'); 782 } else { 783 formObject.find('span.give-currency-symbol').removeClass('give-cs-dropdown-hidden'); 784 formObject.find('select[name="give-cs-currency"]').removeClass('give-cs-remove-cursor').show(); 785 formObject.find('.give-cs-mini-dropdown-icon').removeClass('give-cs-reset-width'); 786 } 787 788 // Re-render the currency drop-down. 789 Give_Currency_Switcher.init_switcher(); 790 791 Object.keys(supportedGateways).forEach(function (key) { 792 if (-1 !== $.inArray(Give.form.fn.getGateway(formObject), supportedGateways[key].gateways)) { 793 // Create currency option using jQuery DOM for security. 794 var currency_option = jQuery('<option/>', { 795 value: key, 796 html: supportedGateways[key]['currency_label'], 797 }); 798 799 // Append currency. 800 formObject.find('.give-cs-select-currency').append(currency_option); 801 } 802 }); 803 804 // Show the currency switch drop-down. 805 formObject.find('.give-currency-switcher-wrap').show(); 806 807 $totalAmountField.attr('data-total', currentAmount); 808 $totalAmountField.text( 809 Give.fn.formatCurrency( 810 currentAmount, 811 { 812 symbol: Give.form.fn.getInfo('currency_symbol', formObject), 813 position: Give.form.fn.getInfo('currency_position', formObject), 814 }, 815 formObject 816 ) 817 ); 818 819 if (!Give_Currency_Switcher.module.isGatewaySupportCurrency(formObject, selectedCurrency)) { 820 Give_Currency_Switcher.module.showCurrencyNotSupportGatewayNotice(formObject, selectedCurrency); 821 822 if (1 === currencyDropdown.length) { 823 // Auto switch to available currency. 824 Give_Currency_Switcher.autoSwitchCurrency(formObject, currencyDropdown.find('option').val()); 825 } else { 826 // Reset Currency switcher. 827 formObject.find('.give-donation-amount .give-text-input').trigger('blur'); 828 829 // Select switched currency. 830 Give_Currency_Switcher.autoSwitchCurrency(formObject, selectedCurrency); 831 832 // Disable Donation now button. 833 formObject.find('#give-purchase-button').attr('disabled', true); 834 835 return; 836 } 837 } 838 839 if (0 < formObject.find('.give-cs-select-currency option[value="' + selectedCurrency + '"]').length) { 840 // Select switched currency. 841 Give_Currency_Switcher.autoSwitchCurrency(formObject, selectedCurrency); 842 } else if (0 < formObject.find('.give-cs-select-currency').length) { 843 $('<span/>', { 844 class: 'give-cs-old-exchange-rates', 845 'data-old_exchange_rates': JSON.stringify( 846 Give_Currency_Switcher.module.getExchangeRates(formObject) 847 ), 848 }).appendTo(formObject); 849 850 // Change to base currency. 851 formObject 852 .find('.give-cs-select-currency') 853 .val(Give_Currency_Switcher.module.getValueOf('give-cs-base-currency', formObject)) 854 .change(); 855 } 856 }, 857 858 /** 859 * This function is used to auto switch currency based on the currency provided. 860 * 861 * @since 1.3.3 862 * 863 * @param {object} formObject Donation Form Object. 864 * @param {string} currency Currency code. 865 */ 866 autoSwitchCurrency: function (formObject, currency) { 867 // Auto switch to the currency provided. 868 formObject.find('.give-cs-select-currency').val(currency).trigger('change'); 869 }, 870 871 /** 872 * Remove sanitized donation total 873 * 874 * @since 1.0 875 * @param e 876 */ 877 remove_sanitize_amount: function (e) { 878 var $form = jQuery(this).closest('form'), // Get the current form. 879 decimal_separator = Give.form.fn.getInfo('decimal_separator', $form), 880 current_total = Give.fn.unFormatCurrency( 881 $form.find('input[name="give-amount"]').val(), 882 decimal_separator 883 ), 884 old_total = Give.fn.unFormatCurrency( 885 $form.find('.give-cs-amount-totals').attr('data-total'), 886 decimal_separator 887 ); 888 889 // If give amount changed then remove old sanitize and custom amount. 890 if (current_total !== old_total) { 891 // Get the currency sign from the selected donation level. 892 $form.find('.give-cs-amount-totals').removeAttr('data-sanitize-amount data-custom_amount'); 893 } 894 }, 895 896 /** 897 * It contains method to get and set value for input fields 898 * in-between the form. 899 * 900 * @since 1.0 901 */ 902 module: { 903 /** 904 * Get value of any particular field. 905 * 906 * @since 1.0 907 * 908 * @param {string} key 909 * @param {jQuery} form jQuery object. 910 * @returns {*} 911 */ 912 getValueOf: function (key, form) { 913 form = 0 < form.length ? form : {}; 914 key = 0 < key.length ? key : ''; 915 916 var result_var = false;
917 918 switch (key) { 919 case 'base_currency': 920 result_var = form.find('*[name="give-cs-base-currency"]').val(); 921 break; 922 923 case 'currency_rate': 924 result_var = form.find('*[name="give-cs-exchange-rate"]').val(); 925 break; 926 927 case 'switched_currency': 928 result_var = form.find('*[name="give-cs-form-currency"]').val(); 929 break; 930 931 case 'acronyms': 932 var give_cs_json = jQuery.parseJSON(give_cs_json_obj); 933 result_var = give_cs_json['form_' + Give.form.fn.getInfo('form-id', form)]['currency_acronym']; 934 break; 935 936 case 'give-cs-currency': 937 case 'selected-currency': 938 result_var = form.find('input[name="give-cs-form-currency"]').val(); 939 break; 940 941 default: 942 result_var = form.find('*[name="' + key + '"]').val(); 943 'undefined' === result_var ? form.attr('*[data-"' + key + '"]') : ''; 944 } 945 return 'undefined' !== result_var ? result_var : ''; 946 }, 947 948 /** 949 * Set value of currency switcher. 950 * 951 * @since 1.0 952 * 953 * @param {string} key 954 * @param {string} value 955 * @param {jQuery} form 956 * @returns {boolean} 957 */ 958 setValueOf: function (key, value, form) { 959 if (0 >= key.length) { 960 return false; 961 } 962 963 form = 0 < form.length ? form : {}; 964 value = '' !== value.length ? value : ''; 965 966 // Get field. 967 var field = form.find('input[name="' + key + '"]'); 968 969 // Set value. 970 'undefined' !== field ? field.val(value) : ''; 971 }, 972 973 /** 974 * Get exchange rates. 975 * 976 * @since 1.0 977 * 978 * @param {jQuery} $form 979 */ 980 getExchangeRates: function ($form) { 981 // Get the give form id. 982 var give_form_id = $form ? Give.form.fn.getInfo('form-id', $form) : 0, 983 give_cs_json = jQuery.parseJSON(give_cs_json_obj), 984 exchange_rates = []; 985 986 if (0 !== give_form_id) { 987 exchange_rates = give_cs_json['form_' + give_form_id]['exchange_rates']; 988 } 989 990 $.each(exchange_rates, function (currency_key, data) { 991 if (-1 !== $.inArray(Give.form.fn.getGateway($form), data.support_gateways)) { 992 exchange_rates[currency_key] = data.rates; 993 } 994 }); 995 996 return exchange_rates; 997 }, 998 999 /** 1000 * Get the Number of the decimal. 1001 * 1002 * @param $form 1003 * @param $currency_code 1004 * @returns {Array|bool} 1005 */ 1006 getRatesDecimalNumber: function ($form, $currency_code) { 1007 if (!$form) { 1008 return false; 1009 } 1010 1011 // Get the give form id. 1012 var give_form_id = Give.form.fn.getInfo('form-id', $form), 1013 give_cs_json = jQuery.parseJSON(give_cs_json_obj), 1014 decimal_numbers = give_cs_json['form_' + give_form_id]['decimal_number']; 1015 1016 return $currency_code ? decimal_numbers[$currency_code] : decimal_numbers; 1017 }, 1018 1019 /** 1020 * Get the custom amount. 1021 * 1022 * @since 1.0 1023 * 1024 * @param $form 1025 * @param $price_id 1026 * @returns {Array} 1027 */ 1028 getCustomAmounts: function ($form, $price_id) { 1029 // Get the give form id. 1030 var give_form_id = $form ? Give.form.fn.getInfo('form-id', $form) : 0, 1031 give_cs_json = jQuery.parseJSON(give_cs_json_obj), 1032 amounts = []; 1033 1034 // Get the custom amounts. 1035 var custom_amounts = give_cs_json['form_' + give_form_id]['custom_amounts']; 1036 1037 $.each(custom_amounts, function (currency_key, currency_data) { 1038 amounts[currency_key] = false; 1039 1040 // If variable has no prices. 1041 if ('' !== $price_id && -1 !== $price_id && undefined !== $price_id) { 1042 if (undefined !== currency_data[$price_id] && false !== currency_data[$price_id]) { 1043 amounts[currency_key] = currency_data[$price_id]['raw_amount']; 1044 } 1045 } else { 1046 if (undefined !== currency_data['raw_amount']) { 1047 amounts[currency_key] = currency_data['raw_amount']; 1048 } 1049 } 1050 }); 1051 return amounts; 1052 }, 1053 1054 /** 1055 * Get the supported currency. 1056 * 1057 * @since 1.0 1058 */ 1059 getSupportedCurrencies: function ($form_object) { 1060 // Return, if currency switcher isn't activate. 1061 if (!Give_Currency_Switcher.module.isCurrencySwitcherEnable($form_object)) { 1062 return; 1063 } 1064 1065 var form_id = $form_object ? Give.form.fn.getInfo('form-id', $form_object) : 0, 1066 give_cs_json = jQuery.parseJSON(give_cs_json_obj); 1067
1068 // Return empty array when form id is not valid. 1069 if ('undefined' === typeof form_id) { 1070 return []; 1071 } 1072 1073 // Bailout, if currency switcher is not enabled. 1074 if ('undefined' === typeof give_cs_json['form_' + form_id]['support_gateways']) { 1075 return; 1076 } 1077 1078 var supported_gateways = [], 1079 supported_gateway_list = give_cs_json['form_' + form_id]['support_gateways'], 1080 form_gateway = Give.form.fn.getGateway($form_object); 1081 1082 Object.keys(supported_gateway_list).forEach(function (key, value) { 1083 if (-1 !== $.inArray(form_gateway, supported_gateway_list[key].gateways)) { 1084 supported_gateways[key] = supported_gateway_list[key]; 1085 supported_gateways[key].currencySetting = give_cs_json['supported_currency'][key]; 1086 } 1087 }); 1088 1089 return supported_gateways; 1090 }, 1091 1092 /** 1093 * Get the supported currency. 1094 * 1095 * @since 1.5.1 1096 */ 1097 getAllSupportedCurrencies: function () { 1098 return jQuery.parseJSON(give_cs_json_obj)['supported_currency']; 1099 }, 1100 1101 /** 1102 * Get Donation amount. 1103 * 1104 * @since 1.0 1105 * 1106 * @param {jQuery} form Donation Form selector 1107 * @param {string} decimal_separator 1108 */ 1109 getDonationAmount: function (form, decimal_separator) { 1110 form = 'undefined' !== form ? form : {}; 1111 1112 decimal_separator = decimal_separator || Give.form.fn.getInfo('decimal_separator', form); 1113 1114 var amount_field = form.find('span.give-cs-amount-totals'); 1115 1116 var /** 1117 * Sanitize amount: When customer put custom amount converted amount may have long decimal precision 1118 * so for accurate conversion we will have to use full amount with original amount. 1119 */ 1120 sanitize_amount = amount_field.attr('data-sanitize-amount'), 1121 /** 1122 * Custom Amount: If the donation form has Give's custom prices option enabled and if donor provide their 1123 * custom value then it will take custom amount instead of donation actual amount. 1124 */ 1125 custom_amount = amount_field.attr('data-custom_amount'), 1126 donation_amount = form.find('input[name="give-amount"]').val(); // Donation amount text field. 1127 1128 /** 1129 * If currency has custom amount when we are storing the conversion 1130 * of amount into "input[name="give-cs-custom-amount"]" field. 1131 * 1132 * For Eg: If custom amount is set for INR is â¹10 then we will store 1133 * the conversion of it with base currency in it. 1134 * 1135 * @type {string} 1136 */ 1137 var donation_custom_amount = 1138 typeof custom_amount !== typeof undefined && false !== custom_amount 1139 ? custom_amount 1140 : 0 < form.find('input[name="give-cs-custom-amount"]').length 1141 ? form.find('input[name="give-cs-custom-amount"]').val() 1142 : ''; 1143 1144 // Return donation form amount based on various action. 1145 return 0 < donation_custom_amount 1146 ? donation_custom_amount 1147 : 0 < sanitize_amount 1148 ? sanitize_amount 1149 : Give.fn.unFormatCurrency(donation_amount, decimal_separator); 1150 }, 1151 1152 /** 1153 * Get form's base amounts. 1154 * 1155 * @param $form 1156 * @param $price_id 1157 */ 1158 getBaseAmounts: function ($form, $price_id) { 1159 var baseAmounts = JSON.parse(Give.form.fn.getInfo('give_cs_base_amounts', $form)); 1160 1161 if (-1 === $price_id) { 1162 $price_id = 0 !== baseAmounts.custom ? 'custom' : 0; 1163 } 1164 1165 return baseAmounts[$price_id]; 1166 }, 1167 1168 /** 1169 * Convert currency to another currency. 1170 * 1171 * @since 1.0 1172 * 1173 * @param {float} amount
1174 * @param {jQuery} form 1175 * @param {string} from_currency 1176 * @param {string} to_currency 1177 * @param {boolean} isForm 1178 * @param {string} priceID 1179 */ 1180 convertCurrency: function (amount, form, from_currency, to_currency, isForm, priceID) { 1181 // Get the selected currency if not passed. 1182 to_currency = to_currency || Give_Currency_Switcher.module.getValueOf('give-cs-currency', form); 1183 1184 if (from_currency === to_currency) { 1185 return amount; 1186 } 1187 1188 isForm = isForm || false; 1189 1190 const oldCurrencyExchangeRates = form 1191 .find('span.give-cs-old-exchange-rates') 1192 .attr('data-old_exchange_rates'); 1193 const getExchangeRates = Give_Currency_Switcher.module.getExchangeRates(form, true); 1194 const currency_rates = 1195 0 < form.find('span.give-cs-old-exchange-rates').length 1196 ? null !== oldCurrencyExchangeRates 1197 ? JSON.parse(oldCurrencyExchangeRates) 1198 : getExchangeRates 1199 : getExchangeRates; 1200 1201 /** 1202 * If this conversion is for donation form then get the amount in base amount always. 1203 * Otherwise, convert the amount as per the currency. 1204 */ 1205 if (!isForm) { 1206 // If switched currency is not give base currency. 1207 if (give_global_vars.currency !== to_currency) { 1208 // If Switched currency is not form currency. 1209 if (give_global_vars.currency !== from_currency) { 1210 amount = amount / currency_rates[from_currency]; // Revert back to the original amount. 1211 } 1212 } else { 1213 // If donation has already changed to donor's preferred currency. 1214 amount = amount / currency_rates[from_currency]; 1215 } 1216 } else { 1217 // If the calculation is about form calculation. 1218 priceID = 'undefined' === typeof priceID ? Give.form.fn.getPriceID(form) : priceID; 1219 amount = Give_Currency_Switcher.module.getBaseAmounts(form, priceID); 1220 } 1221 1222 return (converted_amount = 1223 0 < currency_rates[to_currency] ? amount * currency_rates[to_currency] : amount); 1224 }, 1225 1226 /** 1227 * Update Fee Recovery base_amount. 1228 * 1229 * @since 1.0 1230 * 1231 * @param {string} old_currency 1232 * @param {string} new_currency 1233 * @param {jQuery} form 1234 * @param {string} decimal_separator 1235 */ 1236 updateFeeAmount: function (old_currency, new_currency, form, decimal_separator) { 1237 // Check if Fee Recovery is activated for this form. 1238 if ( 1239 0 < form.find('input[name="give-fee-recovery-settings"]').length && 1240 undefined !== typeof Give_Fee_Recovery 1241 ) { 1242 // Get fee recovery settings. 1243 var fee_recovery_data = JSON.parse(form.find('input[name="give-fee-recovery-settings"]').val()); 1244 1245 if (false === fee_recovery_data.fee_recovery) { 1246 return; 1247 } 1248 1249 // If the setting is configured for all gateways. 1250 if ( 1251 'undefined' !== typeof fee_recovery_data.fee_data.all_gateways && 1252 '' !== fee_recovery_data.fee_data.all_gateways.base_amount 1253 ) { 1254 fee_recovery_data.fee_data.all_gateways.base_amount = 1255 Give_Currency_Switcher.module.convertCurrency( 1256 fee_recovery_data.fee_data.all_gateways.base_amount, 1257 form, 1258 old_currency, 1259 new_currency 1260 ); 1261 } else { 1262 // If setting is configured for each gateway separately. 1263 $.each(fee_recovery_data.fee_data, function (gateway_key, data) { 1264 fee_recovery_data.fee_data[gateway_key].base_amount = 1265 Give_Currency_Switcher.module.convertCurrency( 1266 fee_recovery_data.fee_data[gateway_key].base_amount, 1267 form, 1268 old_currency, 1269 new_currency 1270 ); 1271 }); 1272 } 1273 1274 // Update Fee Recovery data. 1275 form.find('input[name="give-fee-recovery-settings"]').val(JSON.stringify(fee_recovery_data)); 1276 form.find('span.give-cs-old-exchange-rates').remove(); 1277 1278 // Update fee amount. 1279 Give_Fee_Recovery.give_fee_update( 1280 form, 1281 true, 1282 form.find('#give-amount').val(), 1283 Give.form.fn.getGateway(form) 1284 ); 1285 } 1286 }, 1287 1288 /** 1289 * Get default minimum amount, Amount in base currency. 1290 * 1291 * @since 1.0 1292 * 1293 * @param $form 1294 * @param $type 1295 */ 1296 getDefaultMinimumAmount: function ($form, $type) { 1297 $type = $type || 'min'; 1298 1299 // Get the currency switcher object data. 1300 var form_data_obj = 1301 jQuery.parseJSON(give_cs_json_obj)['form_' + Give.form.fn.getInfo('form-id', $form)]; 1302 1303 return 'min' === $type ? form_data_obj['minimum_amount'] : form_data_obj['maximum_amount']; 1304 }, 1305 1306 /** 1307 * Update currency switched message. 1308 * 1309 * @since 1.0 1310 * 1311 * @param form 1312 * @param price_id 1313 */ 1314 updateCurrencyMessage: function (form, price_id) { 1315 if (!Give_Currency_Switcher.module.isCurrencySwitcherEnable(form)) { 1316 return; 1317 } 1318 1319 // Get the custom amounts per currency. 1320 var custom_amounts = Give_Currency_Switcher.module.getCustomAmounts(form, price_id), 1321 message_container = form.find('.give-currency-switcher-msg'), 1322 switched_currency = Give.form.fn.getInfo('currency_code', form); 1323 1324 if (null === switched_currency) { 1325 form.find('#give-purchase-button').attr('disabled', true); 1326 return false;
1327 } 1328 1329 // Get the rate number of decimals. 1330 var rates_number_decimals = Give_Currency_Switcher.module.getRatesDecimalNumber(form); 1331 1332 // If we have custom amount. 1333 if (custom_amounts[switched_currency]) { 1334 message_container.text(give_currency_switcher.cs_custom_price_message).show(); 1335 } else { 1336 var current_rate = parseFloat(form.find('input[name="give-cs-exchange-rate"]').val()); 1337 1338 if (0 !== current_rate) { 1339 message_container.text( 1340 message_container 1341 .data('rawtext') 1342 .replace('{new_currency}', switched_currency) 1343 .replace( 1344 '{new_currency_rate}', 1345 Give.fn.formatCurrency( 1346 current_rate, 1347 {precision: rates_number_decimals[switched_currency]}, 1348 form 1349 ) 1350 ) 1351 .replace('{base_currency}', give_global_vars.currency) 1352 ); 1353 } 1354 } 1355 1356 // If base currency and switched currency are same then hide this message. 1357 give_global_vars.currency === switched_currency || 0 === current_rate 1358 ? form.find('.give-currency-switcher-msg-wrap').hide() 1359 : form.find('.give-currency-switcher-msg-wrap').show(); 1360 }, 1361 1362 /** 1363 * Check if currency switcher is enabled or not. 1364 * 1365 * @since 1.0 1366 * @param $form 1367 */ 1368 isCurrencySwitcherEnable: function ($form) { 1369 if ('undefined' === typeof give_cs_json_obj || 'null' === give_cs_json_obj) { 1370 return false; 1371 } 1372 1373 const csConfig = jQuery.parseJSON(give_cs_json_obj), 1374 formId = Give.form.fn.getInfo('form-id', $form), 1375 formDataKey = 'form_' + formId; 1376 1377 if (!csConfig.hasOwnProperty(formDataKey)) { 1378 return false; 1379 } 1380 1381 // Get the currency switcher object data. 1382 return jQuery.parseJSON(give_cs_json_obj)[formDataKey]; 1383 }, 1384 1385 /** 1386 * Get the supported currency. 1387 * 1388 * @since 1.3.1 1389 */ 1390 isGatewaySupportCurrency: function ($form_object, new_currency) { 1391 var form_id = $form_object ? Give.form.fn.getInfo('form-id', $form_object) : 0, 1392 give_cs_json = jQuery.parseJSON(give_cs_json_obj); 1393 1394 if (0 === form_id) { 1395 return false; 1396 } 1397 1398 var currency_code = new_currency; 1399 if ('undefined' === typeof new_currency) { 1400 currency_code = Give.form.fn.getInfo('currency_code', $form_object); 1401 } 1402 1403 var supported_gateway_list = give_cs_json['form_' + form_id]['support_gateways'], 1404 form_gateway = Give.form.fn.getGateway($form_object), 1405 currency_based_gateways = supported_gateway_list[currency_code].gateways; 1406 1407 return -1 !== $.inArray(form_gateway, currency_based_gateways); 1408 }, 1409 1410 /**
1411 * Display Currency not Support Notice. 1412 * 1413 * @param $form_object 1414 * @param new_currency 1415 */ 1416 showCurrencyNotSupportGatewayNotice: function ($form_object, new_currency) { 1417 // Remove currency not supported old notices. 1418 $form_object.find('#give_currency_not_support.give_notice').parent().remove(); 1419 1420 $form_object.find('#give-purchase-button').attr('disabled', true); 1421 $form_object.find('.give-currency-switcher-msg-wrap').hide(); 1422 1423 var currency_chosen = new_currency, 1424 form_currency = $form_object.data('currency_code'); 1425 1426 if ('undefined' === typeof currency_chosen) { 1427 currency_chosen = form_currency; 1428 } 1429 1430 // Add notice if currency is not support by selected gateway. 1431 $('<div/>', { 1432 class: 'give_notices give_errors', 1433 id: 'give_error_error', 1434 }) 1435 .append( 1436 $('<p/>', { 1437 class: 'give_error give_notice', 1438 id: 'give_currency_not_support', 1439 'data-dismissible': '1', 1440 'dismiss-type': 'manual', 1441 }).html( 1442 give_currency_switcher.currency_not_support_message 1443 .replace('{currency_code}', currency_chosen) 1444 .replace( 1445 '{payment_gateway}', 1446 $form_object 1447 .find('#give-gateway-option-' + Give.form.fn.getGateway($form_object)) 1448 .text() 1449 ) + 1450 '<img class="notice-dismiss give-notice-close" src="' + 1451 give_currency_switcher.notice_dismiss_image + 1452 '">' 1453 ) 1454 ) 1455 .prependTo($form_object); 1456 }, 1457 }, 1458 1459 /** 1460 * Prevent drop-down, if we don't have currency other than base currency. 1461 * 1462 * @since 1.0 1463 * @param e 1464 * @returns {boolean} 1465 */ 1466 chosenCurrency: function (e) { 1467 if (1 >= $(this).find('option').length) { 1468 e.preventDefault(); 1469 return false; 1470 } 1471 }, 1472 }; 1473 1474 $(function () { 1475 // Call initially. 1476 Give_Currency_Switcher.init(); 1477 }); 1478})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.