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   * 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 core_external\external_api;
  31  use core_external\external_function_parameters;
  32  use core_external\external_value;
  33  use core_external\external_single_structure;
  34  
  35  class get_config_for_js extends external_api {
  36  
  37      /**
  38       * Returns description of method parameters.
  39       *
  40       * @return external_function_parameters
  41       */
  42      public static function execute_parameters(): external_function_parameters {
  43          return new external_function_parameters([
  44              'component' => new external_value(PARAM_COMPONENT, 'Component'),
  45              'paymentarea' => new external_value(PARAM_AREA, 'Payment area in the component'),
  46              'itemid' => new external_value(PARAM_INT, 'An identifier for payment area in the component'),
  47          ]);
  48      }
  49  
  50      /**
  51       * Returns the config values required by the PayPal JavaScript SDK.
  52       *
  53       * @param string $component
  54       * @param string $paymentarea
  55       * @param int $itemid
  56       * @return string[]
  57       */
  58      public static function execute(string $component, string $paymentarea, int $itemid): array {
  59          self::validate_parameters(self::execute_parameters(), [
  60              'component' => $component,
  61              'paymentarea' => $paymentarea,
  62              'itemid' => $itemid,
  63          ]);
  64  
  65          $config = helper::get_gateway_configuration($component, $paymentarea, $itemid, 'paypal');
  66          $payable = helper::get_payable($component, $paymentarea, $itemid);
  67          $surcharge = helper::get_gateway_surcharge('paypal');
  68  
  69          return [
  70              'clientid' => $config['clientid'],
  71              'brandname' => $config['brandname'],
  72              'cost' => helper::get_rounded_cost($payable->get_amount(), $payable->get_currency(), $surcharge),
  73              'currency' => $payable->get_currency(),
  74          ];
  75      }
  76  
  77      /**
  78       * Returns description of method result value.
  79       *
  80       * @return external_single_structure
  81       */
  82      public static function execute_returns(): external_single_structure {
  83          return new external_single_structure([
  84              'clientid' => new external_value(PARAM_TEXT, 'PayPal client ID'),
  85              'brandname' => new external_value(PARAM_TEXT, 'Brand name'),
  86              'cost' => new external_value(PARAM_FLOAT, 'Cost with gateway surcharge'),
  87              'currency' => new external_value(PARAM_TEXT, 'Currency'),
  88          ]);
  89      }
  90  }