From bc3b86b70517fed9b0a9f588c16c01f9b0648aaa Mon Sep 17 00:00:00 2001 From: matej Date: Fri, 26 Sep 2025 21:15:08 +0200 Subject: [PATCH] Added WooCommerce AluxPay Payment Gateway plugin --- aluxpay-payment-gateway/README.txt | 141 ++++++++++ .../aluxpay-payment-gateway.php | 89 +++++++ .../class-aluxpay-payment-gateway.php | 248 ++++++++++++++++++ .../includes/class-payment-return-handler.php | 81 ++++++ 4 files changed, 559 insertions(+) create mode 100644 aluxpay-payment-gateway/README.txt create mode 100644 aluxpay-payment-gateway/aluxpay-payment-gateway.php create mode 100644 aluxpay-payment-gateway/includes/class-aluxpay-payment-gateway.php create mode 100644 aluxpay-payment-gateway/includes/class-payment-return-handler.php diff --git a/aluxpay-payment-gateway/README.txt b/aluxpay-payment-gateway/README.txt new file mode 100644 index 0000000..732d943 --- /dev/null +++ b/aluxpay-payment-gateway/README.txt @@ -0,0 +1,141 @@ +=== Custom Payment Gateway === +Contributors: ALUXB +Tags: woocommerce, payment, paypal, gateway +Requires at least: 5.8 +Tested up to: 6.3 +Requires PHP: 7.4 +Stable tag: 1.0.0 +License: GPLv2 or later +License URI: https://www.gnu.org/licenses/gpl-2.0.html + +A custom WooCommerce payment gateway that redirects to a standalone payment website with PayPal integration. + +== Description == + +This plugin adds a custom payment gateway to WooCommerce that redirects customers to a standalone payment processing website. The payment website handles PayPal transactions securely and returns the customer back to WooCommerce with the payment status. + +**Features:** + +* Seamless integration with WooCommerce checkout +* Redirects to standalone payment website +* Secure PayPal payment processing +* Automatic order status updates +* Test mode for development +* Debug logging capabilities +* Webhook support for payment notifications + +== Installation == + +1. Upload the plugin files to the `/wp-content/plugins/custom-payment-gateway` directory, or install the plugin through the WordPress plugins screen directly. +2. Activate the plugin through the 'Plugins' screen in WordPress +3. Go to WooCommerce → Settings → Payments +4. Enable "Custom Payment Gateway" +5. Click "Manage" to configure the settings +6. Enter your payment website URL +7. Save changes + +== Configuration == + +**Required Settings:** + +* **Title**: The payment method name shown to customers (e.g., "PayPal Payment") +* **Description**: Text shown to customers during checkout +* **Payment Website URL**: The URL of your standalone payment website (e.g., https://payment.yoursite.com/payment) + +**Optional Settings:** + +* **Test Mode**: Enable to use test/sandbox environment +* **Debug Log**: Enable to log all gateway activities for troubleshooting + +== Payment Flow == + +1. Customer proceeds to checkout on WooCommerce +2. Selects "Custom Payment Gateway" as payment method +3. Customer is redirected to the standalone payment website with order details +4. Customer completes payment via PayPal +5. Customer is redirected back to WooCommerce +6. Order status is automatically updated based on payment result + +== Webhook Configuration == + +Your standalone payment website should send webhook notifications to: +`https://yoursite.com/?wc-api=custom_payment_gateway` + +Webhook payload should be JSON: +```json +{ + "order_id": 123, + "transaction_id": "ABC123XYZ", + "status": "completed" +} +``` + +== URL Parameters == + +When redirecting to the payment website, the following parameters are sent: + +* `wc_order_id` - WooCommerce order ID +* `total` - Order total amount +* `currency` - Currency code (e.g., USD, EUR) +* `description` - Order description +* `customer_email` - Customer email address +* `return_url` - URL to return after successful payment +* `cancel_url` - URL to return if payment is cancelled +* `_wpnonce` - Security nonce + +== Frequently Asked Questions == + += Does this work with PayPal? = + +Yes, this gateway is designed to work with a standalone payment website that processes PayPal payments. + += Can I test payments without going live? = + +Yes, enable "Test Mode" in the gateway settings and use PayPal sandbox credentials on your payment website. + += Where can I find the debug logs? = + +Enable "Debug log" in settings. Logs are stored in: WooCommerce → Status → Logs + += How do I handle payment returns? = + +The plugin automatically handles returns from your payment website. Your payment website should redirect back to the `return_url` with appropriate parameters. + +== Changelog == + += 1.0.0 = +* Initial release +* Basic payment gateway functionality +* Redirect to standalone payment website +* PayPal integration support +* Webhook handler +* Debug logging + +== Upgrade Notice == + += 1.0.0 = +Initial release of Custom Payment Gateway. + +== Support == + +For support, please contact: support@yoursite.com + +== Technical Requirements == + +* WordPress 5.8 or higher +* WooCommerce 5.0 or higher +* PHP 7.4 or higher +* Standalone payment processing website +* HTTPS/SSL certificate (required for production) + +== Integration with Your Payment Website == + +Your payment website should: + +1. Accept GET parameters from WooCommerce (order_id, total, currency, etc.) +2. Process payment via PayPal +3. Send webhook notification to WooCommerce +4. Redirect customer back to return_url with payment status + +Example redirect after successful payment: +`https://yourstore.com/checkout/order-received/123/?key=wc_order_xyz&cpg_return=1&order_id=123&transaction_id=ABC123&payment_status=success` \ No newline at end of file diff --git a/aluxpay-payment-gateway/aluxpay-payment-gateway.php b/aluxpay-payment-gateway/aluxpay-payment-gateway.php new file mode 100644 index 0000000..420800c --- /dev/null +++ b/aluxpay-payment-gateway/aluxpay-payment-gateway.php @@ -0,0 +1,89 @@ +' . __('Settings', 'aluxpay-payment-gateway') . '', + ); + return array_merge($plugin_links, $links); + } + +} else { + /** + * WooCommerce not active notice + */ + add_action('admin_notices', 'cpg_woocommerce_missing_notice'); + + function cpg_woocommerce_missing_notice() { + echo '

