Added WooCommerce AluxPay Payment Gateway plugin
This commit is contained in:
141
aluxpay-payment-gateway/README.txt
Normal file
141
aluxpay-payment-gateway/README.txt
Normal file
@@ -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`
|
||||||
89
aluxpay-payment-gateway/aluxpay-payment-gateway.php
Normal file
89
aluxpay-payment-gateway/aluxpay-payment-gateway.php
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Plugin Name: AluxPay Payment Gateway
|
||||||
|
* Plugin URI: https://affordableluxurybags.is
|
||||||
|
* Description: AluxPay payment gateway that redirects to standalone payment website with PayPal integration
|
||||||
|
* Version: 1.0.0
|
||||||
|
* Author: ALUXB
|
||||||
|
* Author URI: https://affordableluxurybags.is
|
||||||
|
* Text Domain: aluxpay-payment-gateway
|
||||||
|
* Domain Path: /languages
|
||||||
|
* Requires at least: 5.8
|
||||||
|
* Requires PHP: 7.4
|
||||||
|
* WC requires at least: 5.0
|
||||||
|
* WC tested up to: 8.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit; // Exit if accessed directly
|
||||||
|
}
|
||||||
|
|
||||||
|
// Declare HPOS compatibility
|
||||||
|
add_action('before_woocommerce_init', function() {
|
||||||
|
if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
|
||||||
|
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Define plugin constants
|
||||||
|
define('CPG_VERSION', '1.0.0');
|
||||||
|
define('CPG_PLUGIN_DIR', plugin_dir_path(__FILE__));
|
||||||
|
define('CPG_PLUGIN_URL', plugin_dir_url(__FILE__));
|
||||||
|
|
||||||
|
// Check if WooCommerce is active
|
||||||
|
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the gateway
|
||||||
|
*/
|
||||||
|
add_action('plugins_loaded', 'cpg_init_gateway');
|
||||||
|
|
||||||
|
function cpg_init_gateway() {
|
||||||
|
|
||||||
|
// Make sure WooCommerce payment gateway class exists
|
||||||
|
if (!class_exists('WC_Payment_Gateway')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Include the gateway class
|
||||||
|
require_once CPG_PLUGIN_DIR . 'includes/class-aluxpay-payment-gateway.php';
|
||||||
|
|
||||||
|
// Include the return handler
|
||||||
|
require_once CPG_PLUGIN_DIR . 'includes/class-payment-return-handler.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add the gateway to WooCommerce
|
||||||
|
*/
|
||||||
|
add_filter('woocommerce_payment_gateways', 'cpg_add_gateway');
|
||||||
|
|
||||||
|
function cpg_add_gateway($gateways) {
|
||||||
|
$gateways[] = 'WC_AluxPay_Payment_Gateway';
|
||||||
|
return $gateways;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add custom action links
|
||||||
|
*/
|
||||||
|
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'cpg_action_links');
|
||||||
|
|
||||||
|
function cpg_action_links($links) {
|
||||||
|
$plugin_links = array(
|
||||||
|
'<a href="' . admin_url('admin.php?page=wc-settings&tab=checkout§ion=aluxpay_payment_gateway') . '">' . __('Settings', 'aluxpay-payment-gateway') . '</a>',
|
||||||
|
);
|
||||||
|
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 '<div class="error"><p><strong>' . sprintf(
|
||||||
|
esc_html__('AluxPay Payment Gateway requires WooCommerce to be installed and active. You can download %s here.', 'aluxpay-payment-gateway'),
|
||||||
|
'<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>'
|
||||||
|
) . '</strong></p></div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,248 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* AluxPay Payment Gateway Class
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
class WC_Aluxpay_Payment_Gateway extends WC_Payment_Gateway {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
|
||||||
|
$this->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'), '<code>' . WC_Log_Handler_File::get_log_file_path($this->id) . '</code>'),
|
||||||
|
'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 '<p>' . __('Your payment has been received and your order is being processed.', 'aluxpay-payment-gateway') . '</p>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Payment Return Handler
|
||||||
|
* Handles return from payment website
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
class CPG_Payment_Return_Handler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
add_action('init', array($this, 'handle_payment_return'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle payment return
|
||||||
|
*/
|
||||||
|
public function handle_payment_return() {
|
||||||
|
|
||||||
|
// Check if this is a payment return
|
||||||
|
if (!isset($_GET['cpg_return']) || $_GET['cpg_return'] !== '1') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get parameters
|
||||||
|
$order_id = isset($_GET['order_id']) ? absint($_GET['order_id']) : 0;
|
||||||
|
$transaction_id = isset($_GET['transaction_id']) ? sanitize_text_field($_GET['transaction_id']) : '';
|
||||||
|
$status = isset($_GET['payment_status']) ? sanitize_text_field($_GET['payment_status']) : '';
|
||||||
|
|
||||||
|
if (!$order_id) {
|
||||||
|
wc_add_notice(__('Invalid order ID', 'aluxpay-payment-gateway'), 'error');
|
||||||
|
wp_redirect(wc_get_checkout_url());
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$order = wc_get_order($order_id);
|
||||||
|
|
||||||
|
if (!$order) {
|
||||||
|
wc_add_notice(__('Order not found', 'aluxpay-payment-gateway'), 'error');
|
||||||
|
wp_redirect(wc_get_checkout_url());
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle based on status
|
||||||
|
if ($status === 'success' && $transaction_id) {
|
||||||
|
|
||||||
|
// Mark payment complete
|
||||||
|
$order->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();
|
||||||
Reference in New Issue
Block a user