| @@ -13,74 +13,74 @@ | ||
| 13 | 13 | */ | 
| 14 | 14 | abstract class AbstractResponse extends BaseAbstractResponse | 
| 15 | 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 | - } | |
| 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 | 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 | - } | |
| 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 | 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))); | |
| 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 | 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 | - } | |
| 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 | 61 | |
| 62 | - return $ciphertext; | |
| 63 | - } | |
| 62 | + return $ciphertext; | |
| 63 | + } | |
| 64 | 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 | - } | |
| 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 | 81 | |
| 82 | - protected function createReturnSignature($message, $salt, $key) | |
| 83 | -    { | |
| 84 | - return strtr($this->createSignature($message, $salt, $key), '+/', '-_'); | |
| 85 | - } | |
| 82 | + protected function createReturnSignature($message, $salt, $key) | |
| 83 | +	{ | |
| 84 | + return strtr($this->createSignature($message, $salt, $key), '+/', '-_'); | |
| 85 | + } | |
| 86 | 86 | } | 
| @@ -10,125 +10,125 @@ | ||
| 10 | 10 | */ | 
| 11 | 11 | class CompletePurchaseResponse extends AbstractResponse | 
| 12 | 12 |  { | 
| 13 | - /** @var array */ | |
| 14 | - protected $merchantParameters; | |
| 15 | - /** @var string */ | |
| 16 | - protected $returnSignature; | |
| 17 | - /** @var boolean */ | |
| 18 | - protected $usingUpcaseParameters = false; | |
| 19 | - /** @var boolean */ | |
| 20 | - protected $usingUpcaseResponse = false; | |
| 13 | + /** @var array */ | |
| 14 | + protected $merchantParameters; | |
| 15 | + /** @var string */ | |
| 16 | + protected $returnSignature; | |
| 17 | + /** @var boolean */ | |
| 18 | + protected $usingUpcaseParameters = false; | |
| 19 | + /** @var boolean */ | |
| 20 | + protected $usingUpcaseResponse = false; | |
| 21 | 21 | |
| 22 | - /** | |
| 23 | - * Constructor | |
| 24 | - * | |
| 25 | - * @param RequestInterface $request the initiating request. | |
| 26 | - * @param mixed $data | |
| 27 | - * | |
| 28 | - * @throws InvalidResponseException If merchant data or order number is missing, or signature does not match | |
| 29 | - */ | |
| 30 | - public function __construct(RequestInterface $request, $data) | |
| 31 | -    { | |
| 32 | - parent::__construct($request, $data); | |
| 22 | + /** | |
| 23 | + * Constructor | |
| 24 | + * | |
| 25 | + * @param RequestInterface $request the initiating request. | |
| 26 | + * @param mixed $data | |
| 27 | + * | |
| 28 | + * @throws InvalidResponseException If merchant data or order number is missing, or signature does not match | |
| 29 | + */ | |
| 30 | + public function __construct(RequestInterface $request, $data) | |
| 31 | +	{ | |
| 32 | + parent::__construct($request, $data); | |
| 33 | 33 | |
| 34 | -        if (!empty($data['Ds_MerchantParameters'])) { | |
| 35 | - $this->merchantParameters = $this->decodeMerchantParameters($data['Ds_MerchantParameters']); | |
| 36 | -        } elseif (!empty($data['DS_MERCHANTPARAMETERS'])) { | |
| 37 | - $this->merchantParameters = $this->decodeMerchantParameters($data['DS_MERCHANTPARAMETERS']); | |
| 38 | - $this->usingUpcaseResponse = true; | |
| 39 | -        } else { | |
| 40 | -            throw new InvalidResponseException('Invalid response from payment gateway (no data)'); | |
| 41 | - } | |
| 34 | +		if (!empty($data['Ds_MerchantParameters'])) { | |
| 35 | + $this->merchantParameters = $this->decodeMerchantParameters($data['Ds_MerchantParameters']); | |
| 36 | +		} elseif (!empty($data['DS_MERCHANTPARAMETERS'])) { | |
| 37 | + $this->merchantParameters = $this->decodeMerchantParameters($data['DS_MERCHANTPARAMETERS']); | |
| 38 | + $this->usingUpcaseResponse = true; | |
| 39 | +		} else { | |
| 40 | +			throw new InvalidResponseException('Invalid response from payment gateway (no data)'); | |
| 41 | + } | |
| 42 | 42 | |
| 43 | -        if (!empty($this->merchantParameters['Ds_Order'])) { | |
| 44 | - $order = $this->merchantParameters['Ds_Order']; | |
| 45 | -        } elseif (!empty($this->merchantParameters['DS_ORDER'])) { | |
| 46 | - $order = $this->merchantParameters['DS_ORDER']; | |
| 47 | - $this->usingUpcaseParameters = true; | |
| 48 | -        } else { | |
| 49 | - throw new InvalidResponseException(); | |
| 50 | - } | |
| 43 | +		if (!empty($this->merchantParameters['Ds_Order'])) { | |
| 44 | + $order = $this->merchantParameters['Ds_Order']; | |
| 45 | +		} elseif (!empty($this->merchantParameters['DS_ORDER'])) { | |
| 46 | + $order = $this->merchantParameters['DS_ORDER']; | |
| 47 | + $this->usingUpcaseParameters = true; | |
| 48 | +		} else { | |
| 49 | + throw new InvalidResponseException(); | |
| 50 | + } | |
| 51 | 51 | |
| 52 | - $this->returnSignature = $this->createReturnSignature( | |
| 53 | - $data[$this->usingUpcaseResponse ? 'DS_MERCHANTPARAMETERS' : 'Ds_MerchantParameters'], | |
| 54 | - $order, | |
| 55 | - base64_decode($this->request->getHmacKey()) | |
| 56 | - ); | |
| 52 | + $this->returnSignature = $this->createReturnSignature( | |
| 53 | + $data[$this->usingUpcaseResponse ? 'DS_MERCHANTPARAMETERS' : 'Ds_MerchantParameters'], | |
| 54 | + $order, | |
| 55 | + base64_decode($this->request->getHmacKey()) | |
| 56 | + ); | |
| 57 | 57 | |
| 58 | -        if ($this->returnSignature != $data[$this->usingUpcaseResponse ? 'DS_SIGNATURE' : 'Ds_Signature']) { | |
| 59 | -            throw new InvalidResponseException('Invalid response from payment gateway (signature mismatch)'); | |
| 60 | - } | |
| 61 | - } | |
| 58 | +		if ($this->returnSignature != $data[$this->usingUpcaseResponse ? 'DS_SIGNATURE' : 'Ds_Signature']) { | |
| 59 | +			throw new InvalidResponseException('Invalid response from payment gateway (signature mismatch)'); | |
| 60 | + } | |
| 61 | + } | |
| 62 | 62 | |
| 63 | - /** | |
| 64 | - * Is the response successful? | |
| 65 | - * | |
| 66 | - * @return boolean | |
| 67 | - */ | |
| 68 | - public function isSuccessful() | |
| 69 | -    { | |
| 70 | - $key = $this->usingUpcaseParameters ? 'DS_RESPONSE' : 'Ds_Response'; | |
| 71 | - return isset($this->merchantParameters[$key]) | |
| 72 | - && is_numeric($this->merchantParameters[$key]) | |
| 73 | - && 0 <= $this->merchantParameters[$key] | |
| 74 | - && 100 > $this->merchantParameters[$key]; | |
| 75 | - } | |
| 63 | + /** | |
| 64 | + * Is the response successful? | |
| 65 | + * | |
| 66 | + * @return boolean | |
| 67 | + */ | |
| 68 | + public function isSuccessful() | |
| 69 | +	{ | |
| 70 | + $key = $this->usingUpcaseParameters ? 'DS_RESPONSE' : 'Ds_Response'; | |
| 71 | + return isset($this->merchantParameters[$key]) | |
| 72 | + && is_numeric($this->merchantParameters[$key]) | |
| 73 | + && 0 <= $this->merchantParameters[$key] | |
| 74 | + && 100 > $this->merchantParameters[$key]; | |
| 75 | + } | |
| 76 | 76 | |
| 77 | - /** | |
| 78 | - * Get the response data, included the decoded merchant parameters if available. | |
| 79 | - * | |
| 80 | - * @return mixed | |
| 81 | - */ | |
| 82 | - public function getData() | |
| 83 | -    { | |
| 84 | - $data = parent::getData(); | |
| 85 | - return is_array($data) && is_array($this->merchantParameters) | |
| 86 | - ? array_merge($data, $this->merchantParameters) | |
| 87 | - : $data; | |
| 88 | - } | |
| 77 | + /** | |
| 78 | + * Get the response data, included the decoded merchant parameters if available. | |
| 79 | + * | |
| 80 | + * @return mixed | |
| 81 | + */ | |
| 82 | + public function getData() | |
| 83 | +	{ | |
| 84 | + $data = parent::getData(); | |
| 85 | + return is_array($data) && is_array($this->merchantParameters) | |
| 86 | + ? array_merge($data, $this->merchantParameters) | |
| 87 | + : $data; | |
| 88 | + } | |
| 89 | 89 | |
| 90 | - /** | |
| 91 | - * Helper method to get a specific merchant parameter if available. | |
| 92 | - * | |
| 93 | - * @param string $key The key to look up | |
| 94 | - * | |
| 95 | - * @return null|mixed | |
| 96 | - */ | |
| 97 | - protected function getKey($key) | |
| 98 | -    { | |
| 99 | -        if ($this->usingUpcaseParameters) { | |
| 100 | - $key = strtoupper($key); | |
| 101 | - } | |
| 102 | - return isset($this->merchantParameters[$key]) ? $this->merchantParameters[$key] : null; | |
| 103 | - } | |
| 90 | + /** | |
| 91 | + * Helper method to get a specific merchant parameter if available. | |
| 92 | + * | |
| 93 | + * @param string $key The key to look up | |
| 94 | + * | |
| 95 | + * @return null|mixed | |
| 96 | + */ | |
| 97 | + protected function getKey($key) | |
| 98 | +	{ | |
| 99 | +		if ($this->usingUpcaseParameters) { | |
| 100 | + $key = strtoupper($key); | |
| 101 | + } | |
| 102 | + return isset($this->merchantParameters[$key]) ? $this->merchantParameters[$key] : null; | |
| 103 | + } | |
| 104 | 104 | |
| 105 | - /** | |
| 106 | - * Get the authorisation code if available. | |
| 107 | - * | |
| 108 | - * @return null|string | |
| 109 | - */ | |
| 110 | - public function getTransactionReference() | |
| 111 | -    { | |
| 112 | -        return $this->getKey('Ds_AuthorisationCode'); | |
| 113 | - } | |
| 105 | + /** | |
| 106 | + * Get the authorisation code if available. | |
| 107 | + * | |
| 108 | + * @return null|string | |
| 109 | + */ | |
| 110 | + public function getTransactionReference() | |
| 111 | +	{ | |
| 112 | +		return $this->getKey('Ds_AuthorisationCode'); | |
| 113 | + } | |
| 114 | 114 | |
| 115 | - /** | |
| 116 | - * Get the merchant response message if available. | |
| 117 | - * | |
| 118 | - * @return null|string | |
| 119 | - */ | |
| 120 | - public function getMessage() | |
| 121 | -    { | |
| 122 | -        return $this->getKey('Ds_Response'); | |
| 123 | - } | |
| 115 | + /** | |
| 116 | + * Get the merchant response message if available. | |
| 117 | + * | |
| 118 | + * @return null|string | |
| 119 | + */ | |
| 120 | + public function getMessage() | |
| 121 | +	{ | |
| 122 | +		return $this->getKey('Ds_Response'); | |
| 123 | + } | |
| 124 | 124 | |
| 125 | - /** | |
| 126 | - * Get the card type if available. | |
| 127 | - * | |
| 128 | - * @return null|string | |
| 129 | - */ | |
| 130 | - public function getCardType() | |
| 131 | -    { | |
| 132 | -        return $this->getKey('Ds_Card_Type'); | |
| 133 | - } | |
| 125 | + /** | |
| 126 | + * Get the card type if available. | |
| 127 | + * | |
| 128 | + * @return null|string | |
| 129 | + */ | |
| 130 | + public function getCardType() | |
| 131 | +	{ | |
| 132 | +		return $this->getKey('Ds_Card_Type'); | |
| 133 | + } | |
| 134 | 134 | } | 
| @@ -9,162 +9,162 @@ | ||
| 9 | 9 | */ | 
| 10 | 10 | class PurchaseRequest extends AbstractRequest | 
| 11 | 11 |  { | 
| 12 | - /** @var string */ | |
| 13 | - protected $liveEndpoint = 'https://sis.redsys.es/sis/realizarPago'; | |
| 14 | - /** @var string */ | |
| 15 | - protected $testEndpoint = 'https://sis-t.redsys.es:25443/sis/realizarPago'; | |
| 16 | - /** @var array */ | |
| 17 | - protected static $consumerLanguages = array( | |
| 18 | - 'es' => '001', // Spanish | |
| 19 | - 'en' => '002', // English | |
| 20 | - 'ca' => '003', // Catalan - same as Valencian (010) | |
| 21 | - 'fr' => '004', // French | |
| 22 | - 'de' => '005', // German | |
| 23 | - 'nl' => '006', // Dutch | |
| 24 | - 'it' => '007', // Italian | |
| 25 | - 'sv' => '008', // Swedish | |
| 26 | - 'pt' => '009', // Portuguese | |
| 27 | - 'pl' => '011', // Polish | |
| 28 | - 'gl' => '012', // Galician | |
| 29 | - 'eu' => '013', // Basque | |
| 30 | - ); | |
| 31 | - | |
| 32 | - public function getCardholder() | |
| 33 | -    { | |
| 34 | -        return $this->getParameter('cardholder'); | |
| 35 | - } | |
| 36 | - | |
| 37 | - public function setCardholder($value) | |
| 38 | -    { | |
| 39 | -        return $this->setParameter('cardholder', $value); | |
| 40 | - } | |
| 41 | - | |
| 42 | - public function getConsumerLanguage() | |
| 43 | -    { | |
| 44 | -        return $this->getParameter('consumerLanguage'); | |
| 45 | - } | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Set the language presented to the consumer | |
| 49 | - * | |
| 50 | - * @param string|int Either the ISO 639-1 code to be converted, or the gateway's own numeric language code | |
| 51 | - */ | |
| 52 | - public function setConsumerLanguage($value) | |
| 53 | -    { | |
| 54 | -        if (is_int($value)) { | |
| 55 | -            if ($value < 0 || $value > 13) { | |
| 56 | - $value = 1; | |
| 57 | - } | |
| 58 | - $value = str_pad($value, 3, '0', STR_PAD_LEFT); | |
| 59 | -        } elseif (!is_numeric($value)) { | |
| 60 | - $value = isset(self::$consumerLanguages[$value]) ? self::$consumerLanguages[$value] : '001'; | |
| 61 | - } | |
| 62 | - | |
| 63 | -        return $this->setParameter('consumerLanguage', $value); | |
| 64 | - } | |
| 65 | - | |
| 66 | - public function getHmacKey() | |
| 67 | -    { | |
| 68 | -        return $this->getParameter('hmacKey'); | |
| 69 | - } | |
| 70 | - | |
| 71 | - public function setHmacKey($value) | |
| 72 | -    { | |
| 73 | -        return $this->setParameter('hmacKey', $value); | |
| 74 | - } | |
| 75 | - | |
| 76 | - public function getMerchantData() | |
| 77 | -    { | |
| 78 | -        return $this->getParameter('merchantData'); | |
| 79 | - } | |
| 80 | - | |
| 81 | - public function setMerchantData($value) | |
| 82 | -    { | |
| 83 | -        return $this->setParameter('merchantData', $value); | |
| 84 | - } | |
| 85 | - | |
| 86 | - public function getMerchantId() | |
| 87 | -    { | |
| 88 | -        return $this->getParameter('merchantId'); | |
| 89 | - } | |
| 90 | - | |
| 91 | - public function setMerchantId($value) | |
| 92 | -    { | |
| 93 | -        return $this->setParameter('merchantId', $value); | |
| 94 | - } | |
| 95 | - | |
| 96 | - public function getMerchantName() | |
| 97 | -    { | |
| 98 | -        return $this->getParameter('merchantName'); | |
| 99 | - } | |
| 100 | - | |
| 101 | - public function setMerchantName($value) | |
| 102 | -    { | |
| 103 | -        return $this->setParameter('merchantName', $value); | |
| 104 | - } | |
| 105 | - | |
| 106 | - public function getTerminalId() | |
| 107 | -    { | |
| 108 | -        return $this->getParameter('terminalId'); | |
| 109 | - } | |
| 110 | - | |
| 111 | - public function setTerminalId($value) | |
| 112 | -    { | |
| 113 | -        return $this->setParameter('terminalId', $value); | |
| 114 | - } | |
| 115 | - | |
| 116 | - /** | |
| 117 | - * Override the abstract method to add requirement that it must start with 4 numeric characters | |
| 118 | - * | |
| 119 | - * @param string|int $value The transaction ID (merchant order) to set for the transaction | |
| 120 | - */ | |
| 121 | - public function setTransactionId($value) | |
| 122 | -    { | |
| 123 | - $start = substr($value, 0, 4); | |
| 124 | - $numerics = 0; | |
| 125 | -        foreach (str_split($start) as $char) { | |
| 126 | -            if (is_numeric($char)) { | |
| 127 | - $numerics++; | |
| 128 | -            } else { | |
| 129 | - break; | |
| 130 | - } | |
| 131 | - } | |
| 132 | - $value = str_pad(substr($start, 0, $numerics), 4, 0, STR_PAD_LEFT).substr($value, $numerics); | |
| 133 | - | |
| 134 | - parent::setTransactionId($value); | |
| 135 | - } | |
| 136 | - | |
| 137 | - public function getData() | |
| 138 | -    { | |
| 139 | -        $this->validate('merchantId', 'terminalId', 'amount', 'currency'); | |
| 140 | - | |
| 141 | - return array( | |
| 142 | - // mandatory fields | |
| 143 | - 'Ds_Merchant_MerchantCode' => $this->getMerchantId(), | |
| 144 | - 'Ds_Merchant_Terminal' => $this->getTerminalId(), | |
| 145 | - 'Ds_Merchant_TransactionType' => '0', // Authorisation | |
| 146 | - 'Ds_Merchant_Amount' => $this->getAmountInteger(), | |
| 147 | - 'Ds_Merchant_Currency' => $this->getCurrencyNumeric(), // uses ISO-4217 codes | |
| 148 | - 'Ds_Merchant_Order' => $this->getTransactionId(), | |
| 149 | - 'Ds_Merchant_MerchantUrl' => $this->getNotifyUrl(), | |
| 150 | - // optional fields | |
| 151 | - 'Ds_Merchant_ProductDescription' => $this->getDescription(), | |
| 152 | - 'Ds_Merchant_Cardholder' => $this->getCardholder(), | |
| 153 | - 'Ds_Merchant_UrlOK' => $this->getReturnUrl(), | |
| 154 | - 'Ds_Merchant_UrlKO' => $this->getReturnUrl(), | |
| 155 | - 'Ds_Merchant_MerchantName' => $this->getMerchantName(), | |
| 156 | - 'Ds_Merchant_ConsumerLanguage' => $this->getConsumerLanguage(), | |
| 157 | - 'Ds_Merchant_MerchantData' => $this->getMerchantData(), | |
| 158 | - ); | |
| 159 | - } | |
| 160 | - | |
| 161 | - public function sendData($data) | |
| 162 | -    { | |
| 163 | - return $this->response = new PurchaseResponse($this, $data); | |
| 164 | - } | |
| 165 | - | |
| 166 | - public function getEndpoint() | |
| 167 | -    { | |
| 168 | - return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint; | |
| 169 | - } | |
| 12 | + /** @var string */ | |
| 13 | + protected $liveEndpoint = 'https://sis.redsys.es/sis/realizarPago'; | |
| 14 | + /** @var string */ | |
| 15 | + protected $testEndpoint = 'https://sis-t.redsys.es:25443/sis/realizarPago'; | |
| 16 | + /** @var array */ | |
| 17 | + protected static $consumerLanguages = array( | |
| 18 | + 'es' => '001', // Spanish | |
| 19 | + 'en' => '002', // English | |
| 20 | + 'ca' => '003', // Catalan - same as Valencian (010) | |
| 21 | + 'fr' => '004', // French | |
| 22 | + 'de' => '005', // German | |
| 23 | + 'nl' => '006', // Dutch | |
| 24 | + 'it' => '007', // Italian | |
| 25 | + 'sv' => '008', // Swedish | |
| 26 | + 'pt' => '009', // Portuguese | |
| 27 | + 'pl' => '011', // Polish | |
| 28 | + 'gl' => '012', // Galician | |
| 29 | + 'eu' => '013', // Basque | |
| 30 | + ); | |
| 31 | + | |
| 32 | + public function getCardholder() | |
| 33 | +	{ | |
| 34 | +		return $this->getParameter('cardholder'); | |
| 35 | + } | |
| 36 | + | |
| 37 | + public function setCardholder($value) | |
| 38 | +	{ | |
| 39 | +		return $this->setParameter('cardholder', $value); | |
| 40 | + } | |
| 41 | + | |
| 42 | + public function getConsumerLanguage() | |
| 43 | +	{ | |
| 44 | +		return $this->getParameter('consumerLanguage'); | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Set the language presented to the consumer | |
| 49 | + * | |
| 50 | + * @param string|int Either the ISO 639-1 code to be converted, or the gateway's own numeric language code | |
| 51 | + */ | |
| 52 | + public function setConsumerLanguage($value) | |
| 53 | +	{ | |
| 54 | +		if (is_int($value)) { | |
| 55 | +			if ($value < 0 || $value > 13) { | |
| 56 | + $value = 1; | |
| 57 | + } | |
| 58 | + $value = str_pad($value, 3, '0', STR_PAD_LEFT); | |
| 59 | +		} elseif (!is_numeric($value)) { | |
| 60 | + $value = isset(self::$consumerLanguages[$value]) ? self::$consumerLanguages[$value] : '001'; | |
| 61 | + } | |
| 62 | + | |
| 63 | +		return $this->setParameter('consumerLanguage', $value); | |
| 64 | + } | |
| 65 | + | |
| 66 | + public function getHmacKey() | |
| 67 | +	{ | |
| 68 | +		return $this->getParameter('hmacKey'); | |
| 69 | + } | |
| 70 | + | |
| 71 | + public function setHmacKey($value) | |
| 72 | +	{ | |
| 73 | +		return $this->setParameter('hmacKey', $value); | |
| 74 | + } | |
| 75 | + | |
| 76 | + public function getMerchantData() | |
| 77 | +	{ | |
| 78 | +		return $this->getParameter('merchantData'); | |
| 79 | + } | |
| 80 | + | |
| 81 | + public function setMerchantData($value) | |
| 82 | +	{ | |
| 83 | +		return $this->setParameter('merchantData', $value); | |
| 84 | + } | |
| 85 | + | |
| 86 | + public function getMerchantId() | |
| 87 | +	{ | |
| 88 | +		return $this->getParameter('merchantId'); | |
| 89 | + } | |
| 90 | + | |
| 91 | + public function setMerchantId($value) | |
| 92 | +	{ | |
| 93 | +		return $this->setParameter('merchantId', $value); | |
| 94 | + } | |
| 95 | + | |
| 96 | + public function getMerchantName() | |
| 97 | +	{ | |
| 98 | +		return $this->getParameter('merchantName'); | |
| 99 | + } | |
| 100 | + | |
| 101 | + public function setMerchantName($value) | |
| 102 | +	{ | |
| 103 | +		return $this->setParameter('merchantName', $value); | |
| 104 | + } | |
| 105 | + | |
| 106 | + public function getTerminalId() | |
| 107 | +	{ | |
| 108 | +		return $this->getParameter('terminalId'); | |
| 109 | + } | |
| 110 | + | |
| 111 | + public function setTerminalId($value) | |
| 112 | +	{ | |
| 113 | +		return $this->setParameter('terminalId', $value); | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * Override the abstract method to add requirement that it must start with 4 numeric characters | |
| 118 | + * | |
| 119 | + * @param string|int $value The transaction ID (merchant order) to set for the transaction | |
| 120 | + */ | |
| 121 | + public function setTransactionId($value) | |
| 122 | +	{ | |
| 123 | + $start = substr($value, 0, 4); | |
| 124 | + $numerics = 0; | |
| 125 | +		foreach (str_split($start) as $char) { | |
| 126 | +			if (is_numeric($char)) { | |
| 127 | + $numerics++; | |
| 128 | +			} else { | |
| 129 | + break; | |
| 130 | + } | |
| 131 | + } | |
| 132 | + $value = str_pad(substr($start, 0, $numerics), 4, 0, STR_PAD_LEFT).substr($value, $numerics); | |
| 133 | + | |
| 134 | + parent::setTransactionId($value); | |
| 135 | + } | |
| 136 | + | |
| 137 | + public function getData() | |
| 138 | +	{ | |
| 139 | +		$this->validate('merchantId', 'terminalId', 'amount', 'currency'); | |
| 140 | + | |
| 141 | + return array( | |
| 142 | + // mandatory fields | |
| 143 | + 'Ds_Merchant_MerchantCode' => $this->getMerchantId(), | |
| 144 | + 'Ds_Merchant_Terminal' => $this->getTerminalId(), | |
| 145 | + 'Ds_Merchant_TransactionType' => '0', // Authorisation | |
| 146 | + 'Ds_Merchant_Amount' => $this->getAmountInteger(), | |
| 147 | + 'Ds_Merchant_Currency' => $this->getCurrencyNumeric(), // uses ISO-4217 codes | |
| 148 | + 'Ds_Merchant_Order' => $this->getTransactionId(), | |
| 149 | + 'Ds_Merchant_MerchantUrl' => $this->getNotifyUrl(), | |
| 150 | + // optional fields | |
| 151 | + 'Ds_Merchant_ProductDescription' => $this->getDescription(), | |
| 152 | + 'Ds_Merchant_Cardholder' => $this->getCardholder(), | |
| 153 | + 'Ds_Merchant_UrlOK' => $this->getReturnUrl(), | |
| 154 | + 'Ds_Merchant_UrlKO' => $this->getReturnUrl(), | |
| 155 | + 'Ds_Merchant_MerchantName' => $this->getMerchantName(), | |
| 156 | + 'Ds_Merchant_ConsumerLanguage' => $this->getConsumerLanguage(), | |
| 157 | + 'Ds_Merchant_MerchantData' => $this->getMerchantData(), | |
| 158 | + ); | |
| 159 | + } | |
| 160 | + | |
| 161 | + public function sendData($data) | |
| 162 | +	{ | |
| 163 | + return $this->response = new PurchaseResponse($this, $data); | |
| 164 | + } | |
| 165 | + | |
| 166 | + public function getEndpoint() | |
| 167 | +	{ | |
| 168 | + return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint; | |
| 169 | + } | |
| 170 | 170 | } |