MediaWiki:FR2013/Resources/MobileBanner.js

From Meta, a Wikimedia project coordination wiki

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.
{{MediaWiki:FR2013/Resources/CurrencyMinimums.js}}
<script>
/**
 * Javascript for mobile banners.
 * This version assumes checkboxes for amount, buttons for payment method
 *
 * Dependencies: MediaWiki:FR2013/Resources/Country2Currency.js
 */

/* Allow overriding country */
mw.loader.using(['mediawiki.util']).then(function() {
    if( mw.util.getParamValue("country") ) {
        Geo.country = mw.util.getParamValue("country");
    }
});

var fundraisingBanner = fundraisingBanner || {};

fundraisingBanner.validateForm = function(form) {
    /* Check the form, especially the amount submitted
     * Return the amount if valid, otherwise return false
     */

    var error = true;
    var amount = null;

    // If there are some amount radio buttons, then look for the checked one
    if (form.amount) {
        for (var i = 0; i < form.amount.length; i++) {
            if (form.amount[i].checked) {
                amount = form.amount[i].value;
            }
        }
    }

    // Check the "other" amount box
    if (form.amountGiven.value !== '') {
        var otherAmount = form.amountGiven.value;
        otherAmount = otherAmount.replace(/[,.](\d)$/, ':$10');
        otherAmount = otherAmount.replace(/[,.](\d)(\d)$/, ':$1$2');
        otherAmount = otherAmount.replace(/[$£€¥,.]/g, '');
        otherAmount = otherAmount.replace(/:/, '.');
        form.amountGiven.value = otherAmount;
        amount = otherAmount;
    }

    // Check amount is a real number
    error = ( amount == null || isNaN(amount) || amount.value <= 0 );

    // Check amount is at least the minimum
    var currency = form.currency_code.value;
    if (amount < getMinimum(currency) || error) {
        alert('{{{validation-error-minimum|{{int:fr2013-dropdown-smallamount-error}}}}}'.replace('$1', getMinimum(currency) + ' ' + currency));
        error = true;
    }

    if ( error ) {
        return false;
    } else {
        return amount;
    }

};

fundraisingBanner.redirectPayment = function(paymentMethod, paymentSubMethod, skipValidation) {

    if ( skipValidation || fundraisingBanner.validateForm(document.paypalcontribution) ) {

        if (typeof paymentSubMethod == 'undefined') {
            paymentSubMethod = '';
        }
        var form = document.paypalcontribution; // we should really change this some day
        var paymentsURL = 'https://payments.wikimedia.org/index.php/Special:GatewayFormChooser';

        form.action = paymentsURL;

        if ( form.language.value === 'pt' && Geo.country === 'BR' ) {
            form.language.value = 'pt-br';
        }


        // WorldPay override for cc
        if( paymentMethod === 'cc-wp' ) {
            paymentMethod = 'cc';
            form.payment_method.value = 'cc';
            form.gateway.value = 'worldpay';
            form.ffname.value = 'worldpay';
        }
        
        // Adyen override for cc
        if( paymentMethod === 'cc-adyen' ) {
            paymentMethod = 'cc';
            form.payment_method.value = 'cc';
            form.gateway.value = 'adyen';
            form.ffname.value = 'adyen';
        }
        
        // Express Checkout override for PayPal
        if( paymentMethod === 'paypal-ec' ) {
            paymentMethod = 'paypal';
            form.payment_method.value = 'paypal';
            form.gateway.value = 'paypal_ec';
        }

        var frequency = $("input[name='frequency']:checked").val();
        if( frequency !== 'monthly' ){
            frequency = 'onetime';
            form.recurring.value = 'false';
        } else {
            form.recurring.value = 'true';
        }

        var mixins = mw.centralNotice.getDataProperty( 'mixins' );

        if ( mixins && mixins.bannerHistoryLogger ) {
            form.bannerhistlog.value = mw.centralNotice.bannerHistoryLogger.id;
        }

        form.utm_key.value = mw.centralNotice.bannerData.cookieCount || 0;

        form.payment_method.value = paymentMethod;
        form.payment_submethod.value = paymentSubMethod;
        
        var full_dotted_payment_method = paymentMethod;
        if ( form.recurring.value == 'true' ) {
            full_dotted_payment_method = 'r' + full_dotted_payment_method;
        }
        if ( paymentSubMethod ) {
            full_dotted_payment_method = form.payment_method.value + '.' + paymentSubMethod;
        }

        form.utm_source.value += '.no-LP.' + full_dotted_payment_method;

        $('.frbanner-submit button, .frbanner-continue button, .frbanner3-continue button').attr('disabled', 'disabled'); // Disable to prevent double submission

        if ( mixins && mixins.bannerHistoryLogger ) {
            mw.centralNotice.bannerHistoryLogger.ensureLogSent().always(function() {
                form.submit();
            });
        } else {
            form.submit();
        }
        
    }

};

$(document).ready( function () {

    var currency = getCurrency();
    var language = mw.config.get('wgUserLanguage');

    // set the form fields
    $("input[name='country']").val(Geo.country);
    $("input[name='currency_code']").val(currency);
    $("input[name='language']").val(mw.config.get('wgUserLanguage'));
    $("input[name='return_to']").val("Thank_You/" + mw.config.get('wgUserLanguage'));

    // Add utm_key with banner counter
    mw.centralNotice.events.bannerLoaded.done(function(e){
        $('[name="paypalcontribution"]').append($('<input>', {
            'type': 'hidden', 'name': 'utm_key', 'value': (mw.centralNotice.bannerData.cookieCount || 0)
        }));
    });

});
</script>