-
Notifications
You must be signed in to change notification settings - Fork 0
/
pay.php
185 lines (161 loc) · 5.28 KB
/
pay.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?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/>.
/**
* Redirects user to the payment page
*
* @package paygw_payeer
* @copyright 2024 Alex Orlov <snickser@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_payment\helper;
require_once(__DIR__ . '/../../../config.php');
global $CFG, $USER, $DB;
require_once($CFG->libdir . '/filelib.php');
require_login();
require_sesskey();
$userid = $USER->id;
$component = required_param('component', PARAM_COMPONENT);
$paymentarea = required_param('paymentarea', PARAM_AREA);
$itemid = required_param('itemid', PARAM_INT);
$description = required_param('description', PARAM_TEXT);
$password = optional_param('password', null, PARAM_TEXT);
$skipmode = optional_param('skipmode', 0, PARAM_INT);
$costself = optional_param('costself', null, PARAM_TEXT);
$config = (object) helper::get_gateway_configuration($component, $paymentarea, $itemid, 'payeer');
$payable = helper::get_payable($component, $paymentarea, $itemid);// Get currency and payment amount.
$currency = $payable->get_currency();
$surcharge = helper::get_gateway_surcharge('payeer');// In case user uses surcharge.
// TODO: Check if currency is IDR. If not, then something went really wrong in config.
$cost = helper::get_rounded_cost($payable->get_amount(), $payable->get_currency(), $surcharge);
// Check self cost.
if (!empty($costself)) {
$cost = $costself;
}
// Check maxcost.
if ($config->maxcost && $cost > $config->maxcost) {
$cost = $config->maxcost;
}
$cost = number_format($cost, 2, '.', '');
// Get course and groups for user.
if ($component == "enrol_fee") {
$cs = $DB->get_record('enrol', ['id' => $itemid]);
$cs->course = $cs->courseid;
} else if ($component == "mod_gwpayments") {
$cs = $DB->get_record('gwpayments', ['id' => $itemid]);
} else if ($paymentarea == "cmfee") {
$cs = $DB->get_record('course_modules', ['id' => $itemid]);
} else if ($paymentarea == "sectionfee") {
$cs = $DB->get_record('course_sections', ['id' => $itemid]);
}
$groupnames = '';
if (!empty($cs->course)) {
$courseid = $cs->course;
if ($gs = groups_get_user_groups($courseid, $userid, true)) {
foreach ($gs as $gr) {
foreach ($gr as $g) {
$groups[] = groups_get_group_name($g);
}
}
if (isset($groups)) {
$groupnames = implode(',', $groups);
}
}
} else {
$courseid = '';
}
// Write tx to db.
$paygwdata = new stdClass();
$paygwdata->courseid = $courseid;
$paygwdata->groupnames = $groupnames;
if (!$transactionid = $DB->insert_record('paygw_payeer', $paygwdata)) {
die(get_string('error_txdatabase', 'paygw_payeer'));
}
$paygwdata->id = $transactionid;
// Build redirect.
$url = helper::get_success_url($component, $paymentarea, $itemid);
// Check passwordmode or skipmode.
if (!empty($password) || $skipmode) {
$success = false;
if ($config->skipmode) {
$success = true;
} else if ($config->passwordmode && !empty($config->password)) {
// Check password.
if ($password === $config->password) {
$success = true;
}
}
if ($success) {
// Make fake pay.
$paymentid = helper::save_payment(
$payable->get_account_id(),
$component,
$paymentarea,
$itemid,
$userid,
0,
$currency,
'payeer'
);
helper::deliver_order($component, $paymentarea, $itemid, $paymentid, $userid);
// Write to DB.
$paygwdata->success = 2;
$paygwdata->paymentid = $paymentid;
$DB->update_record('paygw_payeer', $paygwdata);
redirect($url, get_string('password_success', 'paygw_payeer'), 0, 'success');
} else {
redirect($url, get_string('password_error', 'paygw_payeer'), 0, 'error');
}
die; // Never.
}
// Save payment.
$paymentid = helper::save_payment(
$payable->get_account_id(),
$component,
$paymentarea,
$itemid,
$userid,
0,
$currency,
'payeer'
);
// Write to DB.
$paygwdata->paymentid = $paymentid;
$DB->update_record('paygw_payeer', $paygwdata);
// Make payment request.
$murl = 'https://payeer.com/merchant/';
$mshop = $config->shopid;
$morderid = $paymentid;
$mamount = number_format($cost, 2, '.', '');
$mcurr = $currency == 'RUR' ? 'RUB' : $currency;
$mdesc = base64_encode($description);
$mkey = $config->apikey;
$arhash = [
$mshop,
$morderid,
$mamount,
$mcurr,
$mdesc,
$mkey,
];
$sign = strtoupper(hash('sha256', implode(":", $arhash)));
redirect($murl . "?
mshop={$mshop}&
morderid={$morderid}&
mamount={$mamount}&
mcurr={$mcurr}&
mdesc={$mdesc}&
m_sign={$sign}
");