Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 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 class contains a list of webservice functions related to the PayPal payment gateway.
  19   *
  20   * @package    paygw_paypal
  21   * @copyright  2020 Shamim Rezaie <shamim@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  declare(strict_types=1);
  26  
  27  namespace paygw_paypal\external;
  28  
  29  use core_payment\helper;
  30  use external_api;
  31  use external_function_parameters;
  32  use external_value;
  33  use external_single_structure;
  34  
  35  defined('MOODLE_INTERNAL') || die();
  36  
  37  require_once($CFG->libdir . '/externallib.php');
  38  
  39  class get_config_for_js extends external_api {
  40  
  41      /**
  42       * Returns description of method parameters.
  43       *
  44       * @return external_function_parameters
  45       */
  46      public static function execute_parameters(): external_function_parameters {
  47          return new external_function_parameters([
  48              'component' => new external_value(PARAM_COMPONENT, 'Component'),
  49              'paymentarea' => new external_value(PARAM_AREA, 'Payment area in the component'),
  50              'itemid' => new external_value(PARAM_INT, 'An identifier for payment area in the component'),
  51          ]);
  52      }
  53  
  54      /**
  55       * Returns the config values required by the PayPal JavaScript SDK.
  56       *
  57       * @param string $component
  58       * @param string $paymentarea
  59       * @param int $itemid
  60       * @return string[]
  61       */
  62      public static function execute(string $component, string $paymentarea, int $itemid): array {
  63          self::validate_parameters(self::execute_parameters(), [
  64              'component' => $component,
  65              'paymentarea' => $paymentarea,
  66              'itemid' => $itemid,
  67          ]);
  68  
  69          $config = helper::get_gateway_configuration($component, $paymentarea, $itemid, 'paypal');
  70          $payable = helper::get_payable($component, $paymentarea, $itemid);
  71          $surcharge = helper::get_gateway_surcharge('paypal');
  72  
  73          return [
  74              'clientid' => $config['clientid'],
  75              'brandname' => $config['brandname'],
  76              'cost' => helper::get_rounded_cost($payable->get_amount(), $payable->get_currency(), $surcharge),
  77              'currency' => $payable->get_currency(),
  78          ];
  79      }
  80  
  81      /**
  82       * Returns description of method result value.
  83       *
  84       * @return external_single_structure
  85       */
  86      public static function execute_returns(): external_single_structure {
  87          return new external_single_structure([
  88              'clientid' => new external_value(PARAM_TEXT, 'PayPal client ID'),
  89              'brandname' => new external_value(PARAM_TEXT, 'Brand name'),
  90              'cost' => new external_value(PARAM_FLOAT, 'Cost with gateway surcharge'),
  91              'currency' => new external_value(PARAM_TEXT, 'Currency'),
  92          ]);
  93      }
  94  }