MediaWiki:MonthlyConvert.js

From Donate
Revision as of 18:55, 2 August 2023 by Pcoombe (talk | contribs) (remove fade effects)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
var mc = {};

// If one-time amount <= left amount, suggest right amount for monthly
// If changing these, please update spreadsheet
// https://docs.google.com/spreadsheets/d/1z36zi8EegPLAvR5FYAgwz8ywKZ50QNB82SpwpTdk-xQ/edit#gid=1258723967
mc.amounts = {
    'USD' : [ // also CAD, AUD, NZD, GBP, EUR
        [ 1.99, 0 ],
        [ 2.35, 1.40 ],
        [ 9, 1.75 ],
        [ 12, 2 ],
        [ 15, 2.5 ],
        [ 18, 3 ],
        [ 21, 3.5 ],
        [ 24, 4 ],
        [ 27, 4.5 ],
        [ 30, 5 ],
        [ 33, 5.5 ],
        [ 36, 6 ],
        [ 39, 6.5 ],
        [ 42, 7 ],
        [ 45, 7.5 ],
        [ 48, 8 ],
        [ 51, 8.5 ],
        [ 54, 9 ],
        [ 57, 9.5 ],
        [ 60, 10 ],
        [ 63, 10.5 ],
        [ 66, 11 ],
        [ 69, 11.5 ],
        [ 72, 12 ],
        [ 75, 12.5 ],
        [ 102, 17 ],
        [ 250, 25 ],
        [ 499, 50 ],
        [ Infinity, 0 ]
    ],
    'INR' : [
        [ 29, 0 ],
        [ 75, 25 ],
        [ 79, 29 ],
        [ 104, 30 ],
        [ 208, 35 ],
        [ 312, 55 ],
        [ 416, 70 ],
        [ 520, 90 ],
        [ 624, 110 ],
        [ 728, 130 ],
        [ 832, 140 ],
        [ 936, 160 ],
        [ 1040, 175 ],
        [ 1144, 190 ],
        [ 1248, 210 ],
        [ 1352, 235 ],
        [ 1456, 245 ],
        [ 1560, 265 ],
        [ 2080, 350 ],
        [ 3120, 530 ],
        [ 4160, 700 ],
        [ 5200, 880 ],
        [ 7800, 1320 ],
        [ 10400, 1760 ],
        [ 15600, 2650 ],
        [ 20800, 3530 ],
        [ 26000, 4420 ],
        [ Infinity, 0 ]
    ],
    'JPY' : [
        [ 299, 0 ],
        [ 900, 150 ],
        [ 1200, 200 ],
        [ 1500, 250 ],
        [ 1800, 300 ],
        [ 2100, 350 ],
        [ 2400, 400 ],
        [ 2700, 450 ],
        [ 3000, 500 ],
        [ 3300, 550 ],
        [ 3600, 600 ],
        [ 3900, 650 ],
        [ 4200, 700 ],
        [ 4500, 750 ],
        [ 4800, 800 ],
        [ 5100, 850 ],
        [ 5400, 900 ],
        [ 5700, 950 ],
        [ 6000, 1000 ],
        [ 6300, 1050 ],
        [ 6600, 1100 ],
        [ 6900, 1150 ],
        [ 7200, 1200 ],
        [ 7500, 1250 ],
        [ 10800, 1800 ],
        [ 18000, 3000 ],
        [ 50000, 6000 ],
        [ Infinity, 0 ]
    ],
    'SEK' : [
    	[ 25, 0 ],
        [ 50, 25 ],
        [ 100, 30 ],
        [ 200, 50 ],
        [ 300, 70 ],
        [ 500, 90 ],
        [ 1000, 110 ],
        [ 2500, 250 ],
        [ 5000, 500 ],
        [ Infinity, 0 ]
    ],
    'HUF' : [
        [ 499, 0 ],
        [ 3000, 500 ],
        [ 6000, 1000 ],
        [ 9000, 1500 ],
        [ 12000, 2000 ],
        [ 18000, 3000 ],
        [ 24000, 4000 ],
        [ 30000, 5000 ],
        [ 36000, 6000 ],
        [ 42000, 7000 ],
        [ 48000, 8000 ],
        [ 54000, 9000 ],
        [ 60000, 10000 ],
        [ Infinity, 0 ]
    ],
    'ILS' : [
        [ 9, 0 ],
        [ 10, 5 ],
        [ 60, 10 ],
        [ 90, 15 ],
        [ 120, 20 ],
        [ 180, 30 ],
        [ 240, 40 ],
        [ 300, 50 ],
        [ 360, 60 ],
        [ 420, 70 ],
        [ 480, 80 ],
        [ 540, 90 ],
        [ 600, 100 ],
        [ Infinity, 0 ]
    ],
    'ZAR' : [
        [ 29, 0 ],
        [ 30, 20 ],
        [ 50, 30 ],
        [ 100, 40 ],
        [ 300, 50 ],
        [ 450, 75 ],
        [ 600, 100 ],
        [ 900, 150 ],
        [ 1200, 200 ],
        [ 1500, 250 ],
        [ 1800, 300 ],
        [ 2400, 400 ],
        [ 3000, 500 ],
        [ 3600, 600 ],
        [ Infinity, 0 ]
    ],
    'MYR' : [ // Also RON, PLN
        [ 9, 0 ],
        [ 30, 5 ],
        [ 50, 10 ],
        [ 120, 20 ],
        [ 180, 30 ],
        [ 240, 40 ],
        [ 300, 50 ],
        [ 360, 60 ],
        [ 420, 70 ],
        [ 480, 80 ],
        [ 540, 90 ],
        [ 600, 100 ],
        [ 900, 150 ],
        [ Infinity, 0 ]
    ],
    'DKK' : [ // Also NOK
        [ 19, 0 ],
        [ 20, 10 ],
        [ 120, 20 ],
        [ 180, 30 ],
        [ 300, 50 ],
        [ 450, 75 ],
        [ 600, 100 ],
        [ 750, 125 ],
        [ 900, 150 ],
        [ 1200, 200 ],
        [ 1500, 250 ],
        [ 1800, 300 ],
        [ 2100, 350 ],
        [ Infinity, 0 ]
    ],
    'CZK' : [
        [ 49, 0 ],
        [ 180, 30 ],
        [ 300, 50 ],
        [ 600, 100 ],
        [ 900, 150 ],
        [ 1200, 200 ],
        [ 1500, 250 ],
        [ 1800, 300 ],
        [ 2100, 350 ],
        [ 2400, 400 ],
        [ 3000, 500 ],
        [ 3600, 600 ],
        [ 4200, 700 ],
        [ 4800, 800 ],
        [ Infinity, 0 ]
    ]
};
mc.amounts.GBP = mc.amounts.USD;
mc.amounts.EUR = mc.amounts.USD;
mc.amounts.CAD = mc.amounts.USD;
mc.amounts.AUD = mc.amounts.USD;
mc.amounts.NZD = mc.amounts.USD;
mc.amounts.RON = mc.amounts.MYR;
mc.amounts.PLN = mc.amounts.MYR;
mc.amounts.NOK = mc.amounts.DKK;

