/** * Make a call to Payment Bridge service with given request parameters * * @param array $request * @return array * @throws Mage_Core_Exception */ protected function _call(array $request) { $response = null; $debugData = array('request' => $request); try { $http = new Varien_Http_Adapter_Curl(); $config = array('timeout' => 30); $http->setConfig($config); $http->write(Zend_Http_Client::POST, $this->getPbridgeEndpoint(), '1.1', array(), $this->_prepareRequestParams($request)); $response = $http->read(); $http->close(); } catch (Exception $e) { $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode()); $this->_debug($debugData); throw $e; } $this->_debug($response); if ($response) { $response = preg_split('/^\\r?$/m', $response, 2); $response = Mage::helper('core')->jsonDecode(trim($response[1])); $debugData['result'] = $response; $this->_debug($debugData); if ($http->getErrno()) { Mage::logException(new Exception(sprintf('Payment Bridge CURL connection error #%s: %s', $http->getErrno(), $http->getError()))); Mage::throwException(Mage::helper('enterprise_pbridge')->__('Unable to communicate with Payment Bridge service.')); } if (isset($response['status']) && $response['status'] == 'Success') { $this->_response = $response; return true; } } $this->_handleError($response); $this->_response = $response; return false; }
/** * This method will send call to external end points using GET or POST method * * @param type $apiType url that needs to be called * @param type $requestType GET or POST * @param type $data model data that needs to be send with url * @return type json * @throws Exception */ public function send($apiType, $requestType, $data) { $curlAdapter = new Varien_Http_Adapter_Curl(); $curlAdapter->setConfig(array('timeout' => 20)); $body_param = Mage::helper('core')->jsonEncode($data); $header = $this->getHeader(); try { $curlAdapter->write($requestType, $apiType, '1.1', $header, $body_param); $result = $curlAdapter->read(); $response = preg_split('/^\\r?$/m', $result, 2); $response = trim($response[1]); $res = NULL; if (!is_null($response) && !empty($response)) { $res = Mage::helper('core')->jsonDecode($response); } else { Mage::log('Encountered problems trying to contact Terapeak'); Mage::log($curlAdapter->getError()); } } catch (Exception $e) { //eat the execption. We should not crash the magento instance if our extention throws exceptions Mage::log('Exception when trying to send data to Terapeak:'); Mage::log($e); } return $res; }
/** * Do the API call * * @param string $methodName * @param array $request * @return array * @throws Mage_Core_Exception */ public function call($methodName, array $request) { $request = $this->_addMethodToRequest($methodName, $request); $eachCallRequest = $this->_prepareEachCallRequest($methodName); if ($this->getUseCertAuthentication()) { if ($key = array_search('SIGNATURE', $eachCallRequest)) { unset($eachCallRequest[$key]); } } $request = $this->_exportToRequest($eachCallRequest, $request); $debugData = array('url' => $this->getApiEndpoint(), $methodName => $request); try { $http = new Varien_Http_Adapter_Curl(); $config = array('timeout' => 30, 'verifypeer' => $this->_config->verifyPeer); if ($this->getUseProxy()) { $config['proxy'] = $this->getProxyHost() . ':' . $this->getProxyPort(); } if ($this->getUseCertAuthentication()) { $config['ssl_cert'] = $this->getApiCertificate(); } $http->setConfig($config); $http->write(Zend_Http_Client::POST, $this->getApiEndpoint(), '1.1', $this->_headers, $this->_buildQuery($request)); $response = $http->read(); } catch (Exception $e) { $debugData['http_error'] = array('error' => $e->getMessage(), 'code' => $e->getCode()); $this->_debug($debugData); throw $e; } $response = preg_split('/^\\r?$/m', $response); $response = trim(end($response)); $response = $this->_deformatNVP($response); $debugData['response'] = $response; $this->_debug($debugData); $response = $this->_postProcessResponse($response); // handle transport error if ($http->getErrno()) { Mage::logException(new Exception(sprintf('PayPal NVP CURL connection error #%s: %s', $http->getErrno(), $http->getError()))); $http->close(); Mage::throwException(Mage::helper('paypal')->__('Unable to communicate with the PayPal gateway.')); } // cUrl resource must be closed after checking it for errors $http->close(); if (!$this->_validateResponse($methodName, $response)) { Mage::logException(new Exception(Mage::helper('paypal')->__("PayPal response hasn't required fields."))); Mage::throwException(Mage::helper('paypal')->__('There was an error processing your order. Please contact us or try again later.')); } $this->_callErrors = array(); if ($this->_isCallSuccessful($response)) { if ($this->_rawResponseNeeded) { $this->setRawSuccessResponseData($response); } return $response; } $this->_handleCallErrors($response); return $response; }
/** * Post a request, note that for a HTTPS URL no port is required * * @param string $url * @param string $path * @param array $params * @return mixed */ protected function _post($url, $timeout, $dataToSend) { $config = array('timeout' => 30); $this->_http->setConfig($config); $this->_http->write(Zend_Http_Client::POST, $url, '1.1', array(), $dataToSend); $response = $this->_http->read(); $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); if ($this->_http->getErrno()) { $this->_http->close(); $this->setError($this->_http->getErrno() . ':' . $this->_http->getError()); return false; } $this->_http->close(); return $response; }
/** * Function to perform the API call to PayPal using API signature * * @param $methodName string is name of API method. * @param $nvpArr array NVP params array * @return array|boolean an associtive array containing the response from the server or false in case of error. */ public function call($methodName, array $nvpArr) { $nvpArr = array_merge(array('METHOD' => $methodName, 'VERSION' => $this->getVersion(), 'USER' => $this->getApiUserName(), 'PWD' => $this->getApiPassword(), 'SIGNATURE' => $this->getApiSignature()), $nvpArr); $nvpReq = ''; foreach ($nvpArr as $k => $v) { $nvpReq .= '&' . $k . '=' . urlencode($v); } $nvpReq = substr($nvpReq, 1); if ($this->getDebug()) { $debug = Mage::getModel('paypal/api_debug')->setApiEndpoint($this->getApiEndpoint())->setRequestBody($nvpReq)->save(); } $http = new Varien_Http_Adapter_Curl(); $config = array(); if ($this->getUseProxy()) { $config['proxy'] = $this->getProxyHost() . ':' . $this->getProxyPort(); } $http->setConfig($config); $http->write(Zend_Http_Client::POST, $this->getApiEndpoint(), '1.1', array(), $nvpReq); $response = $http->read(); $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); if ($this->getDebug()) { $debug->setResponseBody($response)->save(); } $nvpReqArray = $this->deformatNVP($nvpReq); $this->getSession()->setNvpReqArray($nvpReqArray); if ($http->getErrno()) { $http->close(); $this->setError(array('type' => 'CURL', 'code' => $http->getErrno(), 'message' => $http->getError())); $this->setRedirectUrl($this->getApiErrorUrl()); return false; } $http->close(); //converting NVPResponse to an Associative Array $nvpResArray = $this->deformatNVP($response); $this->getSession()->setLastCallMethod($methodName)->setResHash($nvpResArray); $ack = strtoupper($nvpResArray['ACK']); if ($ack == 'SUCCESS') { $this->unsError(); return $nvpResArray; } $errorArr = array('type' => 'API', 'ack' => $ack); if (isset($nvpResArray['VERSION'])) { $errorArr['version'] = $nvpResArray['VERSION']; } if (isset($nvpResArray['CORRELATIONID'])) { $errorArr['correlation_id'] = $nvpResArray['CORRELATIONID']; } for ($i = 0; isset($nvpResArray['L_SHORTMESSAGE' . $i]); $i++) { $errorArr['code'] = $nvpResArray['L_ERRORCODE' . $i]; $errorArr['short_message'] = $nvpResArray['L_SHORTMESSAGE' . $i]; $errorArr['long_message'] = $nvpResArray['L_LONGMESSAGE' . $i]; } $this->setError($errorArr); $this->setRedirectUrl($this->getApiErrorUrl()); return false; }
/** * Do the API call * * @param string $methodName * @param array $request * @return array * @throws Mage_Core_Exception */ public function call($methodName, array $request) { $request = $this->_addMethodToRequest($methodName, $request); $eachCallRequest = $this->_prepareEachCallRequest($methodName); $request = $this->_exportToRequest($eachCallRequest, $request); $debugData = array('url' => $this->getApiEndpoint(), $methodName => $request); try { $http = new Varien_Http_Adapter_Curl(); $config = array('timeout' => 30); if ($this->getUseProxy()) { $config['proxy'] = $this->getProxyHost() . ':' . $this->getProxyPort(); } $http->setConfig($config); $http->write(Zend_Http_Client::POST, $this->getApiEndpoint(), '1.1', array(), $this->_buildQuery($request)); $response = $http->read(); } catch (Exception $e) { $debugData['http_error'] = array('error' => $e->getMessage(), 'code' => $e->getCode()); $this->_debug($debugData); throw $e; } $http->close(); $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); $response = $this->_deformatNVP($response); $debugData['response'] = $response; $this->_debug($debugData); // handle transport error if ($http->getErrno()) { Mage::logException(new Exception(sprintf('PayPal NVP CURL connection error #%s: %s', $http->getErrno(), $http->getError()))); Mage::throwException(Mage::helper('paypal')->__('Unable to communicate with the PayPal gateway.')); } $this->_callErrors = array(); if ($this->_isCallSuccessful($response)) { if ($this->_rawResponseNeeded) { $this->setRawSuccessResponseData($response); } return $response; } $this->_handleCallErrors($response); return $response; }
/** * Send HTTP POST request to magentocommerce.com * * @return null */ protected function _processPostRequest() { try { /** @var $app Mage_XmlConnect_Model_Application */ $app = Mage::helper('xmlconnect')->getApplication(); $params = $app->getSubmitParams(); $appConnectorUrl = Mage::getStoreConfig('xmlconnect/mobile_application/magentocommerce_url'); $curl = new Varien_Http_Adapter_Curl(); $verifyPeerValue = Mage::getStoreConfig('xmlconnect/mobile_application/curl_ssl_verifypeer'); $curl->setConfig(array('timeout' => Mage_XmlConnect_Helper_Data::CURLOPT_DEFAULT_TIMEOUT, 'verifypeer' => $verifyPeerValue, 'verifyhost' => 2, 'header' => false)); $mCommerceUrl = $appConnectorUrl . $params['key']; $curl->write(Zend_Http_Client::POST, $mCommerceUrl, CURL_HTTP_VERSION_1_1, array(), $params); $result = $curl->read(); if (false === $result) { Mage::log('Curl error: ' . $curl->getError()); $curl->close(); Mage::throwException($this->__('Request internal error.')); } $curl->close(); // Assert that we received an expected message in reponse. $resultArray = json_decode($result, true); $app->setResult($result); $success = isset($resultArray['success']) && $resultArray['success'] === true; $app->setSuccess($success); if (!$app->getSuccess()) { $message = isset($resultArray['message']) ? $resultArray['message'] : ''; if (is_array($message)) { $message = implode(' ,', $message); } Mage::throwException($this->__('Submit App failure. %s', $message)); } } catch (Exception $e) { throw $e; } }
/** * Do the API call * * @param string $methodName * @param array $request * @return array * @throws Mage_Core_Exception */ public function call($methodName, array $request) { $request = $this->_addMethodToRequest($methodName, $request); $request = $this->_exportToRequest($this->_eachCallRequest, $request); if ($this->getDebug()) { $requestDebug = $request; foreach ($this->_debugReplacePrivateDataKeys as $key) { if (isset($request[$key])) { $requestDebug[$key] = '***'; } } $debug = Mage::getModel('paypal/api_debug')->setApiEndpoint($this->getApiEndpoint())->setRequestBody(var_export($requestDebug, 1))->save(); } $http = new Varien_Http_Adapter_Curl(); $config = array('timeout' => 30); if ($this->getUseProxy()) { $config['proxy'] = $this->getProxyHost() . ':' . $this->getProxyPort(); } $http->setConfig($config); $http->write(Zend_Http_Client::POST, $this->getApiEndpoint(), '1.1', array(), $this->_buildQuery($request)); $response = $http->read(); $http->close(); $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); $response = $this->_deformatNVP($response); if ($this->getDebug()) { $debug->setResponseBody(var_export($response, 1))->save(); } // handle transport error if ($http->getErrno()) { Mage::logException(new Exception(sprintf('PayPal NVP CURL connection error #%s: %s', $http->getErrno(), $http->getError()))); Mage::throwException(Mage::helper('paypal')->__('Unable to communicate with PayPal gateway.')); } if ($this->_isCallSuccessful($response)) { return $response; } $this->_handleCallErrors($response); return $response; }
protected function _call(Varien_Object $payment) { if ($this->getDebug()) { $writer = new Zend_Log_Writer_Stream($this->getLogPath()); $logger = new Zend_Log($writer); $logger->info("entering _call()"); } // Generate any needed values // Create expiry dat in format "MM/YY" $date_expiry = str_pad($payment->getCcExpMonth(), 2, '0', STR_PAD_LEFT) . '/' . substr($payment->getCcExpYear(), 2, 2); $transaction_id = $payment->getOrder()->getIncrementId(); $messageTimestamp = date('YdmHis000000+000', time()); if ($this->getDebug()) { $logger->info(var_export($payment->getOrder()->getData(), true)); } // Build the XML request $doc = new SimpleXMLElement('<NABTransactMessage></NABTransactMessage>'); $messageInfo = $doc->addChild('MessageInfo'); $messageInfo->addChild('messageID', substr(md5($transaction_id . $messageTimestamp), 0, 30)); $messageInfo->addChild('messageTimestamp', htmlentities($messageTimestamp)); $messageInfo->addChild('timeoutValue', htmlentities('60')); $messageInfo->addChild('apiVersion', htmlentities('xml-4.2')); $merchantInfo = $doc->addChild('MerchantInfo'); $merchantInfo->addChild('merchantID', htmlentities($this->getUsername())); $merchantInfo->addChild('password', htmlentities($this->getPassword())); $doc->addChild('RequestType', 'Payment'); // Currently Nab only allows one transaction per request $txnList = $doc->addChild('Payment')->addChild('TxnList'); $txnList->addAttribute('count', '1'); //htmlentities($transaction_id) $txn = $txnList->addChild('Txn'); $txn->addAttribute('ID', '1'); // Currently must be set to "1" $txn->addChild('txnType', '0'); // Set to "0" for payment $txn->addChild('txnSource', '23'); // Set to "23" for SecureXML $txn->addChild('amount', htmlentities($this->getAmount() * 100)); // In cents $txn->addChild('currency', htmlentities($payment->getOrder()->getBaseCurrencyCode())); $txn->addChild('purchaseOrderNo', htmlentities($transaction_id)); $cc = $txn->addChild('CreditCardInfo'); $cc->addChild('cardNumber', htmlentities($payment->getCcNumber())); $cc->addChild('cvv', htmlentities($payment->getCcCid())); $cc->addChild('expiryDate', htmlentities($date_expiry)); $xml = $doc->asXML(); // DEBUG if ($this->getDebug()) { $logger->info($xml); } // Send the data via HTTP POST and get the response $http = new Varien_Http_Adapter_Curl(); $http->setConfig(array('timeout' => 30)); $http->write(Zend_Http_Client::POST, $this->getGatewayUrl(), '1.1', array(), $xml); $response = $http->read(); if ($http->getErrno()) { $http->close(); $this->setError(array('message' => $http->getError())); return false; } // DEBUG if ($this->getDebug()) { $logger->info($response); } $http->close(); // Strip out header tags $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); // Parse the XML object $xmlObj = simplexml_load_string($response); // Build an associative array with returned values $result = array(); $result['Status'] = array(); $result['Payment'] = array('TxnList' => array('Txn' => array())); $xpath = $xmlObj->xpath('/NABTransactMessage/Status/statusCode'); $result['Status']['statusCode'] = $xpath !== false && isset($xpath[0]) ? (string) $xpath[0] : ''; $xpath = $xmlObj->xpath('/NABTransactMessage/Status/statusDescription'); $result['Status']['statusDescription'] = $xpath !== false && isset($xpath[0]) ? (string) $xpath[0] : ''; $xpath = $xmlObj->xpath('/NABTransactMessage/Payment/TxnList/Txn/approved'); $result['Payment']['TxnList']['Txn']['approved'] = $xpath !== false && isset($xpath[0]) ? (string) $xpath[0] : ''; $xpath = $xmlObj->xpath('/NABTransactMessage/Payment/TxnList/Txn/responseCode'); $result['Payment']['TxnList']['Txn']['responseCode'] = $xpath !== false && isset($xpath[0]) ? (string) $xpath[0] : ''; $xpath = $xmlObj->xpath('/NABTransactMessage/Payment/TxnList/Txn/responseText'); $result['Payment']['TxnList']['Txn']['responseText'] = $xpath !== false && isset($xpath[0]) ? (string) $xpath[0] : ''; return $result; }
public function postRequest(array $proArr) { $proArr = array_merge(array('PARTNER' => $this->getPartner(), 'USER' => $this->getApiUser(), 'VENDOR' => $this->getApiVendor(), 'PWD' => $this->getApiPassword(), 'TRXTYPE' => $this->getTrxtype(), 'REQUEST_ID' => $this->_generateRequestId()), $proArr); $proReq = ''; foreach ($proArr as $k => $v) { //payflow gateway doesn't allow urlencoding. //$proReq .= '&'.$k.'='.urlencode($v); $proReq .= '&' . $k . '=' . $v; } $proReq = substr($proReq, 1); if ($this->getDebug()) { $debug = Mage::getModel('paypaluk/api_debug')->setRequestBody($proReq)->save(); } $http = new Varien_Http_Adapter_Curl(); $config = array('timeout' => 30); $http->setConfig($config); $http->write(Zend_Http_Client::POST, $this->getApiUrl(), '1.1', array(), $proReq); $response = $http->read(); $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); if ($this->getDebug()) { $debug->setResponseBody($response)->save(); } if ($http->getErrno()) { $http->close(); $this->setError(array('type' => 'CURL', 'code' => $http->getErrno(), 'message' => $http->getError())); $this->setRedirectUrl($this->getApiErrorUrl()); return false; } $http->close(); $result = Mage::getModel('paypaluk/api_result'); $valArray = $this->deformatNVP($response); foreach ($valArray as $k => $v) { $result->setData(strtolower($k), $v); } $result->setResultCode($result->getResult())->setRespmsg($result->getRespmsg()); /* $client = new Varien_Http_Client(); $uri = $this->getApiUrl(); $client->setUri($uri) ->setConfig(array( 'maxredirects'=>5, 'timeout'=>30, )) ->setMethod(Zend_Http_Client::POST) ->setParameterPost($proArr) ->setHeaders('X-VPS-VIT-CLIENT-CERTIFICATION-ID: 33baf5893fc2123d8b191d2d011b7fdc') ->setHeaders('X-VPS-Request-ID: ' . $this->_generateRequestId()) ->setHeaders('X-VPS-CLIENT-TIMEOUT: ' . $this->_clientTimeout) ->setUrlEncodeBody(false); $result = Mage::getModel('paypaluk/api_result'); try { $response = $client->request(); $response = strstr($response->getBody(), 'RESULT'); $valArray = explode('&', $response); foreach($valArray as $val) { $valArray2 = explode('=', $val); $result->setData(strtolower($valArray2[0]), $valArray2[1]); } $result->setResultCode($result->getResult()) ->setRespmsg($result->getRespmsg()); if ($this->getDebug()) { $debug->setResponseBody($response) ->save(); } } catch (Exception $e) { $result->setResultCode(-1) ->setRespmsg($e->getMessage()); } */ return $result; }
/** * Send a HTTP Post request * * @param string $url * @param array $data = array * @return false|string */ public function makeHttpPostRequest($url, array $data = array()) { if (!$this->hasValidCurlMethods()) { return $this->_makeLegacyHttpPostRequest($url, $data); } $curl = new Varien_Http_Adapter_Curl(); $curl->setConfig(array('verifypeer' => strpos($url, 'https://') !== false, 'header' => true, 'timeout' => 15, 'referrer' => Mage::helper('wordpress')->getBaseUrl('wp-login.php'))); $curl->addOption(CURLOPT_FOLLOWLOCATION, false); $curl->addOption(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'); $curl->write(Zend_Http_Client::POST, $url, '1.1', array(), $data); $response = $curl->read(); if ($curl->getErrno() || $curl->getError()) { throw new Exception(Mage::helper('wordpress')->__('CURL (%s): %s', $curl->getErrno(), $curl->getError())); } $curl->close(); return $response; }
/** * Send parameters to eWay gateway. * * @param string $xml * @param string $url * @return array|false */ protected function call($xml, $url) { $http = new Varien_Http_Adapter_Curl(); $config = array('timeout' => Fontis_EwayAu_Helper_Data::DEFAULT_TIMEOUT); $http->setConfig($config); $http->write(Zend_Http_Client::POST, $url, Zend_Http_Client::HTTP_1, array(), $xml); $response = $http->read(); $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); if ($http->getErrno()) { $http->close(); $this->setError(array('message' => $http->getErrno() . ' - ' . $http->getError())); return false; } $http->close(); if (($parsedResArr = $this->parseXmlResponse($response)) === false) { $this->setError(array('message' => 'Invalid response from gateway.')); return false; } if ($parsedResArr['ewayTrxnStatus'] == 'True') { $this->unsError(); return $parsedResArr; } if (isset($parsedResArr['ewayTrxnError'])) { $this->setError(array('message' => $parsedResArr['ewayTrxnError'])); } return false; }
/** * Get ipn data, send verification to PayPal, run corresponding handler */ public function processIpnRequest() { if (!$this->_ipnFormData) { return; } $sReq = ''; foreach ($this->_ipnFormData as $k => $v) { $sReq .= '&' . $k . '=' . urlencode(stripslashes($v)); } // append ipn command $sReq .= "&cmd=_notify-validate"; $sReq = substr($sReq, 1); $url = rtrim(Mage::helper('enterprise_pbridge')->getBridgeBaseUrl(), '/') . '/ipn.php?action=PaypalIpn'; try { $http = new Varien_Http_Adapter_Curl(); $http->write(Zend_Http_Client::POST, $url, '1.1', array(), $sReq); $response = $http->read(); } catch (Exception $e) { throw $e; } if ($error = $http->getError()) { $this->_notifyAdmin(Mage::helper('enterprise_pbridge')->__('IPN postback HTTP error: %s', $error)); return; } if (false !== preg_match('~VERIFIED~si', $response)) { $this->processIpnVerified(); } else { // TODO: possible PCI compliance issue - the $sReq may contain data that is supposed to be encrypted $this->_notifyAdmin(Mage::helper('enterprise_pbridge')->__('IPN postback Validation error: %s', $sReq)); } }
/** * * @param mixed $apiCallResult * @param Varien_Http_Adapter_Curl $apiService */ public function initialize($apiCallResult, Varien_Http_Adapter_Curl $apiService) { $this->_result = $apiCallResult; $this->_error = $apiService->getError(); $this->_httpStatus = $apiService->getInfo(CURLINFO_HTTP_CODE); }
/** * Send params to gateway * * @param string $xml * @return bool | array */ public function call($xml) { if ($this->getDebug()) { $debug = AO::getModel('eway/api_debug')->setRequestBody($xml)->save(); } $http = new Varien_Http_Adapter_Curl(); $config = array('timeout' => 30); $http->setConfig($config); $http->write(Zend_Http_Client::POST, $this->getApiGatewayUrl(), '1.1', array(), $xml); $response = $http->read(); $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); if ($this->getDebug()) { $debug->setResponseBody($response)->save(); } if ($http->getErrno()) { $http->close(); $this->setError(array('message' => $http->getError())); return false; } $http->close(); $parsedResArr = $this->parseXmlResponse($response); if ($parsedResArr['ewayTrxnStatus'] == 'True') { $this->unsError(); return $parsedResArr; } if (isset($parsedResArr['ewayTrxnError'])) { $this->setError(array('message' => $parsedResArr['ewayTrxnError'])); } return false; }
/** * Making a call to gateway * * @param string $requestStr * @return bool | array */ public function call($requestStr) { if ($this->getDebugFlag()) { $debug = Mage::getModel('paybox/api_debug')->setRequestBody($requestStr)->save(); } $recall = true; $recallCounter = 0; while ($recall && $recallCounter < 3) { $recall = false; $this->unsError(); $http = new Varien_Http_Adapter_Curl(); $config = array('timeout' => 30); $http->setConfig($config); $http->write(Zend_Http_Client::POST, $this->getPayboxUrl($recallCounter), '1.1', array(), $requestStr); $response = $http->read(); $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); if ($http->getErrno()) { $http->close(); if ($this->getDebugFlag()) { $debug->setResponseBody($response)->save(); } $this->setError(array('message' => $http->getError())); return false; } $http->close(); $parsedResArr = $this->parseResponseStr($response); //primary gateway is down, need to recall to backup gateway if ($parsedResArr['CODEREPONSE'] == '00001' || $parsedResArr['CODEREPONSE'] == '00097' || $parsedResArr['CODEREPONSE'] == '00098') { $recallCounter++; $recall = true; } } if ($this->getDebugFlag()) { $debug->setResponseBody($response)->save(); } //if backup gateway was down too if ($recall) { $this->setError(array('message' => Mage::helper('paybox')->__('Paybox payment gateway is not available right now'))); return false; } if ($parsedResArr['CODEREPONSE'] == '00000') { return $parsedResArr; } if (isset($parsedResArr['COMMENTAIRE'])) { $this->setError(array('message' => $parsedResArr['CODEREPONSE'] . ':' . $parsedResArr['COMMENTAIRE'])); } return false; }
/** * Get ipn data, send verification to PayPal, run corresponding handler */ public function processIpnRequest() { if (!$this->_ipnFormData) { return; } // debug requested if ($this->_config->debugFlag) { Mage::getModel('paypal/api_debug')->setApiEndpoint($this->_config->getPaypalUrl())->setRequestBody(var_export($this->_ipnFormData, 1))->save(); } $sReq = ''; $sReqDebug = ''; foreach ($this->_ipnFormData as $k => $v) { $sReq .= '&' . $k . '=' . urlencode(stripslashes($v)); $sReqDebug .= '&' . $k . '='; } // append ipn command $sReq .= "&cmd=_notify-validate"; $sReq = substr($sReq, 1); $http = new Varien_Http_Adapter_Curl(); $http->write(Zend_Http_Client::POST, $this->_config->getPaypalUrl(), '1.1', array(), $sReq); $response = $http->read(); // debug postback request & response if ($this->_config->debugFlag) { Mage::getModel('paypal/api_debug')->setApiEndpoint($this->_config->getPaypalUrl())->setRequestBody($sReq)->setResponseBody($response)->save(); } if ($error = $http->getError()) { $this->_notifyAdmin(Mage::helper('paypal')->__('PayPal IPN postback HTTP error: %s', $error)); return; } $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); if ($response == 'VERIFIED') { $this->processIpnVerified(); } else { // TODO: possible PCI compliance issue - the $sReq may contain data that is supposed to be encrypted $this->_notifyAdmin(Mage::helper('paypal')->__('PayPal IPN postback Validation error: %s', $sReq)); } }
/** * Do the API call * * @param string $methodName * @param array $request * @return array * @throws Mage_Core_Exception */ public function call($methodName, array $request) { $request['method'] = $methodName; $request = $this->_exportToRequest($this->_eachCallRequest, $request); if ($this->getDebug()) { $requestDebug = $request; foreach ($this->_debugReplacePrivateDataKeys as $key) { if (isset($request[$key])) { $requestDebug[$key] = '***'; } } $debug = Mage::getModel('paypal/api_debug')->setApiEndpoint($this->getApiEndpoint())->setRequestBody(var_export($requestDebug, 1))->save(); } $http = new Varien_Http_Adapter_Curl(); $config = array('timeout' => 30); if ($this->getUseProxy()) { $config['proxy'] = $this->getProxyHost() . ':' . $this->getProxyPort(); } $http->setConfig($config); $http->write(Zend_Http_Client::POST, $this->getApiEndpoint(), '1.1', array(), http_build_query($request)); $response = $http->read(); $http->close(); $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); $response = $this->_deformatNVP($response); if ($this->getDebug()) { $debug->setResponseBody(var_export($response, 1))->save(); } // handle transport error if ($http->getErrno()) { Mage::logException(new Exception(sprintf('PayPal NVP CURL connection error #%s: %s', $http->getErrno(), $http->getError()))); Mage::throwException(Mage::helper('paypal')->__('Unable to communicate with PayPal gateway.')); } $ack = strtoupper($response['ACK']); $this->_callWarnings = array(); if ($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING') { // collect warnings if ($ack == 'SUCCESSWITHWARNING') { for ($i = 0; isset($response["L_ERRORCODE{$i}"]); $i++) { $this->_callWarnings[] = $response["L_ERRORCODE{$i}"]; } } return $response; } // handle logical errors $errors = array(); for ($i = 0; isset($response["L_ERRORCODE{$i}"]); $i++) { $longMessage = isset($response["L_LONGMESSAGE{$i}"]) ? preg_replace('/\\.$/', '', $response["L_LONGMESSAGE{$i}"]) : ''; $shortMessage = preg_replace('/\\.$/', '', $response["L_SHORTMESSAGE{$i}"]); $errors[] = $longMessage ? sprintf('%s (#%s: %s).', $longMessage, $response["L_ERRORCODE{$i}"], $shortMessage) : sprintf('#%s: %s.', $response["L_ERRORCODE{$i}"], $shortMessage); } if ($errors) { $errors = implode(' ', $errors); $e = new Exception(sprintf('PayPal NVP gateway errors: %s Corellation ID: %s. Version: %s.', $errors, isset($response['CORRELATIONID']) ? $response['CORRELATIONID'] : '', isset($response['VERSION']) ? $response['VERSION'] : '')); Mage::logException($e); Mage::throwException(Mage::helper('paypal')->__('PayPal geteway rejected request. %s', $errors)); } return $response; }
/** * */ protected function _callCapture(Varien_Object $payment) { if ($this->getDebug()) { $writer = new Zend_Log_Writer_Stream($this->getLogPath()); $logger = new Zend_Log($writer); $logger->info('entering _call()'); $logger->info('identificacao: ' . $this->getConfigData('codigo_gateway')); $logger->info('modulo: ' . $this->_modulo); $logger->info('ambiente: ' . $this->getConfigData('ambiente')); $logger->info('tid: ' . $payment->getCcTransId()); } // Generate any needed values $nvpArr = array('identificacao' => $this->getConfigData('codigo_gateway'), 'operacao' => 'Captura', 'modulo' => $this->_modulo, 'ambiente' => $this->getConfigData('ambiente'), 'tid' => $payment->getCcTransId()); if ($this->getDebug()) { $logger->info(var_export($payment->getOrder()->getData(), TRUE)); } $nvpReq = ''; foreach ($nvpArr as $k => $v) { $nvpReq .= '&' . $k . '=' . urlencode($v); } $nvpReq = substr($nvpReq, 1); // DEBUG if ($this->getDebug()) { $logger->info($nvpReq); } // Send the data via HTTP POST and get the response $http = new Varien_Http_Adapter_Curl(); $http->setConfig(array('timeout' => 30)); $http->write(Zend_Http_Client::POST, $this->getGatewayUrl(), '1.1', array(), $nvpReq); $response = $http->read(); if ($http->getErrno()) { $http->close(); $this->setError(array('message' => $http->getError())); return false; } // DEBUG if ($this->getDebug()) { $logger->info($response); } $http->close(); // Strip out header tags $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); // Parse the XML object $xmlObj = simplexml_load_string($response); // Build an associative array with returned values $result = array(); $xpath = $xmlObj->xpath('/ars'); $result['ars'] = $xpath !== FALSE ? $xpath[0] : ''; $xpath = $xmlObj->xpath('/tid'); $result['tid'] = $xpath !== FALSE ? $xpath[0] : ''; $xpath = $xmlObj->xpath('/lr'); $result['lr'] = $xpath !== FALSE ? $xpath[0] : ''; $xpath = $xmlObj->xpath('/cap'); $result['cap'] = $xpath !== FALSE ? $xpath[0] : ''; $xpath = $xmlObj->xpath('/free'); $result['free'] = $xpath !== FALSE ? $xpath[0] : ''; return $result; }
/** * Send a HTTP Post request * * @param string $url * @param array $data = array * @return false|string */ public function makeHttpPostRequest($url, array $data = array()) { if (!$this->hasValidCurlMethods()) { foreach ($data as $key => $value) { $data[$key] = urlencode($key) . '=' . urlencode($value); } $body = implode('&', $data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, count($data)); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_USERAGENT, self::CURL_USERAGENT); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if (strpos($url, 'https://') !== false) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); } $response = curl_exec($ch); if (curl_errno($ch) || curl_error($ch)) { throw new Exception(Mage::helper('wordpress')->__('CURL (%s): %s', curl_errno($ch), curl_error($ch))); } curl_close($ch); return $response; } $curl = new Varien_Http_Adapter_Curl(); $curl->setConfig(array('verifypeer' => strpos($url, 'https://') !== false, 'header' => true, 'timeout' => 15, 'referrer' => Mage::helper('wordpress')->getBaseUrl('wp-login.php'))); $curl->addOption(CURLOPT_FOLLOWLOCATION, false); $curl->addOption(CURLOPT_USERAGENT, self::CURL_USERAGENT); $curl->addOption(CURLOPT_REFERER, true); $curl->write(Zend_Http_Client::POST, $url, '1.1', array('Expect:'), $data); $response = $curl->read(); if ($curl->getErrno() || $curl->getError()) { throw new Exception(Mage::helper('wordpress')->__('CURL (%s): %s', $curl->getErrno(), $curl->getError())); } $curl->close(); return $response; }
/** * Send params to gateway * * @param string $xml * @return bool | array */ public function call($xml) { $debugData = array('request' => $xml); try { $http = new Varien_Http_Adapter_Curl(); $config = array('timeout' => 30); $http->setConfig($config); $http->write(Zend_Http_Client::POST, $this->getApiGatewayUrl(), '1.1', array(), $xml); $response = $http->read(); $response = preg_split('/^\\r?$/m', $response, 2); $response = trim($response[1]); $debugData['result'] = $response; } catch (Exception $e) { $debugData['result'] = array('error' => $e->getMessage(), 'code' => $e->getCode()); $this->_debug($debugData); throw $e; } $this->_debug($debugData); if ($http->getErrno()) { $http->close(); $this->setError(array('message' => $http->getError())); return false; } $http->close(); $parsedResArr = $this->parseXmlResponse($response); if ($parsedResArr['ewayTrxnStatus'] == 'True') { $this->unsError(); return $parsedResArr; } if (isset($parsedResArr['ewayTrxnError'])) { $this->setError(array('message' => $parsedResArr['ewayTrxnError'])); } return false; }