Overview
  • Namespace
  • Class

Namespaces

  • Mypos
    • IPC

Classes

  • Mypos\IPC\Base
  • Mypos\IPC\Card
  • Mypos\IPC\CardStore
  • Mypos\IPC\Cart
  • Mypos\IPC\Config
  • Mypos\IPC\Customer
  • Mypos\IPC\Defines
  • Mypos\IPC\GetTxnStatus
  • Mypos\IPC\Helper
  • Mypos\IPC\IAPurchase
  • Mypos\IPC\IAStoreCard
  • Mypos\IPC\IAStoredCardUpdate
  • Mypos\IPC\IPCGetTxnLog
  • Mypos\IPC\Loader
  • Mypos\IPC\MandateManagement
  • Mypos\IPC\Purchase
  • Mypos\IPC\Refund
  • Mypos\IPC\RequestMoney
  • Mypos\IPC\Response
  • Mypos\IPC\Reversal

Exceptions

  • Mypos\IPC\IPC_Exception
  1 <?php
  2 
  3 namespace Mypos\IPC;
  4 
  5 /**
  6  * IPC Response class. Parse and validate income data
  7  */
  8 class Response
  9 {
 10 
 11     /**
 12      * @var Config;
 13      */
 14     private $cnf;
 15     private $raw_data, $format, $data, $signature;
 16 
 17     /**
 18      *
 19      * @param Config $cnf
 20      * @param string|array $raw_data
 21      * @param string $format COMMUNICATION_FORMAT_JSON|COMMUNICATION_FORMAT_XML|COMMUNICATION_FORMAT_POST
 22      */
 23     public function __construct(Config $cnf, $raw_data, $format)
 24     {
 25         $this->cnf = $cnf;
 26         $this->_setData($raw_data, $format);
 27     }
 28 
 29     /**
 30      * Static class to create Response object
 31      * @param string|array $raw_data
 32      * @param type $format
 33      * @return \self
 34      */
 35     public static function getInstance(Config $cnf, $raw_data, $format)
 36     {
 37         return new self($cnf, $raw_data, $format);
 38     }
 39 
 40     /**
 41      * Validate Signature param from IPC repsonce
 42      * @return boolean
 43      */
 44     public function isSignatureCorrect()
 45     {
 46         try {
 47             $this->_verifySignature();
 48         } catch (Exception $ex) {
 49             return false;
 50         }
 51         return true;
 52     }
 53 
 54     /**
 55      * Request param: Signature
 56      * @return string
 57      */
 58     function getSignature()
 59     {
 60         return $this->signature;
 61     }
 62 
 63     /**
 64      * Request param: Status
 65      * @return int
 66      */
 67     function getStatus()
 68     {
 69         return Helper::getArrayVal($this->getData(CASE_LOWER), 'status');
 70     }
 71 
 72     /**
 73      * Request param: StatusMsg
 74      * @return string
 75      */
 76     function getStatusMsg()
 77     {
 78         return Helper::getArrayVal($this->getData(CASE_LOWER), 'statusmsg');
 79     }
 80 
 81     /**
 82      * Return IPC Response in array
 83      * @param int $case CASE_LOWER|CASE_UPPER
 84      * @return array
 85      * @throws IPC_Exception
 86      */
 87     function getData($case = null)
 88     {
 89         if ($case !== null) {
 90             if (!in_array($case, array(CASE_LOWER, CASE_UPPER))) {
 91                 throw new IPC_Exception('Invalid Key Case!');
 92             }
 93             return array_change_key_case($this->data, $case);
 94         }
 95 
 96         return $this->data;
 97     }
 98 
 99     /**
100      * Return IPC Response in original format json/xml/array
101      * @return string|array
102      */
103     function getDataRaw()
104     {
105         return $this->raw_data;
106     }
107 
108     #############################################
109 
110     private function _setData($raw_data, $format)
111     {
112         if (empty($raw_data)) {
113             throw new IPC_Exception('Invalid Reponce data');
114         }
115 
116         $this->format = $format;
117         $this->raw_data = $raw_data;
118 
119         switch ($this->format) {
120             case Defines::COMMUNICATION_FORMAT_JSON:
121                 $this->data = json_decode($this->raw_data, 1);
122                 break;
123             case Defines::COMMUNICATION_FORMAT_XML:
124                 $this->data = (array)new \SimpleXMLElement($this->raw_data);
125                 if (isset($this->data['@attributes'])) {
126                     unset($this->data['@attributes']);
127                 }
128                 break;
129             case Defines::COMMUNICATION_FORMAT_POST:
130                 $this->data = $this->raw_data;
131                 break;
132             default:
133                 throw new IPC_Exception('Invalid response format!');
134                 break;
135         }
136 
137         if (empty($this->data)) {
138             throw new IPC_Exception('No IPC Response!');
139         }
140 
141         $this->_exctractSignature() && $this->_verifySignature();
142         return $this;
143     }
144 
145     private function _exctractSignature()
146     {
147         if (is_array($this->data) && !empty($this->data)) {
148             foreach ($this->data as $k => $v) {
149                 if (strtolower($k) == 'signature') {
150                     $this->signature = $v;
151                     unset($this->data[$k]);
152                 }
153             }
154         }
155         return true;
156     }
157 
158     private function _verifySignature()
159     {
160         if (empty($this->signature)) {
161             throw new IPC_Exception('Missing request signature!');
162         }
163 
164         if (!$this->cnf) {
165             throw new IPC_Exception('Missing config object!');
166         }
167 
168 
169         $pubKeyId = openssl_get_publickey($this->cnf->getAPIPublicKey());
170         if (!openssl_verify($this->_getSignData(), base64_decode($this->signature), $pubKeyId, Defines::SIGNATURE_ALGO)) {
171             throw new IPC_Exception('Signature check failed!');
172         }
173     }
174 
175     private function _getSignData()
176     {
177         return base64_encode(implode('-', Helper::getValuesFromMultiDemensionalArray($this->data)));
178     }
179 
180 }
181 
API documentation generated by ApiGen