-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback.php
119 lines (101 loc) · 3.58 KB
/
callback.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
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
/**
* Plugin administration pages are defined here.
*
* @package paygw_cryptocloud
* @copyright 2024 Alex Orlov <snickser@gmail.com>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_payment\helper;
use paygw_cryptocloud\notifications;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
require("../../../config.php");
global $CFG, $USER, $DB;
require_once($CFG->libdir . '/filelib.php');
defined('MOODLE_INTERNAL') || die();
$status = required_param('status', PARAM_TEXT);
$invoiceid = required_param('invoice_id', PARAM_ALPHANUMEXT);
$amount = required_param('amount_crypto', PARAM_FLOAT);
$currency = required_param('currency', PARAM_TEXT);
$orderid = required_param('order_id', PARAM_INT);
$token = required_param('token', PARAM_TEXT);
if ($status !== 'success') {
die('FAIL. Payment not successed.');
}
if (!$cryptocloudtx = $DB->get_record('paygw_cryptocloud', [ 'paymentid' => $orderid, 'invoiceid' => 'INV-' . $invoiceid ])) {
die('FAIL. Not a valid transaction.');
}
if (!$payment = $DB->get_record('payments', ['id' => $cryptocloudtx->paymentid])) {
die('FAIL. Not a valid payment.');
}
$component = $payment->component;
$paymentarea = $payment->paymentarea;
$itemid = $payment->itemid;
$paymentid = $payment->id;
$userid = $payment->userid;
// Get apikey and secretkey.
$config = (object) helper::get_gateway_configuration($component, $paymentarea, $itemid, 'cryptocloud');
$decoded = JWT::decode($token, new Key($config->secretkey, 'HS256'));
if (empty($decoded->id)) {
die('FAIL. Invalid token.');
}
// Check invoice on site.
$location = 'https://api.cryptocloud.plus/v2/invoice/merchant/info';
$options = [
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_TIMEOUT' => 30,
'CURLOPT_HTTPHEADER' => [
'Content-Type: application/json',
'Authorization: Token ' . $config->apikey,
],
];
$jsondata = json_encode(["uuids" => [$invoiceid]]);
$curl = new curl();
$jsonresponse = $curl->post($location, $jsondata, $options);
$response = json_decode($jsonresponse, false);
// Check invoice status.
if ($response->status !== 'success') {
die('FAIL. Invoice check not successed.');
}
if ($response->result[0]->invoice_status !== 'success') {
die('FAIL. Invoice not successed.');
}
if ($response->result[0]->status !== 'paid') {
die('FAIL. Invoice not paid.');
}
// Deliver order.
helper::deliver_order($component, $paymentarea, $itemid, $paymentid, $userid);
// Notify user.
notifications::notify(
$userid,
$payment->amount,
$payment->currency,
$paymentid,
'Success completed'
);
// Update paygw.
if ($response->result[0]->test_mode == true) {
$cryptocloudtx->success = 3;
} else {
$cryptocloudtx->success = 1;
}
if (!$DB->update_record('paygw_cryptocloud', $cryptocloudtx)) {
die('FAIL. Update db error.');
} else {
echo json_encode(['message' => 'Postback received']);
}