Commit | Line | Data |
---|---|---|
4865d2a0 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 | * Contains helper class for the payment subsystem. | |
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_payment; | |
26 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | /** | |
30 | * Helper class for the payment subsystem. | |
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 helper { | |
36 | ||
37 | /** | |
38 | * Returns an accumulated list of supported currencies by all payment gateways. | |
39 | * | |
40 | * @return string[] An array of the currency codes in the three-character ISO-4217 format | |
41 | */ | |
42 | public static function get_supported_currencies(): array { | |
43 | $currencies = []; | |
44 | ||
45 | $plugins = \core_plugin_manager::instance()->get_enabled_plugins('pg'); | |
46 | foreach ($plugins as $plugin) { | |
47 | $classname = '\pg_' . $plugin . '\gateway'; | |
48 | ||
49 | $currencies += $classname::get_supported_currencies(); | |
50 | } | |
51 | ||
52 | $currencies = array_unique($currencies); | |
53 | ||
54 | return $currencies; | |
55 | } | |
f3d75264 SR |
56 | |
57 | /** | |
58 | * Returns the list of gateways that can process payments in the given currency. | |
59 | * | |
60 | * @param string $currency The currency in the three-character ISO-4217 format. | |
61 | * @return string[] | |
62 | */ | |
63 | public static function get_gateways_for_currency(string $currency): array { | |
64 | $gateways = []; | |
65 | ||
66 | $plugins = \core_plugin_manager::instance()->get_enabled_plugins('pg'); | |
67 | foreach ($plugins as $plugin) { | |
68 | $classname = '\pg_' . $plugin . '\gateway'; | |
69 | ||
70 | $currencies = $classname::get_supported_currencies(); | |
71 | if (in_array($currency, $currencies)) { | |
72 | $gateways[] = $plugin; | |
73 | } | |
74 | } | |
75 | ||
76 | return $gateways; | |
77 | } | |
c2321a26 SR |
78 | |
79 | /** | |
80 | * Requires the JS libraries for the pay button. | |
81 | */ | |
82 | public static function gateways_modal_requirejs(): void { | |
83 | global $PAGE; | |
84 | ||
85 | static $done = false; | |
86 | if ($done) { | |
87 | return; | |
88 | } | |
89 | ||
90 | $PAGE->requires->js_call_amd('core_payment/gateways_modal', 'registerEventListeners', ['#gateways-modal-trigger']); | |
91 | $done = true; | |
92 | } | |
93 | ||
94 | /** | |
95 | * Returns the attributes to place on a pay button. | |
96 | * | |
97 | * @param float $amount Amount of payment | |
98 | * @param string $currency Currency of payment | |
ab6ca275 SR |
99 | * @param string $component Name of the component that the componentid belongs to |
100 | * @param int $componentid An internal identifier that is used by the component | |
101 | * @param string $description Description of the payment | |
c2321a26 SR |
102 | * @return array |
103 | */ | |
ab6ca275 SR |
104 | public static function gateways_modal_link_params(float $amount, string $currency, string $component, int $componentid, |
105 | string $description): array { | |
c2321a26 SR |
106 | return [ |
107 | 'id' => 'gateways-modal-trigger', | |
108 | 'role' => 'button', | |
109 | 'data-amount' => $amount, | |
110 | 'data-currency' => $currency, | |
ed04c382 SR |
111 | 'data-component' => $component, |
112 | 'data-componentid' => $componentid, | |
ab6ca275 | 113 | 'data-description' => $description, |
c2321a26 SR |
114 | ]; |
115 | } | |
5337ca48 SR |
116 | |
117 | /** | |
118 | * Asks the cost from the related component. | |
119 | * | |
120 | * @param string $component Name of the component that the componentid belongs to | |
121 | * @param int $componentid An internal identifier that is used by the component | |
122 | * @return array['amount' => float, 'currency' => string] | |
123 | * @throws \moodle_exception | |
124 | */ | |
125 | public static function get_cost(string $component, int $componentid): array { | |
126 | $cost = component_class_callback("$component\\payment\\provider", 'get_cost', [$componentid]); | |
127 | ||
128 | if ($cost === null) { | |
129 | throw new \moodle_exception('callbacknotimplemented', 'core_payment', '', $component); | |
130 | } | |
131 | ||
132 | return $cost; | |
133 | } | |
134 | ||
135 | /** | |
136 | * Delivers what the user paid for. | |
137 | * | |
138 | * @param string $component Name of the component that the componentid belongs to | |
139 | * @param int $componentid An internal identifier that is used by the component | |
140 | * @return bool Whether successful or not | |
141 | * @throws \moodle_exception | |
142 | */ | |
143 | public static function deliver_order(string $component, int $componentid): bool { | |
144 | $result = component_class_callback("$component\\payment\\provider", 'deliver_order', [$componentid]); | |
145 | ||
146 | if ($result === null) { | |
147 | throw new \moodle_exception('callbacknotimplemented', 'core_payment', '', $component); | |
148 | } | |
149 | ||
150 | return $result; | |
151 | } | |
4865d2a0 | 152 | } |