diff --git a/classes/payment/service_provider.php b/classes/payment/service_provider.php index f4277ab4..c4abedd0 100644 --- a/classes/payment/service_provider.php +++ b/classes/payment/service_provider.php @@ -47,9 +47,7 @@ public static function get_payable(string $paymentarea, int $itemid): \core_paym global $DB, $USER; // Get the fake item in case of topping up the wallet. - if (!$item = $DB->get_record('enrol_wallet_items', ['id' => $itemid]) && $paymentarea == 'walletenrol') { - $item = $DB->get_record('enrol', ['id'=> $itemid]); // Legacy. - } + $item = $DB->get_record('enrol_wallet_items', ['id' => $itemid], '*', MUST_EXIST); // In this case we get the default settings. $account = get_config('enrol_wallet', 'paymentaccount'); @@ -66,14 +64,11 @@ public static function get_payable(string $paymentarea, int $itemid): \core_paym */ public static function get_success_url(string $paymentarea, int $itemid): \moodle_url { global $DB; - if (!$item = $DB->get_record('enrol_wallet_items', ['id' => $itemid]) && $paymentarea == 'walletenrol') { - $item = $DB->get_record('enrol', ['id'=> $itemid], '*', MUST_EXIST); // Legacy. - } + $item = $DB->get_record('enrol_wallet_items', ['id' => $itemid], '*', IGNORE_MISSING); // Check if the payment is for enrolment or topping up the wallet. if ($paymentarea == 'walletenrol' && $item) { - if (!$courseid = $item->courseid ?? false) { // Legacy. - $courseid = $DB->get_field('enrol', 'courseid', ['enrol' => 'wallet', 'id' => $item->instanceid], MUST_EXIST); - } + + $courseid = $DB->get_field('enrol', 'courseid', ['enrol' => 'wallet', 'id' => $item->instanceid], MUST_EXIST); return new \moodle_url('/course/view.php', ['id' => $courseid]); @@ -96,9 +91,7 @@ public static function deliver_order(string $paymentarea, int $itemid, int $paym global $DB, $CFG; require_once($CFG->dirroot.'/enrol/wallet/lib.php'); // Get the fake item in case of topping up the wallet. - if (!$item = $DB->get_record('enrol_wallet_items', ['id' => $itemid]) && $paymentarea == 'walletenrol') { - $item = $DB->get_record('enrol', ['id' => $itemid], '*', MUST_EXIST); - } + $item = $DB->get_record('enrol_wallet_items', ['id' => $itemid], '*', MUST_EXIST); // Check if the payment is for enrolment or topping up the wallet. if ($paymentarea == 'walletenrol') { diff --git a/lib.php b/lib.php index ec75d2c5..259c1933 100644 --- a/lib.php +++ b/lib.php @@ -2031,6 +2031,10 @@ public static function show_payment_info(stdClass $instance, $fee) { return ''; } + if (!class_exists('\core_payment\helper')) { + return ''; + } + $balance = (float)transactions::get_user_balance($USER->id); $cost = (float)$fee - $balance; @@ -2054,7 +2058,10 @@ public static function show_payment_info(stdClass $instance, $fee) { 'userid' => $USER->id, 'instanceid' => $instance->id, ]; - $id = $DB->insert_record('enrol_wallet_items', $payrecord); + if (!$id = $DB->get_field('enrol_wallet_items', 'id', $payrecord, IGNORE_MULTIPLE)) { + $id = $DB->insert_record('enrol_wallet_items', $payrecord); + } + $data = [ 'isguestuser' => isguestuser() || !isloggedin(), 'cost' => \core_payment\helper::get_cost_as_string($cost, $instance->currency), diff --git a/tests/payment/service_provider_test.php b/tests/payment/service_provider_test.php index 5a79a66e..5fbe0578 100644 --- a/tests/payment/service_provider_test.php +++ b/tests/payment/service_provider_test.php @@ -48,7 +48,7 @@ public function test_get_payable_walletenrol(): void { $generator = $this->getDataGenerator(); $account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']); $course = $generator->create_course(); - + $user = $generator->create_user(); $data = [ 'courseid' => $course->id, 'customint1' => $account->get('id'), @@ -57,8 +57,16 @@ public function test_get_payable_walletenrol(): void { 'roleid' => $studentrole->id, ]; $id = $walletplugin->add_instance($course, $data); - - $payable = service_provider::get_payable('walletenrol', $id); + $payrecord = [ + 'cost' => 250, + 'currency' => 'USD', + 'userid' => $user->id, + 'instanceid' => $id, + ]; + if (!$itemid = $DB->get_field('enrol_wallet_items', 'id', $payrecord, IGNORE_MULTIPLE)) { + $itemid = $DB->insert_record('enrol_wallet_items', $payrecord); + } + $payable = service_provider::get_payable('walletenrol', $itemid); $this->assertEquals($account->get('id'), $payable->get_account_id()); $this->assertEquals(250, $payable->get_amount()); @@ -107,6 +115,7 @@ public function test_get_success_url_walletenrol(): void { $generator = $this->getDataGenerator(); $account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']); $course = $generator->create_course(); + $user = $generator->create_user(); $data = [ 'courseid' => $course->id, @@ -116,8 +125,16 @@ public function test_get_success_url_walletenrol(): void { 'roleid' => $studentrole->id, ]; $id = $walletplugin->add_instance($course, $data); - - $successurl = service_provider::get_success_url('walletenrol', $id); + $payrecord = [ + 'cost' => 250, + 'currency' => 'USD', + 'userid' => $user->id, + 'instanceid' => $id, + ]; + if (!$itemid = $DB->get_field('enrol_wallet_items', 'id', $payrecord, IGNORE_MULTIPLE)) { + $itemid = $DB->insert_record('enrol_wallet_items', $payrecord); + } + $successurl = service_provider::get_success_url('walletenrol', $itemid); $this->assertEquals( $CFG->wwwroot . '/course/view.php?id=' . $course->id, $successurl->out(false) @@ -173,7 +190,8 @@ public function test_deliver_order_walletenrol(): void { $course = $generator->create_course(); $context = \context_course::instance($course->id); $user = $generator->create_user(); - + $this->setUser($user); + global $USER; $data = [ 'courseid' => $course->id, 'customint1' => $account->get('id'), @@ -182,14 +200,22 @@ public function test_deliver_order_walletenrol(): void { 'roleid' => $studentrole->id, ]; $id = $walletplugin->add_instance($course, $data); - + $payrecord = [ + 'cost' => 250, + 'currency' => 'USD', + 'userid' => $USER->id, + 'instanceid' => $id, + ]; + if (!$itemid = $DB->get_field('enrol_wallet_items', 'id', $payrecord, IGNORE_MULTIPLE)) { + $itemid = $DB->insert_record('enrol_wallet_items', $payrecord); + } $paymentid = $generator->get_plugin_generator('core_payment')->create_payment([ 'accountid' => $account->get('id'), 'amount' => 10, 'userid' => $user->id, ]); - service_provider::deliver_order('walletenrol', $id, $paymentid, $user->id); + service_provider::deliver_order('walletenrol', $itemid, $paymentid, $user->id); $this->assertTrue(is_enrolled($context, $user)); $this->assertTrue(user_has_role_assignment($user->id, $studentrole->id, $context->id)); }