-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.php
125 lines (107 loc) · 4.63 KB
/
process.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
<?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/>.
/*
* Various helper methods for interacting with the cardinity API
*
* @package paygw_cardinity
* @copyright 2021 Brain station 23 ltd.
* @author Brain station 23 ltd.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_payment\helper;
use paygw_cardinity\cardinity_helper;
global $CFG, $USER, $DB;
require_once(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot . '/course/lib.php');
defined('MOODLE_INTERNAL') || die();
require_login();
$status = required_param('status', PARAM_TEXT);
$courseid = required_param("courseid", PARAM_INT);
$component = required_param('component', PARAM_ALPHANUMEXT);
$paymentarea = required_param('paymentarea', PARAM_ALPHANUMEXT);
$itemid = required_param('itemid', PARAM_INT);
// Load Cardinity Configuration.
$config = (object)helper::get_gateway_configuration($component, $paymentarea, $itemid, 'cardinity');
$consumerkey = $config->consumerkey;
$consumersecret = $config->consumersecret;
$projectsecret = $config->secretkey;
$message = '';
$data['amount'] = required_param('amount', PARAM_RAW);
$data['country'] = required_param('country', PARAM_RAW);
$data['created'] = required_param('created', PARAM_RAW);
$data['currency'] = required_param('currency', PARAM_RAW);
$data['description'] = required_param('description', PARAM_RAW);
$data['id'] = required_param('id', PARAM_RAW);
$data['live'] = optional_param('live', '', PARAM_RAW);
$data['order_id'] = required_param('order_id', PARAM_RAW);
$data['payment_method'] = required_param('payment_method', PARAM_RAW);
$data['project_id'] = required_param('project_id', PARAM_RAW);
$data['signature'] = required_param('signature', PARAM_RAW);
$data['status'] = required_param('status', PARAM_RAW);
$data['type'] = required_param('type', PARAM_RAW);
foreach ($data as $key => $value) {
if ($key == 'signature') {
continue;
}
$message .= $key . $value;
}
$signature = hash_hmac('sha256', $message, $projectsecret);
if ($signature == $data['signature']) {
// Init Cardinity Helper.
$cardinityhelper = new cardinity_helper($consumerkey, $consumersecret);
// Check Payment Status.
if ($cardinityhelper->check_payment_status($data['id'])) {
$paymentrecord = new stdClass();
$paymentrecord->courseid = $courseid;
$paymentrecord->itemid = $itemid;
$paymentrecord->userid = $USER->id;
$paymentrecord->currency = $data['currency'];
$paymentrecord->payment_status = $status;
$paymentrecord->payment_id = $data['id'];
$paymentrecord->txn_id = $data['order_id'];
$paymentrecord->timeupdated = time();
$DB->insert_record('paygw_cardinity', $paymentrecord);
// Deliver course.
$payable = helper::get_payable($component, $paymentarea, $itemid);
$cost = helper::get_rounded_cost($payable->get_amount(),
$payable->get_currency(),
helper::get_gateway_surcharge('cardinity'));
$paymentid = helper::save_payment(
$payable->get_account_id(),
$component,
$paymentarea,
$itemid,
$USER->id,
$cost,
$payable->get_currency(),
'cardinity'
);
helper::deliver_order($component, $paymentarea, $itemid, $paymentid, $USER->id);
// Find redirection.
$url = new moodle_url('/');
// Method only exists in 3.11+.
if (method_exists('\core_payment\helper', 'get_success_url')) {
$url = helper::get_success_url($component, $paymentarea, $itemid);
} else if ($component == 'enrol_fee' && $paymentarea == 'fee') {
$courseid = $DB->get_field('enrol', 'courseid', ['enrol' => 'fee', 'id' => $itemid]);
if (!empty($courseid)) {
$url = course_get_url($courseid);
}
}
redirect($url, get_string('paymentsuccessful', 'paygw_cardinity'), 0, 'success');
}
}
redirect(new moodle_url('/'), get_string('paymentcancelled', 'paygw_cardinity'));