|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Omnipay\Redsys\Message; |
| 4 | + |
| 5 | +use Omnipay\Common\Message\AbstractResponse as BaseAbstractResponse; |
| 6 | +use Omnipay\Common\Exception\RuntimeException; |
| 7 | + |
| 8 | +/** |
| 9 | + * Abstract Response |
| 10 | + * |
| 11 | + * This abstract class extends the base Omnipay AbstractResponse in order |
| 12 | + * to provide some common encoding and decoding functions. |
| 13 | + */ |
| 14 | +abstract class AbstractResponse extends BaseAbstractResponse |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Encode merchant parameters |
| 18 | + * |
| 19 | + * @param array $data The parameters to encode |
| 20 | + * |
| 21 | + * @return string Encoded data |
| 22 | + */ |
| 23 | + protected function encodeMerchantParameters($data) |
| 24 | + { |
| 25 | + return base64_encode(json_encode($data)); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Decode merchant parameters |
| 30 | + * |
| 31 | + * @param string $data The encoded string of parameters |
| 32 | + * |
| 33 | + * @return array Decoded data |
| 34 | + */ |
| 35 | + protected function decodeMerchantParameters($data) |
| 36 | + { |
| 37 | + return (array)json_decode(base64_decode(strtr($data, '-_', '+/'))); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Encrypt message with given key and default IV |
| 42 | + * |
| 43 | + * @todo function_exists() vs extension_loaded()? |
| 44 | + * |
| 45 | + * @param string $message The message to encrypt |
| 46 | + * @param string $key The key used to encrypt the message |
| 47 | + * |
| 48 | + * @return string Encrypted message |
| 49 | + * |
| 50 | + * @throws RuntimeException |
| 51 | + */ |
| 52 | + protected function encryptMessage($message, $key) |
| 53 | + { |
| 54 | + $iv = implode(array_map("chr", array(0, 0, 0, 0, 0, 0, 0, 0))); |
| 55 | + |
| 56 | + if (function_exists('mcrypt_encrypt')) { |
| 57 | + $ciphertext = mcrypt_encrypt(MCRYPT_3DES, $key, $message, MCRYPT_MODE_CBC, $iv); |
| 58 | + } else { |
| 59 | + throw new RuntimeException('No valid encryption extension installed'); |
| 60 | + } |
| 61 | + |
| 62 | + return $ciphertext; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Create signature hash used to verify messages |
| 67 | + * |
| 68 | + * @todo Add if-check on algorithm to match against signature version as new param? |
| 69 | + * |
| 70 | + * @param string $message The message to encrypt |
| 71 | + * @param string $salt Unique salt used to generate the ciphertext |
| 72 | + * @param string $key The key used to encrypt the message |
| 73 | + * |
| 74 | + * @return string Generated signature |
| 75 | + */ |
| 76 | + protected function createSignature($message, $salt, $key) |
| 77 | + { |
| 78 | + $ciphertext = $this->encryptMessage($salt, $key); |
| 79 | + return base64_encode(hash_hmac('sha256', $message, $ciphertext, true)); |
| 80 | + } |
| 81 | + |
| 82 | + protected function createReturnSignature($message, $salt, $key) |
| 83 | + { |
| 84 | + return strtr($this->createSignature($message, $salt, $key), '+/', '-_'); |
| 85 | + } |
| 86 | +} |
0 commit comments