/** * Create an api request and inject the app token. * * @param string Path to the rest method being called * @param string Request method: GET, POST, PUT or DELETE * @param mixed An array of parameters or raw post data. Raw data isn't accepted for GET. * @return null * @throws Exception Invalid or failed requests throw exceptions. */ protected function apiCall($path, $requestMethod, $params = array()) { $requestMethod = strtoupper($requestMethod); switch ($requestMethod) { case 'GET': $setParamMethod = 'setParameterGet'; if (!is_array($params)) { throw new Exception('GET parameters can\'t be provided as raw data.'); } break; case 'POST': case 'PUT': case 'DELETE': $setParamMethod = 'setParameterPost'; break; default: throw new Exception('Invalid request method'); } $client = new Varien_Http_Client($this->serverLocation . $path); $client->setMethod($requestMethod); if (is_array($params)) { foreach ($params as $paramKey => $paramValue) { call_user_func(array($client, $setParamMethod), $paramKey, $paramValue); } } else { $client->setRawData($params); } $response = $client->request(); if ($response->isSuccessful()) { return json_decode($response->getBody()); } else { throw new Exception('Request failed'); } }
/** * This function returns full transaction details for a specified transaction ID. * * @link http://www.authorize.net/support/ReportingGuide_XML.pdf * @link http://developer.authorize.net/api/transaction_details/ * @param string $transactionId * @return Varien_Object */ protected function _getTransactionDetails($transactionId) { $requestBody = sprintf('<?xml version="1.0" encoding="utf-8"?>' . '<getTransactionDetailsRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">' . '<merchantAuthentication><name>%s</name><transactionKey>%s</transactionKey></merchantAuthentication>' . '<transId>%s</transId>' . '</getTransactionDetailsRequest>', $this->getConfigData('login'), $this->getConfigData('trans_key'), $transactionId); $client = new Varien_Http_Client(); $uri = $this->getConfigData('cgi_url_td'); $client->setUri($uri ? $uri : self::CGI_URL_TD); $client->setConfig(array('timeout' => 45)); $client->setHeaders(array('Content-Type: text/xml')); $client->setMethod(Zend_Http_Client::POST); $client->setRawData($requestBody); $debugData = array('request' => $requestBody); try { $responseBody = $client->request()->getBody(); $debugData['result'] = $responseBody; $this->_debug($debugData); libxml_use_internal_errors(true); $responseXmlDocument = new Varien_Simplexml_Element($responseBody); libxml_use_internal_errors(false); } catch (Exception $e) { Mage::throwException(Mage::helper('paygate')->__('Payment updating error.')); } $response = new Varien_Object(); $response->setResponseCode((string) $responseXmlDocument->transaction->responseCode)->setResponseReasonCode((string) $responseXmlDocument->transaction->responseReasonCode); return $response; }
/** * Send request for tracking * * @param array $trackings * @return void */ protected function _getXMLTracking($trackings) { $xmlStr = '<?xml version="1.0" encoding="UTF-8"?>' . '<req:KnownTrackingRequest' . ' xmlns:req="http://www.dhl.com"' . ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . ' xsi:schemaLocation="http://www.dhl.com TrackingRequestKnown.xsd" />'; $xml = new SimpleXMLElement($xmlStr); $requestNode = $xml->addChild('Request', '', ''); $serviceHeaderNode = $requestNode->addChild('ServiceHeader', '', ''); $serviceHeaderNode->addChild('SiteID', (string) $this->getConfigData('id')); $serviceHeaderNode->addChild('Password', (string) $this->getConfigData('password')); $xml->addChild('LanguageCode', 'EN', ''); foreach ($trackings as $tracking) { $xml->addChild('AWBNumber', $tracking, ''); } /* * Checkpoint details selection flag * LAST_CHECK_POINT_ONLY * ALL_CHECK_POINTS */ $xml->addChild('LevelOfDetails', 'ALL_CHECK_POINTS', ''); /* * Value that indicates for getting the tracking details with the additional * piece details and its respective Piece Details, Piece checkpoints along with * Shipment Details if queried. * * S-Only Shipment Details * B-Both Shipment & Piece Details * P-Only Piece Details * Default is āSā */ //$xml->addChild('PiecesEnabled', 'ALL_CHECK_POINTS'); $request = $xml->asXML(); $request = utf8_encode($request); $responseBody = $this->_getCachedQuotes($request); if ($responseBody === null) { $debugData = array('request' => $request); try { $client = new Varien_Http_Client(); $client->setUri((string) $this->getConfigData('gateway_url')); $client->setConfig(array('maxredirects' => 0, 'timeout' => 30)); $client->setRawData($request); $responseBody = $client->request(Varien_Http_Client::POST)->getBody(); $debugData['result'] = $responseBody; $this->_setCachedQuotes($request, $responseBody); } catch (Exception $e) { $this->_errors[$e->getCode()] = $e->getMessage(); $responseBody = ''; } $this->_debug($debugData); } $this->_parseXmlTrackingResponse($trackings, $responseBody); }
public function approveOrder(Varien_Event_Observer $observer) { if (!$this->_helper->isActive()) { return; } if (!$this->_helper->isEligible()) { return; } $this->_helper->log("approveOrder: begin"); $order = $observer->getEvent()->getOrder(); $orderId = $order->getId(); $orderIncrementId = $order->getIncrementId(); $this->_helper->log("approveOrder: orderid: {$orderId} orderIncrementId: {$orderIncrementId}"); $url = rtrim($this->_helper->getApiUrl(), '/') . '/approveOrder'; $client = new Varien_Http_Client($url); $client->setMethod(Varien_Http_Client::POST)->setConfig(array('timeout' => 8000, 'maxredirects' => 2)); $quoteId = $order->getQuoteId(); $deliverytime = Mage::getModel('milkman_deliveries/deliverytime'); $deliverytime->load($quoteId, 'quote_id'); $sessionId = $deliverytime->getSessionId(); $data = (object) ['privateKey' => $this->_helper->getPrivateKey(), 'sessionId' => $sessionId, 'externalTrackingCode' => $orderIncrementId]; // $client->setParameterPost($data); $json = utf8_encode(json_encode($data, JSON_UNESCAPED_SLASHES)); $this->_helper->log("approveOrder: json: {$json}"); $client->setRawData($json); $error = true; try { $response = $client->request(); $body = $response->getBody(); if ($response->isSuccessful()) { $result = json_decode($body, true); $checkJson = JSON_ERROR_NONE == json_last_error(); $checkSuccess = isset($result['result']['success']) && $result['result']['success']; if ($checkJson && $checkSuccess) { $error = false; $this->_helper->log("approveOrder: success"); } else { if (json_last_error() > 0) { $this->_helper->log("approveOrder: json error code: " . json_last_error()); } else { $this->_helper->log("approveOrder: data error"); } } } else { $this->_helper->log("approveOrder: rest error"); } } catch (Exception $e) { $body = $response->getBody(); $this->_helper->log("approveOrder: exception error"); } $this->_helper->log("approveOrder: body: " . $body); if ($error) { $this->_helper->log("approveOrder: error"); Mage::throwException($this->_helper->__("An error occurred during order confirmation.")); } }
public function postData($json, $endpoint) { $apiURL = Mage::getStoreConfig('settings/endpoint_url') . '/' . $endpoint; $appID = Mage::getStoreConfig('reachly_handleevent_options/section_one/field_app_id'); $secretKey = Mage::getStoreConfig('reachly_handleevent_options/section_one/field_secret_key'); $auth = $appID . ":" . base64_encode(hash_hmac('sha256', $json, $secretKey)); $client = new Varien_Http_Client(); $client->setUri($apiURL)->setMethod('POST')->setConfig(array('maxredirects' => 0, 'timeout' => 15)); $client->setHeaders(array('Content-Length: ' . strlen($json), 'Authorization: ' . $auth)); $client->setRawData($json, "application/json;charset=UTF-8"); $reqCounter = 0; do { $success = true; try { $response = $client->request(); } catch (Zend_Http_Client_Exception $e) { $success = false; $reqCounter++; } } while (!$success && $reqCounter < 3); }