MDL-69166 core_payment: rename some functions
authorMarina Glancy <marina@moodle.com>
Wed, 7 Oct 2020 08:53:01 +0000 (19:53 +1100)
committerShamim Rezaie <shamim@moodle.com>
Tue, 27 Oct 2020 04:34:55 +0000 (15:34 +1100)
13 files changed:
admin/settings/payment.php
lib/db/services.php
payment/amd/build/gateways_modal.min.js
payment/amd/build/gateways_modal.min.js.map
payment/amd/build/repository.min.js
payment/amd/build/repository.min.js.map
payment/amd/src/gateways_modal.js
payment/amd/src/repository.js
payment/classes/external/get_available_gateways.php [moved from payment/classes/external/get_gateways_for_currency.php with 92% similarity]
payment/classes/helper.php
payment/gateway/paypal/classes/external/get_config_for_js.php
payment/gateway/paypal/classes/external/transaction_complete.php
payment/tests/accounts_test.php

index d8d8dd5..ab4fe79 100644 (file)
@@ -1,6 +1,26 @@
 <?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
-// This file defines settingpages and externalpages under the "Payment" category
+/**
+ * Adds payments links to the admin tree
+ *
+ * @package    core
+ * @copyright  2020 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
 
 $ADMIN->add('payment', new admin_externalpage(
     'paymentaccounts',
index b8152e3..ef653f0 100644 (file)
@@ -2720,10 +2720,10 @@ $functions = array(
         'ajax'          => 'true',
         'capabilities'  => '',
     ],
-    'core_payment_get_gateways_for_currency' => [
-        'classname'   => 'core_payment\external\get_gateways_for_currency',
+    'core_payment_get_available_gateways' => [
+        'classname'   => 'core_payment\external\get_available_gateways',
         'methodname'  => 'execute',
-        'description' => 'Get the list of payment gateways that support the given currency',
+        'description' => 'Get the list of payment gateways that support the given component/area',
         'type'        => 'read',
         'ajax'        => true,
     ],
index ddfcd0e..8bd0a55 100644 (file)
Binary files a/payment/amd/build/gateways_modal.min.js and b/payment/amd/build/gateways_modal.min.js differ
index a902861..7c6c31a 100644 (file)
Binary files a/payment/amd/build/gateways_modal.min.js.map and b/payment/amd/build/gateways_modal.min.js.map differ
index 7e70376..7506386 100644 (file)
Binary files a/payment/amd/build/repository.min.js and b/payment/amd/build/repository.min.js differ
index b764ceb..e36de5b 100644 (file)
Binary files a/payment/amd/build/repository.min.js.map and b/payment/amd/build/repository.min.js.map differ
index 1dacc63..ad64845 100644 (file)
@@ -25,7 +25,7 @@
 import ModalFactory from 'core/modal_factory';
 import Templates from 'core/templates';
 import {get_string as getString} from 'core/str';
-import {getGatewaysSupportingCurrency} from './repository';
+import {getAvailableGateways} from './repository';
 import Selectors from './selectors';
 import ModalEvents from 'core/modal_events';
 import PaymentEvents from 'core_payment/events';
@@ -127,7 +127,7 @@ const show = async(rootNode, {
         }
     });
 
-    const gateways = await getGatewaysSupportingCurrency(rootNode.dataset.component, rootNode.dataset.paymentarea,
+    const gateways = await getAvailableGateways(rootNode.dataset.component, rootNode.dataset.paymentarea,
         rootNode.dataset.componentid);
     const context = {
         gateways
index d81c79a..7b4ccbb 100644 (file)
@@ -32,9 +32,9 @@ import Ajax from 'core/ajax';
  * @param {number} componentId
  * @returns {Promise<{shortname: string, name: string, description: String}[]>}
  */
