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
|
|
|
* Prints a particular instance of paypal |
19
|
|
|
* |
20
|
|
|
* You can have a rather longer description of the file as well, |
21
|
|
|
* if you like, and it can span multiple lines. |
22
|
|
|
* |
23
|
|
|
* @package availability_paypal |
24
|
|
|
* @copyright 2015 Daniel Neis Araujo <[email protected]> |
25
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
require_once('../../../config.php'); |
29
|
|
|
|
30
|
|
|
$contextid = required_param('contextid', PARAM_INT); |
31
|
|
|
|
32
|
|
|
$context = context::instance_by_id($contextid); |
33
|
|
|
$instanceid = $context->instanceid; |
34
|
|
View Code Duplication |
if ($context instanceof context_module) { |
|
|
|
|
35
|
|
|
$availability = $DB->get_field('course_modules', 'availability', array('id' => $instanceid), MUST_EXIST); |
36
|
|
|
$availability = json_decode($availability); |
37
|
|
|
foreach ($availability->c as $condition) { |
38
|
|
|
if ($condition->type == 'paypal') { |
39
|
|
|
// TODO: handle more than one paypal for this context. |
40
|
|
|
$paypal = $condition; |
41
|
|
|
break; |
42
|
|
|
} else { |
43
|
|
|
print_error('no paypal condition for this context.'); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} else { |
47
|
|
|
// TODO: handle sections. |
48
|
|
|
print_error('support to sections not yet implemented.'); |
49
|
|
|
} |
50
|
|
|
$coursecontext = $context->get_course_context(); |
51
|
|
|
$course = $DB->get_record('course', array('id' => $coursecontext->instanceid)); |
52
|
|
|
|
53
|
|
|
require_login($course); |
54
|
|
|
|
55
|
|
|
if ($paymenttnx = $DB->get_record('availability_paypal_tnx', array('userid' => $USER->id, 'contextid' => $contextid))) { |
56
|
|
|
|
57
|
|
|
if ($paymenttnx->payment_status == 'Completed') { |
58
|
|
|
redirect($context->get_url(), get_string('paymentcompleted', 'availability_paypal')); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$PAGE->set_url('/availability/condition/paypal/view.php', array('contextid' => $contextid)); |
63
|
|
|
$PAGE->set_title($course->fullname); |
64
|
|
|
$PAGE->set_heading($course->fullname); |
65
|
|
|
|
66
|
|
|
$PAGE->navbar->add($paypal->itemname); |
67
|
|
|
|
68
|
|
|
echo $OUTPUT->header(); |
69
|
|
|
|
70
|
|
|
echo $OUTPUT->heading($paypal->itemname); |
71
|
|
|
|
72
|
|
|
if ($paymenttnx && ($paymenttnx->payment_status == 'Pending')) { |
73
|
|
|
echo get_string('paymentpending', 'availability_paypal'); |
74
|
|
|
} else { |
75
|
|
|
|
76
|
|
|
// Calculate localised and "." cost, make sure we send PayPal the same value, |
77
|
|
|
// please note PayPal expects amount with 2 decimal places and "." separator. |
78
|
|
|
$localisedcost = format_float($paypal->cost, 2, true); |
79
|
|
|
$cost = format_float($paypal->cost, 2, false); |
80
|
|
|
|
81
|
|
|
if (isguestuser()) { // Force login only for guest user, not real users with guest role. |
82
|
|
|
if (empty($CFG->loginhttps)) { |
83
|
|
|
$wwwroot = $CFG->wwwroot; |
84
|
|
|
} else { |
85
|
|
|
// This actually is not so secure ;-), 'cause we're in unencrypted connection... |
86
|
|
|
$wwwroot = str_replace("http://", "https://", $CFG->wwwroot); |
87
|
|
|
} |
88
|
|
|
echo '<div class="mdl-align"><p>'.get_string('paymentrequired', 'availability_paypal').'</p>'; |
89
|
|
|
echo '<div class="mdl-align"><p>'.get_string('paymentwaitremider', 'availability_paypal').'</p>'; |
90
|
|
|
echo '<p><b>'.get_string('cost').": $instance->currency $localisedcost".'</b></p>'; |
91
|
|
|
echo '<p><a href="'.$wwwroot.'/login/">'.get_string('loginsite').'</a></p>'; |
92
|
|
|
echo '</div>'; |
93
|
|
|
} else { |
94
|
|
|
// Sanitise some fields before building the PayPal form. |
95
|
|
|
$userfullname = fullname($USER); |
96
|
|
|
$userfirstname = $USER->firstname; |
97
|
|
|
$userlastname = $USER->lastname; |
98
|
|
|
$useraddress = $USER->address; |
99
|
|
|
$usercity = $USER->city; |
100
|
|
|
?> |
101
|
|
|
<p><?php print_string("paymentrequired", 'availability_paypal') ?></p> |
102
|
|
|
<p><b><?php echo get_string("cost").": {$paypal->currency} {$localisedcost}"; ?></b></p> |
103
|
|
|
<p><img alt="<?php print_string('paypalaccepted', 'availability_paypal') ?>" |
104
|
|
|
title="<?php print_string('paypalaccepted', 'availability_paypal') ?>" |
105
|
|
|
src="https://www.paypal.com/en_US/i/logo/PayPal_mark_60x38.gif" /></p> |
106
|
|
|
<p><?php print_string("paymentinstant", 'availability_paypal') ?></p> |
107
|
|
|
<?php |
108
|
|
|
if (empty($CFG->usepaypalsandbox)) { |
109
|
|
|
$paypalurl = 'https://www.paypal.com/cgi-bin/webscr'; |
110
|
|
|
} else { |
111
|
|
|
$paypalurl = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; |
112
|
|
|
} |
113
|
|
|
?> |
114
|
|
|
<form action="<?php echo $paypalurl ?>" method="post"> |
115
|
|
|
|
116
|
|
|
<input type="hidden" name="cmd" value="_xclick" /> |
117
|
|
|
<input type="hidden" name="charset" value="utf-8" /> |
118
|
|
|
<input type="hidden" name="business" value="<?php p($paypal->businessemail)?>" /> |
119
|
|
|
<input type="hidden" name="item_name" value="<?php p($paypal->itemname) ?>" /> |
120
|
|
|
<input type="hidden" name="item_number" value="<?php p($paypal->itemnumber) ?>" /> |
121
|
|
|
<input type="hidden" name="quantity" value="1" /> |
122
|
|
|
<input type="hidden" name="on0" value="<?php print_string("user") ?>" /> |
123
|
|
|
<input type="hidden" name="os0" value="<?php p($userfullname) ?>" /> |
124
|
|
|
<input type="hidden" name="custom" value="<?php echo "{$USER->id}-{$contextid}" ?>" /> |
125
|
|
|
|
126
|
|
|
<input type="hidden" name="currency_code" value="<?php p($paypal->currency) ?>" /> |
127
|
|
|
<input type="hidden" name="amount" value="<?php p($cost) ?>" /> |
128
|
|
|
|
129
|
|
|
<input type="hidden" name="for_auction" value="false" /> |
130
|
|
|
<input type="hidden" name="no_note" value="1" /> |
131
|
|
|
<input type="hidden" name="no_shipping" value="1" /> |
132
|
|
|
<input type="hidden" name="notify_url" value="<?php echo "{$CFG->wwwroot}/availability/condition/paypal/ipn.php" ?>" /> |
133
|
|
|
<?php $returnurl = $CFG->wwwroot . '/availability/condition/paypal/view.php?contextid=' . $contextid; ?> |
134
|
|
|
<input type="hidden" name="return" value="<?php echo $returnurl; ?>" /> |
135
|
|
|
<input type="hidden" name="cancel_return" value="<?php echo $returnurl; ?>" /> |
136
|
|
|
<input type="hidden" name="rm" value="2" /> |
137
|
|
|
<input type="hidden" name="cbt" value="<?php print_string("continue", 'availability_paypal') ?>" /> |
138
|
|
|
|
139
|
|
|
<input type="hidden" name="first_name" value="<?php p($userfirstname) ?>" /> |
140
|
|
|
<input type="hidden" name="last_name" value="<?php p($userlastname) ?>" /> |
141
|
|
|
<input type="hidden" name="address" value="<?php p($useraddress) ?>" /> |
142
|
|
|
<input type="hidden" name="city" value="<?php p($usercity) ?>" /> |
143
|
|
|
<input type="hidden" name="email" value="<?php p($USER->email) ?>" /> |
144
|
|
|
<input type="hidden" name="country" value="<?php p($USER->country) ?>" /> |
145
|
|
|
|
146
|
|
|
<input type="submit" value="<?php print_string("sendpaymentbutton", "availability_paypal") ?>" /> |
147
|
|
|
</form> |
148
|
|
|
<?php |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
// Finish the page. |
152
|
|
|
echo $OUTPUT->footer(); |
153
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.