/** * Method validates HPP response hash. * * @param HppResponse $hppResponse * @param string $secret */ public static function validateResponse(HppResponse $hppResponse, $secret) { self::Initialise(); if (!$hppResponse->isHashValid($secret)) { self::$logger->error("HppResponse contains an invalid security hash."); throw new RealexValidationException("HppResponse contains an invalid security hash", array("HppResponse contains an invalid security hash")); } }
private static function Initialise() { if (self::$initialised) { return; } self::$logger = RXPLogger::getLogger(__CLASS__); self::$mappers = array(); self::$mappers[HppRequest::GetClassName()] = new RequestMapper(); self::$mappers[HppResponse::GetClassName()] = new ResponseMapper(); self::$initialised = true; }
/** * * Receives a Json string and generates a domain object * * @param string $value * * @return HppResponse */ public function ReadValue($value) { $array = json_decode($value, true); $array = new SafeArrayAccess($array, ""); if ($array) { $hppResponse = new HppResponse(); $hppResponse->setMerchantId($array['MERCHANT_ID']); $hppResponse->setAccount($array['ACCOUNT']); $hppResponse->setOrderId($array['ORDER_ID']); $hppResponse->setAmount($array['AMOUNT']); $hppResponse->setAuthCode($array['AUTHCODE']); $hppResponse->setTimeStamp($array['TIMESTAMP']); $hppResponse->setHash($array['SHA1HASH']); $hppResponse->setResult($array['RESULT']); $hppResponse->setMessage($array['MESSAGE']); $hppResponse->setCvnResult($array['CVNRESULT']); $hppResponse->setPasRef($array['PASREF']); $hppResponse->setBatchId($array['BATCHID']); $hppResponse->setEci($array['ECI']); $hppResponse->setCavv($array['CAVV']); $hppResponse->setXid($array['XID']); $hppResponse->setCommentOne($array['COMMENT1']); $hppResponse->setCommentTwo($array['COMMENT2']); $hppResponse->setTss($array['TSS']); $hppResponse->setAVSAddressResult($array['AVSADDRESSRESULT']); $hppResponse->setAVSPostCodeResult($array['AVSPOSTCODERESULT']); foreach ($array->getUnderLayingArray() as $key => $value) { if (!$this->isKnownProperty($key)) { $hppResponse->setSupplementaryDataValue($key, $value); } } return $hppResponse; } return $array; }
/** * Checks request supplementary data matches expected values. * * @param HppResponse $hppResponse * @param PHPUnit_Framework_TestCase $testCase */ public static function checkValidHppResponseSupplementaryData(HppResponse $hppResponse, PHPUnit_Framework_TestCase $testCase) { $supplementaryData = $hppResponse->getSupplementaryData(); $testCase->assertEquals(self::UNKNOWN_ONE_VALUE, $supplementaryData[self::UNKNOWN_ONE_KEY], "Json conversion incorrect Unknown one"); $testCase->assertEquals(self::UNKNOWN_TWO_VALUE, $supplementaryData[self::UNKNOWN_TWO_KEY], "Json conversion incorrect Unknown one"); $testCase->assertEquals(self::UNKNOWN_THREE_VALUE, $supplementaryData[self::UNKNOWN_THREE_KEY], "Json conversion incorrect Unknown one"); $testCase->assertEquals(self::UNKNOWN_FOUR_VALUE, $supplementaryData[self::UNKNOWN_FOUR_KEY], "Json conversion incorrect Unknown one"); $testCase->assertEquals(sizeof(self::$SUPPLEMENTARY_DATA), sizeof($supplementaryData), "Json conversion incorrect size"); }
/** * <p> * Method produces JSON from <code>HppResponse</code> object. * Carries out the following actions: * <ul> * <li>Validates inputs</li> * <li>Generates defaults for security hash, order ID and time stamp (if required)</li> * <li>Base64 encodes inputs</li> * <li>Serialises response object to JSON</li> * </ul> * </p> * * @param HppResponse $hppResponse * @return string */ public function responseToJson(HppResponse $hppResponse) { $this->logger->info("Converting HppResponse to JSON."); $json = null; //generate hash $this->logger->debug("Generating hash."); $hppResponse->hash($this->secret); //encode $this->logger->debug("Encoding object."); try { $hppResponse = $hppResponse->encode(self::ENCODING_CHARSET); } catch (Exception $e) { $this->logger->error("Exception encoding HPP response.", $e); throw new RealexException("Exception encoding HPP response.", $e); } //convert to JSON $this->logger->debug("Converting to JSON."); $json = JsonUtils::toJson($hppResponse); return $json; }