From 7fa470de03beffe43804d041dfbd2f727c4f9946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sat, 25 May 2013 22:47:28 +0200 Subject: [PATCH] MDL-38408 always validate PayPal dates and cost, improve float formatting --- enrol/paypal/edit.php | 1 + enrol/paypal/edit_form.php | 16 +++++++--------- enrol/paypal/enrol.html | 2 +- enrol/paypal/ipn.php | 4 +++- enrol/paypal/lib.php | 7 ++++++- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/enrol/paypal/edit.php b/enrol/paypal/edit.php index 8955cb7d812..6175b3bc440 100644 --- a/enrol/paypal/edit.php +++ b/enrol/paypal/edit.php @@ -47,6 +47,7 @@ $plugin = enrol_get_plugin('paypal'); if ($instanceid) { $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'paypal', 'id'=>$instanceid), '*', MUST_EXIST); + $instance->cost = format_float($instance->cost, 2, true); } else { require_capability('moodle/course:enrolconfig', $context); // no instance yet, we have to add new instance diff --git a/enrol/paypal/edit_form.php b/enrol/paypal/edit_form.php index 9943a4c38e8..857cc2ceecc 100644 --- a/enrol/paypal/edit_form.php +++ b/enrol/paypal/edit_form.php @@ -46,7 +46,7 @@ class enrol_paypal_edit_form extends moodleform { $mform->addElement('text', 'cost', get_string('cost', 'enrol_paypal'), array('size'=>4)); $mform->setType('cost', PARAM_RAW); // Use unformat_float to get real value. - $mform->setDefault('cost', $plugin->get_config('cost')); + $mform->setDefault('cost', format_float($plugin->get_config('cost'), 2, true)); $paypalcurrencies = $plugin->get_currencies(); $mform->addElement('select', 'currency', get_string('currency', 'enrol_paypal'), $paypalcurrencies); @@ -90,15 +90,13 @@ class enrol_paypal_edit_form extends moodleform { list($instance, $plugin, $context) = $this->_customdata; - if ($data['status'] == ENROL_INSTANCE_ENABLED) { - if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) { - $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_paypal'); - } + if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) { + $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_paypal'); + } - if (!is_numeric($data['cost'])) { - $errors['cost'] = get_string('costerror', 'enrol_paypal'); - - } + $cost = str_replace(get_string('decsep', 'langconfig'), '.', $data['cost']); + if (!is_numeric($cost)) { + $errors['cost'] = get_string('costerror', 'enrol_paypal'); } return $errors; diff --git a/enrol/paypal/enrol.html b/enrol/paypal/enrol.html index 875e3ac8cea..c9d5865f404 100644 --- a/enrol/paypal/enrol.html +++ b/enrol/paypal/enrol.html @@ -2,7 +2,7 @@

-

currency} {$cost}"; ?>

+

currency} {$localisedcost}"; ?>

<?php print_string('paypalaccepted', 'enrol_paypal') ?>

0) { $cost = (float) $plugin_instance->cost; } + // Use the same rounding of floats as on the enrol form. + $cost = format_float($cost, 2, false); + if ($data->payment_gross < $cost) { - $cost = format_float($cost, 2); message_paypal_error_to_admin("Amount paid is not enough ($data->payment_gross < $cost))", $data); die; diff --git a/enrol/paypal/lib.php b/enrol/paypal/lib.php index f423ccccecc..fda630213fd 100644 --- a/enrol/paypal/lib.php +++ b/enrol/paypal/lib.php @@ -190,6 +190,11 @@ class enrol_paypal_plugin extends enrol_plugin { echo '

'.get_string('nocost', 'enrol_paypal').'

'; } else { + // Calculate localised and "." cost, make sure we send PayPal the same value, + // please note PayPal expects amount with 2 decimal places and "." separator. + $localisedcost = format_float($cost, 2, true); + $cost = format_float($cost, 2, false); + if (isguestuser()) { // force login only for guest user, not real users with guest role if (empty($CFG->loginhttps)) { $wwwroot = $CFG->wwwroot; @@ -199,7 +204,7 @@ class enrol_paypal_plugin extends enrol_plugin { $wwwroot = str_replace("http://", "https://", $CFG->wwwroot); } echo '

'.get_string('paymentrequired').'

'; - echo '

'.get_string('cost').": $instance->currency $cost".'

'; + echo '

'.get_string('cost').": $instance->currency $localisedcost".'

'; echo '

'.get_string('loginsite').'

'; echo '
'; } else {