1
- <?php
1
+ <?php
2
2
3
3
namespace overint ;
4
4
5
- use \Exception
5
+ use \Exception ;
6
6
7
7
class PaypalIPN
8
8
{
9
-
10
- /**
11
- * @var bool $use_sandbox Indicates if the sandbox endpoint is used.
12
- */
13
- private $ use_sandbox = false ;
14
- /**
15
- * @var bool $use_local_certs Indicates if the local certificates are used.
16
- */
17
- private $ use_local_certs = true ;
9
+ /** @var bool $useSandbox Indicates if the sandbox endpoint is used. */
10
+ private $ useSandbox = false ;
11
+ /** @var bool $useLocalCerts Indicates if the local certificates are used. */
12
+ private $ useLocalCerts = true ;
18
13
19
14
/** Production Postback URL */
20
15
const VERIFY_URI = 'https://ipnpb.paypal.com/cgi-bin/webscr ' ;
@@ -35,7 +30,7 @@ class PaypalIPN
35
30
*/
36
31
public function useSandbox ()
37
32
{
38
- $ this ->use_sandbox = true ;
33
+ $ this ->useSandbox = true ;
39
34
}
40
35
41
36
/**
@@ -45,7 +40,7 @@ public function useSandbox()
45
40
*/
46
41
public function usePHPCerts ()
47
42
{
48
- $ this ->use_local_certs = false ;
43
+ $ this ->useLocalCerts = false ;
49
44
}
50
45
51
46
@@ -55,7 +50,7 @@ public function usePHPCerts()
55
50
*/
56
51
public function getPaypalUri ()
57
52
{
58
- if ($ this ->use_sandbox ) {
53
+ if ($ this ->useSandbox ) {
59
54
return self ::SANDBOX_VERIFY_URI ;
60
55
} else {
61
56
return self ::VERIFY_URI ;
@@ -72,23 +67,18 @@ public function getPaypalUri()
72
67
*/
73
68
function verifyIPN ()
74
69
{
75
- if ( ! count ($ _POST ))
76
- {
70
+ if (!count ($ _POST )) {
77
71
throw new Exception ("Missing POST Data " );
78
72
}
79
73
80
74
$ raw_post_data = file_get_contents ('php://input ' );
81
75
$ raw_post_array = explode ('& ' , $ raw_post_data );
82
76
$ myPost = [];
83
- foreach ($ raw_post_array as $ keyval )
84
- {
77
+ foreach ($ raw_post_array as $ keyval ) {
85
78
$ keyval = explode ('= ' , $ keyval );
86
- if (count ($ keyval ) == 2 )
87
- {
88
- if ($ keyval [0 ] === 'payment_date ' )
89
- {
90
- if (substr_count ($ keyval [1 ], '+ ' ) === 1 )
91
- {
79
+ if (count ($ keyval ) == 2 ) {
80
+ if ($ keyval [0 ] === 'payment_date ' ) {
81
+ if (substr_count ($ keyval [1 ], '+ ' ) === 1 ) {
92
82
$ keyval [1 ] = str_replace ('+ ' , '%2B ' , $ keyval [1 ]);
93
83
}
94
84
}
@@ -97,17 +87,13 @@ function verifyIPN()
97
87
}
98
88
$ req = 'cmd=_notify-validate ' ;
99
89
$ get_magic_quotes_exists = false ;
100
- if (function_exists ('get_magic_quotes_gpc ' ))
101
- {
90
+ if (function_exists ('get_magic_quotes_gpc ' )) {
102
91
$ get_magic_quotes_exists = true ;
103
92
}
104
- foreach ($ myPost as $ key => $ value )
105
- {
106
- if ($ get_magic_quotes_exists == true && get_magic_quotes_gpc () == 1 )
107
- {
93
+ foreach ($ myPost as $ key => $ value ) {
94
+ if ($ get_magic_quotes_exists == true && get_magic_quotes_gpc () == 1 ) {
108
95
$ value = urlencode (stripslashes ($ value ));
109
- } else
110
- {
96
+ } else {
111
97
$ value = urlencode ($ value );
112
98
}
113
99
$ req .= "& $ key= $ value " ;
@@ -121,35 +107,30 @@ function verifyIPN()
121
107
curl_setopt ($ ch , CURLOPT_SSLVERSION , 6 );
122
108
curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 1 );
123
109
curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , 2 );
124
- if ($ this ->use_local_certs )
125
- {
110
+ if ($ this ->useLocalCerts ) {
126
111
curl_setopt ($ ch , CURLOPT_CAINFO , __DIR__ . "/cert/cacert.pem " );
127
112
}
128
113
curl_setopt ($ ch , CURLOPT_FORBID_REUSE , 1 );
129
114
curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , 30 );
130
- curl_setopt ($ ch , CURLOPT_HTTPHEADER , [ 'Connection: Close ' ]);
115
+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , ['Connection: Close ' ]);
131
116
$ res = curl_exec ($ ch );
132
117
$ info = curl_getinfo ($ ch );
133
118
$ http_code = $ info ['http_code ' ];
134
119
135
- if ($ http_code != 200 )
136
- {
120
+ if ($ http_code != 200 ) {
137
121
throw new Exception ("PayPal responded with http code $ http_code " );
138
122
}
139
123
140
- if ( ! ( $ res ))
141
- {
124
+ if (!($ res )) {
142
125
$ errno = curl_errno ($ ch );
143
126
$ errstr = curl_error ($ ch );
144
127
curl_close ($ ch );
145
128
throw new Exception ("cURL error: [ $ errno] $ errstr " );
146
129
}
147
130
curl_close ($ ch );
148
- if ($ res == self ::VALID )
149
- {
131
+ if ($ res == self ::VALID ) {
150
132
return true ;
151
- } else
152
- {
133
+ } else {
153
134
return false ;
154
135
}
155
136
}
0 commit comments