MediaWiki:DonationForm T201415.js: Difference between revisions

Content deleted Content added
update minimums
rm amountGiven usage
 
(10 intermediate revisions by the same user not shown)
Line 1:
/* jshint strict:false */
/** MediaWiki:DonationForm.js - loaded on all donation forms
* TODO: lots of cleanup
Line 213 ⟶ 214:
/* Localize the amount errors. Call when initialising form. */
donationForm.localizeErrors = function() {
var formcurrency = documentdonationForm.forms['donateForm']currency;
var currency = form.currency_code.value;
var minAmount = donationForm.minimums[ currency ];
 
Line 253:
var hpcSet = mw.util.getParamValue('hpcSet');
 
var currency = $("input[name='currency_code']")donationForm.val()currency;
var language = mw.config.get('wgUserLanguage');
 
Line 362:
[ 175, [ 175, 200, 300, 400, 500, 750, 1000 ] ],
[ 200, [ 200, 225, 300, 400, 500, 750, 1000 ] ],
],
'2018control' : [
[ 0, [ 5, 10, 20, 25, 35, 50, 100 ] ],
Line 527:
};
 
var format = formats[currency][language] || formats[currency]["'default"'] || formats[currency] || '\t';
 
// Radio button amounts
Line 658:
/* Form functions */
function clearOther(box) {
document.getElementById("'input_amount_other"').checked = true;
box.value = "";
}
 
function selectOther() {
document.getElementById("'input_amount_other"').checked = true;
}
 
function selectAmount() {
$('#input_amount_other_box').val('');
$('input[name="amountGiven"]').val('');
}
 
Line 674 ⟶ 673:
/**
* Validate form, and if it looks good submit to payments
*
* @param {string} paymentMethod - method e.g. 'cc', 'paypal'
* @param {string} paymentSubMethod - submethod e.g. 'rtbt_ideal' (a submethod of 'rtbt')
Line 680 ⟶ 679:
*/
donationForm.redirectPayment = function( paymentMethod, paymentSubMethod, skipAmountValidation ) {
 
var form = document.forms['donateForm'];
 
if ( donationForm.validate( skipAmountValidation ) ) {
 
var uri = new mw.Uri('https://payments.wikimedia.org/index.php/Special:GatewayFormChooser');
if (typeof paymentSubMethod == 'undefined') {
var paymentSubMethodparams = ''{};
}
var paymentsURL = 'https://payments.wikimedia.org/index.php/Special:GatewayFormChooser';
 
form.action = paymentsURL;
 
// Overrides for specific cc gateways
if (typeof paymentSubMethod == 'undefined') {
if ( paymentSubMethodpaymentMethod === 'cc-adyen'; ) {
params.payment_method = 'cc';
}
params.gateway = 'adyen';
params.ffname = 'adyen';
// Temporary fix for https://phabricator.wikimedia.org/T254274#6188493
} else if ( paymentMethod === 'cc-dlocal' ) {
formparams.gateway.valuepayment_method = 'ingenicocc';
params.gateway = 'astropay';
} else {
params.payment_method = paymentMethod;
}
 
//if Adyen( overridepaymentSubMethod for) cc{
params.payment_submethod = paymentSubMethod;
if ( paymentMethod === 'cc-adyen' ) {
paymentMethod = 'cc';
form.payment_method.value = 'cc';
form.gateway.value = 'adyen';
form.ffname.value = 'adyen';
}
// dLocal override for cc
if ( paymentMethod === 'cc-dlocal' ) {
paymentMethod = 'cc';
form.payment_method.value = 'cc';
form.gateway.value = 'astropay';
}
 
var frequency = $("'input[name='"frequency'"]:checked"').val();
if ( frequency !== 'monthly' ) {
frequencyparams.recurring = 'onetime'false;
form.recurring.value = 'false';
} else {
formparams.recurring.value = 'true';
}
 
formparams.payment_method.valuecurrency_code = paymentMethoddonationForm.currency;
formparams.payment_submethod.valuecountry = paymentSubMethoddonationForm.country;
params.uselang = mw.config.get('wgUserLanguage');
 
var full_dotted_payment_methodamount = paymentMethoddonationForm.getAmount();
if ( form.recurring$('#ptf-checkbox').value == prop('truechecked') ) {
full_dotted_payment_methodamount = 'r'amount + full_dotted_payment_methoddonationForm.calculateFee( amount );
donationForm.extraData.ptf = 1;
}
ifparams.amount (= paymentSubMethod ) {amount;
 
full_dotted_payment_method = form.payment_method.value + '.' + paymentSubMethod;
// Email optin
if ( $('input[name="opt_in"]').length > 0 ) {
var opt_inValue = $('input[name="opt_in"]:checked').val();
params.opt_in = opt_inValue; // donationForm.validate() already checked it's 1 or 0
}
 
// Default to variant = panExplain for India
// PTF - TODO: clean up form so we only use 'amount' and not 'amountGiven'
// TODO: make this default payments side - https://phabricator.wikimedia.org/T259146
if ( $('#ptf-checkbox').prop('checked') ) {
if ( var amount1donationForm.country === donationForm.getAmount('IN' ); {
documentparams.getElementById('input_amount_other').checkedvariant = true'panExplain';
}
form.amountGiven.value = amount1 + donationForm.calculateFee( amount1 );
 
donationForm.extraData.ptf = 1;
if ( mw.util.getParamValue( 'pym_variant' ) ) {
params.variant = mw.util.getParamValue( 'pym_variant' );
}
if ( mw.util.getParamValue( 'pym_appeal' ) ) {
params.appeal = mw.util.getParamValue( 'pym_appeal' );
}
 
donationForm.extraData.time = Math.round( (Date.now() - donationForm.loadedTime)/1000 );
 
form// Tracking data
params.utm_medium.value = mw.util.getParamValue( 'utm_medium' );
formparams.utm_campaign.value = mw.util.getParamValue( 'utm_campaign' );
formparams.utm_source.value = donationForm.buildUtmSource() +params '.' + full_dotted_payment_method);
formparams.utm_key.value = donationForm.buildUtmKey( donationForm.extraData );
if ( document.referrer ) { // TODO: do we need this?
// Strip protocol to stop firewall complaining
params.referrer = document.referrer.replace(/https?:\/\//i, '');
}
 
uri.extend( params );
donationForm.goToPayments( uri );
 
form.method = 'GET';
form.submit();
} else {
donationForm.extraData.validateError = 1; // Flag they had an error, even if fixed later
Line 758:
 
return false; // don't submit if called by a button
};
 
 
donationForm.goToPayments = function( uri ) {
if ( window.top !== window.self ) {
// In a frame, open payments in a new tab
window.open( uri.toString() );
} else {
window.location.href = uri.toString();
}
};
 
Line 763 ⟶ 773:
* Build a utm_source value, including the landing page info.
*
* Own function so it can be overriden for weird tests
* Note this doesn't include payment method, that's added in redirectPayment method
*
* @param {Object} params
* @return {string} utm_source
*/
donationForm.buildUtmSource = function( params ) {
 
var utm_source = mw.util.getParamValue( 'utm_source' ) + '.';
 
var fullDottedPaymentMethod = params.payment_method;
if ( params.recurring ) {
fullDottedPaymentMethod = 'r' + fullDottedPaymentMethod;
}
if ( params.payment_submethod ) {
fullDottedPaymentMethod = fullDottedPaymentMethod + '.' + params.payment_submethod;
}
 
/* Get URL parameter, but remove parts using old format. Allow fallback to a default value */
Line 786 ⟶ 805:
utm_source += getParam( 'form-template' , 'Form-template-' , 'default' ) + '~';
utm_source += getParam( 'form-countryspecific', 'Form-countryspecific-', 'control' );
 
utm_source += '.' + fullDottedPaymentMethod;
 
return utm_source;
Line 800 ⟶ 821:
var existingUtmKey = mw.util.getParamValue('utm_key'),
dataArray = [];
 
if ( existingUtmKey ) {
dataArray.push( existingUtmKey );
Line 834 ⟶ 855:
otherAmount = otherAmount.replace(/[\$£€¥,.]/g, '');
otherAmount = otherAmount.replace(/:/, '.');
form.amountGiven.value = otherAmount; // TODO: change this
amount = otherAmount;
donationForm.extraData.otherAmt = 1;
Line 867 ⟶ 887:
 
if ( form.opt_in ) {
if ( $("'input[name='"opt_in'"]:checked"').val() === undefined ) {
$('#error-optin').show();
error = true;
Line 884 ⟶ 904:
donationForm.validateAmount = function() {
 
var form = document.forms['donateForm'];
var amount = donationForm.getAmount();
var minAmount = donationForm.minimums[ donationForm.currency ] || 1;
var currency = form.currency_code.value;
var minAmount = donationForm.minimums[ currency ] || 1;
 
if ( amount === null || isNaN(amount) || amount <= 0 || amount < minAmount ) {
Line 931 ⟶ 949:
*/
donationForm.calculateFee = function( amount ) {
 
var form = document.forms['donateForm'];
 
// Minimum fee/PTF amounts. Default is 0.35.
Line 960 ⟶ 976:
 
var feeMultiplier = 0.04,
feeMinimum = feeMinimums[ formdonationForm.currency_code.valuecurrency ] || 0.35,
feeAmount = amount * feeMultiplier;
 
if ( feeAmount < feeMinimum ) {
feeAmount = feeMinimum;
}
return parseFloat( feeAmount.toFixed(2) );
Line 972 ⟶ 988:
donationForm.initOptin = function() {
$('.optin-options').on('change', function(e) {
 
$('#error-optin').hide();
 
Line 996 ⟶ 1,012:
 
var form = document.forms['donateForm'];
 
// These get used in quite a few places
donationForm.currency = form.currency_code.value;
donationForm.country = mw.util.getParamValue('country');
 
// Block typing symbols in input field, otherwise Safari allows them and then chokes
Line 1,009 ⟶ 1,029:
}
var chr = String.fromCharCode(e.which);
if ("'0123456789., "'.indexOf(chr) === -1) {
return false;
}
Line 1,037 ⟶ 1,057:
 
if ( form ) {
form.utm_medium.value = mw.util.getParamValue( 'utm_medium' );
form.utm_campaign.value = mw.util.getParamValue( 'utm_campaign' );
form.utm_key.value = mw.util.getParamValue( 'utm_key' );
 
// Strip protocol to stop firewall throwing fits
form.referrer.value = document.referrer.replace(/https?:\/\//i, "");
 
// hide frequency options for some countries
if ( donationForm.noRecurringCountries.indexOf( formdonationForm.country.value ) !== -1 ) {
$('#frequency_onetime').prop('checked', true);
$('.frequency-options, #cancel-monthly, #donate-recurring-smallprint').hide();
}
 
addCardTypesClass( formdonationForm.country.value );
}
 
Line 1,057 ⟶ 1,071:
 
// Disable logo link
$("'#p-logo a"').attr(" { href",: "'#"', title: '' } );
$("#p-logo a").attr("title", "");
 
// These don't need to be tabbable on the landing page
$('#searchInput, .mw-jump-link').attr('tabindex', '-1');
 
$("'.input_amount_other"').click(function() {
$("'#input_amount_other_box"').focus();
});
 
// Allow preselecting monthly
if( mw.util.getParamValue('monthly')
&& donationForm.noRecurringCountries.indexOf( formdonationForm.country.value ) === -1 ) {
$('#frequency_monthly').click();
}
 
// Default to variant = panExplain for India
// https://phabricator.wikimedia.org/T259146
if ( form.country.value === 'IN' ) {
form.variant.value = 'panExplain';
}