2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This is the external API for this component.
20 * @package core_payment
21 * @copyright 2019 Shamim Rezaie <shamim@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_payment\external;
27 use core_payment\helper;
29 use external_function_parameters;
31 use external_single_structure;
32 use external_multiple_structure;
34 defined('MOODLE_INTERNAL') || die();
36 require_once($CFG->libdir . '/externallib.php');
38 class get_gateways_for_currency extends external_api {
41 * Returns description of method parameters.
43 * @return external_function_parameters
45 public static function execute_parameters(): external_function_parameters {
46 return new external_function_parameters([
47 'component' => new external_value(PARAM_COMPONENT, 'Component'),
48 'paymentarea' => new external_value(PARAM_AREA, 'Payment area in the component'),
49 'componentid' => new external_value(PARAM_INT, 'An identifier for payment area in the component')
54 * Returns the list of gateways that can process payments in the given currency.
56 * @param string $component
57 * @param string $paymentarea
58 * @param int $componentid
61 public static function execute(string $component, string $paymentarea, int $componentid): array {
63 $params = external_api::validate_parameters(self::execute_parameters(), [
64 'component' => $component,
65 'paymentarea' => $paymentarea,
66 'componentid' => $componentid,
70 $gateways = helper::get_gateways_for_currency($params['component'], $params['paymentarea'], $params['componentid']);
73 'currency' => $currency
74 ] = helper::get_cost($params['component'], $params['paymentarea'], $params['componentid']);
76 foreach ($gateways as $gateway) {
77 $surcharge = helper::get_gateway_surcharge($gateway);
79 'shortname' => $gateway,
80 'name' => get_string('gatewayname', 'pg_' . $gateway),
81 'description' => get_string('gatewaydescription', 'pg_' . $gateway),
82 'surcharge' => $surcharge,
83 'cost' => helper::get_cost_as_string(helper::get_cost_with_surcharge($amount, $surcharge, $currency), $currency),
91 * Returns description of method result value.
93 * @return external_multiple_structure
95 public static function execute_returns(): external_multiple_structure {
96 return new external_multiple_structure(
97 new external_single_structure([
98 'shortname' => new external_value(PARAM_PLUGIN, 'Name of the plugin'),
99 'name' => new external_value(PARAM_TEXT, 'Human readable name of the gateway'),
100 'description' => new external_value(PARAM_TEXT, 'description of the gateway'),
101 'surcharge' => new external_value(PARAM_INT, 'percentage of surcharge when using the gateway'),
102 'cost' => new external_value(PARAM_TEXT,
103 'Cost in human-readable form (amount plus surcharge with currency sign)'),