MDL-69166 core_payment: Add paymentarea to the payment subsystem
[moodle.git] / enrol / fee / classes / payment / provider.php
CommitLineData
05574af1
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 * Payment subsystem callback implementation for enrol_fee.
19 *
20 * @package enrol_fee
21 * @category payment
22 * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace enrol_fee\payment;
27
28/**
29 * Payment subsystem callback implementation for enrol_fee.
30 *
31 * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class provider implements \core_payment\local\callback\provider {
35
36 /**
37 * Callback function that returns the enrolment cost for the course that $instanceid enrolment instance belongs to.
38 *
7d10f352 39 * @param string $paymentarea
05574af1 40 * @param int $instanceid The enrolment instance id
895f38cc 41 * @return array['amount' => float, 'currency' => string, 'accountid' => int]
05574af1 42 */
7d10f352 43 public static function get_cost(string $paymentarea, int $instanceid): array {
05574af1
SR
44 global $DB;
45
46 $instance = $DB->get_record('enrol', ['enrol' => 'fee', 'id' => $instanceid], '*', MUST_EXIST);
47
48 return [
49 'amount' => (float) $instance->cost,
50 'currency' => $instance->currency,
895f38cc 51 'accountid' => $instance->customint1,
05574af1
SR
52 ];
53 }
54
55 /**
56 * Callback function that delivers what the user paid for to them.
57 *
7d10f352 58 * @param string $paymentarea
05574af1 59 * @param int $instanceid The enrolment instance id
15a4e4c8 60 * @param int $paymentid payment id as inserted into the 'payments' table, if needed for reference
05574af1
SR
61 * @return bool Whether successful or not
62 */
7d10f352 63 public static function deliver_order(string $paymentarea, int $instanceid, int $paymentid): bool {
05574af1
SR
64 global $DB, $USER;
65
66 $instance = $DB->get_record('enrol', ['enrol' => 'fee', 'id' => $instanceid], '*', MUST_EXIST);
67
68 $plugin = enrol_get_plugin('fee');
69
70 if ($instance->enrolperiod) {
71 $timestart = time();
72 $timeend = $timestart + $instance->enrolperiod;
73 } else {
74 $timestart = 0;
75 $timeend = 0;
76 }
77
78 $plugin->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
79
80 return true;
81 }
82}