MDL-69166 core_payment: rename some functions

This commit is contained in:
Marina Glancy
2020-10-27 15:45:00 +11:00
committed by Shamim Rezaie
parent 087845bb30
commit 90fbc58de6
13 changed files with 74 additions and 34 deletions
@@ -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),
];
}
+21 -17
View 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;
}