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]

   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   * Payment module test data generator class
  19   *
  20   * @package    core_payment
  21   * @category   test
  22   * @copyright  2020 Marina Glancy
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  class core_payment_generator extends component_generator_base {
  26  
  27      /** @var int */
  28      protected $accountcounter = 0;
  29  
  30      /**
  31       * Create a payment account
  32       *
  33       * @param array $data account data (name, idnumber, enabled) and additionally field 'gateways' that can include
  34       *    a list of gateways that should be mock-enabled for this account.
  35       */
  36      public function create_payment_account(array $data = []): \core_payment\account {
  37          $this->accountcounter++;
  38          $gateways = [];
  39          if (!empty($data['gateways'])) {
  40              $gateways = preg_split('/,/', $data['gateways']);
  41          }
  42          unset($data['gateways']);
  43          $account = \core_payment\helper::save_payment_account(
  44              (object)($data + ['name' => 'Test '.$this->accountcounter, 'idnumber' => '', 'enabled' => 1]));
  45          foreach ($gateways as $gateway) {
  46              \core_payment\helper::save_payment_gateway(
  47                  (object)['accountid' => $account->get('id'), 'gateway' => $gateway, 'enabled' => 1]);
  48          }
  49          return $account;
  50      }
  51  
  52      /**
  53       * Create a payment account
  54       *
  55       * @param array $data
  56       */
  57      public function create_payment(array $data): int {
  58          global $DB;
  59          if (empty($data['accountid']) || !\core_payment\account::get_record(['id' => $data['accountid']])) {
  60              throw new coding_exception('Account id is not specified or does not exist');
  61          }
  62  
  63          if (empty($data['amount'])) {
  64              throw new coding_exception('Amount must be specified');
  65          }
  66  
  67          $gateways = \core\plugininfo\paygw::get_enabled_plugins();
  68          if (empty($data['gateway'])) {
  69              $data['gateway'] = reset($gateways);
  70          }
  71  
  72          $id = $DB->insert_record('payments', $data +
  73              [
  74                  'component' => 'testcomponent',
  75                  'paymentarea' => 'teatarea',
  76                  'itemid' => 0,
  77                  'currency' => 'AUD',
  78              ]);
  79          return $id;
  80      }
  81  
  82  }