mc.getSuggestedAmount = function ( amount, currency ) {
    var i,
        amountsForCurrency = mc.amounts[ currency ],
        numAmounts;
    if ( !amountsForCurrency ) {
        return 0;
    }
    numAmounts = amountsForCurrency.length;
    for ( i = 0; i < numAmounts; i++ ) {
        if ( amount <= amountsForCurrency[ i ][ 0 ] ) {
            return amountsForCurrency[ i ][ 1 ];
        }
    }
    return 0;
};

mc.formatAmount = function ( amount, currency, locale ) {
    var formattedAmount;
    try {
        formattedAmount = amount.toLocaleString( locale, { currency: currency, style: 'currency' } );
    } catch ( e ) {
        // Assume a two decimal place currency for fallback
        formattedAmount = currency + ' ' + amount.toFixed( 2 );
    }
    return formattedAmount;
};

mc.getOtherAmount = function() {
    var otherInput = document.getElementById('mc-other-amount-input'),
        otherAmount = null;
    if ( otherInput.value !== '' ) {
        otherAmount = donationForm.parseOtherAmount( otherInput.value );
    }
    return otherAmount;
};

/**
 * Check if selected amount is valid i.e. a positive number, between minimum and maximum.
 * If not, show an error and return false.
 */
mc.validateOtherAmount = function() {

    var amount = mc.getOtherAmount();
    var minAmount = donationForm.minimums[ donationForm.currency ] || 1;

    if ( amount === null || isNaN(amount) || amount <= 0 || amount < minAmount ) {
        $('.mc-edit-amount').addClass('mc-haserror');
        $('.mc-error-bigamount').hide();
        $('.mc-error-smallamount').show();
        return false;
    } else if ( amount > donationForm.maxUSD * minAmount ) {
        $('.mc-edit-amount').addClass('mc-haserror');
        $('.mc-error-smallamount').hide();
        $('.mc-error-bigamount').show();
        return false;
    } else {
        $('.mc-edit-amount').removeClass('mc-haserror');
        $('.mc-error-smallamount, .mc-error-bigamount').hide();
        return true;
    }

};

