81 lines
2.4 KiB
PHP
81 lines
2.4 KiB
PHP
<?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(); |