Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 400 and 403] [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 core_external\external_api;
  29  use core_external\external_value;
  30  use core_external\external_single_structure;
  31  use core_external\external_multiple_structure;
  32  use core_external\external_function_parameters;
  33  
  34  class get_available_gateways extends external_api {
  35  
  36      /**
  37       * Returns description of method parameters.
  38       *
  39       * @return external_function_parameters
  40       */
  41      public static function execute_parameters(): external_function_parameters {
  42          return new external_function_parameters([
  43              'component' => new external_value(PARAM_COMPONENT, 'Component'),
  44              'paymentarea' => new external_value(PARAM_AREA, 'Payment area in the component'),
  45              'itemid' => new external_value(PARAM_INT, 'An identifier for payment area in the component')
  46          ]);
  47      }
  48  
  49      /**
  50       * Returns the list of gateways that can process payments in the given currency.
  51       *
  52       * @param string $component
  53       * @param string $paymentarea
  54       * @param int $itemid
  55       * @return \stdClass[]
  56       */
  57      public static function execute(string $component, string $paymentarea, int $itemid): array {
  58  
  59          $params = external_api::validate_parameters(self::execute_parameters(), [
  60              'component' => $component,
  61              'paymentarea' => $paymentarea,
  62              'itemid' => $itemid,
  63          ]);
  64  
  65          $list = [];
  66          $gateways = helper::get_available_gateways($params['component'], $params['paymentarea'], $params['itemid']);
  67          $payable = helper::get_payable($params['component'], $params['paymentarea'], $params['itemid']);
  68          $amount = $payable->get_amount();
  69          $currency = $payable->get_currency();
  70  
  71          foreach ($gateways as $gateway) {
  72              $surcharge = helper::get_gateway_surcharge($gateway);
  73              $list[] = (object)[
  74                  'shortname' => $gateway,
  75                  'name' => get_string('gatewayname', 'paygw_' . $gateway),
  76                  'description' => get_string('gatewaydescription', 'paygw_' . $gateway),
  77                  'surcharge' => $surcharge,
  78                  'cost' => helper::get_cost_as_string($amount, $currency, $surcharge),
  79              ];
  80          }
  81  
  82          return $list;
  83      }
  84  
  85      /**
  86       * Returns description of method result value.
  87       *
  88       * @return external_multiple_structure
  89       */
  90      public static function execute_returns(): external_multiple_structure {
  91          return new external_multiple_structure(
  92                  new external_single_structure([
  93                      'shortname' => new external_value(PARAM_PLUGIN, 'Name of the plugin'),
  94                      'name' => new external_value(PARAM_TEXT, 'Human readable name of the gateway'),
  95                      'description' => new external_value(PARAM_RAW, 'description of the gateway'),
  96                      'surcharge' => new external_value(PARAM_INT, 'percentage of surcharge when using the gateway'),
  97                      'cost' => new external_value(PARAM_TEXT,
  98                          'Cost in human-readable form (amount plus surcharge with currency sign)'),
  99                  ])
 100          );
 101      }
 102  }