Changeset 42 for trunk/lib/PayPal.inc.php
- Timestamp:
- Dec 18, 2005 12:16:03 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/PayPal.inc.php
r41 r42 1 1 <?php 2 2 /** 3 * The PayPal:: class provides functions for creating PayPal buttons and for 3 * The PayPal:: class provides functions for creating PayPal buttons and for 4 4 * receiving PayPal's Instant Payment Notification (IPN) service. 5 5 * … … 8 8 */ 9 9 class PayPal { 10 10 11 11 // General object parameters. 12 12 var $_params = array( … … 14 14 'test_mode' => false, 15 15 ); 16 17 // Options used for specific buttons and links. 16 17 // Options used for specific buttons and links. 18 18 var $_default_button_options = array(); 19 19 20 20 // Array of buttons created by newButton(). 21 21 var $_buttons = array(); 22 22 23 23 // Store the response from the last IPN. 24 24 var $_ipn_response; 25 25 26 26 /** 27 27 * Constructor. … … 38 38 $url = 'www.paypal.com'; 39 39 } 40 40 41 41 $this->_default_button_options = array( 42 42 '_global' => array( … … 59 59 ); 60 60 } 61 62 /** 63 * Updates the _default_button_options array with options used for 61 62 /** 63 * Updates the _default_button_options array with options used for 64 64 * specific buttons, or all buttons if $type is null. 65 65 * 66 66 * @access public 67 67 * 68 * @param mixed $type The type of button to set defaults. If null, 68 * @param mixed $type The type of button to set defaults. If null, 69 69 * sets the global button types. 70 70 * @param array $options Options to set for button. … … 78 78 return false; 79 79 } 80 80 81 81 if (is_null($type) || '_global' == $type) { 82 82 $this->_default_button_options['_global'] = array_merge($this->_default_button_options['_global'], $options); … … 85 85 return false; 86 86 } 87 87 88 88 $this->_default_button_options[$type] = array_merge($this->_default_button_options[$type], $options); 89 89 return true; 90 90 } 91 91 92 92 /** 93 93 * Creates a new element in the _buttons array. Uses _default_button_options … … 108 108 return false; 109 109 } 110 110 111 111 if (!is_array($options) || empty($options)) { 112 112 App::logMsg(sprintf('Invalid options: %s', getDump($options)), LOG_WARNING, __FILE__, __LINE__); 113 113 return false; 114 114 } 115 115 116 116 if (isset($this->_buttons[$name])) { 117 117 App::logMsg(sprintf('Overwriting existing button name: %s', getDump($this->_buttons[$name])), LOG_DEBUG, __FILE__, __LINE__); 118 118 } 119 119 120 120 $this->_buttons[$name] = array( 121 121 'type' => $type, … … 141 141 return false; 142 142 } 143 143 144 144 $query_string = ''; 145 145 $delim = ''; … … 156 156 $search = array('/%2F/'); 157 157 $replace = array('/'); 158 158 159 159 return $this->_buttons[$name]['options']['link_url'] . preg_replace($search, $replace, $query_string); 160 160 } … … 183 183 ?> 184 184 <form action="<?php echo $this->_buttons[$name]['options']['button_url']; ?>" method="post"> 185 <?php 185 <?php 186 186 if (is_array($this->_buttons[$name]['options']) && !empty($this->_buttons[$name]['options'])) { 187 187 foreach ($this->_buttons[$name]['options'] as $key=>$val) { … … 192 192 } 193 193 } 194 } 194 } 195 195 ?> 196 196 <input type="image" src="<?php echo $this->_buttons[$name]['options']['submit_img']; ?>" border="0" name="submit" alt="<?php echo $this->_buttons[$name]['options']['submit_text']; ?>" /> … … 241 241 function incomingIPNRequest() 242 242 { 243 if ($_SERVER['REQUEST_METHOD'] == 'POST' 243 if ($_SERVER['REQUEST_METHOD'] == 'POST' 244 244 && $_SERVER['CONTENT_TYPE'] == 'application/x-www-form-urlencoded' 245 245 && !empty($_POST)) { … … 258 258 */ 259 259 function processIPN() 260 { 260 { 261 261 if (getPost('test_ipn') == '1' || $this->getParam('test_mode')) { 262 262 App::logMsg(sprintf('Processing PayPal IPN in test mode: %s', getDump(getFormData())), LOG_DEBUG, __FILE__, __LINE__); … … 266 266 $url = parse_url($this->getParam('paypal_url')); 267 267 } 268 268 269 269 // Read POST request and add 'cmd'. 270 270 $received_data = getPost(); … … 273 273 $return_data .= '&' . $post_key . '=' . urlencode($post_val); 274 274 } 275 275 276 276 // Set the port number based on the scheme. 277 if ($url['scheme'] == "https") { 277 if ($url['scheme'] == "https") { 278 278 $url['port'] = 443; 279 279 $ssl = 'ssl://'; … … 282 282 $ssl = ''; 283 283 } 284 284 285 285 // Open connection to PayPal server. 286 $fp = fsockopen($ssl . $url['host'], $url['port'], $errnum, $errstr, 30); 286 $fp = fsockopen($ssl . $url['host'], $url['port'], $errnum, $errstr, 30); 287 287 288 288 if (!$fp) { … … 290 290 return false; 291 291 } else { 292 fputs($fp, "POST {$url['path']} HTTP/1.1\r\n"); 293 fputs($fp, "Host: {$url['host']}\r\n"); 294 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 295 fputs($fp, "Content-length: " . strlen($return_data) . "\r\n"); 296 fputs($fp, "Connection: close\r\n\r\n"); 297 fputs($fp, $return_data . "\r\n\r\n"); 298 292 fputs($fp, "POST {$url['path']} HTTP/1.1\r\n"); 293 fputs($fp, "Host: {$url['host']}\r\n"); 294 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 295 fputs($fp, "Content-length: " . strlen($return_data) . "\r\n"); 296 fputs($fp, "Connection: close\r\n\r\n"); 297 fputs($fp, $return_data . "\r\n\r\n"); 298 299 299 // Loop through the response lines from the server. 300 300 $this->_ipn_response = ''; … … 303 303 } 304 304 fclose($fp); 305 305 306 306 App::logMsg(sprintf('IPN response received: %s', $this->_ipn_response), LOG_NOTICE, __FILE__, __LINE__); 307 307 return true; 308 308 } 309 309 } 310 310 311 311 /** 312 312 * Checks the response received from PayPal's IPN upon calling processIPN(). … … 322 322 return false; 323 323 } 324 324 325 325 if (empty($this->_ipn_response)) { 326 326 App::logMsg(sprintf('Cannot verify IPN, response empty.', null), LOG_WARNING, __FILE__, __LINE__); 327 327 return false; 328 328 } 329 329 330 330 if (preg_match('/VERIFIED/', $this->_ipn_response)) { 331 331 App::logMsg(sprintf('IPN verified!', null), LOG_DEBUG, __FILE__, __LINE__); … … 339 339 } 340 340 } 341 342 341 342 343 343 } // End of class. 344 344
Note: See TracChangeset
for help on using the changeset viewer.