MediaWiki:MonthlyConvert.js: Difference between revisions

From Donate
Jump to navigation Jump to search
Content deleted Content added
allow MC for Apple Pay
add code to validate and update button when other amount input changes
Line 242: Line 242:
params.recurring = true;
params.recurring = true;
callback( params );
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( '' );
}
});
});



Revision as of 15:14, 4 February 2022

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
        [ 2.74, 0 ],
        [ 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 ]
    ],
    'GBP' : [ // also EUR
        [ 1.99, 0 ],
        [ 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' : [
        [ 300, 150 ],
        [ 500, 175 ],
        [ 1000, 250 ],
        [ 1500, 350 ],
        [ 3000, 500 ],
        [ 5000, 750 ],
        [ 10000, 1000 ],
        [ 25000, 2500 ],
        [ 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' : [
        [ 50, 25 ],
        [ 100, 30 ],
        [ 200, 50 ],
        [ 300, 70 ],
        [ 500, 90 ],
        [ 1000, 110 ],
        [ 2500, 250 ],
        [ 5000, 500 ],
        [ Infinity, 0 ]
    ]
};
mc.amounts.EUR = mc.amounts.GBP;
mc.amounts.CAD = mc.amounts.USD;
mc.amounts.AUD = mc.amounts.USD;
mc.amounts.NZD = mc.amounts.USD;

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_code,
        suggestedAmount = mc.getSuggestedAmount( initialAmount, currency ),
        locale = donationForm.getLocale( params.uselang, params.country ),
        skipMc;

    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 === 'rtbt' && params.payment_submethod === 'rtbt_ideal' ) ||
        ( params.payment_method === 'cc' && params.gateway === 'adyen' )
    ) {
        // 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' ).fadeOut( function () {
                $( '.mc-edit-amount' ).fadeIn();
                $( '.mc-other-amount-input' ).focus();
            } );
        }
    } );

    $( '.mc-choice .mc-back' ).on( 'click keypress', function ( e ) {
        if ( e.which === 13 || e.type === 'click' ) {
            $( '.mc-edit-amount' ).fadeOut( function () {
                $('.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' ).fadeOut( function () {
                $( '.mc-choice' ).fadeIn();
            } );
        }
    } );

});