MDL-69166 core_payment: Add paymentarea to the payment subsystem
[moodle.git] / payment / gateway / paypal / classes / external / get_config_for_js.php
CommitLineData
9a7aed4a
SR
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 pg_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
25declare(strict_types=1);
26
27namespace pg_paypal\external;
28
895f38cc 29use core_payment\helper;
9a7aed4a
SR
30use external_api;
31use external_function_parameters;
32use external_value;
33use external_single_structure;
34
35defined('MOODLE_INTERNAL') || die();
36
37require_once($CFG->libdir . '/externallib.php');
38
39class 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 {
895f38cc 47 return new external_function_parameters([
7d10f352
SR
48 'component' => new external_value(PARAM_COMPONENT, 'Component'),
49 'paymentarea' => new external_value(PARAM_AREA, 'Payment area in the component'),
50 'componentid' => new external_value(PARAM_INT, 'An identifier for payment area in the component'),
895f38cc 51 ]);
9a7aed4a
SR
52 }
53
54 /**
7d10f352 55 * Returns the config values required by the PayPal JavaScript SDK.
9a7aed4a 56 *
7d10f352
SR
57 * @param string $component
58 * @param string $paymentarea
59 * @param int $componentid
9a7aed4a
SR
60 * @return string[]
61 */
7d10f352 62 public static function execute(string $component, string $paymentarea, int $componentid): array {
895f38cc
MG
63 self::validate_parameters(self::execute_parameters(), [
64 'component' => $component,
7d10f352 65 'paymentarea' => $paymentarea,
895f38cc
MG
66 'componentid' => $componentid,
67 ]);
68
7d10f352
SR
69 $config = helper::get_gateway_configuration($component, $paymentarea, $componentid, 'paypal');
70 $cost = helper::get_cost($component, $paymentarea, $componentid);
15a4e4c8 71 $surcharge = helper::get_gateway_surcharge('paypal');
9a7aed4a
SR
72
73 return [
895f38cc
MG
74 'clientid' => $config['clientid'],
75 'brandname' => $config['brandname'],
15a4e4c8
MG
76 'cost' => helper::get_cost_with_surcharge($cost['amount'], $surcharge, $cost['currency']),
77 'currency' => $cost['currency'],
9a7aed4a
SR
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'),
15a4e4c8
MG
90 'cost' => new external_value(PARAM_FLOAT, 'Cost with gateway surcharge'),
91 'currency' => new external_value(PARAM_TEXT, 'Currency'),
9a7aed4a
SR
92 ]);
93 }
94}