Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Buda9 committed Sep 22, 2024
1 parent 3a6aec2 commit d5ac8ba
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
21 changes: 14 additions & 7 deletions classes/util/balance_op.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ public function apply_referral_on_topup($amount) {
debugging("Entering apply_referral_on_topup method. User ID: {$this->userid}, Amount: $amount", DEBUG_DEVELOPER);

$referralenabled = get_config('enrol_wallet', 'referral_on_topup');
debugging("Referral on top-up enabled: " . ($referralenabled ? 'Yes' : 'No'), DEBUG_DEVELOPER);

if (!$referralenabled) {
debugging("Referral on top-up is not enabled.", DEBUG_DEVELOPER);
return false;
Expand All @@ -257,11 +259,11 @@ public function apply_referral_on_topup($amount) {
$referralamount = (float)get_config('enrol_wallet', 'referral_amount');
$minimumtopup = (float)get_config('enrol_wallet', 'referral_topup_minimum');

debugging("Attempting to apply referral. User ID: {$this->userid}, Amount: $amount, Minimum: $minimumtopup", DEBUG_DEVELOPER);
debugging("Referral amount: $referralamount, Minimum top-up: $minimumtopup", DEBUG_DEVELOPER);

// Check if the top-up amount meets the minimum requirement.
if ($amount < $minimumtopup) {
debugging("Top-up amount less than minimum. Exiting.", DEBUG_DEVELOPER);
debugging("Top-up amount ($amount) less than minimum ($minimumtopup). Exiting.", DEBUG_DEVELOPER);
return false;
}

Expand All @@ -286,13 +288,18 @@ public function apply_referral_on_topup($amount) {
debugging("Credited referred user. Result: " . var_export($referredResult, true), DEBUG_DEVELOPER);

// Mark the referral as released.
$DB->set_field('enrol_wallet_hold_gift', 'released', 1, ['id' => $hold->id]);
$DB->set_field('enrol_wallet_hold_gift', 'timemodified', time(), ['id' => $hold->id]);
debugging("Marked referral as released.", DEBUG_DEVELOPER);
$updateData = [
'id' => $hold->id,
'released' => 1,
'timemodified' => time(),
'courseid' => $hold->courseid ?? SITEID // Use SITEID (usually 1) if courseid is null
];
$updateResult = $DB->update_record('enrol_wallet_hold_gift', $updateData);
debugging("Marked referral as released. Update result: " . var_export($updateResult, true), DEBUG_DEVELOPER);

return true;
} else {
debugging("No unreleased hold gift found for user.", DEBUG_DEVELOPER);
debugging("No unreleased hold gift found for user {$this->userid}. SQL: SELECT * FROM {enrol_wallet_hold_gift} WHERE referred = {$this->userid} AND released = 0", DEBUG_DEVELOPER);
return false;
}
}
Expand Down Expand Up @@ -1068,4 +1075,4 @@ public static function create_from_section($section, $userid = 0) {
$category = $util->get_course_category();
return new self($userid, $category);
}
}
}
58 changes: 45 additions & 13 deletions extra/topup.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
$val = optional_param('value', 0, PARAM_FLOAT);
$category = optional_param('category', 0, PARAM_INT);
$return = optional_param('return', '', PARAM_LOCALURL);
$success = optional_param('success', 0, PARAM_BOOL);

debugging("Parameters: instanceid=$instanceid, courseid=$courseid, confirm=$confirm, account=$account, currency=$currency, val=$val, category=$category", DEBUG_DEVELOPER);
debugging("Parameters: instanceid=$instanceid, courseid=$courseid, confirm=$confirm, account=$account, currency=$currency, val=$val, category=$category, success=$success", DEBUG_DEVELOPER);

$urlparams = [
'instanceid' => $instanceid,
Expand Down Expand Up @@ -80,20 +81,51 @@

require_login();

if ($confirm) {
debugging("Payment confirmed, applying referral bonus", DEBUG_DEVELOPER);
// Payment has been confirmed, apply the referral bonus if applicable
$balance_op = new \enrol_wallet\util\balance_op($USER->id);
$result = $balance_op->apply_referral_on_topup($value);
debugging("Checking hold gift record for user {$USER->id}", DEBUG_DEVELOPER);
$hold = $DB->get_record('enrol_wallet_hold_gift', ['referred' => $USER->id, 'released' => 0]);
if ($hold) {
debugging("Found hold gift record: " . var_export($hold, true), DEBUG_DEVELOPER);
} else {
debugging("No hold gift record found for user {$USER->id}", DEBUG_DEVELOPER);
}

$referralenabled = get_config('enrol_wallet', 'referral_on_topup');
$referralamount = (float)get_config('enrol_wallet', 'referral_amount');
$minimumtopup = (float)get_config('enrol_wallet', 'referral_topup_minimum');
debugging("Referral config: enabled={$referralenabled}, amount={$referralamount}, minimum={$minimumtopup}", DEBUG_DEVELOPER);

if ($success) {
debugging("Payment successful, checking referral eligibility", DEBUG_DEVELOPER);

if ($result) {
debugging("Referral bonus applied successfully", DEBUG_DEVELOPER);
\core\notification::success(get_string('referral_success', 'enrol_wallet'));
// Check if referral on top-up is enabled
$referralenabled = get_config('enrol_wallet', 'referral_on_topup');
debugging("Referral on top-up enabled: " . ($referralenabled ? 'Yes' : 'No'), DEBUG_DEVELOPER);

if (!$referralenabled) {
debugging("Referral on top-up is not enabled. Skipping referral bonus application.", DEBUG_DEVELOPER);
} else {
debugging("Referral bonus not applied", DEBUG_DEVELOPER);
\core\notification::info(get_string('referral_not_applied', 'enrol_wallet'));
// Check if the user is eligible for a referral bonus
$hold = $DB->get_record('enrol_wallet_hold_gift', ['referred' => $USER->id, 'released' => 0]);

if ($hold) {
debugging("User is eligible for referral bonus. Referrer ID: {$hold->referrer}", DEBUG_DEVELOPER);

// Payment has been successful, apply the referral bonus if applicable
$balance_op = new \enrol_wallet\util\balance_op($USER->id);
$result = $balance_op->apply_referral_on_topup($value);

if ($result) {
debugging("Referral bonus applied successfully", DEBUG_DEVELOPER);
\core\notification::success(get_string('referral_success', 'enrol_wallet'));
} else {
debugging("Referral bonus not applied", DEBUG_DEVELOPER);
\core\notification::info(get_string('referral_not_applied', 'enrol_wallet'));
}
} else {
debugging("User is not eligible for referral bonus. No unreleased hold gift found.", DEBUG_DEVELOPER);
}
}

debugging("Redirecting to: " . $url, DEBUG_DEVELOPER);
redirect($url);
} else {
debugging("Displaying payment confirmation page", DEBUG_DEVELOPER);
Expand Down Expand Up @@ -126,7 +158,7 @@
'data-paymentarea' => "wallettopup",
'data-itemid' => "$id",
'data-cost' => "$value",
'data-successurl' => $baseurl->out(false, ['confirm' => 1, 'sesskey' => sesskey()]),
'data-successurl' => $baseurl->out(false, ['success' => 1, 'sesskey' => sesskey()]),
'data-description' => "$desc",
];

Expand Down

0 comments on commit d5ac8ba

Please sign in to comment.