1
1
<?php
2
2
3
- class PaypalIPN {
3
+ class PaypalIPN
4
+ {
5
+
6
+ private $ use_sandbox = false ;
4
7
5
- private $ use_sandbox = false ;
6
8
private $ use_local_certs = true ;
7
9
8
10
/*
9
11
* PayPal IPN postback endpoints
10
12
*/
11
13
12
- const VERIFY_URI = 'https://ipnpb.paypal.com/cgi-bin/webscr ' ;
14
+ const VERIFY_URI = 'https://ipnpb.paypal.com/cgi-bin/webscr ' ;
13
15
const SANDBOX_VERIFY_URI = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr ' ;
14
16
15
17
/*
@@ -25,24 +27,25 @@ class PaypalIPN {
25
27
* should not be enabled in production).
26
28
* @return void
27
29
*/
28
- public function useSandbox ()
30
+ public function useSandbox ()
29
31
{
30
- $ this ->use_sandbox = true ;
32
+ $ this ->use_sandbox = true ;
31
33
}
32
34
33
35
34
36
/**
35
37
* Determine endpoint to post the verification data to.
36
38
* @return string
37
39
*/
38
- public function getPaypalUri ()
40
+ public function getPaypalUri ()
39
41
{
40
- if ($ this ->use_sandbox )
41
- {
42
- return self ::SANDBOX_VERIFY_URI ;
43
- } else {
44
- return self ::VERIFY_URI ;
45
- }
42
+ if ($ this ->use_sandbox )
43
+ {
44
+ return self ::SANDBOX_VERIFY_URI ;
45
+ } else
46
+ {
47
+ return self ::VERIFY_URI ;
48
+ }
46
49
}
47
50
48
51
@@ -54,54 +57,63 @@ public function getPaypalUri()
54
57
* @throws Exception
55
58
*/
56
59
function verifyIPN ()
57
- {
58
- if (! count ($ _POST ))
60
+ {
61
+ if ( ! count ($ _POST ))
59
62
{
60
63
throw new Exception ("Missing POST Data " );
61
64
}
62
65
63
- $ raw_post_data = file_get_contents ('php://input ' );
64
- $ raw_post_array = explode ('& ' , $ raw_post_data );
65
- $ myPost = [];
66
- foreach ($ raw_post_array as $ keyval ) {
67
- $ keyval = explode ('= ' , $ keyval );
68
- if (count ($ keyval ) == 2 ) {
69
- if ($ keyval [0 ] === 'payment_date ' ) {
70
- if (substr_count ($ keyval [1 ], '+ ' ) === 1 )
71
- $ keyval [1 ] = str_replace ('+ ' , '%2B ' , $ keyval [1 ]);
72
- }
73
- $ myPost [$ keyval [0 ]] = urldecode ($ keyval [1 ]);
74
- }
75
- }
76
- $ req = 'cmd=_notify-validate ' ;
77
- $ get_magic_quotes_exists = false ;
78
- if (function_exists ('get_magic_quotes_gpc ' )) {
79
- $ get_magic_quotes_exists = true ;
80
- }
81
- foreach ($ myPost as $ key => $ value ) {
82
- if ($ get_magic_quotes_exists == true && get_magic_quotes_gpc () == 1 ) {
83
- $ value = urlencode (stripslashes ($ value ));
84
- } else {
85
- $ value = urlencode ($ value );
86
- }
87
- $ req .= "& $ key= $ value " ;
88
- }
89
-
90
- $ ch = curl_init ($ this ->getPaypalUri ());
91
- curl_setopt ($ ch , CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
92
- curl_setopt ($ ch , CURLOPT_POST , 1 );
93
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
94
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ req );
95
- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 1 );
96
- curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , 2 );
66
+ $ raw_post_data = file_get_contents ('php://input ' );
67
+ $ raw_post_array = explode ('& ' , $ raw_post_data );
68
+ $ myPost = [];
69
+ foreach ($ raw_post_array as $ keyval )
70
+ {
71
+ $ keyval = explode ('= ' , $ keyval );
72
+ if (count ($ keyval ) == 2 )
73
+ {
74
+ if ($ keyval [0 ] === 'payment_date ' )
75
+ {
76
+ if (substr_count ($ keyval [1 ], '+ ' ) === 1 )
77
+ {
78
+ $ keyval [1 ] = str_replace ('+ ' , '%2B ' , $ keyval [1 ]);
79
+ }
80
+ }
81
+ $ myPost [$ keyval [0 ]] = urldecode ($ keyval [1 ]);
82
+ }
83
+ }
84
+ $ req = 'cmd=_notify-validate ' ;
85
+ $ get_magic_quotes_exists = false ;
86
+ if (function_exists ('get_magic_quotes_gpc ' ))
87
+ {
88
+ $ get_magic_quotes_exists = true ;
89
+ }
90
+ foreach ($ myPost as $ key => $ value )
91
+ {
92
+ if ($ get_magic_quotes_exists == true && get_magic_quotes_gpc () == 1 )
93
+ {
94
+ $ value = urlencode (stripslashes ($ value ));
95
+ } else
96
+ {
97
+ $ value = urlencode ($ value );
98
+ }
99
+ $ req .= "& $ key= $ value " ;
100
+ }
101
+
102
+ $ ch = curl_init ($ this ->getPaypalUri ());
103
+ curl_setopt ($ ch , CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
104
+ curl_setopt ($ ch , CURLOPT_POST , 1 );
105
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
106
+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ req );
107
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 1 );
108
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , 2 );
97
109
if ($ this ->use_local_certs )
98
110
{
99
111
curl_setopt ($ ch , CURLOPT_CAINFO , __DIR__ . "/cert/cacert.pem " );
100
112
}
101
- curl_setopt ($ ch , CURLOPT_FORBID_REUSE , 1 );
102
- curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , 30 );
103
- curl_setopt ($ ch , CURLOPT_HTTPHEADER , ['Connection: Close ' ]);
104
- $ res = curl_exec ($ ch );
113
+ curl_setopt ($ ch , CURLOPT_FORBID_REUSE , 1 );
114
+ curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , 30 );
115
+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , [ 'Connection: Close ' ]);
116
+ $ res = curl_exec ($ ch );
105
117
$ info = curl_getinfo ($ ch );
106
118
$ http_code = $ info ['http_code ' ];
107
119
@@ -110,19 +122,21 @@ function verifyIPN()
110
122
throw new Exception ("PayPal responded with http code $ http_code " );
111
123
}
112
124
113
- if (!($ res )) {
125
+ if ( ! ( $ res ))
126
+ {
114
127
$ errno = curl_errno ($ ch );
115
128
$ errstr = curl_error ($ ch );
116
129
curl_close ($ ch );
117
130
throw new Exception ("cURL error: [ $ errno] $ errstr " );
118
- }
119
- curl_close ($ ch );
120
- if ($ res == self ::VALID )
121
- {
122
- return true ;
123
- } else {
124
- return false ;
125
- }
126
- }
131
+ }
132
+ curl_close ($ ch );
133
+ if ($ res == self ::VALID )
134
+ {
135
+ return true ;
136
+ } else
137
+ {
138
+ return false ;
139
+ }
140
+ }
127
141
}
128
142
0 commit comments