/** * Test sending a message, expecting an exception due to failure response. * */ public function testSendMessageFailure404() { // Dummy and Mock required objects $statusCode = 404; $this->setExpectedException("com\\realexpayments\\remote\\sdk\\RealexException", "Exception communicating with Realex"); try { $endpoint = 'https://some-test-endpoint'; $onlyAllowHttps = true; $xml = "<element>test response xml</element>"; $httpResponse = new HttpResponse(); $httpResponse->setResponseCode($statusCode); $httpResponse->setBody($xml); /** @var HttpConfiguration $configurationMock */ $configurationMock = \Phockito::mock("com\\realexpayments\\remote\\sdk\\http\\HttpConfiguration"); \Phockito::when($configurationMock->getEndPoint())->return($endpoint); \Phockito::when($configurationMock->isOnlyAllowHttps())->return($onlyAllowHttps); /** @var HttpClient $httpClientMock */ $httpClientMock = \Phockito::mock("com\\realexpayments\\remote\\sdk\\http\\HttpClient"); /** @var HttpRequest $anything */ \Phockito::when($httpClientMock->execute(\Hamcrest_Core_IsAnything::anything(), \Hamcrest_Core_IsAnything::anything()))->return($httpResponse); // execute the method $response = HttpUtils::sendMessage($xml, $httpClientMock, $configurationMock); } catch (RealexException $e) { throw $e; } catch (Exception $e) { $this->fail("Unexpected exception:" . $e->getMessage()); } }
/** * <p> * Sends the request to Realex. Actions: * * <ol> * <li>Generates any defaults on the request e.g. hash, time stamp order ID.</li> * <li>Marshals request to XML.</li> * <li>Sends request to Realex.</li> * <li>Unmarshals response.</li> * <li>Checks result code (If response is an error then throws {@link RealexServerException}).</li> * <li>Validates response hash (If invalid throws {@link RealexException}).</li> * </ol> * </p> * * @param iRequest $request * * @return iResponse */ public function send(iRequest $request) { $this->logger->info("Sending XML request to Realex."); //generate any required defaults e.g. order ID, time stamp, hash $request->generateDefaults($this->secret); //convert request to XML $this->logger->debug("Marshalling request object to XML."); $xmlRequest = $request->toXml(); //send request to Realex. $xmlResult = HttpUtils::sendMessage($xmlRequest, $this->httpClient, $this->httpConfiguration); //log the response $this->logger->trace("Response XML from server: " . $xmlResult); //convert XML to response object $this->logger->debug("Unmarshalling XML to response object."); $response = $request->responseFromXml($xmlResult); //throw exception if short response returned (indicating request could not be processed). if (ResponseUtils::isBasicResponse($response->getResult())) { $this->logger->error("Error response received from Realex with code " . $response->getResult() . " and message " . $response->getMessage() . "."); throw new RealexServerException($response->getTimeStamp(), $response->getOrderId(), $response->getResult(), $response->getMessage()); } //validate response hash $this->logger->debug("Verifying response hash."); if (!$response->isHashValid($this->secret)) { //Hash invalid. Throw exception. $this->logger->error("Response hash is invalid. This response's validity cannot be verified."); throw new RealexException("Response hash is invalid. This response's validity cannot be verified."); } return $response; }