MDL-69166 core_payment: Add paymentarea to the payment subsystem
[moodle.git] / payment / classes / external / get_gateways_for_currency.php
CommitLineData
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
25namespace core_payment\external;
26
15a4e4c8 27use core_payment\helper;
f3d75264
SR
28use external_api;
29use external_function_parameters;
30use external_value;
31use external_single_structure;
32use external_multiple_structure;
33
34defined('MOODLE_INTERNAL') || die();
35
36require_once($CFG->libdir . '/externallib.php');
37
38class get_gateways_for_currency extends external_api {
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'),
49 'componentid' => new external_value(PARAM_INT, 'An identifier for payment area in the component')
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
15a4e4c8 58 * @param int $componentid
f3d75264
SR
59 * @return \stdClass[]
60 */
7d10f352 61 public static function execute(string $component, string $paymentarea, int $componentid): array {
f3d75264
SR
62
63 $params = external_api::validate_parameters(self::execute_parameters(), [
15a4e4c8 64 'component' => $component,
7d10f352 65 'paymentarea' => $paymentarea,
15a4e4c8 66 'componentid' => $componentid,
f3d75264
SR
67 ]);
68
69 $list = [];
7d10f352 70 $gateways = helper::get_gateways_for_currency($params['component'], $params['paymentarea'], $params['componentid']);
15a4e4c8
MG
71 [
72 'amount' => $amount,
73 'currency' => $currency
7d10f352 74 ] = helper::get_cost($params['component'], $params['paymentarea'], $params['componentid']);
f3d75264
SR
75
76 foreach ($gateways as $gateway) {
7d10f352 77 $surcharge = helper::get_gateway_surcharge($gateway);
f3d75264
SR
78 $list[] = (object)[
79 'shortname' => $gateway,
80 'name' => get_string('gatewayname', 'pg_' . $gateway),
81 'description' => get_string('gatewaydescription', 'pg_' . $gateway),
15a4e4c8
MG
82 'surcharge' => $surcharge,
83 'cost' => helper::get_cost_as_string(helper::get_cost_with_surcharge($amount, $surcharge, $currency), $currency),
f3d75264
SR
84 ];
85 }
86
87 return $list;
88 }
89
90 /**
91 * Returns description of method result value.
92 *
93 * @return external_multiple_structure
94 */
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'),
1882bb47 101 'surcharge' => new external_value(PARAM_INT, 'percentage of surcharge when using the gateway'),
15a4e4c8
MG
102 'cost' => new external_value(PARAM_TEXT,
103 'Cost in human-readable form (amount plus surcharge with currency sign)'),
f3d75264
SR
104 ])
105 );
106 }
107}