mc.main = function( params, callback ) {

    var initialAmount = params.amount,
        currency = params.currency || 'USD',
        suggestedAmount = mc.getSuggestedAmount( initialAmount, currency ),
        locale = donationForm.getLocale( params.uselang, params.country ),
        skipMc;

	if ( mw.util.getParamValue( 'monthlyconvert' ) === 'false' || mw.util.getParamValue( 'monthlyconvert' ) === 'none' ) {
		skipMc = true;
	}

	if ( mw.util.getParamValue( 'utm_medium' ) === 'endowment' ) {
		// No recurring for endowment
		skipMc = true;
	}

    if ( params.recurring ) {
        // Already recurring
        skipMc = true;
    }

    if ( donationForm.noRecurringCountries.indexOf( params.country ) !== -1 ) {
        // Country where we don't offer any recurring
        skipMc = true;
    }

    if ( suggestedAmount === 0 ) {
        // Amount too low, or we don't have any suggestions
        skipMc = true;
    }

    if (
        ( params.payment_method === 'paypal' ) ||
        ( params.payment_method === 'apple' ) ||
        ( params.payment_method === 'google' ) ||
        ( params.payment_method === 'rtbt' && params.payment_submethod === 'rtbt_ideal' ) ||
        ( params.payment_method === 'bt' && params.payment_submethod === 'upi' ) ||
        ( params.payment_method === 'bt' && params.payment_submethod === 'paytmwallet' )
    ) {
        // suitable method!
    } else {
        skipMc = true;
    }

    if ( skipMc ) {
        callback( params );
    } else {
        $('.mc-currencycode').text( currency );
        $('.mc-amt-initial').text( mc.formatAmount( initialAmount, currency, locale) );
        $('.mc-amt-suggest').text( mc.formatAmount( suggestedAmount, currency, locale ) );

        // Block typing symbols in Other field
        donationForm.otherInputControl( document.getElementById( 'mc-other-amount-input' ) );

        $('.mc-no-button').on( 'click', function() {
            params.amount = initialAmount;
            params.recurring = false;
            callback( params );
        });

        $('.mc-yes-button').on( 'click', function() {
            donationForm.extraData.donateMC = 1;
            donationForm.extraData.originalAmt = initialAmount;
            params.amount = suggestedAmount;
            params.recurring = true;
            callback( params );
        });

        $('#mc-other-amount-input').on('input change', function() {
            if ( mc.validateOtherAmount() ) {
                $( '.mc-amt-custom' ).text( mc.formatAmount( mc.getOtherAmount(), currency, locale ) );
            } else {
                $( '.mc-amt-custom' ).text( '' );
            }
        });

        $( '.mc-donate-monthly-button' ).on( 'click', function() {
            if ( mc.validateOtherAmount() ) {
                donationForm.extraData.donateMC = 1;
                donationForm.extraData.originalAmt = initialAmount;
                params.amount = mc.getOtherAmount();
                params.recurring = true;
                callback( params );
            }
        });

        $('.mc-modal-screen').show();
        $('body').addClass('modal-open');
    }

};

$(function() {

    $( '.mc-diff-amount-link' ).on( 'click keypress', function ( e ) {
        if ( e.which === 13 || e.type === 'click' ) {
            $( '.mc-choice' ).hide();
            $( '.mc-edit-amount' ).show();
            $( '.mc-other-amount-input' ).focus();
        }
    } );

    $( '.mc-choice .mc-back' ).on( 'click keypress', function ( e ) {
        if ( e.which === 13 || e.type === 'click' ) {
            $( '.mc-edit-amount' ).hide();
            $('.mc-modal-screen').hide();
            $('body').removeClass('modal-open');
        }
    } );

    $( '.mc-edit-amount .mc-back' ).on( 'click keypress', function ( e ) {
        if ( e.which === 13 || e.type === 'click' ) {
            $( '.mc-edit-amount' ).hide();
            $( '.mc-choice' ).show();
        }
    } );

});