Skip to content

Commit

Permalink
MDL-69166 core_payment: Addressing various integration points
Browse files Browse the repository at this point in the history
- Add help for 'payment account' field in the enrol instance form
- Remove MOODLE_INTERNALs when not necessary
- Add $userid to deliver_order
- Check if provider classes implement the provider interface
- Rename get_cost to get_payable
- get_payable returns payable object
- Improve registerEventListeners and added init
- Rename payment\provider to payment\service_provider
  • Loading branch information
rezaies committed Oct 27, 2020
1 parent d5a9d6e commit f5d94d6
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 92 deletions.
Expand Up @@ -31,25 +31,22 @@
* @copyright 2020 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_payment\local\callback\provider {
class service_provider implements \core_payment\local\callback\service_provider {

/**
* Callback function that returns the enrolment cost for the course that $instanceid enrolment instance belongs to.
* Callback function that returns the enrolment cost and the accountid
* for the course that $instanceid enrolment instance belongs to.
*
* @param string $paymentarea
* @param int $instanceid The enrolment instance id
* @return array['amount' => float, 'currency' => string, 'accountid' => int]
* @return \core_payment\local\entities\payable
*/
public static function get_cost(string $paymentarea, int $instanceid): array {
public static function get_payable(string $paymentarea, int $instanceid): \core_payment\local\entities\payable {
global $DB;

$instance = $DB->get_record('enrol', ['enrol' => 'fee', 'id' => $instanceid], '*', MUST_EXIST);

return [
'amount' => (float) $instance->cost,
'currency' => $instance->currency,
'accountid' => $instance->customint1,
];
return new \core_payment\local\entities\payable($instance->cost, $instance->currency, $instance->customint1);
}

/**
Expand All @@ -58,10 +55,11 @@ public static function get_cost(string $paymentarea, int $instanceid): array {
* @param string $paymentarea
* @param int $instanceid The enrolment instance id
* @param int $paymentid payment id as inserted into the 'payments' table, if needed for reference
* @param int $userid The userid the order is going to deliver to
* @return bool Whether successful or not
*/
public static function deliver_order(string $paymentarea, int $instanceid, int $paymentid): bool {
global $DB, $USER;
public static function deliver_order(string $paymentarea, int $instanceid, int $paymentid, int $userid): bool {
global $DB;

$instance = $DB->get_record('enrol', ['enrol' => 'fee', 'id' => $instanceid], '*', MUST_EXIST);

Expand All @@ -75,7 +73,7 @@ public static function deliver_order(string $paymentarea, int $instanceid, int $
$timeend = 0;
}

$plugin->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend);
$plugin->enrol_user($instance, $userid, $instance->roleid, $timestart, $timeend);

return true;
}
Expand Down
1 change: 1 addition & 0 deletions enrol/fee/classes/plugin.php
Expand Up @@ -313,6 +313,7 @@ public function edit_instance_form($instance, MoodleQuickForm $mform, $context)
$mform->addElement('hidden', 'customint1');
$mform->setType('customint1', PARAM_INT);
}
$mform->addHelpButton('customint1', 'paymentaccount', 'enrol_fee');

$mform->addElement('text', 'cost', get_string('cost', 'enrol_fee'), array('size' => 4));
$mform->setType('cost', PARAM_RAW);
Expand Down
2 changes: 2 additions & 0 deletions enrol/fee/lang/en/enrol_fee.php
Expand Up @@ -43,6 +43,8 @@
$string['fee:unenrol'] = 'Unenrol users from course';
$string['fee:unenrolself'] = 'Unenrol self from course';
$string['nocost'] = 'There is no cost to enrol in this course!';
$string['paymentaccount'] = 'Payment account';
$string['paymentaccount_help'] = 'Enrolment fees will be paid to this account.';
$string['pluginname'] = 'Enrolment on payment';
$string['pluginname_desc'] = 'The enrolment on payment enrolment method allows you to set up courses requiring a payment. If the fee for any course is set to zero, then students are not asked to pay for entry. There is a site-wide fee that you set here as a default for the whole site and then a course setting that you can set for each course individually. The course fee overrides the site fee.';
$string['purchasedescription'] = 'Enrolment in course {$a}';
Expand Down
3 changes: 2 additions & 1 deletion enrol/fee/templates/payment_region.mustache
Expand Up @@ -58,6 +58,7 @@
class="btn btn-secondary"
type="button"
id="gateways-modal-trigger-{{ uniqid }}"
data-action="core_payment/triggerPayment"
data-component="enrol_fee"
data-paymentarea="fee"
data-itemid="{{instanceid}}"
Expand All @@ -70,6 +71,6 @@
</div>
{{#js}}
require(['core_payment/gateways_modal'], function(modal) {
modal.registerEventListeners(document.querySelector('#gateways-modal-trigger-{{ uniqid }}'));
modal.init();
});
{{/js}}
1 change: 0 additions & 1 deletion lang/en/payment.php
Expand Up @@ -31,7 +31,6 @@
$string['accountname_help'] = 'How this account will be identified for teachers or managers who set up payments (for example in the course enrolment plugin)';
$string['accountnotavailable'] = 'Not available';
$string['paymentaccountsexplained'] = 'Create one or multiple payment accounts for this site. Each account includes configuration for available payment gateways. The person who configures payments on the site (for example, payment for the course enrolment) will be able to chose from the available accounts.';
$string['callbacknotimplemented'] = 'The callback is not implemented for component {$a}.';
$string['createaccount'] = 'Create payment account';
$string['deleteorarchive'] = 'Delete or archive';
$string['eventaccountcreated'] = 'Payment account created';
Expand Down
2 changes: 1 addition & 1 deletion payment/amd/build/gateways_modal.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f5d94d6

Please sign in to comment.