From 05574af1a675a48b64f3caa16e533395db174331 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Thu, 9 Jan 2020 01:43:51 +1100 Subject: [PATCH] MDL-69166 enrol_fee: Implement callback functions for payment subsystem --- enrol/fee/classes/payment/provider.php | 78 ++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 enrol/fee/classes/payment/provider.php diff --git a/enrol/fee/classes/payment/provider.php b/enrol/fee/classes/payment/provider.php new file mode 100644 index 00000000000..56e40e2ff18 --- /dev/null +++ b/enrol/fee/classes/payment/provider.php @@ -0,0 +1,78 @@ +. + +/** + * Payment subsystem callback implementation for enrol_fee. + * + * @package enrol_fee + * @category payment + * @copyright 2020 Shamim Rezaie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace enrol_fee\payment; + +/** + * Payment subsystem callback implementation for enrol_fee. + * + * @copyright 2020 Shamim Rezaie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class provider implements \core_payment\local\callback\provider { + + /** + * Callback function that returns the enrolment cost for the course that $instanceid enrolment instance belongs to. + * + * @param int $instanceid The enrolment instance id + * @return array['amount' => float, 'currency' => string] + */ + public static function get_cost(int $instanceid): array { + global $DB; + + $instance = $DB->get_record('enrol', ['enrol' => 'fee', 'id' => $instanceid], '*', MUST_EXIST); + + return [ + 'amount' => (float) $instance->cost, + 'currency' => $instance->currency, + ]; + } + + /** + * Callback function that delivers what the user paid for to them. + * + * @param int $instanceid The enrolment instance id + * @return bool Whether successful or not + */ + public static function deliver_order(int $instanceid): bool { + global $DB, $USER; + + $instance = $DB->get_record('enrol', ['enrol' => 'fee', 'id' => $instanceid], '*', MUST_EXIST); + + $plugin = enrol_get_plugin('fee'); + + if ($instance->enrolperiod) { + $timestart = time(); + $timeend = $timestart + $instance->enrolperiod; + } else { + $timestart = 0; + $timeend = 0; + } + + $plugin->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend); + + return true; + } +}