Skip to content

Commit

Permalink
rollback payment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fmido88 authored Nov 14, 2023
1 parent bee6bf6 commit f528c5c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
17 changes: 5 additions & 12 deletions classes/payment/service_provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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]);

Expand All @@ -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') {
Expand Down
9 changes: 8 additions & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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),
Expand Down
42 changes: 34 additions & 8 deletions tests/payment/service_provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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());
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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'),
Expand All @@ -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));
}
Expand Down

0 comments on commit f528c5c

Please sign in to comment.