id = 'aluxpay_payment_gateway'; $this->icon = ''; // URL to icon (optional) $this->method_title = __('AluxPay Payment Gateway', 'aluxpay-payment-gateway'); $this->method_description = __('Redirects customers to a standalone payment website for secure PayPal payments.', 'aluxpay-payment-gateway'); $this->has_fields = false; // Load the settings $this->init_form_fields(); $this->init_settings(); // Define user set variables $this->title = $this->get_option('title'); $this->description = $this->get_option('description'); $this->payment_url = $this->get_option('payment_url'); $this->enabled = $this->get_option('enabled'); $this->testmode = 'yes' === $this->get_option('testmode'); $this->debug = 'yes' === $this->get_option('debug', 'no'); // Logs if ($this->debug) { $this->log = wc_get_logger(); } // Actions add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); add_action('woocommerce_api_' . $this->id, array($this, 'webhook_handler')); add_action('woocommerce_thankyou_' . $this->id, array($this, 'thankyou_page')); // Customer Emails add_action('woocommerce_email_before_order_table', array($this, 'email_instructions'), 10, 3); } /** * Initialize Gateway Settings Form Fields */ public function init_form_fields() { $this->form_fields = array( 'enabled' => array( 'title' => __('Enable/Disable', 'aluxpay-payment-gateway'), 'label' => __('Enable AluxPay Payment Gateway', 'aluxpay-payment-gateway'), 'type' => 'checkbox', 'description' => '', 'default' => 'no' ), 'title' => array( 'title' => __('Title', 'aluxpay-payment-gateway'), 'type' => 'text', 'description' => __('This controls the title which the user sees during checkout.', 'aluxpay-payment-gateway'), 'default' => __(' PayPal or Credit/Debit Card Payment ', 'aluxpay-payment-gateway'), 'desc_tip' => true, ), 'description' => array( 'title' => __('Description', 'aluxpay-payment-gateway'), 'type' => 'textarea', 'description' => __('Payment method description that the customer will see on your checkout.', 'aluxpay-payment-gateway'), 'default' => __('You will be redirected to complete payment securely with PayPal or Credit Card.', 'aluxpay-payment-gateway'), 'desc_tip' => true, ), 'payment_url' => array( 'title' => __('Payment Website URL', 'aluxpay-payment-gateway'), 'type' => 'text', 'description' => __('The URL of your standalone payment website (e.g., https://payment.yoursite.com)', 'aluxpay-payment-gateway'), 'default' => 'http://localhost:5173/payment', 'desc_tip' => true, ), 'testmode' => array( 'title' => __('Test mode', 'aluxpay-payment-gateway'), 'label' => __('Enable Test Mode', 'aluxpay-payment-gateway'), 'type' => 'checkbox', 'description' => __('Place the payment gateway in test mode using test API keys.', 'aluxpay-payment-gateway'), 'default' => 'yes', 'desc_tip' => true, ), 'debug' => array( 'title' => __('Debug log', 'aluxpay-payment-gateway'), 'label' => __('Enable logging', 'aluxpay-payment-gateway'), 'type' => 'checkbox', 'description' => sprintf(__('Log events inside %s Note: this may log personal information. We recommend using this for debugging purposes only.', 'aluxpay-payment-gateway'), '' . WC_Log_Handler_File::get_log_file_path($this->id) . ''), 'default' => 'no', ), ); } /** * Process the payment and return the result */ public function process_payment($order_id) { $order = wc_get_order($order_id); if ($this->debug) { $this->log->info('Processing payment for order #' . $order_id, array('source' => $this->id)); } // Mark as pending payment $order->update_status('pending', __('Awaiting payment via AluxPay Payment Gateway.', 'aluxpay-payment-gateway')); // Reduce stock levels wc_reduce_stock_levels($order_id); // Remove cart WC()->cart->empty_cart(); // Build redirect URL with order parameters $redirect_url = $this->get_payment_redirect_url($order); if ($this->debug) { $this->log->info('Redirecting to: ' . $redirect_url, array('source' => $this->id)); } // Return redirect return array( 'result' => 'success', 'redirect' => $redirect_url ); } /** * Build payment redirect URL */ private function get_payment_redirect_url($order) { // Build description without special characters OR encode it $description = sprintf( __('Order %s from %s', 'aluxpay-payment-gateway'), $order->get_id(), get_bloginfo('name') ); $params = array( 'wc_order_id' => $order->get_id(), 'total' => $order->get_total(), 'currency' => $order->get_currency(), 'description' => $description, 'customer_email' => $order->get_billing_email(), 'return_url' => $this->get_return_url($order), 'cancel_url' => wc_get_checkout_url(), '_wpnonce' => wp_create_nonce('cpg_payment_' . $order->get_id()), ); // Add nonce for security $params['_wpnonce'] = wp_create_nonce('cpg_payment_' . $order->get_id()); return add_query_arg($params, $this->payment_url); } /** * Webhook handler for payment updates */ public function webhook_handler() { if ($this->debug) { $this->log->info('Webhook received', array('source' => $this->id)); } // Get the raw POST data $raw_post = file_get_contents('php://input'); $decoded = json_decode($raw_post); if (!$decoded || !isset($decoded->order_id) || !isset($decoded->transaction_id)) { if ($this->debug) { $this->log->error('Invalid webhook data received', array('source' => $this->id)); } status_header(400); die('Invalid data'); } $order_id = absint($decoded->order_id); $transaction_id = sanitize_text_field($decoded->transaction_id); $status = sanitize_text_field($decoded->status); $order = wc_get_order($order_id); if (!$order) { if ($this->debug) { $this->log->error('Order not found: ' . $order_id, array('source' => $this->id)); } status_header(404); die('Order not found'); } // Process based on status if ($status === 'completed' || $status === 'COMPLETED') { // Payment complete $order->payment_complete($transaction_id); // Add order note $order->add_order_note( sprintf(__('Payment completed via AluxPay Payment Gateway. Transaction ID: %s', 'aluxpay-payment-gateway'), $transaction_id) ); if ($this->debug) { $this->log->info('Payment completed for order #' . $order_id . ' Transaction ID: ' . $transaction_id, array('source' => $this->id)); } status_header(200); die('Success'); } else { // Payment failed $order->update_status('failed', __('Payment failed or was declined.', 'aluxpay-payment-gateway')); if ($this->debug) { $this->log->warning('Payment failed for order #' . $order_id, array('source' => $this->id)); } status_header(200); die('Marked as failed'); } } /** * Output for the order received page */ public function thankyou_page($order_id) { if ($this->description) { echo wpautop(wptexturize($this->description)); } $order = wc_get_order($order_id); if ($order && $order->get_status() === 'processing') { echo '

' . __('Your payment has been received and your order is being processed.', 'aluxpay-payment-gateway') . '

'; } } /** * Add content to the WC emails */ public function email_instructions($order, $sent_to_admin, $plain_text = false) { if ($this->description && !$sent_to_admin && $this->id === $order->get_payment_method()) { echo wpautop(wptexturize($this->description)) . PHP_EOL; } } }