-export const getGatewaysSupportingCurrency = (component, paymentArea, componentId) => {
+export const getAvailableGateways = (component, paymentArea, componentId) => {
     const request = {
-        methodname: 'core_payment_get_gateways_for_currency',
+        methodname: 'core_payment_get_available_gateways',
         args: {
             component,
             paymentarea: paymentArea,
@@ -35,7 +35,7 @@ defined('MOODLE_INTERNAL') || die();
 
 require_once($CFG->libdir . '/externallib.php');
 
-class get_gateways_for_currency extends external_api {
+class get_available_gateways extends external_api {
 
     /**
      * Returns description of method parameters.
@@ -67,7 +67,7 @@ class get_gateways_for_currency extends external_api {
         ]);
 
         $list = [];
-        $gateways = helper::get_gateways_for_currency($params['component'], $params['paymentarea'], $params['componentid']);
+        $gateways = helper::get_available_gateways($params['component'], $params['paymentarea'], $params['componentid']);
         [
             'amount' => $amount,
             'currency' => $currency
@@ -80,7 +80,7 @@ class get_gateways_for_currency extends external_api {
                 'name' => get_string('gatewayname', 'pg_' . $gateway),
                 'description' => get_string('gatewaydescription', 'pg_' . $gateway),
                 'surcharge' => $surcharge,
-                'cost' => helper::get_cost_as_string(helper::get_cost_with_surcharge($amount, $surcharge, $currency), $currency),
+                'cost' => helper::get_cost_as_string($amount, $currency, $surcharge),
             ];
         }
 
index fe12f80..73de0cc 100644 (file)
@@ -67,7 +67,7 @@ class helper {
      * @param int $componentid
      * @return string[]
      */
-    public static function get_gateways_for_currency(string $component, string $paymentarea, int $componentid): array {
+    public static function get_available_gateways(string $component, string $paymentarea, int $componentid): array {
         $gateways = [];
 
         [
@@ -97,33 +97,37 @@ class helper {
     }
 
     /**
-     * Calculates the cost with the surcharge
+     * Rounds the cost based on the currency fractional digits, can also apply surcharge
      *
      * @param float $amount amount in the currency units
-     * @param float $surcharge surcharge in percents
      * @param string $currency currency, used for calculating the number of fractional digits
+     * @param float $surcharge surcharge in percents
      * @return float
      */
-    public static function get_cost_with_surcharge(float $amount, float $surcharge, string $currency): float {
-        return round($amount + $amount * $surcharge / 100, 2); // TODO number of digits depends on currency.
+    public static function get_rounded_cost(float $amount, string $currency, float $surcharge = 0): float {
+        $amount = $amount * (100 + $surcharge) / 100;
+
+        $locale = get_string('localecldr', 'langconfig');
+        $fmt = \NumberFormatter::create($locale, \NumberFormatter::CURRENCY);
+        $localisedcost = numfmt_format_currency($fmt, $amount, $currency);
+
+        return numfmt_parse_currency($fmt, $localisedcost, $currency);
     }
 
     /**
-     * Returns human-readable amount with fixed number of fractional digits and currency indicator
+     * Returns human-readable amount with correct number of fractional digits and currency indicator, can also apply surcharge
      *
-     * @param float $amount
-     * @param string $currency
+     * @param float $amount amount in the currency units
+     * @param string $currency The currency
+     * @param float $surcharge surcharge in percents
      * @return string
-     * @throws \coding_exception
      */
-    public static function get_cost_as_string(float $amount, string $currency): string {
-        if (class_exists('NumberFormatter') && function_exists('numfmt_format_currency')) {
-            $locale = get_string('localecldr', 'langconfig');
-            $fmt = \NumberFormatter::create($locale, \NumberFormatter::CURRENCY);
-            $localisedcost = numfmt_format_currency($fmt, $amount, $currency);
-        } else {
-            $localisedcost = sprintf("%.2f %s", $amount, $currency); // TODO number of digits depends on currency.
-        }
+    public static function get_cost_as_string(float $amount, string $currency, float $surcharge = 0): string {
+        $amount = $amount * (100 + $surcharge) / 100;
+
+        $locale = get_string('localecldr', 'langconfig');
+        $fmt = \NumberFormatter::create($locale, \NumberFormatter::CURRENCY);
+        $localisedcost = numfmt_format_currency($fmt, $amount, $currency);
 
         return $localisedcost;
     }
index a467f92..bf6ccc6 100644 (file)
@@ -73,7 +73,7 @@ class get_config_for_js extends external_api {
         return [
             'clientid' => $config['clientid'],
             'brandname' => $config['brandname'],
-            'cost' => helper::get_cost_with_surcharge($cost['amount'], $surcharge, $cost['currency']),
+            'cost' => helper::get_rounded_cost($cost['amount'], $cost['currency'], $surcharge),
             'currency' => $cost['currency'],
         ];
     }
index 9186b6f..2a89528 100644 (file)
@@ -84,7 +84,7 @@ class transaction_complete extends external_api {
 
         // Add surcharge if there is any.
         $surcharge = helper::get_gateway_surcharge('paypal');
-        $amount = helper::get_cost_with_surcharge($amount, $surcharge, $currency);
+        $amount = helper::get_rounded_cost($amount, $currency, $surcharge);
 
         $paypalhelper = new paypal_helper($config->clientid, $config->secret, $sandbox);
         $orderdetails = $paypalhelper->get_order_details($orderid);
index d273441..d023798 100644 (file)
@@ -127,4 +127,20 @@ class accounts_testcase extends advanced_testcase {
         helper::restore_payment_account(account::get_record(['id' => $account->get('id')]));
         $this->assertEquals(0, $DB->get_field('payment_accounts', 'archived', ['id' => $account->get('id')]));
     }
+
+    public function test_format_cost() {
+        $amount = 5.345;
+
+        $currency = 'IRR';
+        $this->assertEquals(5, helper::get_rounded_cost($amount, $currency));
+        $this->assertEquals('IRR'."\xc2\xa0".'5', helper::get_cost_as_string($amount, $currency));
+        $this->assertEquals(6, helper::get_rounded_cost($amount, $currency, 12));
+        $this->assertEquals('IRR'."\xc2\xa0".'6', helper::get_cost_as_string($amount, $currency, 12));
+
+        $currency = 'USD';
+        $this->assertEquals(5.34, helper::get_rounded_cost($amount, $currency));
+        $this->assertEquals('USD'."\xc2\xa0".'5.34', helper::get_cost_as_string($amount, $currency));
+        $this->assertEquals(5.40, helper::get_rounded_cost($amount, $currency, 1));
+        $this->assertEquals('USD'."\xc2\xa0".'5.40', helper::get_cost_as_string($amount, $currency, 1));
+    }
 }