' . sprintf( + esc_html__('AluxPay Payment Gateway requires WooCommerce to be installed and active. You can download %s here.', 'aluxpay-payment-gateway'), + 'WooCommerce' + ) . '

'; + } +} \ No newline at end of file diff --git a/aluxpay-payment-gateway/includes/class-aluxpay-payment-gateway.php b/aluxpay-payment-gateway/includes/class-aluxpay-payment-gateway.php new file mode 100644 index 0000000..6f9ee97 --- /dev/null +++ b/aluxpay-payment-gateway/includes/class-aluxpay-payment-gateway.php @@ -0,0 +1,248 @@ +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 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.', '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) { + + $params = array( + 'wc_order_id' => $order->get_id(), + 'total' => $order->get_total(), + 'currency' => $order->get_currency(), + 'description' => sprintf(__('Order #%s from %s', 'aluxpay-payment-gateway'), $order->get_id(), get_bloginfo('name')), + 'customer_email' => $order->get_billing_email(), + 'return_url' => $this->get_return_url($order), + 'cancel_url' => wc_get_checkout_url(), + ); + + // 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; + } + } +} \ No newline at end of file diff --git a/aluxpay-payment-gateway/includes/class-payment-return-handler.php b/aluxpay-payment-gateway/includes/class-payment-return-handler.php new file mode 100644 index 0000000..72950e6 --- /dev/null +++ b/aluxpay-payment-gateway/includes/class-payment-return-handler.php @@ -0,0 +1,81 @@ +payment_complete($transaction_id); + $order->add_order_note( + sprintf(__('Payment completed. Transaction ID: %s', 'aluxpay-payment-gateway'), $transaction_id) + ); + + // Redirect to success page + wp_redirect($order->get_checkout_order_received_url()); + exit; + + } elseif ($status === 'cancelled' || $status === 'failed') { + + // Mark as failed + $order->update_status('failed', __('Payment was cancelled or failed.', 'aluxpay-payment-gateway')); + + wc_add_notice(__('Payment was not completed. Please try again.', 'aluxpay-payment-gateway'), 'error'); + wp_redirect(wc_get_checkout_url()); + exit; + + } else { + + // Unknown status + wc_add_notice(__('Payment status unknown. Please contact support.', 'aluxpay-payment-gateway'), 'error'); + wp_redirect(wc_get_checkout_url()); + exit; + } + } +} + +new CPG_Payment_Return_Handler(); \ No newline at end of file