MDL-69166 core_payment: WS to get list of gateways supporting a currency
[moodle.git] / payment / classes / external / get_gateways_for_currency.php
CommitLineData
9f2d8a0b
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
25namespace core_payment\external;
26
27use external_api;
28use external_function_parameters;
29use external_value;
30use external_single_structure;
31use external_multiple_structure;
32
33defined('MOODLE_INTERNAL') || die();
34
35require_once($CFG->libdir . '/externallib.php');
36
37class get_gateways_for_currency extends external_api {
38
39 /**
40 * Returns description of method parameters.
41 *
42 * @return external_function_parameters
43 */
44 public static function execute_parameters(): external_function_parameters {
45 return new external_function_parameters(
46 ['currency' => new external_value(PARAM_ALPHA, 'Currency code')]
47 );
48 }
49
50 /**
51 * Returns the list of gateways that can process payments in the given currency.
52 *
53 * @param string $currency The currency in the three-character ISO-4217 format.
54 * @return \stdClass[]
55 */
56 public static function execute(string $currency): array {
57
58 $params = external_api::validate_parameters(self::execute_parameters(), [
59 'currency' => $currency,
60 ]);
61
62 $list = [];
63 $gateways = \core_payment\helper::get_gateways_for_currency($params['currency']);
64
65 foreach ($gateways as $gateway) {
66 $list[] = (object)[
67 'shortname' => $gateway,
68 'name' => get_string('gatewayname', 'pg_' . $gateway),
69 'description' => get_string('gatewaydescription', 'pg_' . $gateway),
70 ];
71 }
72
73 return $list;
74 }
75
76 /**
77 * Returns description of method result value.
78 *
79 * @return external_multiple_structure
80 */
81 public static function execute_returns(): external_multiple_structure {
82 return new external_multiple_structure(
83 new external_single_structure([
84 'shortname' => new external_value(PARAM_PLUGIN, 'Name of the plugin'),
85 'name' => new external_value(PARAM_TEXT, 'Human readable name of the gateway'),
86 'description' => new external_value(PARAM_TEXT, 'description of the gateway'),
87 ])
88 );
89 }
90}