Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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      /**
  86       * Sets the current plugin as enabled or disabled
  87       * When enabling tries to guess the sortorder based on default rank returned by the plugin.
  88       *
  89       * @param bool $newstate
  90       */
  91      public function set_enabled(bool $newstate = true) {
  92          $enabled = self::get_enabled_plugins();
  93          if (array_key_exists($this->name, $enabled) == $newstate) {
  94              // Nothing to do.
  95              return;
  96          }
  97          if ($newstate) {
  98              // Enable gateway plugin.
  99              $plugins = \core_plugin_manager::instance()->get_plugins_of_type('paygw');
 100              if (!array_key_exists($this->name, $plugins)) {
 101                  // Can not be enabled.
 102                  return;
 103              }
 104              $enabled[$this->name] = $this->name;
 105              self::set_enabled_plugins($enabled);
 106          } else {
 107              // Disable gateway plugin.
 108              unset($enabled[$this->name]);
 109              self::set_enabled_plugins($enabled);
 110          }
 111      }
 112  
 113      /**
 114       * Set the list of enabled payment gateways in the specified sort order
 115       * To be used when changing settings or in unit tests.
 116       *
 117       * @param string|array $list list of plugin names without frankenstyle prefix - comma-separated string or an array
 118       */
 119      public static function set_enabled_plugins($list) {
 120          if (empty($list)) {
 121              $list = [];
 122          } else if (!is_array($list)) {
 123              $list = explode(',', $list);
 124          }
 125          if ($list) {
 126              $plugins = \core_plugin_manager::instance()->get_installed_plugins('paygw');
 127              $list = array_intersect($list, array_keys($plugins));
 128          }
 129          set_config('paygw_plugins_sortorder', join(',', $list));
 130          \core_plugin_manager::reset_caches();
 131      }
 132  
 133      /**
 134       * Returns the list of currencies that the payment gateway supports.
 135       *
 136       * @return string[] An array of the currency codes in the three-character ISO-4217 format
 137       */
 138      public function get_supported_currencies(): array {
 139          $classname = '\paygw_'.$this->name.'\gateway';
 140  
 141          return $classname::get_supported_currencies();
 142      }
 143  }