MediaWiki : MonthlyConvert.js

From Donate
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)
  • Edge: Hold Ctrl while clicking Refresh , or 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
 =
 {

    'EUR'
 :
 [
 // also GBP

        [
 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
 ]

    ],

    '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
 ]

    ],

    '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'
 :
 [
 // Also NOK

        [
 29
,
 0
 ],

        [
 35
,
 20
 ],

        [
 55
,
 25
 ],

        [
 105
,
 30
 ],

        [
 160
,
 40
 ],

        [
 210
,
 50
 ],

        [
 360
,
 60
 ],

        [
 450
,
 75
 ],

        [
 600
,
 100
 ],

        [
 800
,
 125
 ],

        [
 900
,
 150
 ],

        [
 1200
,
 200
 ],

        [
 1500
,
 250
 ],

        [
 1800
,
 300
 ],

        [
 2100
,
 350
 ],

        [
 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'
 :
 [

        [
 19
,
 0
 ],

        [
 25
,
 10
 ],

        [
 120
,
 20
 ],

        [
 250
,
 40
 ],

        [
 350
,
 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
 ]

    ],

    'CLP'
 :
 [

        [
 1999
,
 0
 ],

        [
 2300
,
 1000
 ],

        [
 2700
,
 1100
 ],

        [
 3300
,
 1200
 ],

        [
 4200
,
 1300
 ],

        [
 5500
,
 1400
 ],

        [
 9000
,
 1500
 ],

        [
 10500
,
 1700
 ],

        [
 16000
,
 2600
 ],

        [
 20800
,
 3400
 ],

        [
 26000
,
 4200
 ],

        [
 31200
,
 5000
 ],

        [
 38400
,
 6400
 ],

        [
 55000
,
 8500
 ],

        [
 Infinity
,
 0
 ]

    ],

    'UYU'
 :
 [

        [
 99
,
 0
 ],

        [
 120
,
 50
 ],

        [
 170
,
 65
 ],

        [
 220
,
 70
 ],

        [
 320
,
 75
 ],

        [
 480
,
 85
 ],

        [
 520
,
 90
 ],

        [
 750
,
 125
 ],

        [
 1050
,
 170
 ],

        [
 1350
,
 225
 ],

        [
 1600
,
 250
 ],

        [
 1800
,
 300
 ],

        [
 2100
,
 320
 ],

        [
 Infinity
,
 0
 ]

    ],

    'PEN'
 :
 [

        [
 9
,
 0
 ],

        [
 12
,
 5
 ],

        [
 17
,
 6
 ],

        [
 26
,
 7
 ],

        [
 48
,
 8
 ],

        [
 55
,
 9
 ],

        [
 78
,
 13
 ],

        [
 105
,
 17
 ],

        [
 130
,
 21
 ],

        [
 160
,
 26
 ],

        [
 180
,
 30
 ],

        [
 210
,
 32
 ],

        [
 Infinity
,
 0
 ]

    ],

    'COP'
 :
 [

        [
 9999
,
 0
 ],

        [
 11300
,
 5000
 ],

        [
 17000
,
 5200
 ],

        [
 22000
,
 5500
 ],

        [
 27000
,
 5800
 ],

        [
 45000
,
 7500
 ],

        [
 55000
,
 9000
 ],

        [
 75000
,
 12500
 ],

        [
 105000
,
 17000
 ],

        [
 120000
,
 20000
 ],

        [
 160000
,
 25000
 ],

        [
 180000
,
 30000
 ],

        [
 250000
,
 34000
 ],

        [
 Infinity
,
 0
 ]

    ],

    'BRL'
 :
 [

        [
 9
,
 0
 ],

        [
 12
,
 6
 ],

        [
 22
,
 7
 ],

        [
 35
,
 8
 ],

        [
 45
,
 9
 ],

        [
 55
,
 10
 ],

        [
 80
,
 12
 ],

        [
 105
,
 16
 ],

        [
 160
,
 25
 ],

        [
 210
,
 35
 ],

        [
 270
,
 45
 ],

        [
 320
,
 50
 ],

        [
 Infinity
,
 0
 ]

    ],

    'MXN'
 :
 [

        [
 39
,
 0
 ],

        [
 48
,
 25
 ],

        [
 60
,
 28
 ],

        [
 110
,
 30
 ],

        [
 160
,
 35
 ],

        [
 260
,
 45
 ],

        [
 270
,
 50
 ],

        [
 350
,
 60
 ],

        [
 550
,
 85
 ],

        [
 650
,
 90
 ],

        [
 750
,
 120
 ],

        [
 1500
,
 160
 ],

        [
 Infinity
,
 0
 ]

    ]

    
};


mc
.
amounts
.
GBP
 =
 mc
.
amounts
.
EUR
;

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
.
SEK
;


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
(
 'wmf_medium'
 )
 ===
 'endowment'
 ||
 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
 (
 params
.
payment_method
 ===
 'paypal'
 &&
 donationForm
.
noRecurringPaypalCountries
.
indexOf
(
 params
.
country
 )
 !==
 -
1
 )
 {

        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
 ===
 '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
();

        document
.
querySelector
(
'.mc-button'
).
focus
();

        $
(
'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
();

        }

    }
 );


});