MDL-69166 core_payment: rename some functions
This commit is contained in:
committed by
Shamim Rezaie
parent
087845bb30
commit
90fbc58de6
@@ -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',
|
||||
|
||||
+3
-3
@@ -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,
|
||||
],
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
define ("core_payment/gateways_modal",["exports","core/modal_factory","core/templates","core/str","./repository","./selectors","core/modal_events","core_payment/events","core/toast","core/notification","./modal_gateways"],function(a,b,c,d,e,f,g,h,i,j,k){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.registerEventListeners=a.registerEventListenersBySelector=void 0;b=l(b);c=l(c);f=l(f);g=l(g);h=l(h);j=l(j);k=l(k);var o="undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};function l(a){return a&&a.__esModule?a:{default:a}}function m(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){c(a);return}if(h.done){b(i)}else{Promise.resolve(i).then(d,e)}}function n(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){var h=a.apply(b,c);function f(a){m(h,d,e,f,g,"next",a)}function g(a){m(h,d,e,f,g,"throw",a)}f(void 0)})}}a.registerEventListenersBySelector=function registerEventListenersBySelector(a){document.querySelectorAll(a).forEach(function(a){p(a)})};var p=function(a){a.addEventListener("click",function(b){b.preventDefault();q(a,{focusOnClose:b.target})})};a.registerEventListeners=p;var q=function(){var a=n(regeneratorRuntime.mark(function a(l){var m,n,o,p,q,u,v,w,x,y,z=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:m=1<z.length&&z[1]!==void 0?z[1]:{},n=m.focusOnClose,o=void 0===n?null:n;a.t0=b.default;a.t1=k.default.TYPE;a.next=5;return(0,d.get_string)("selectpaymenttype","core_payment");case 5:a.t2=a.sent;a.next=8;return c.default.render("core_payment/gateways_modal",{});case 8:a.t3=a.sent;a.t4={type:a.t1,title:a.t2,body:a.t3};a.next=12;return a.t0.create.call(a.t0,a.t4);case 12:p=a.sent;q=p.getRoot()[0];(0,i.addToastRegion)(q);p.show();p.getRoot().on(g.default.hidden,function(){p.destroy();try{o.focus()}catch(a){}});p.getRoot().on(h.default.proceed,function(a){var b=(q.querySelector(f.default.values.gateway)||{value:""}).value;if(b){t(b,l.dataset.component,l.dataset.paymentarea,l.dataset.componentid,l.dataset.description,function(a){var b=a.success,c=a.message,d=void 0===c?"":c;p.hide();if(b){j.default.addNotification({message:d,type:"success"});location.reload()}else{j.default.alert("",d)}})}else{(0,d.get_string)("nogatewayselected","core_payment").then(function(a){return(0,i.add)(a)})}a.preventDefault()});q.addEventListener("change",function(a){if(a.target.matches(f.default.elements.gateways)){s(q,l.dataset.cost)}});a.next=21;return(0,e.getGatewaysSupportingCurrency)(l.dataset.component,l.dataset.paymentarea,l.dataset.componentid);case 21:u=a.sent;v={gateways:u};a.next=25;return c.default.renderForPromise("core_payment/gateways",v);case 25:w=a.sent;x=w.html;y=w.js;c.default.replaceNodeContents(q.querySelector(f.default.regions.gatewaysContainer),x,y);r(q);a.next=32;return s(q,l.dataset.cost);case 32:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),r=function(a){var b=a.querySelectorAll(f.default.elements.gateways);if(1==b.length){b[0].checked=!0}},s=function(){var a=n(regeneratorRuntime.mark(function a(b){var d,e,g,h,i,j,k,l=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:d=1<l.length&&l[1]!==void 0?l[1]:"";e=b.querySelector(f.default.values.gateway);g=parseInt((e||{dataset:{surcharge:0}}).dataset.surcharge);h=(e||{dataset:{cost:d}}).dataset.cost;a.next=6;return c.default.renderForPromise("core_payment/fee_breakdown",{fee:h,surcharge:g});case 6:i=a.sent;j=i.html;k=i.js;c.default.replaceNodeContents(b.querySelector(f.default.regions.costContainer),j,k);case 10:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),t=function(){var a=n(regeneratorRuntime.mark(function a(b,c,d,e,f,g){var h;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:a.next=2;return"function"==typeof o.define&&o.define.amd?new Promise(function(a,c){o.require(["pg_".concat(b,"/gateways_modal")],a,c)}):"undefined"!=typeof module&&module.exports&&"undefined"!=typeof require||"undefined"!=typeof module&&module.component&&o.require&&"component"===o.require.loader?Promise.resolve(require(("pg_".concat(b,"/gateways_modal")))):Promise.resolve(o["pg_".concat(b,"/gateways_modal")]);case 2:h=a.sent;h.process(c,d,e,f,g);case 4:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}()});
|
||||
define ("core_payment/gateways_modal",["exports","core/modal_factory","core/templates","core/str","./repository","./selectors","core/modal_events","core_payment/events","core/toast","core/notification","./modal_gateways"],function(a,b,c,d,e,f,g,h,i,j,k){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.registerEventListeners=a.registerEventListenersBySelector=void 0;b=l(b);c=l(c);f=l(f);g=l(g);h=l(h);j=l(j);k=l(k);var o="undefined"!=typeof window?window:"undefined"!=typeof self?self:"undefined"!=typeof global?global:{};function l(a){return a&&a.__esModule?a:{default:a}}function m(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){c(a);return}if(h.done){b(i)}else{Promise.resolve(i).then(d,e)}}function n(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){var h=a.apply(b,c);function f(a){m(h,d,e,f,g,"next",a)}function g(a){m(h,d,e,f,g,"throw",a)}f(void 0)})}}a.registerEventListenersBySelector=function registerEventListenersBySelector(a){document.querySelectorAll(a).forEach(function(a){p(a)})};var p=function(a){a.addEventListener("click",function(b){b.preventDefault();q(a,{focusOnClose:b.target})})};a.registerEventListeners=p;var q=function(){var a=n(regeneratorRuntime.mark(function a(l){var m,n,o,p,q,u,v,w,x,y,z=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:m=1<z.length&&z[1]!==void 0?z[1]:{},n=m.focusOnClose,o=void 0===n?null:n;a.t0=b.default;a.t1=k.default.TYPE;a.next=5;return(0,d.get_string)("selectpaymenttype","core_payment");case 5:a.t2=a.sent;a.next=8;return c.default.render("core_payment/gateways_modal",{});case 8:a.t3=a.sent;a.t4={type:a.t1,title:a.t2,body:a.t3};a.next=12;return a.t0.create.call(a.t0,a.t4);case 12:p=a.sent;q=p.getRoot()[0];(0,i.addToastRegion)(q);p.show();p.getRoot().on(g.default.hidden,function(){p.destroy();try{o.focus()}catch(a){}});p.getRoot().on(h.default.proceed,function(a){var b=(q.querySelector(f.default.values.gateway)||{value:""}).value;if(b){t(b,l.dataset.component,l.dataset.paymentarea,l.dataset.componentid,l.dataset.description,function(a){var b=a.success,c=a.message,d=void 0===c?"":c;p.hide();if(b){j.default.addNotification({message:d,type:"success"});location.reload()}else{j.default.alert("",d)}})}else{(0,d.get_string)("nogatewayselected","core_payment").then(function(a){return(0,i.add)(a)})}a.preventDefault()});q.addEventListener("change",function(a){if(a.target.matches(f.default.elements.gateways)){s(q,l.dataset.cost)}});a.next=21;return(0,e.getAvailableGateways)(l.dataset.component,l.dataset.paymentarea,l.dataset.componentid);case 21:u=a.sent;v={gateways:u};a.next=25;return c.default.renderForPromise("core_payment/gateways",v);case 25:w=a.sent;x=w.html;y=w.js;c.default.replaceNodeContents(q.querySelector(f.default.regions.gatewaysContainer),x,y);r(q);a.next=32;return s(q,l.dataset.cost);case 32:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),r=function(a){var b=a.querySelectorAll(f.default.elements.gateways);if(1==b.length){b[0].checked=!0}},s=function(){var a=n(regeneratorRuntime.mark(function a(b){var d,e,g,h,i,j,k,l=arguments;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:d=1<l.length&&l[1]!==void 0?l[1]:"";e=b.querySelector(f.default.values.gateway);g=parseInt((e||{dataset:{surcharge:0}}).dataset.surcharge);h=(e||{dataset:{cost:d}}).dataset.cost;a.next=6;return c.default.renderForPromise("core_payment/fee_breakdown",{fee:h,surcharge:g});case 6:i=a.sent;j=i.html;k=i.js;c.default.replaceNodeContents(b.querySelector(f.default.regions.costContainer),j,k);case 10:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}(),t=function(){var a=n(regeneratorRuntime.mark(function a(b,c,d,e,f,g){var h;return regeneratorRuntime.wrap(function(a){while(1){switch(a.prev=a.next){case 0:a.next=2;return"function"==typeof o.define&&o.define.amd?new Promise(function(a,c){o.require(["pg_".concat(b,"/gateways_modal")],a,c)}):"undefined"!=typeof module&&module.exports&&"undefined"!=typeof require||"undefined"!=typeof module&&module.component&&o.require&&"component"===o.require.loader?Promise.resolve(require(("pg_".concat(b,"/gateways_modal")))):Promise.resolve(o["pg_".concat(b,"/gateways_modal")]);case 2:h=a.sent;h.process(c,d,e,f,g);case 4:case"end":return a.stop();}}},a)}));return function(){return a.apply(this,arguments)}}()});
|
||||
//# sourceMappingURL=gateways_modal.min.js.map
|
||||
|
||||
File diff suppressed because one or more lines are too long
Vendored
+1
-1
@@ -1,2 +1,2 @@
|
||||
define ("core_payment/repository",["exports","core/ajax"],function(a,b){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.getGatewaysSupportingCurrency=void 0;b=function(a){return a&&a.__esModule?a:{default:a}}(b);var c=function(a,c,d){return b.default.call([{methodname:"core_payment_get_gateways_for_currency",args:{component:a,paymentarea:c,componentid:d}}])[0]};a.getGatewaysSupportingCurrency=c});
|
||||
define ("core_payment/repository",["exports","core/ajax"],function(a,b){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.getAvailableGateways=void 0;b=function(a){return a&&a.__esModule?a:{default:a}}(b);var c=function(a,c,d){return b.default.call([{methodname:"core_payment_get_available_gateways",args:{component:a,paymentarea:c,componentid:d}}])[0]};a.getAvailableGateways=c});
|
||||
//# sourceMappingURL=repository.min.js.map
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"version":3,"sources":["../src/repository.js"],"names":["getGatewaysSupportingCurrency","component","paymentArea","componentId","Ajax","call","methodname","args","paymentarea","componentid"],"mappings":"6KAwBA,uDAUO,GAAMA,CAAAA,CAA6B,CAAG,SAACC,CAAD,CAAYC,CAAZ,CAAyBC,CAAzB,CAAyC,CASlF,MAAOC,WAAKC,IAAL,CAAU,CARD,CACZC,UAAU,CAAE,wCADA,CAEZC,IAAI,CAAE,CACFN,SAAS,CAATA,CADE,CAEFO,WAAW,CAAEN,CAFX,CAGFO,WAAW,CAAEN,CAHX,CAFM,CAQC,CAAV,EAAqB,CAArB,CACV,CAVM,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Repository for payment subsystem.\n *\n * @module core_payment/repository\n * @package core_payment\n * @copyright 2020 Shamim Rezaie <[email protected]>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Ajax from 'core/ajax';\n\n/**\n * Returns the list of gateways that can process payments in the given currency.\n *\n * @param {string} component\n * @param {string} paymentArea\n * @param {number} componentId\n * @returns {Promise<{shortname: string, name: string, description: String}[]>}\n */\nexport const getGatewaysSupportingCurrency = (component, paymentArea, componentId) => {\n const request = {\n methodname: 'core_payment_get_gateways_for_currency',\n args: {\n component,\n paymentarea: paymentArea,\n componentid: componentId,\n }\n };\n return Ajax.call([request])[0];\n};\n"],"file":"repository.min.js"}
|
||||
{"version":3,"sources":["../src/repository.js"],"names":["getAvailableGateways","component","paymentArea","componentId","Ajax","call","methodname","args","paymentarea","componentid"],"mappings":"oKAwBA,uDAUO,GAAMA,CAAAA,CAAoB,CAAG,SAACC,CAAD,CAAYC,CAAZ,CAAyBC,CAAzB,CAAyC,CASzE,MAAOC,WAAKC,IAAL,CAAU,CARD,CACZC,UAAU,CAAE,qCADA,CAEZC,IAAI,CAAE,CACFN,SAAS,CAATA,CADE,CAEFO,WAAW,CAAEN,CAFX,CAGFO,WAAW,CAAEN,CAHX,CAFM,CAQC,CAAV,EAAqB,CAArB,CACV,CAVM,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Repository for payment subsystem.\n *\n * @module core_payment/repository\n * @package core_payment\n * @copyright 2020 Shamim Rezaie <[email protected]>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Ajax from 'core/ajax';\n\n/**\n * Returns the list of gateways that can process payments in the given currency.\n *\n * @param {string} component\n * @param {string} paymentArea\n * @param {number} componentId\n * @returns {Promise<{shortname: string, name: string, description: String}[]>}\n */\nexport const getAvailableGateways = (component, paymentArea, componentId) => {\n const request = {\n methodname: 'core_payment_get_available_gateways',\n args: {\n component,\n paymentarea: paymentArea,\n componentid: componentId,\n }\n };\n return Ajax.call([request])[0];\n};\n"],"file":"repository.min.js"}
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
+3
-3
@@ -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
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user