Commit | Line | Data |
---|---|---|
f3d75264 SR |
1 | <?php |
2 | // This file is part of Moodle - http://moodle.org/ | |
3 | // | |
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. | |
8 | // | |
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. | |
13 | // | |
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/>. | |
16 | ||
17 | /** | |
18 | * This is the external API for this component. | |
19 | * | |
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 | |
23 | */ | |
24 | ||
25 | namespace core_payment\external; | |
26 | ||
15a4e4c8 | 27 | use core_payment\helper; |
f3d75264 SR |
28 | use external_api; |
29 | use external_function_parameters; | |
30 | use external_value; | |
31 | use external_single_structure; | |
32 | use external_multiple_structure; | |
33 | ||
34 | defined('MOODLE_INTERNAL') || die(); | |
35 | ||
36 | require_once($CFG->libdir . '/externallib.php'); | |
37 | ||
90fbc58d | 38 | class get_available_gateways extends external_api { |
f3d75264 SR |
39 | |
40 | /** | |
41 | * Returns description of method parameters. | |
42 | * | |
43 | * @return external_function_parameters | |
44 | */ | |
45 | public static function execute_parameters(): external_function_parameters { | |
7d10f352 SR |
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'), | |
52305c9a | 49 | 'itemid' => new external_value(PARAM_INT, 'An identifier for payment area in the component') |
7d10f352 | 50 | ]); |
f3d75264 SR |
51 | } |
52 | ||
53 | /** | |
54 | * Returns the list of gateways that can process payments in the given currency. | |
55 | * | |
15a4e4c8 | 56 | * @param string $component |
7d10f352 | 57 | * @param string $paymentarea |
52305c9a | 58 | * @param int $itemid |
f3d75264 SR |
59 | * @return \stdClass[] |
60 | */ | |
52305c9a | 61 | public static function execute(string $component, string $paymentarea, int $itemid): array { |
f3d75264 SR |
62 | |
63 | $params = external_api::validate_parameters(self::execute_parameters(), [ | |
15a4e4c8 | 64 | 'component' => $component, |
7d10f352 | 65 | 'paymentarea' => $paymentarea, |
52305c9a | 66 | 'itemid' => $itemid, |
f3d75264 SR |
67 | ]); |
68 | ||
69 | $list = []; | |
52305c9a | 70 | $gateways = helper::get_available_gateways($params['component'], $params['paymentarea'], $params['itemid']); |
b63f17b5 SR |
71 | $payable = helper::get_payable($params['component'], $params['paymentarea'], $params['itemid']); |
72 | $amount = $payable->get_amount(); | |
73 | $currency = $payable->get_currency(); | |
f3d75264 SR |
74 | |
75 | foreach ($gateways as $gateway) { | |
7d10f352 | 76 | $surcharge = helper::get_gateway_surcharge($gateway); |
f3d75264 SR |
77 | $list[] = (object)[ |
78 | 'shortname' => $gateway, | |
6b3d163a SR |
79 | 'name' => get_string('gatewayname', 'paygw_' . $gateway), |
80 | 'description' => get_string('gatewaydescription', 'paygw_' . $gateway), | |
15a4e4c8 | 81 | 'surcharge' => $surcharge, |
90fbc58d | 82 | 'cost' => helper::get_cost_as_string($amount, $currency, $surcharge), |
f3d75264 SR |
83 | ]; |
84 | } | |
85 | ||
86 | return $list; | |
87 | } | |
88 | ||
89 | /** | |
90 | * Returns description of method result value. | |
91 | * | |
92 | * @return external_multiple_structure | |
93 | */ | |
94 | public static function execute_returns(): external_multiple_structure { | |
95 | return new external_multiple_structure( | |
96 | new external_single_structure([ | |
97 | 'shortname' => new external_value(PARAM_PLUGIN, 'Name of the plugin'), | |
98 | 'name' => new external_value(PARAM_TEXT, 'Human readable name of the gateway'), | |
a9b83b3c | 99 | 'description' => new external_value(PARAM_RAW, 'description of the gateway'), |
1882bb47 | 100 | 'surcharge' => new external_value(PARAM_INT, 'percentage of surcharge when using the gateway'), |
15a4e4c8 MG |
101 | 'cost' => new external_value(PARAM_TEXT, |
102 | 'Cost in human-readable form (amount plus surcharge with currency sign)'), | |
f3d75264 SR |
103 | ]) |
104 | ); | |
105 | } | |
106 | } |