Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   * Contains subplugin info class for payment gateways.
  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\plugininfo;
  26  
  27  /**
  28   * Payment gateway subplugin info class.
  29   *
  30   * @copyright 2019 Shamim Rezaie <shamim@moodle.com>
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class paygw extends base {
  34  
  35      public static function plugintype_supports_disabling(): bool {
  36          return true;
  37      }
  38  
  39      public function is_uninstall_allowed() {
  40          return true;
  41      }
  42  
  43      public function get_settings_section_name() {
  44          return 'paymentgateway' . $this->name;
  45      }
  46  
  47      public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  48          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  49          /** @var \admin_root $ADMIN */
  50          $ADMIN = $adminroot; // May be used in settings.php.
  51          $plugininfo = $this; // Also can be used inside settings.php.
  52  
  53          if (!$this->is_installed_and_upgraded()) {
  54              return;
  55          }
  56  
  57          if (!$hassiteconfig) {
  58              return;
  59          }
  60  
  61          $section = $this->get_settings_section_name();
  62  
  63          $settings = null;
  64          if (file_exists($this->full_path('settings.php'))) {
  65              $settings = new \admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
  66              include($this->full_path('settings.php')); // This may also set $settings to null.
  67          }
  68          if ($settings) {
  69              $ADMIN->add($parentnodename, $settings);
  70          }
  71      }
  72  
  73      public static function get_manage_url() {
  74          return new \moodle_url('/admin/settings.php', array('section' => 'managepaymentgateways'));
  75      }
  76  
  77      public static function get_enabled_plugins() {
  78          global $CFG;
  79  
  80          $order = (!empty($CFG->paygw_plugins_sortorder)) ? explode(',', $CFG->paygw_plugins_sortorder) : [];
  81          if ($order) {
  82              $plugins = \core_plugin_manager::instance()->get_installed_plugins('paygw');
  83              $order = array_intersect($order, array_keys($plugins));
  84          }
  85  
  86          return array_combine($order, $order);
  87      }
  88  
  89      public static function enable_plugin(string $pluginname, int $enabled): bool {
  90          global $CFG;
  91  
  92          $haschanged = false;
  93          $plugins = [];
  94          if (!empty($CFG->paygw_plugins_sortorder)) {
  95              $plugins = array_flip(explode(',', $CFG->paygw_plugins_sortorder));
  96          }
  97          // Only set visibility if it's different from the current value.
  98          if ($enabled && !array_key_exists($pluginname, $plugins)) {
  99              $plugins[$pluginname] = $pluginname;
 100              $haschanged = true;
 101          } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
 102              unset($plugins[$pluginname]);
 103              $haschanged = true;
 104          }
 105  
 106          if ($haschanged) {
 107              add_to_config_log('paygw_plugins_sortorder', !$enabled, $enabled, $pluginname);
 108              self::set_enabled_plugins(array_flip($plugins));
 109          }
 110  
 111          return $haschanged;
 112      }
 113  
 114      /**
 115       * Sets the current plugin as enabled or disabled
 116       * When enabling tries to guess the sortorder based on default rank returned by the plugin.
 117       *
 118       * @param bool $newstate
 119       */
 120      public function set_enabled(bool $newstate = true) {
 121          self::enable_plugin($this->name, $newstate);
 122      }
 123  
 124      /**
 125       * Set the list of enabled payment gateways in the specified sort order
 126       * To be used when changing settings or in unit tests.
 127       *
 128       * @param string|array $list list of plugin names without frankenstyle prefix - comma-separated string or an array
 129       */
 130      public static function set_enabled_plugins($list) {
 131          if (empty($list)) {
 132              $list = [];
 133          } else if (!is_array($list)) {
 134              $list = explode(',', $list);
 135          }
 136          if ($list) {
 137              $plugins = \core_plugin_manager::instance()->get_installed_plugins('paygw');
 138              $list = array_intersect($list, array_keys($plugins));
 139          }
 140          set_config('paygw_plugins_sortorder', join(',', $list));
 141          \core_plugin_manager::reset_caches();
 142      }
 143  
 144      /**
 145       * Returns the list of currencies that the payment gateway supports.
 146       *
 147       * @return string[] An array of the currency codes in the three-character ISO-4217 format
 148       */
 149      public function get_supported_currencies(): array {
 150          $classname = '\paygw_'.$this->name.'\gateway';
 151  
 152          return $classname::get_supported_currencies();
 153      }
 154  }