Skip to content

Commit

Permalink
Merge branch 'MDL-70287-master-3' of git://github.com/rezaies/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Mar 10, 2021
2 parents fc238f3 + 9317849 commit 2c0038f
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 22 deletions.
17 changes: 16 additions & 1 deletion enrol/fee/classes/payment/service_provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class service_provider implements \core_payment\local\callback\service_provider
* Callback function that returns the enrolment cost and the accountid
* for the course that $instanceid enrolment instance belongs to.
*
* @param string $paymentarea
* @param string $paymentarea Payment area
* @param int $instanceid The enrolment instance id
* @return \core_payment\local\entities\payable
*/
Expand All @@ -49,6 +49,21 @@ public static function get_payable(string $paymentarea, int $instanceid): \core_
return new \core_payment\local\entities\payable($instance->cost, $instance->currency, $instance->customint1);
}

/**
* Callback function that returns the URL of the page the user should be redirected to in the case of a successful payment.
*
* @param string $paymentarea Payment area
* @param int $instanceid The enrolment instance id
* @return \moodle_url
*/
public static function get_success_url(string $paymentarea, int $instanceid): \moodle_url {
global $DB;

$courseid = $DB->get_field('enrol', 'courseid', ['enrol' => 'fee', 'id' => $instanceid], MUST_EXIST);

return new \moodle_url('/course/view.php', ['id' => $courseid]);
}

/**
* Callback function that delivers what the user paid for to them.
*
Expand Down
1 change: 1 addition & 0 deletions enrol/fee/classes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public function enrol_page_hook(stdClass $instance) {
'instanceid' => $instance->id,
'description' => get_string('purchasedescription', 'enrol_fee',
format_string($course->fullname, true, ['context' => $context])),
'successurl' => \enrol_fee\payment\service_provider::get_success_url('fee', $instance->id)->out(false),
];
echo $OUTPUT->render_from_template('enrol_fee/payment_region', $data);
}
Expand Down
4 changes: 4 additions & 0 deletions enrol/fee/templates/payment_region.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@
* data-itemid
* data-cost
* data-description
* data-successurl
Context variables required for this template:
* cost - Human readable cost string including amount and currency
* instanceid - Id of the enrolment instance
* description - The description for this purchase
* successurl - The URL of the course
Example context (json):
{
"cost": "$108.50",
"instanceid": 11,
"description": "Enrolment in course Introduction to algorithms",
"successurl": "https://moodlesite/course/view.php?id=2",
"isguestuser": false
}

Expand All @@ -63,6 +66,7 @@
data-paymentarea="fee"
data-itemid="{{instanceid}}"
data-cost="{{cost}}"
data-successurl="{{successurl}}"
data-description={{# quote }}{{description}}{{/ quote }}
>
{{# str }} sendpaymentbutton, enrol_fee {{/ str }}
Expand Down
133 changes: 133 additions & 0 deletions enrol/fee/tests/payment/service_provider_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for the enrol_fee's payment subsystem callback implementation.
*
* @package enrol_fee
* @category test
* @copyright 2021 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace enrol_fee\payment;

/**
* Unit tests for the enrol_fee's payment subsystem callback implementation.
*
* @coversDefaultClass service_provider
*/
class service_provider_testcase extends \advanced_testcase {

/**
* Test for service_provider::get_payable().
*
* @covers ::get_payable
*/
public function test_get_payable() {
global $DB;
$this->resetAfterTest();

$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$feeplugin = enrol_get_plugin('fee');
$generator = $this->getDataGenerator();
$account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']);
$course = $generator->create_course();

$data = [
'courseid' => $course->id,
'customint1' => $account->get('id'),
'cost' => 250,
'currency' => 'USD',
'roleid' => $studentrole->id,
];
$id = $feeplugin->add_instance($course, $data);

$payable = service_provider::get_payable('fee', $id);

$this->assertEquals($account->get('id'), $payable->get_account_id());
$this->assertEquals(250, $payable->get_amount());
$this->assertEquals('USD', $payable->get_currency());
}

/**
* Test for service_provider::get_success_url().
*
* @covers ::get_success_url
*/
public function test_get_success_url() {
global $CFG, $DB;
$this->resetAfterTest();

$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$feeplugin = enrol_get_plugin('fee');
$generator = $this->getDataGenerator();
$account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']);
$course = $generator->create_course();

$data = [
'courseid' => $course->id,
'customint1' => $account->get('id'),
'cost' => 250,
'currency' => 'USD',
'roleid' => $studentrole->id,
];
$id = $feeplugin->add_instance($course, $data);

$successurl = service_provider::get_success_url('fee', $id);
$this->assertEquals(
$CFG->wwwroot . '/course/view.php?id=' . $course->id,
$successurl->out(false)
);
}

/**
* Test for service_provider::deliver_order().
*
* @covers ::deliver_order
*/
public function test_deliver_order() {
global $DB;
$this->resetAfterTest();

$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$feeplugin = enrol_get_plugin('fee');
$generator = $this->getDataGenerator();
$account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']);
$course = $generator->create_course();
$context = \context_course::instance($course->id);
$user = $generator->create_user();

$data = [
'courseid' => $course->id,
'customint1' => $account->get('id'),
'cost' => 250,
'currency' => 'USD',
'roleid' => $studentrole->id,
];
$id = $feeplugin->add_instance($course, $data);

$paymentid = $generator->get_plugin_generator('core_payment')->create_payment([
'accountid' => $account->get('id'),
'amount' => 10,
'userid' => $user->id
]);

service_provider::deliver_order('fee', $id, $paymentid, $user->id);
$this->assertTrue(is_enrolled($context, $user));
$this->assertTrue(user_has_role_assignment($user->id, $studentrole->id, $context->id));
}
}
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.

Loading

0 comments on commit 2c0038f

Please sign in to comment.