MDL-69166 enrol_fee: Show a warning when no currency is supported
[moodle.git] / payment / classes / helper.php
CommitLineData
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
25namespace core_payment;
26
27defined('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 */
35class 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 78
1882bb47
SR
79 /**
80 * Returns the percentage of surcharge that is applied when using a gateway
81 *
82 * @param string $gateway Name of the gateway
83 * @return int
84 */
85 public static function get_gateway_surcharge(string $gateway): int {
86 return get_config('pg_' . $gateway, 'surcharge') ?: 0;
87 }
88
c2321a26
SR
89 /**
90 * Returns the attributes to place on a pay button.
91 *
92 * @param float $amount Amount of payment
93 * @param string $currency Currency of payment
ab6ca275
SR
94 * @param string $component Name of the component that the componentid belongs to
95 * @param int $componentid An internal identifier that is used by the component
96 * @param string $description Description of the payment
c2321a26
SR
97 * @return array
98 */
ab6ca275
SR
99 public static function gateways_modal_link_params(float $amount, string $currency, string $component, int $componentid,
100 string $description): array {
c2321a26
SR
101 return [
102 'id' => 'gateways-modal-trigger',
103 'role' => 'button',
104 'data-amount' => $amount,
105 'data-currency' => $currency,
ed04c382
SR
106 'data-component' => $component,
107 'data-componentid' => $componentid,
ab6ca275 108 'data-description' => $description,
c2321a26
SR
109 ];
110 }
5337ca48
SR
111
112 /**
113 * Asks the cost from the related component.
114 *
115 * @param string $component Name of the component that the componentid belongs to
116 * @param int $componentid An internal identifier that is used by the component
117 * @return array['amount' => float, 'currency' => string]
118 * @throws \moodle_exception
119 */
120 public static function get_cost(string $component, int $componentid): array {
121 $cost = component_class_callback("$component\\payment\\provider", 'get_cost', [$componentid]);
122
123 if ($cost === null) {
124 throw new \moodle_exception('callbacknotimplemented', 'core_payment', '', $component);
125 }
126
127 return $cost;
128 }
129
130 /**
131 * Delivers what the user paid for.
132 *
133 * @param string $component Name of the component that the componentid belongs to
134 * @param int $componentid An internal identifier that is used by the component
135 * @return bool Whether successful or not
136 * @throws \moodle_exception
137 */
138 public static function deliver_order(string $component, int $componentid): bool {
139 $result = component_class_callback("$component\\payment\\provider", 'deliver_order', [$componentid]);
140
141 if ($result === null) {
142 throw new \moodle_exception('callbacknotimplemented', 'core_payment', '', $component);
143 }
144
145 return $result;
146 }
3c3b43a5
SR
147
148 /**
149 * Stores essential information about the payment and returns the "id" field of the payment record in DB.
150 * Each payment gateway may then store the additional information their way.
151 *
152 * @param string $component Name of the component that the componentid belongs to
153 * @param int $componentid An internal identifier that is used by the component
154 * @param int $userid Id of the user who is paying
155 * @param float $amount Amount of payment
156 * @param string $currency Currency of payment
157 * @param string $gateway The gateway that is used for the payment
158 * @return int
159 */
160 public static function save_payment(string $component, int $componentid, int $userid, float $amount, string $currency,
161 string $gateway): int {
162 global $DB;
163
164 $record = new \stdClass();
165 $record->component = $component;
166 $record->componentid = $componentid;
167 $record->userid = $userid;
168 $record->amount = $amount;
169 $record->currency = $currency;
170 $record->gateway = $gateway;
171 $record->timecreated = $record->timemodified = time();
172
173 $id = $DB->insert_record('payments', $record);
174
175 return $id;
176 }
1882bb47
SR
177
178 /**
179 * This functions adds the settings that are common for all payment gateways.
180 *
181 * @param \admin_settingpage $settings The settings object
182 * @param string $gateway The gateway name prefic with pg_
183 */
184 public static function add_common_gateway_settings(\admin_settingpage $settings, string $gateway): void {
185 $settings->add(new \admin_setting_configtext($gateway . '/surcharge', get_string('surcharge', 'core_payment'),
186 get_string('surcharge_desc', 'core_payment'), 0, PARAM_INT));
187
188 }
4865d2a0 189}