Config Class Overview
The Config class in the Mypos\IPC SDK is responsible for storing and managing all configuration parameters needed to make secure API requests. This includes private and public keys, store credentials, URLs, and localization settings.
Class Details
- Namespace:
Mypos\IPC - File:
Config.php - Purpose: Centralize and manage API configuration such as credentials, keys, URLs, and encryption setup.
Method Summary
| Method | Return Type | Description |
|---|---|---|
setPrivateKeyPath(string $path) | Config | Set the file path for the private RSA key. |
getAPIPublicKey() | string | Get the IPC API public RSA key (raw string). |
setAPIPublicKey(string $publicKey) | Config | Set the IPC API public RSA key (as string). |
setAPIPublicKeyPath(string $path) | Config | Set the IPC API public RSA key from a file. |
getEncryptPublicKey() | string | Get the public RSA key used for encrypting sensitive data. |
setEncryptPublicKey(string $key) | Config | Set the encryption public key as a string. |
setEncryptPublicKeyPath(string $path) | Config | Set the encryption public key from a file path. |
getLang() | string | Get the current language (ISO 639-1 code). |
setLang(string $lang) | Config | Set the language used in API communication. |
getDeveloperKey() | string | Get the developer key. |
setDeveloperKey(string $developerKey) | Config | Set your myPOS developer key. |
getSource() | string | Get the custom source of the request. |
setSource(string $source) | void | Set the custom source (optional parameter). |
validate() | bool | Validate the current configuration. |
getKeyIndex() | string | Get the key index used for request signing. |
setKeyIndex(int $keyIndex) | Config | Set the key index for signing requests. |
getIpcURL() | string | Get the IPC API base URL. |
setIpcURL(string $ipc_url) | Config | Set the IPC API URL. |
getSid() | int | Get the store ID. |
setSid(int $sid) | Config | Set the store ID. |
getWallet() | string | Get the store’s wallet number. |
setWallet(string $wallet) | Config | Set the wallet number. |
getVersion() | string | Get the current API version. |
setVersion(string $version) | Config | Set the API version. |
getPrivateKey() | string | Get the private RSA key (raw string). |
setPrivateKey(string $privateKey) | Config | Set the private RSA key as a string. |
loadConfigurationPackage($configurationPackage) | Config | Decrypt and load a full configuration package from a single encrypted string. |
Usage Example
<?php
namespace Mypos\IPC;
/**
* IPC Configuration class
*/
class Config
{
private $privateKey = null;
private $APIPublicKey = null;
private $encryptPublicKey = null;
private $keyIndex = null;
private $sid;
private $wallet;
private $lang = 'en';
private $version = '1.4';
private $ipc_url = 'https://www.mypos.com/vmp/checkout';
private $developerKey;
private $source = 'SDK_PHP_1.4';
/**
* Store private RSA key as a filepath
*
* @param string $path File path
*
* @return Config
* @throws IPC_Exception
*/
public function setPrivateKeyPath($path)
{
if (!is_file($path) || !is_readable($path)) {
throw new IPC_Exception('Private key not found in:'.$path);
}
$this->privateKey = file_get_contents($path);
return $this;
}
/**
* IPC API public RSA key
*
* @return string
*/
public function getAPIPublicKey()
{
return $this->APIPublicKey;
}
/**
* IPC API public RSA key
*
* @param string $publicKey
*
* @return Config
*/
public function setAPIPublicKey($publicKey)
{
$this->APIPublicKey = $publicKey;
return $this;
}
/**
* IPC API public RSA key as a filepath
*
* @param string $path
*
* @return Config
* @throws IPC_Exception
*/
public function setAPIPublicKeyPath($path)
{
if (!is_file($path) || !is_readable($path)) {
throw new IPC_Exception('Public key not found in:'.$path);
}
$this->APIPublicKey = file_get_contents($path);
return $this;
}
/**
* Public RSA key using for encryption sensitive data
*
* @return string
*/
public function getEncryptPublicKey()
{
return $this->encryptPublicKey;
}
/**
* Public RSA key using for encryption sensitive data
*
* @param string $key
*
* @return Config
*/
public function setEncryptPublicKey($key)
{
$this->encryptPublicKey = $key;
return $this;
}
/**
* Public RSA key using for encryption sensitive data
*
* @param string $path File path
*
* @return Config
* @throws IPC_Exception
*/
public function setEncryptPublicKeyPath($path)
{
if (!is_file($path) || !is_readable($path)) {
throw new IPC_Exception('Key not found in:'.$path);
}
$this->encryptPublicKey = file_get_contents($path);
return $this;
}
/**
* Language code (ISO 639-1)
*
* @return string
*/
public function getLang()
{
return $this->lang;
}
/**
* Language code (ISO 639-1)
*
* @param string $lang
*
* @return Config
*/
public function setLang($lang)
{
$this->lang = $lang;
return $this;
}
/**
* Store private RSA key
*
* @return string
*/
public function getDeveloperKey()
{
return $this->developerKey;
}
/**
* Set myPOS developer key.
*
* @param string $developerKey
*
* @return Config
*/
public function setDeveloperKey($developerKey)
{
$this->developerKey = $developerKey;
return $this;
}
/**
* @return string
*/
public function getSource()
{
return $this->source;
}
/**
* Additional parameter to specify the source of request
*
* @param string $source
*/
public function setSource($source)
{
$this->source = $source;
}
/**
* Validate all set config details
*
* @return boolean
* @throws IPC_Exception
*/
public function validate()
{
if ($this->getKeyIndex() == null || !is_numeric($this->getKeyIndex())) {
throw new IPC_Exception('Invalid Key Index');
}
if ($this->getIpcURL() == null || !Helper::isValidURL($this->getIpcURL())) {
throw new IPC_Exception('Invalid IPC URL');
}
if ($this->getSid() == null || !is_numeric($this->getSid())) {
throw new IPC_Exception('Invalid SID');
}
if ($this->getWallet() == null || !is_numeric($this->getWallet())) {
throw new IPC_Exception('Invalid Wallet number');
}
if ($this->getVersion() == null) {
throw new IPC_Exception('Invalid IPC Version');
}
if (!openssl_get_privatekey($this->getPrivateKey())) {
throw new IPC_Exception('Invalid Private key');
}
return true;
}
/**
* Keyindex used for signing request
*
* @return string
*/
public function getKeyIndex()
{
return $this->keyIndex;
}
/**
* Keyindex used for signing request
*
* @param int $keyIndex
*
* @return Config
*/
public function setKeyIndex($keyIndex)
{
$this->keyIndex = $keyIndex;
return $this;
}
/**
* IPC API URL
*
* @return string
*/
public function getIpcURL()
{
return $this->ipc_url;
}
/**
* IPC API URL
*
* @param string $ipc_url
*
* @return Config
*/
public function setIpcURL($ipc_url)
{
$this->ipc_url = $ipc_url;
return $this;
}
/**
* Store ID
*
* @return int
*/
public function getSid()
{
return $this->sid;
}
/**
* Store ID
*
* @param int $sid
*
* @return Config
*/
public function setSid($sid)
{
$this->sid = $sid;
return $this;
}
/**
* Wallet number
*
* @return string
*/
public function getWallet()
{
return $this->wallet;
}
/**
* Wallet number
*
* @param string $wallet
*
* @return Config
*/
public function setWallet($wallet)
{
$this->wallet = $wallet;
return $this;
}
/**
* API Version
*
* @return string
*/
public function getVersion()
{
return $this->version;
}
/**
* API Version
*
* @param string $version
*
* @return Config
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Store private RSA key
*
* @return string
*/
public function getPrivateKey()
{
return $this->privateKey;
}
/**
* Store private RSA key
*
* @param string $privateKey
*
* @return Config
*/
public function setPrivateKey($privateKey)
{
$this->privateKey = $privateKey;
return $this;
}
/**
* Decrypt data string and set configuration parameters
*
* @param string $configurationPackage
* @return Config
* @throws IPC_Exception
*/
public function loadConfigurationPackage($configurationPackage)
{
$decoded = base64_decode($configurationPackage);
if (!$decoded) {
throw new IPC_Exception('Invalid autogenerated data');
}
$data = json_decode($decoded, true);
if (!$data) {
throw new IPC_Exception('Invalid autogenerated data');
}
foreach ($data as $key => $value) {
switch($key) {
case 'sid':
$this->setSid($value);
continue;
case 'cn':
$this->setWallet($value);
continue;
case 'pk':
$this->setPrivateKey($value);
continue;
case 'pc':
$this->setAPIPublicKey($value);
$this->setEncryptPublicKey($value);
continue;
case 'idx':
$this->setKeyIndex($value);
continue;
default:
throw new IPC_Exception('Unknown autogenerated authentication data parameter: ' . $key);
}
}
return $this;
}
}
Why Use the Config Class?
- Centralizes all configuration in one reusable object
- Handles both string and file-based RSA key management
- Supports secure key-based request signing and data encryption
- Easy to inject into API classes like
Purchase,Refund,CardStore, etc.