MDL-69166 core_payment: WS to get list of gateways supporting a currency
authorShamim Rezaie <shamim@moodle.com>
Wed, 29 Jan 2020 08:09:53 +0000 (19:09 +1100)
committerShamim Rezaie <shamim@moodle.com>
Mon, 26 Oct 2020 21:37:08 +0000 (08:37 +1100)
lib/db/services.php
payment/classes/external/get_gateways_for_currency.php [new file with mode: 0644]
payment/classes/helper.php

index 9f21e40..b8152e3 100644 (file)
@@ -2720,6 +2720,13 @@ $functions = array(
         'ajax'          => 'true',
         'capabilities'  => '',
     ],
+    'core_payment_get_gateways_for_currency' => [
+        'classname'   => 'core_payment\external\get_gateways_for_currency',
+        'methodname'  => 'execute',
+        'description' => 'Get the list of payment gateways that support the given currency',
+        'type'        => 'read',
+        'ajax'        => true,
+    ],
 );
 
 $services = array(
diff --git a/payment/classes/external/get_gateways_for_currency.php b/payment/classes/external/get_gateways_for_currency.php
new file mode 100644 (file)
index 0000000..4028e63
--- /dev/null
@@ -0,0 +1,90 @@
+<?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 is the external API for this component.
+ *
+ * @package    core_payment
+ * @copyright  2019 Shamim Rezaie <shamim@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core_payment\external;
+
+use external_api;
+use external_function_parameters;
+use external_value;
+use external_single_structure;
+use external_multiple_structure;
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once($CFG->libdir . '/externallib.php');
+
+class get_gateways_for_currency extends external_api {
+
+    /**
+     * Returns description of method parameters.
+     *
+     * @return external_function_parameters
+     */
+    public static function execute_parameters(): external_function_parameters {
+        return new external_function_parameters(
+                ['currency' => new external_value(PARAM_ALPHA, 'Currency code')]
+        );
+    }
+
+    /**
+     * Returns the list of gateways that can process payments in the given currency.
+     *
+     * @param string $currency The currency in the three-character ISO-4217 format.
+     * @return \stdClass[]
+     */
+    public static function execute(string $currency): array {
+
+        $params = external_api::validate_parameters(self::execute_parameters(), [
+            'currency' => $currency,
+        ]);
+
+        $list = [];
+        $gateways = \core_payment\helper::get_gateways_for_currency($params['currency']);
+
+        foreach ($gateways as $gateway) {
+            $list[] = (object)[
+                'shortname' => $gateway,
+                'name' => get_string('gatewayname', 'pg_' . $gateway),
+                'description' => get_string('gatewaydescription', 'pg_' . $gateway),
+            ];
+        }
+
+        return $list;
+    }
+
+    /**
+     * Returns description of method result value.
+     *
+     * @return external_multiple_structure
+     */
+    public static function execute_returns(): external_multiple_structure {
+        return new external_multiple_structure(
+                new external_single_structure([
+                    'shortname' => new external_value(PARAM_PLUGIN, 'Name of the plugin'),
+                    'name' => new external_value(PARAM_TEXT, 'Human readable name of the gateway'),
+                    'description' => new external_value(PARAM_TEXT, 'description of the gateway'),
+                ])
+        );
+    }
+}
index 2a1a745..4e611d9 100644 (file)
@@ -53,4 +53,26 @@ class helper {
 
         return $currencies;
     }
+
+    /**
+     * Returns the list of gateways that can process payments in the given currency.
+     *
+     * @param string $currency The currency in the three-character ISO-4217 format.
+     * @return string[]
+     */
+    public static function get_gateways_for_currency(string $currency): array {
+        $gateways = [];
+
+        $plugins = \core_plugin_manager::instance()->get_enabled_plugins('pg');
+        foreach ($plugins as $plugin) {
+            $classname = '\pg_' . $plugin . '\gateway';
+
+            $currencies = $classname::get_supported_currencies();
+            if (in_array($currency, $currencies)) {
+                $gateways[] = $plugin;
+            }
+        }
+
+        return $gateways;
+    }
 }