Response Class
The Response class is used to parse, validate, and extract data returned by any myPOS IPC API method. It verifies the signature of the response and gives you access to the structured response content in your preferred format (JSON, XML, or array).
Class Details
- Namespace:
Mypos\IPC - Located at:
Response.php - Purpose: To safely extract and validate response data from the myPOS IPC system.
Method Summary
| Method | Return Type | Description |
|---|---|---|
__construct(Config $cnf, string|array $raw_data, string $format) | - | Initializes the response with config, raw response data, and format. |
getInstance(Config $cnf, string|array $raw_data, string $format) | Response | Static factory method to create a response instance. |
isSignatureCorrect() | bool | Verifies the digital signature of the IPC response. |
getSignature() | string | Returns the signature string from the response. |
getStatus() | int | Returns the IPC status code. |
getStatusMsg() | string | Returns a human-readable status message. |
getData([int $case]) | array | Returns the parsed response data as an associative array. |
getDataRaw() | string | array | Returns the raw response data (string or array depending on format). |
Security Note
The Response class performs digital signature verification using the public API key to ensure the integrity and authenticity of the data returned from myPOS.
Example Usage
<?php
namespace Mypos\IPC;
/**
* IPC Response class. Parse and validate income data
*/
class Response
{
/**
* @var Config;
*/
private $cnf;
private $raw_data, $format, $data, $signature;
/**
*
* @param Config $cnf
* @param string|array $raw_data
* @param string $format COMMUNICATION_FORMAT_JSON|COMMUNICATION_FORMAT_XML|COMMUNICATION_FORMAT_POST
*/
public function __construct(Config $cnf, $raw_data, $format)
{
$this->cnf = $cnf;
$this->_setData($raw_data, $format);
}
private function _setData($raw_data, $format)
{
if (empty($raw_data)) {
throw new IPC_Exception('Invalid Response data');
}
$this->format = $format;
$this->raw_data = $raw_data;
switch ($this->format) {
case Defines::COMMUNICATION_FORMAT_JSON:
$this->data = json_decode($this->raw_data, 1);
break;
case Defines::COMMUNICATION_FORMAT_XML:
$this->data = (array)new \SimpleXMLElement($this->raw_data);
if (isset($this->data['@attributes'])) {
unset($this->data['@attributes']);
}
break;
case Defines::COMMUNICATION_FORMAT_POST:
$this->data = $this->raw_data;
break;
default:
throw new IPC_Exception('Invalid response format!');
break;
}
if (empty($this->data)) {
throw new IPC_Exception('No IPC Response!');
}
$this->_extractSignature() && $this->_verifySignature();
return $this;
}
private function _extractSignature()
{
if (is_array($this->data) && !empty($this->data)) {
foreach ($this->data as $k => $v) {
if (strtolower($k) == 'signature') {
$this->signature = $v;
unset($this->data[$k]);
}
}
}
return true;
}
private function _verifySignature()
{
if (empty($this->signature)) {
throw new IPC_Exception('Missing request signature!');
}
if (!$this->cnf) {
throw new IPC_Exception('Missing config object!');
}
$pubKeyId = openssl_get_publickey($this->cnf->getAPIPublicKey());
if (!openssl_verify($this->_getSignData(), base64_decode($this->signature), $pubKeyId, Defines::SIGNATURE_ALGO)) {
throw new IPC_Exception('Signature check failed!');
}
}
private function _getSignData()
{
return base64_encode(implode('-', Helper::getValuesFromMultiDimensionalArray($this->data)));
}
/**
* Static class to create Response object
*
* @param Config $cnf
* @param string|array $raw_data
* @param string $format
*
* @return Response
*/
public static function getInstance(Config $cnf, $raw_data, $format)
{
return new self($cnf, $raw_data, $format);
}
/**
* Validate Signature param from IPC response
*
* @return boolean
*/
public function isSignatureCorrect()
{
try {
$this->_verifySignature();
} catch (\Exception $ex) {
return false;
}
return true;
}
/**
* Request param: Signature
*
* @return string
*/
function getSignature()
{
return $this->signature;
}
#############################################
/**
* Request param: Status
*
* @return int
*/
function getStatus()
{
return Helper::getArrayVal($this->getData(CASE_LOWER), 'status');
}
/**
* Return IPC Response in array
*
* @param int $case CASE_LOWER|CASE_UPPER
*
* @return array
* @throws IPC_Exception
*/
function getData($case = null)
{
if ($case !== null) {
if (!in_array($case, [
CASE_LOWER,
CASE_UPPER,
])) {
throw new IPC_Exception('Invalid Key Case!');
}
return array_change_key_case($this->data, $case);
}
return $this->data;
}
/**
* Request param: StatusMsg
*
* @return string
*/
function getStatusMsg()
{
return Helper::getArrayVal($this->getData(CASE_LOWER), 'statusmsg');
}
/**
* Return IPC Response in original format json/xml/array
*
* @return string|array
*/
function getDataRaw()
{
return $this->raw_data;
}
}
Use Cases
Use the Response class when:
- You want to safely parse and verify any IPC API response
- You need to extract detailed response data in array or raw format
- You want to check transaction status codes and messages
- You require response signature validation for security and compliance