-
Notifications
You must be signed in to change notification settings - Fork 0
/
success.php
executable file
·105 lines (89 loc) · 3.51 KB
/
success.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
//This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* hitpay enrolments Success page.
*
* @package enrol_hitpay
* @copyright 2022 Brain station 23 ltd.
* @author Brain station 23 ltd.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once("../../config.php");
global $DB, $USER, $OUTPUT, $CFG, $PAGE;
$status = required_param('status', PARAM_TEXT);
$reference= required_param('reference', PARAM_RAW);
$parameters= required_param('data', PARAM_RAW);
$userid= $USER->id;
$str_arr = preg_split ("/\_/", $parameters);
$courseid = $str_arr[0];
$instanceid = $str_arr[1];
$currency = $str_arr[2];
$amount = $str_arr[3];
$config = get_config('enrol_hitpay');
$productionenv = $config->productionenv;
if($productionenv == 0) {
$apiurl = "https://api.sandbox.hit-pay.com/v1/payment-requests/";
}
else {
$apiurl ="https://api.hit-pay.com/v1/payment-requests/";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl.$reference);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'X-Business-Api-Key:'.$config->apikey;
$headers[] = 'X-Requested-With: XMLHttpRequest';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$result = json_decode($result);
if ($reference == $result->id && $result->payments[0]->status == 'succeeded' && $result->payments[0]->currency==strtolower($currency) && $result->payments[0]->amount==$amount && $USER->username == $result->payments[0]->buyer_name) {
$condition = array();
$condition['courseid'] = $courseid;
$condition['instanceid'] = $instanceid;
$condition['userid'] = $userid;
if($DB->record_exists('enrol_hitpay_log', $condition)) {
$data = (object) array (
'courseid' => $courseid,
'userid' => $userid,
'instanceid' => $instanceid,
'currency' => $currency,
'amount' => $amount,
'status' => $result->payments[0]->status,
'reference' => $reference,
'payment_id' => $result->payments[0]->id,
'username' => $result->payments[0]->buyer_name,
'created_at' => $result->payments[0]->created_at,
'updated_at' => $result->payments[0]->updated_at
);
if($DB->insert_record('enrol_hitpay', $data)){
$plugin = enrol_get_plugin('hitpay');
$plugininstance= $DB->get_record("enrol", array("id" => $instanceid, "enrol" => "hitpay", "status" => 0));
$plugin->enrol_user($plugininstance, $userid, $plugininstance->roleid);
$url = new moodle_url('/course/view.php?id='. $courseid);
redirect($url);
}
}
else {
$url = new moodle_url('/course/view.php?id='. $courseid);
redirect($url);
}
}