From 9f2d8a0bdc7d10b97d8d43a4719ded2b71bf20a3 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Wed, 29 Jan 2020 19:09:53 +1100 Subject: [PATCH] MDL-69166 core_payment: WS to get list of gateways supporting a currency --- lib/db/services.php | 7 ++ .../external/get_gateways_for_currency.php | 90 +++++++++++++++++++ payment/classes/helper.php | 22 +++++ 3 files changed, 119 insertions(+) create mode 100644 payment/classes/external/get_gateways_for_currency.php diff --git a/lib/db/services.php b/lib/db/services.php index 9f21e403908..b8152e31389 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -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 index 00000000000..4028e638fe7 --- /dev/null +++ b/payment/classes/external/get_gateways_for_currency.php @@ -0,0 +1,90 @@ +. + +/** + * This is the external API for this component. + * + * @package core_payment + * @copyright 2019 Shamim Rezaie + * @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'), + ]) + ); + } +} diff --git a/payment/classes/helper.php b/payment/classes/helper.php index 2a1a745a08c..4e611d99e91 100644 --- a/payment/classes/helper.php +++ b/payment/classes/helper.php @@ -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; + } }