From 51b73e431ee2ae5b4a3a5358349e44024730a2b1 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Thu, 3 Dec 2020 15:55:11 +1100 Subject: [PATCH] MDL-70287 core_payment: Add get_success_url to service_provider --- .../fee/classes/payment/service_provider.php | 15 ++ .../tests/payment/service_provider_test.php | 133 ++++++++++++++++++ payment/classes/helper.php | 13 ++ .../local/callback/service_provider.php | 9 ++ payment/upgrade.txt | 5 + 5 files changed, 175 insertions(+) create mode 100644 enrol/fee/tests/payment/service_provider_test.php create mode 100644 payment/upgrade.txt diff --git a/enrol/fee/classes/payment/service_provider.php b/enrol/fee/classes/payment/service_provider.php index 02497be1cfe9b..8490b6d102b58 100644 --- a/enrol/fee/classes/payment/service_provider.php +++ b/enrol/fee/classes/payment/service_provider.php @@ -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. * diff --git a/enrol/fee/tests/payment/service_provider_test.php b/enrol/fee/tests/payment/service_provider_test.php new file mode 100644 index 0000000000000..80783ff8800ec --- /dev/null +++ b/enrol/fee/tests/payment/service_provider_test.php @@ -0,0 +1,133 @@ +. + +/** + * Unit tests for the enrol_fee's payment subsystem callback implementation. + * + * @package enrol_fee + * @category test + * @copyright 2021 Shamim Rezaie + * @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)); + } +} diff --git a/payment/classes/helper.php b/payment/classes/helper.php index 39c4343d82923..43a205733ffff 100644 --- a/payment/classes/helper.php +++ b/payment/classes/helper.php @@ -198,6 +198,19 @@ public static function get_payable(string $component, string $paymentarea, int $ return component_class_callback($providerclass, 'get_payable', [$paymentarea, $itemid]); } + /** + * Fetches the URL of the page the user should be redirected to from the related component + * + * @param string $component Name of the component that the paymentarea and itemid belong to + * @param string $paymentarea Payment area + * @param int $itemid An identifier that is known to the component + * @return \moodle_url + */ + public static function get_success_url(string $component, string $paymentarea, int $itemid): \moodle_url { + $providerclass = static::get_service_provider_classname($component); + return component_class_callback($providerclass, 'get_success_url', [$paymentarea, $itemid]); + } + /** * Returns the gateway configuration for given component and gateway * diff --git a/payment/classes/local/callback/service_provider.php b/payment/classes/local/callback/service_provider.php index ba91668db6f5e..9bd56478401a0 100644 --- a/payment/classes/local/callback/service_provider.php +++ b/payment/classes/local/callback/service_provider.php @@ -44,6 +44,15 @@ interface service_provider { */ public static function get_payable(string $paymentarea, int $itemid): \core_payment\local\entities\payable; + /** + * 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 $itemid An identifier that is known to the plugin + * @return \moodle_url + */ + public static function get_success_url(string $paymentarea, int $itemid): \moodle_url; + /** * Callback function that delivers what the user paid for to them. * diff --git a/payment/upgrade.txt b/payment/upgrade.txt new file mode 100644 index 0000000000000..04adc193e2bb8 --- /dev/null +++ b/payment/upgrade.txt @@ -0,0 +1,5 @@ +This files describes API changes in /payment/*, +information provided here is intended especially for developers. + +=== 3.11 === +* Service provider plugins using the payment subsystem are now required to implement the get_success_url method.