/** * Generate MAC string from array of fields. * * @param array $data Array of VK_* fields * @param string $encoding Encoding * * @return string MAC key */ protected function generateSignature(array $data, $encoding = 'UTF-8') { // Request mac $fields = array('ver', 'id', 'ecuno', 'eamount', 'cur', 'datetime', 'feedBackUrl', 'delivery'); if (isset($data['respcode'])) { // Response mac $fields = array('ver', 'id', 'ecuno', 'receipt_no', 'eamount', 'cur', 'respcode', 'datetime', 'msgdata', 'actiontext'); $data['receipt_no'] = ProtocolHelper::mbStrPad($data['receipt_no'], 6, "0", STR_PAD_LEFT, $encoding); $data['msgdata'] = ProtocolHelper::mbStrPad($data['respcode'] === self::PAYMENT_RESPONSE_ABORT && strlen($data['msgdata']) == 0 ? ' ' : $data['msgdata'], 40, " ", STR_PAD_RIGHT, $encoding); $data['respcode'] = ProtocolHelper::mbStrPad($data['respcode'], 3, "0", STR_PAD_LEFT, $encoding); $data['actiontext'] = ProtocolHelper::mbStrPad($data['actiontext'], 40, " ", STR_PAD_RIGHT, $encoding); } if (isset($data['feedBackUrl'])) { $data['feedBackUrl'] = ProtocolHelper::mbStrPad($data['feedBackUrl'], 128); } // Pad to correct length $data['ver'] = ProtocolHelper::mbStrPad($data['ver'], 3, "0", STR_PAD_LEFT, $encoding); $data['id'] = ProtocolHelper::mbStrPad($data['id'], 10, " ", STR_PAD_RIGHT, $encoding); $data['ecuno'] = ProtocolHelper::mbStrPad($data['ecuno'], 12, "0", STR_PAD_LEFT, $encoding); $data['eamount'] = ProtocolHelper::mbStrPad($data['eamount'], 12, "0", STR_PAD_LEFT, $encoding); $data['cur'] = ProtocolHelper::mbStrPad($data['cur'], 3, " ", STR_PAD_RIGHT, $encoding); $data['datetime'] = ProtocolHelper::mbStrPad($data['datetime'], 14, " ", STR_PAD_RIGHT, $encoding); $mac = ''; foreach ($fields as $key) { // Check if field exists if (!isset($data[$key]) || $data[$key] === false) { throw new \UnexpectedValueException(vsprintf('Field %s must be set to use ECommerce protocol.', array($key))); } $mac .= $data[$key]; } return $mac; }