File indexing completed on 2025-05-04 05:32:14
0001 <?php 0002 0003 /** 0004 * ocs-webserver 0005 * 0006 * Copyright 2016 by pling GmbH. 0007 * 0008 * This file is part of ocs-webserver. 0009 * 0010 * This program is free software: you can redistribute it and/or modify 0011 * it under the terms of the GNU Affero General Public License as 0012 * published by the Free Software Foundation, either version 3 of the 0013 * License, or (at your option) any later version. 0014 * 0015 * This program is distributed in the hope that it will be useful, 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0018 * GNU Affero General Public License for more details. 0019 * 0020 * You should have received a copy of the GNU Affero General Public License 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0022 **/ 0023 abstract class Local_Payment_PayPal_Base 0024 { 0025 /** @var \Zend_Config */ 0026 protected $_config; 0027 /** @var \Zend_Log */ 0028 protected $_logger; 0029 0030 /** 0031 * @param array|Zend_config $config 0032 * @param Zend_Log_Writer_Abstract $logger 0033 * 0034 * @throws Exception 0035 */ 0036 function __construct($config, $logger = null) 0037 { 0038 if (is_array($config)) { 0039 $this->_config = new Zend_Config($config); 0040 } else { 0041 if ($config instanceof Zend_Config) { 0042 $this->_config = $config; 0043 } 0044 } 0045 if (is_null($logger)) { 0046 $this->_logger = Zend_Registry::get('logger'); 0047 } else { 0048 if ($logger instanceof Zend_Log) { 0049 $this->_logger = $logger; 0050 } else { 0051 throw new Exception('Logger must be an instance of Zend_Log'); 0052 } 0053 } 0054 0055 $this->_paymentUserData = new Local_Payment_PayPal_UserData(); 0056 } 0057 0058 /** 0059 * @param array $request 0060 * @param string $apiName 0061 * @param string $apiOperation 0062 * @param bool $withAuthHeader 0063 * 0064 * @return array 0065 * @throws Local_Payment_Exception 0066 * @throws Zend_Http_Client_Exception 0067 */ 0068 protected function _makeRequest($request, $apiName, $apiOperation, $withAuthHeader = true) 0069 { 0070 $url = $this->_config->api->endpoint . '/' . $apiName . '/' . $apiOperation; 0071 $http = new Zend_Http_Client($url); 0072 if (true === $withAuthHeader) { 0073 $http->setHeaders($this->_buildHeader($this->_config)); 0074 } 0075 $http->setMethod(Zend_Http_Client::POST); 0076 $http->setParameterPost($request); 0077 // Increasing the HTTP timeout 0078 $http->setConfig(array('timeout' => 60)); 0079 0080 try { 0081 $response = $http->request(); 0082 } catch (Zend_Http_Client_Exception $e) { 0083 throw new Local_Payment_Exception('Error while request PayPal website.', 0, $e); 0084 } 0085 0086 if (false === $response) { 0087 $this->_logger->err(__METHOD__ . " - Error while request PayPal Website.\n Server replay was: " . $http->getLastResponse() 0088 ->getStatus() 0089 . PHP_EOL . $http->getLastResponse()->getMessage() . PHP_EOL); 0090 $this->_logger->err(__METHOD__ . ' - Last Request: ' . print_r($http->getLastRequest(), true)); 0091 $this->_logger->err(__METHOD__ . ' - Headers: ' . print_r($response->getHeaders(), true)); 0092 $this->_logger->err(__METHOD__ . ' - Body: ' . print_r($response->getBody(), true) . PHP_EOL); 0093 } else { 0094 $this->_logger->debug(__METHOD__ . ' - Last Request: ' . print_r($http->getLastRequest(), true)); 0095 $this->_logger->debug(__METHOD__ . ' - Headers: ' . print_r($response->getHeaders(), true)); 0096 $this->_logger->debug(__METHOD__ . ' - Body: ' . print_r($response->getBody(), true) . PHP_EOL); 0097 } 0098 0099 $resultArray = $this->_parseRawMessage($response->getBody()); 0100 $this->_logger->debug(__METHOD__ . ' - resultArray' . print_r($resultArray, true) . PHP_EOL); 0101 0102 return $resultArray; 0103 } 0104 0105 /** 0106 * Build all HTTP headers required for the API call. 0107 * 0108 * @access protected 0109 * 0110 * @param array|Zend_Config $config 0111 * 0112 * @return array $headers 0113 */ 0114 protected function _buildHeader($config = null) 0115 { 0116 if (is_array($config)) { 0117 $config = new Zend_Config($config); 0118 } 0119 $header = array( 0120 'X-PAYPAL-SECURITY-USERID: ' . $config->security->userid, 0121 'X-PAYPAL-SECURITY-PASSWORD: ' . $config->security->password, 0122 'X-PAYPAL-SECURITY-SIGNATURE: ' . $config->security->signature, 0123 'X-PAYPAL-REQUEST-DATA-FORMAT: ' . $config->request->data->format, 0124 'X-PAYPAL-RESPONSE-DATA-FORMAT: ' . $config->response->data->format, 0125 'X-PAYPAL-APPLICATION-ID: ' . $config->application->id 0126 ); 0127 0128 if (APPLICATION_ENV == 'development') { 0129 array_push($header, 'X-PAYPAL-SANDBOX-EMAIL-ADDRESS: ' . $config->sandbox->email); 0130 } 0131 0132 return $header; 0133 } 0134 0135 /** 0136 * @param string $raw_post 0137 * 0138 * @return array 0139 */ 0140 protected function _parseRawMessage($raw_post) 0141 { 0142 //log_message('error', "testing"); 0143 if (empty($raw_post)) { 0144 return array(); 0145 } # else: 0146 $parsedPost = array(); 0147 $pairs = explode('&', $raw_post); 0148 foreach ($pairs as $pair) { 0149 list($key, $value) = explode('=', $pair, 2); 0150 $key = urldecode($key); 0151 $value = urldecode($value); 0152 # This is look for a key as simple as 'return_url' or as complex as 'somekey[x].property' 0153 // preg_match('/(\w+)(?:\[(\d+)\])?(?:\.(\w+))?/', $key, $key_parts); 0154 preg_match('/(\w+)(?:(?:\[|\()(\d+)(?:\]|\)))?(?:\.(\w+))?/', $key, $key_parts); 0155 switch (count($key_parts)) { 0156 case 4: 0157 # Original key format: somekey[x].property 0158 # Converting to $post[somekey][x][property] 0159 if (false === isset($parsedPost[$key_parts[1]])) { 0160 if (empty($key_parts[2]) && '0' != $key_parts[2]) { 0161 $parsedPost[$key_parts[1]] = array($key_parts[3] => $value); 0162 } else { 0163 $parsedPost[$key_parts[1]] = array($key_parts[2] => array($key_parts[3] => $value)); 0164 } 0165 } else { 0166 if (false === isset($parsedPost[$key_parts[1]][$key_parts[2]])) { 0167 if (empty($key_parts[2]) && '0' != $key_parts[2]) { 0168 $parsedPost[$key_parts[1]][$key_parts[3]] = $value; 0169 } else { 0170 $parsedPost[$key_parts[1]][$key_parts[2]] = array($key_parts[3] => $value); 0171 } 0172 } else { 0173 $parsedPost[$key_parts[1]][$key_parts[2]][$key_parts[3]] = $value; 0174 } 0175 } 0176 break; 0177 case 3: 0178 # Original key format: somekey[x] 0179 # Converting to $post[somekey][x] 0180 if (!isset($parsedPost[$key_parts[1]])) { 0181 $parsedPost[$key_parts[1]] = array(); 0182 } 0183 $parsedPost[$key_parts[1]][$key_parts[2]] = $value; 0184 break; 0185 default: 0186 # No special format 0187 $parsedPost[$key] = $value; 0188 break; 0189 } 0190 #switch 0191 } 0192 0193 #foreach 0194 0195 return $parsedPost; 0196 } 0197 0198 }