ffb23831165e79e2d5d703c1bd4f2514fc37cb20
[WP-Plugins/glm-member-db-registrations.git] /
1 <?php
2 namespace PayPal\IPN;
3 use PayPal\Core\PPHttpConfig;
4 use PayPal\Exception\PPConfigurationException;
5 use PayPal\Core\PPConfigManager;
6 use PayPal\Core\PPConstants;
7 use PayPal\Core\PPConnectionManager;
8 /**
9  *
10  *
11  */
12 class PPIPNMessage {
13
14         const IPN_CMD = 'cmd=_notify-validate';
15
16         /*
17          *@var boolian
18         *
19         */
20         private $isIpnVerified;
21
22         /*
23          *@var config
24         *
25         */
26         private $config;
27         /**
28          *
29          * @var array
30          */
31         private $ipnData = array();
32
33         /**
34          *
35          * @param string $postData OPTIONAL post data. If null,
36          *                              the class automatically reads incoming POST data
37          *                              from the input stream
38         */
39         public function __construct($postData='', $config = null) {
40                 if($config == null)
41                 {
42                         $conf = PPConfigManager::getInstance();
43                         $this->config = $conf->config;
44                 }
45                 else
46                 {
47                         $this->config = $config;
48                 }
49                 if($postData == '') {
50                         // reading posted data from directly from $_POST may causes serialization issues with array data in POST
51                         // reading raw POST data from input stream instead.
52                         $postData = file_get_contents('php://input');
53                 }
54
55                 $rawPostArray = explode('&', $postData);
56                 foreach ($rawPostArray as $keyValue) {
57                         $keyValue = explode ('=', $keyValue);
58                         if (count($keyValue) == 2)
59                                 $this->ipnData[$keyValue[0]] = urldecode($keyValue[1]);
60                 }
61                 //var_dump($this->ipnData);
62         }
63
64         /**
65          * Returns a hashmap of raw IPN data
66          *
67          * @return array
68          */
69         public function getRawData() {
70                 return $this->ipnData;
71         }
72
73         /**
74          * Validates a IPN message
75          *
76          * @return boolean
77          */
78         public function validate() {
79                 if(isset($this->isIpnVerified))
80                 {
81                         return $this->isIpnVerified;
82                 }
83                 else
84                 {
85                         $request = self::IPN_CMD;
86                         if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc() == 1) {
87                                 $get_magic_quotes_exists = true;
88                         } else {
89                                 $get_magic_quotes_exists = false;
90                         }
91                         foreach ($this->ipnData as $key => $value) {
92                                 if($get_magic_quotes_exists) {
93                                         $value = urlencode(stripslashes($value));
94                                 } else {
95                                         $value = urlencode($value);
96                                 }
97                                 $request .= "&$key=$value";
98                         }
99                                 
100                         $httpConfig = new PPHttpConfig($this->setEndpoint());
101                         $httpConfig->addCurlOption(CURLOPT_FORBID_REUSE, 1);
102                         $httpConfig->addCurlOption(CURLOPT_HTTPHEADER, array('Connection: Close'));
103
104                         $connection = PPConnectionManager::getInstance()->getConnection($httpConfig, $this->config);
105                         $response = $connection->execute($request);
106                         if($response == 'VERIFIED') {
107                                 $this->isIpnVerified = true;
108                                 return true;
109                         }
110                         $this->isIpnVerified = false;
111                         return false; // value is 'INVALID'
112                 }
113         }
114
115         /**
116          * Returns the transaction id for which
117          * this IPN was generated, if one is available
118          *
119          * @return string
120          */
121         public function getTransactionId() {
122                 if(isset($this->ipnData['txn_id'])) {
123                         return $this->ipnData['txn_id'];
124                 } else if(isset($this->ipnData['transaction[0].id'])) {
125                         $idx = 0;
126                         do {
127                                 $transId[] =  $this->ipnData["transaction[$idx].id"];
128                                 $idx++;
129                         } while(isset($this->ipnData["transaction[$idx].id"]));
130                         return $transId;
131                 }
132         }
133
134         /**
135          * Returns the transaction type for which
136          * this IPN was generated
137          *
138          * @return string
139          */
140         public function getTransactionType() {
141                 return $this->ipnData['transaction_type'];
142         }
143         
144         private function setEndpoint()
145         {
146                 if(isset($this->config['service.EndPoint.IPN']))
147                 {
148                         $url = $this->config['service.EndPoint.IPN'];
149                 }
150                 else if(isset($this->config['mode']))
151                 {
152                         if(strtoupper($this->config['mode']) == 'SANDBOX')
153                         {
154                                 $url = PPConstants::IPN_SANDBOX_ENDPOINT;
155                         }
156                         else if (strtoupper($this->config['mode']) == 'LIVE')
157                         {
158                                 $url = PPConstants::IPN_LIVE_ENDPOINT;
159                         }
160                         else
161                         {
162                                 throw new PPConfigurationException('mode should be LIVE or SANDBOX');
163                         }
164                 }
165                 else
166                 {
167                         throw new PPConfigurationException('No COnfig file found');
168                 }
169                 return $url;
170         }
171
172 }