Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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