Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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  
  27  use core_payment\helper;
  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  
  38  class get_available_gateways 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 {
  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              'itemid' => new external_value(PARAM_INT, 'An identifier for payment area in the component')
  50          ]);
  51      }
  52  
  53      /**
  54       * Returns the list of gateways that can process payments in the given currency.
  55       *
  56       * @param string $component
  57       * @param string $paymentarea
  58       * @param int $itemid
  59       * @return \stdClass[]
  60       */
  61      public static function execute(string $component, string $paymentarea, int $itemid): array {
  62  
  63          $params = external_api::validate_parameters(self::execute_parameters(), [
  64              'component' => $component,
  65              'paymentarea' => $paymentarea,
  66              'itemid' => $itemid,
  67          ]);
  68  
  69          $list = [];
  70          $gateways = helper::get_available_gateways($params['component'], $params['paymentarea'], $params['itemid']);
  71          $payable = helper::get_payable($params['component'], $params['paymentarea'], $params['itemid']);
  72          $amount = $payable->get_amount();
  73          $currency = $payable->get_currency();
  74  
  75          foreach ($gateways as $gateway) {
  76              $surcharge = helper::get_gateway_surcharge($gateway);
  77              $list[] = (object)[
  78                  'shortname' => $gateway,
  79                  'name' => get_string('gatewayname', 'paygw_' . $gateway),
  80                  'description' => get_string('gatewaydescription', 'paygw_' . $gateway),
  81                  'surcharge' => $surcharge,
  82                  'cost' => helper::get_cost_as_string($amount, $currency, $surcharge),
  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'),
  99                      'description' => new external_value(PARAM_RAW, 'description of the gateway'),
 100                      'surcharge' => new external_value(PARAM_INT, 'percentage of surcharge when using the gateway'),
 101                      'cost' => new external_value(PARAM_TEXT,
 102                          'Cost in human-readable form (amount plus surcharge with currency sign)'),
 103                  ])
 104          );
 105      }
 106  }