api_client = new RedsysAPI(); $this->environment = get_option('eb_redsys_test_mode') === 'yes' ? 'test' : 'live'; } /** * Create merchant parameters */ public function createMerchantParameters($params) { // Convert params to JSON $json = json_encode($params); // Encode in base64 return $this->api_client->encodeBase64($json); } /** * Create merchant signature */ public function createMerchantSignature($key, $data) { return $this->api_client->createMerchantSignature($key, $data); } /** * Decode merchant parameters */ public function decodeMerchantParameters($data) { $decoded = $this->api_client->decodeBase64($data); return json_decode($decoded, true); } /** * Check signature */ public function check_signature($merchant_parameters, $signature, $key) { $calculated_signature = $this->api_client->createMerchantSignatureNotif($key, $merchant_parameters); return $signature === $calculated_signature; } /** * Get endpoint URL */ public function get_endpoint() { if ($this->environment === 'test') { return 'https://sis-t.redsys.es:25443/sis/realizarPago'; } else { return 'https://sis.redsys.es/sis/realizarPago'; } } /** * Create notification signature */ public function createNotificationSignature($key, $data) { return $this->api_client->createMerchantSignatureNotif($key, $data); } /** * Format order number */ public function formatOrderNumber($booking_id) { // Ensure order number is exactly 12 digits return str_pad($booking_id, 12, '0', STR_PAD_LEFT); } /** * Format amount */ public function formatAmount($amount) { // Convert to cents and ensure no decimal places return number_format($amount * 100, 0, '', ''); } /** * Create future payment parameters */ public function createFuturePaymentParams($params, $execution_date) { $params['DS_MERCHANT_TRANSACTIONTYPE'] = 'L'; // Deferred payment $params['DS_MERCHANT_DATEFRECUENCY'] = '1'; // Daily frequency $params['DS_MERCHANT_CHARGEEXPIRYDATE'] = date('d/m/Y', strtotime($execution_date)); return $params; } /** * Create subscription parameters */ public function createSubscriptionParams($params) { $params['DS_MERCHANT_TRANSACTIONTYPE'] = 'L'; $params['DS_MERCHANT_COF_INI'] = 'S'; // Initial recurring payment $params['DS_MERCHANT_COF_TYPE'] = 'R'; // Recurring payment type return $params; } /** * Create subsequent payment parameters */ public function createSubsequentPaymentParams($params, $reference_id) { $params['DS_MERCHANT_TRANSACTIONTYPE'] = '0'; $params['DS_MERCHANT_IDENTIFIER'] = $reference_id; $params['DS_MERCHANT_DIRECTPAYMENT'] = 'true'; return $params; } /** * Parse notification response */ public function parseNotification($data) { if (!isset($data['Ds_SignatureVersion']) || !isset($data['Ds_MerchantParameters']) || !isset($data['Ds_Signature'])) { return false; } $decoded_params = $this->decodeMerchantParameters($data['Ds_MerchantParameters']); return array( 'params' => $decoded_params, 'signature' => $data['Ds_Signature'] ); } /** * Validate response code */ public function isSuccessResponse($response_code) { // Response codes less than 100 indicate success return $response_code < 100; } /** * Get response message */ public function getResponseMessage($response_code) { $messages = array( '0000' => __('Transaction authorized for payments and pre-authorizations', 'eb-redsys-gateway'), '0900' => __('Transaction authorized for refunds and confirmations', 'eb-redsys-gateway'), '0101' => __('Card expired', 'eb-redsys-gateway'), '0102' => __('Card temporarily suspended or under suspicion of fraud', 'eb-redsys-gateway'), '0104' => __('Operation not allowed for this type of card', 'eb-redsys-gateway'), '0116' => __('Insufficient funds', 'eb-redsys-gateway'), '0118' => __('Card not registered', 'eb-redsys-gateway'), '0129' => __('Security code (CVV2/CVC2) incorrect', 'eb-redsys-gateway'), '0180' => __('Card not recognized', 'eb-redsys-gateway'), '0184' => __('Cardholder authentication failed', 'eb-redsys-gateway'), '0190' => __('Transaction declined without specific reason', 'eb-redsys-gateway'), '0191' => __('Wrong expiration date', 'eb-redsys-gateway'), '0904' => __('Merchant not registered at FUC', 'eb-redsys-gateway'), '0909' => __('System error', 'eb-redsys-gateway'), '0912' => __('Issuer not available', 'eb-redsys-gateway'), '0913' => __('Duplicate order', 'eb-redsys-gateway'), '0944' => __('Wrong session', 'eb-redsys-gateway'), '0950' => __('Refund operation not allowed', 'eb-redsys-gateway'), ); return isset($messages[$response_code]) ? $messages[$response_code] : __('Unknown error', 'eb-redsys-gateway'); } /** * Log error */ public function log_error($message, $data = array()) { if (function_exists('eb_log')) { $log_message = 'Redsys API Error: ' . $message; if (!empty($data)) { $log_message .= ' | Data: ' . print_r($data, true); } eb_log($log_message); } } }