id = 'redsys'; $this->title = __('Credit Card (Redsys)', 'eb-redsys-gateway'); $this->description = __('Pay securely via credit card using Redsys.', 'eb-redsys-gateway'); // Load settings $this->test_mode = get_option('eb_redsys_test_mode', 'yes'); $this->merchant_code = get_option('eb_redsys_merchant_code'); $this->terminal = get_option('eb_redsys_terminal'); $this->encryption_key = get_option('eb_redsys_encryption_key'); // Initialize API $this->api = new Redsys_API(); // Hooks add_action('eb_payment_gateway_redsys', array($this, 'process_payment')); add_action('eb_payment_gateway_redsys_callback', array($this, 'process_callback')); } public function get_endpoint() { return ($this->test_mode === 'yes') ? 'https://sis-t.redsys.es:25443/sis/realizarPago' : 'https://sis.redsys.es/sis/realizarPago'; } public function process_payment($booking_data) { $order_id = $booking_data['booking_id']; $amount = $booking_data['amount']; // Prepare payment data $payment_data = array( 'DS_MERCHANT_AMOUNT' => $amount * 100, // Amount in cents 'DS_MERCHANT_ORDER' => $order_id, 'DS_MERCHANT_MERCHANTCODE' => $this->merchant_code, 'DS_MERCHANT_CURRENCY' => '978', // EUR 'DS_MERCHANT_TRANSACTIONTYPE' => '0', // Authorization 'DS_MERCHANT_TERMINAL' => $this->terminal, 'DS_MERCHANT_MERCHANTURL' => add_query_arg('gateway', 'redsys', eb_get_callback_url()), 'DS_MERCHANT_URLOK' => add_query_arg('result', 'success', eb_get_return_url($order_id)), 'DS_MERCHANT_URLKO' => add_query_arg('result', 'failure', eb_get_return_url($order_id)) ); // For future dated payments if (!empty($booking_data['check_in_date'])) { $payment_data['DS_MERCHANT_DATEFRECUENCY'] = '1'; // Daily frequency $payment_data['DS_MERCHANT_CHARGEEXPIRYDATE'] = date('d/m/Y', strtotime($booking_data['check_in_date'])); $payment_data['DS_MERCHANT_TRANSACTIONTYPE'] = 'L'; // Deferred payment } // Generate signature $signature = $this->api->createMerchantSignature($this->encryption_key, $payment_data); // Build form $form = '
'; $form .= ''; echo $form; exit; } public function process_callback() { $params = $_POST['Ds_MerchantParameters']; $signature = $_POST['Ds_Signature']; // Verify signature if (!$this->api->check_signature($params, $signature, $this->encryption_key)) { wp_die('Invalid signature', 'Redsys Error', array('response' => 403)); } $response = $this->api->decode_parameters($params); // Check response code if ($response['Ds_Response'] < 100) { // Payment successful $order_id = $response['Ds_Order']; $transaction_id = $response['Ds_AuthorisationCode']; // Update booking status eb_update_booking_status($order_id, 'completed', $transaction_id); echo "OK"; // Response to Redsys exit; } // Payment failed $order_id = $response['Ds_Order']; eb_update_booking_status($order_id, 'failed'); echo "KO"; exit; } }