|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +/** |
| 18 | + * Listens payment result callback from WeePay |
| 19 | + * |
| 20 | + * @package enrol_weepaypayment |
| 21 | + * @copyright 2019 Dualcube Team |
| 22 | + * @copyright 2021 WebTech Bilişim |
| 23 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 24 | + */ |
| 25 | + |
| 26 | +// Disable moodle specific debug messages and any errors in output, |
| 27 | +// comment out when debugging or better look into error log! |
| 28 | +define('NO_DEBUG_DISPLAY', true); |
| 29 | + |
| 30 | +require("../../config.php"); |
| 31 | +require_once("lib.php"); |
| 32 | +require_once($CFG->libdir . '/enrollib.php'); |
| 33 | +require_once($CFG->libdir . '/filelib.php'); |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * Send payment error message to the admin. |
| 38 | + * |
| 39 | + * @param string $subject |
| 40 | + * @param stdClass $data |
| 41 | + */ |
| 42 | +function message_weepaypayment_error_to_admin($subject, $data) |
| 43 | +{ |
| 44 | + die($subject); |
| 45 | + $admin = get_admin(); |
| 46 | + $site = get_site(); |
| 47 | + |
| 48 | + $message = "$site->fullname: Transaction failed.\n\n$subject\n\n"; |
| 49 | + |
| 50 | + foreach ($data as $key => $value) { |
| 51 | + $message .= s($key) . " => " . s($value) . "\n"; |
| 52 | + } |
| 53 | + |
| 54 | + $subject = "Weepay PAYMENT ERROR: " . $subject; |
| 55 | + $fullmessage = $message; |
| 56 | + $fullmessagehtml = html_to_text('<p>' . $message . '</p>'); |
| 57 | + |
| 58 | + // Send test email. |
| 59 | + ob_start(); |
| 60 | + $success = email_to_user($admin, $admin, $subject, $fullmessage, $fullmessagehtml); |
| 61 | + $smtplog = ob_get_contents(); |
| 62 | + ob_end_clean(); |
| 63 | +} |
| 64 | + |
| 65 | +$data = []; |
| 66 | + |
| 67 | +// Weepay START |
| 68 | +$plugin = enrol_get_plugin('weepaypayment'); |
| 69 | + |
| 70 | +require_once "weepay/weepayBootstrap.php"; |
| 71 | + |
| 72 | +weepayBootstrap::initialize(); |
| 73 | +$options = new \weepay\Auth(); |
| 74 | +$options->setBayiID($plugin->get_config('merchantid')); |
| 75 | +$options->setApiKey($plugin->get_config('publishablekey')); |
| 76 | +$options->setSecretKey($plugin->get_config('secretkey')); |
| 77 | +if ($plugin->get_config('sandboxmode')) { |
| 78 | + $options->setBaseUrl("https://api.weepay.co/"); |
| 79 | +} else { |
| 80 | + $options->setBaseUrl("https://api.weepay.co/"); |
| 81 | +} |
| 82 | + |
| 83 | +//Request |
| 84 | +$request = new \weepay\Request\GetPaymentRequest(); |
| 85 | +$request->setOrderId($_GET['oid']); |
| 86 | +$request->setLocale(\weepay\Model\Locale::TR); |
| 87 | + |
| 88 | +$getPaymentRequest = \weepay\Model\GetPaymentRequestInitialize::create($request, $options); |
| 89 | + |
| 90 | +$PaymentInfo = json_decode($getPaymentRequest->getRawResult()); |
| 91 | + |
| 92 | +if ($getPaymentRequest->getStatus() == 'success') { |
| 93 | + |
| 94 | + $OrderID = $_GET['oid']; |
| 95 | + $Payment = explode("-", $OrderID); |
| 96 | + $userId = $Payment[1]; |
| 97 | + $courseId = $Payment[2]; |
| 98 | + $instanceId = $Payment[3]; |
| 99 | + |
| 100 | + // If Payment Success |
| 101 | + if($getPaymentRequest->getPaymentStatus() == 'SUCCESS'){ |
| 102 | + |
| 103 | + // User Validation |
| 104 | + if (!$user = $DB->get_record("user", array("id" => $userId))) { |
| 105 | + message_weepaypayment_error_to_admin("Not a valid user id", $data); |
| 106 | + redirect($CFG->wwwroot); |
| 107 | + } |
| 108 | + // Course - Payment Validation |
| 109 | + if (!$course = $DB->get_record("course", array("id" => $courseId))) { |
| 110 | + message_weepaypayment_error_to_admin("Not a valid course id", $data); |
| 111 | + redirect($CFG->wwwroot); |
| 112 | + } |
| 113 | + // Context Validation |
| 114 | + if (!$context = context_course::instance( |
| 115 | + $course->id, |
| 116 | + IGNORE_MISSING |
| 117 | + )) { |
| 118 | + message_weepaypayment_error_to_admin("Not a valid context id", $data); |
| 119 | + redirect($CFG->wwwroot); |
| 120 | + } |
| 121 | + |
| 122 | + $PAGE->set_context($context); |
| 123 | + |
| 124 | + if (!$plugininstance = $DB->get_record("enrol", array("id" => $instanceId, "status" => 0))) { |
| 125 | + message_weepaypayment_error_to_admin("Not a valid instance id", $data); |
| 126 | + redirect($CFG->wwwroot); |
| 127 | + } |
| 128 | + |
| 129 | + if ($courseId != $plugininstance->courseid) { |
| 130 | + message_weepaypayment_error_to_admin("Course Id does not match to the course settings, received: " . $data->courseid, $data); |
| 131 | + redirect($CFG->wwwroot); |
| 132 | + } |
| 133 | + |
| 134 | + // ALL CLEAR ! |
| 135 | + |
| 136 | + $paymentData = new stdClass; |
| 137 | + $paymentData->payment_id = $PaymentInfo->data->orderId; |
| 138 | + $paymentData->course_id = $courseId; |
| 139 | + $paymentData->user_id = $userId; |
| 140 | + $paymentData->instance_id = $instanceId; |
| 141 | + $paymentData->price = $PaymentInfo->data->price; |
| 142 | + $paymentData->paid_price = $PaymentInfo->data->price; |
| 143 | + $paymentData->currency = $PaymentInfo->data->currency; |
| 144 | + $paymentData->payment_status = $PaymentInfo->data->paymentStatus; |
| 145 | + $paymentData->pending_reason = ""; |
| 146 | + $paymentData->reason_code = ""; |
| 147 | + $paymentData->time_updated = time(); |
| 148 | + |
| 149 | + $DB->insert_record("enrol_weepaypayment", $paymentData); |
| 150 | + |
| 151 | + // Enrol Period Config |
| 152 | + if ($plugininstance->enrolperiod) { |
| 153 | + $timestart = time(); |
| 154 | + $timeend = $timestart + $plugininstance->enrolperiod; |
| 155 | + } else { |
| 156 | + $timestart = 0; |
| 157 | + $timeend = 0; |
| 158 | + } |
| 159 | + |
| 160 | + // Enrol user. |
| 161 | + $plugin->enrol_user($plugininstance, $user->id, $plugininstance->roleid, $timestart, $timeend); |
| 162 | + |
| 163 | + // Pass $view=true to filter hidden caps if the user cannot see them. |
| 164 | + if ($users = get_users_by_capability( |
| 165 | + $context, |
| 166 | + 'moodle/course:update', |
| 167 | + 'u.*', |
| 168 | + 'u.id ASC', |
| 169 | + '', |
| 170 | + '', |
| 171 | + '', |
| 172 | + '', |
| 173 | + false, |
| 174 | + true |
| 175 | + )) { |
| 176 | + $users = sort_by_roleassignment_authority($users, $context); |
| 177 | + $teacher = array_shift($users); |
| 178 | + } else { |
| 179 | + $teacher = false; |
| 180 | + } |
| 181 | + |
| 182 | + $mailstudents = $plugin->get_config('mailstudents'); |
| 183 | + $mailteachers = $plugin->get_config('mailteachers'); |
| 184 | + $mailadmins = $plugin->get_config('mailadmins'); |
| 185 | + $shortname = format_string($course->shortname, true, array('context' => $context)); |
| 186 | + |
| 187 | + $coursecontext = context_course::instance($course->id); |
| 188 | + |
| 189 | + if (!empty($mailstudents)) { |
| 190 | + $a = new stdClass(); |
| 191 | + $a->coursename = format_string($course->fullname, true, array('context' => $coursecontext)); |
| 192 | + $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id"; |
| 193 | + |
| 194 | + $userfrom = empty($teacher) ? core_user::get_support_user() : $teacher; |
| 195 | + $subject = get_string("enrolmentnew", 'enrol', $shortname); |
| 196 | + $fullmessage = get_string('welcometocoursetext', '', $a); |
| 197 | + $fullmessagehtml = html_to_text('<p>' . get_string('welcometocoursetext', '', $a) . '</p>'); |
| 198 | + |
| 199 | + // Send test email. |
| 200 | + ob_start(); |
| 201 | + $success = email_to_user($user, $userfrom, $subject, $fullmessage, $fullmessagehtml); |
| 202 | + $smtplog = ob_get_contents(); |
| 203 | + ob_end_clean(); |
| 204 | + } |
| 205 | + |
| 206 | + if (!empty($mailteachers) && !empty($teacher)) { |
| 207 | + $a->course = format_string($course->fullname, true, array('context' => $coursecontext)); |
| 208 | + $a->user = fullname($user); |
| 209 | + |
| 210 | + $subject = get_string("enrolmentnew", 'enrol', $shortname); |
| 211 | + $fullmessage = get_string('enrolmentnewuser', 'enrol', $a); |
| 212 | + $fullmessagehtml = html_to_text('<p>' . get_string('enrolmentnewuser', 'enrol', $a) . '</p>'); |
| 213 | + |
| 214 | + // Send test email. |
| 215 | + ob_start(); |
| 216 | + $success = email_to_user($teacher, $user, $subject, $fullmessage, $fullmessagehtml); |
| 217 | + $smtplog = ob_get_contents(); |
| 218 | + ob_end_clean(); |
| 219 | + } |
| 220 | + |
| 221 | + if (!empty($mailadmins)) { |
| 222 | + $a->course = format_string($course->fullname, true, array('context' => $coursecontext)); |
| 223 | + $a->user = fullname($user); |
| 224 | + $admins = get_admins(); |
| 225 | + foreach ($admins as $admin) { |
| 226 | + $subject = get_string("enrolmentnew", 'enrol', $shortname); |
| 227 | + $fullmessage = get_string('enrolmentnewuser', 'enrol', $a); |
| 228 | + $fullmessagehtml = html_to_text('<p>' . get_string('enrolmentnewuser', 'enrol', $a) . '</p>'); |
| 229 | + |
| 230 | + // Send test email. |
| 231 | + ob_start(); |
| 232 | + $success = email_to_user($admin, $user, $subject, $fullmessage, $fullmessagehtml); |
| 233 | + $smtplog = ob_get_contents(); |
| 234 | + ob_end_clean(); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + $destination = "$CFG->wwwroot/course/view.php?id=$course->id"; |
| 239 | + |
| 240 | + $fullname = format_string($course->fullname, true, array('context' => $context)); |
| 241 | + |
| 242 | + if (is_enrolled($context, null, '', true)) { // TODO: use real weepay check. |
| 243 | + redirect($destination, get_string('paymentthanks', '', $fullname)); |
| 244 | + } else { // Somehow they aren't enrolled yet! |
| 245 | + $PAGE->set_url($destination); |
| 246 | + echo $OUTPUT->header(); |
| 247 | + $a = new stdClass(); |
| 248 | + $a->teacher = get_string('defaultcourseteacher'); |
| 249 | + $a->fullname = $fullname; |
| 250 | + notice(get_string('paymentsorry', '', $a), $destination); |
| 251 | + } |
| 252 | + |
| 253 | + } |
| 254 | + else { |
| 255 | + echo "<p>" . print_r($getPaymentRequest->getErrorCode()) . ":" . $getPaymentRequest->getMessage() . "</p>"; |
| 256 | + redirect($CFG->wwwroot); |
| 257 | + } |
| 258 | + |
| 259 | +} else { |
| 260 | + echo "<p>" . print_r($getPaymentRequest->getErrorCode()) . ":" . $getPaymentRequest->getMessage() . "</p>"; |
| 261 | + redirect($CFG->wwwroot); |
| 262 | +} |
0 commit comments