-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ipn.php
120 lines (98 loc) · 3.14 KB
/
Ipn.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
<?php
/**
* Paypal IPN Library
*
* Requires the PHP cURL extension
* Had some help from Elliot "Haughinator" Haugin on this one, so props to him
*
* @package Flame
* @subpackage Paypal
* @copyright 2009, Jamie Rumbelow
* @author Jamie Rumbelow <http://www.jamierumbelow.net>
* @license GPLv3
* @version 1.0.1
*/
class Ipn {
private $ipn_data;
private $ipn_url;
private $curl;
public function __construct() {
$this->add_data('cmd', '_xclick');
$this->add_data('rm', 2);
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_HEADER , 0);
curl_setopt($this->curl, CURLOPT_VERBOSE, 1);
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 120);
}
public function set_url($url) {
$this->ipn_url = $url;
}
public function add_data($key, $value) {
if ($key == 'custom') {
$value = json_encode($value);
}
$this->ipn_data[$key] = $value;
}
public function request() {
//Build up the HTML page
$string = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
$string .= '<html>';
$string .= '<head>';
$string .= '<title>Redirecting to PayPal...</title>';
$string .= '<script type="text/javascript">
window.onload = function() {
document.forms[\'paypal_form\'].submit();
}
</script>';
$string .= '</head>';
$string .= '<body>';
$string .= '<h2>Redirecting to PayPal</h2>';
$string .= '<form action=\''.$this->ipn_url.'\' method=\'post\' name=\'paypal_form\'>';
foreach ($this->ipn_data as $key => $value) {
$string .= '<input type=\'hidden\' name=\''.$key.'\' value=\''.$value.'\' />';
}
$string .= '<p>If you don\'t get redirected to PayPal within five seconds <input type=\'submit\' value=\'click here!\' /></p>';
$string .= '</form>';
$string .= '</body>';
$string .= '</html>';
echo($string);
exit;
}
public function validate() {
$post_string = "cmd=_notify-validate";
foreach ( $_POST as $key => $value )
{
$this->add_data($key, $value);
$value = urlencode(stripslashes($value));
$post_string .= "&$key=$value";
}
$new_custom = json_decode(base64_decode(base64_decode($this->ipn_data['custom'])));
$this->decrypted_custom = array();
foreach ( $new_custom as $key => $value )
{
$this->decrypted_custom[$key] = $value;
}
if ( $this->ipn_data['payment_status'] !== 'Completed' )
{
return FALSE;
}
curl_setopt($this->curl, CURLOPT_URL, $this->ipn_url);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($post_string)));
$result = curl_exec($this->curl);
if ( !$result )
{
return FALSE;
}
$valid_results = array('VERIFIED', 'INVALID');
if ( !in_array($result, $valid_results) || !$result === 'INVALID' )
{
return FALSE;
}
return array('paypal_data' => $this->ipn_data, 'custom_data' => $this->decrypted_custom);
}
}