id = 'alatpay'; $this->method_title = esc_html__('ALATPay', 'alatpay'); $this->icon = apply_filters('woocommerce_alatpay_icon', esc_url(plugins_url('assets/images/AlatPayGroup.png', __FILE__))); $this->method_description = $this->get_admin_method_description(); $this->init_form_fields(); $this->init_settings(); $this->title = $this->get_option('title'); $this->description = $this->get_option('description'); $this->test_mode = $this->get_option('test_mode'); $this->business_id = $this->get_option('business_id'); $this->enabled = $this->get_option('enabled'); $this->api_key = $this->get_option('api_key'); $this->instructions = $this->get_option('instructions', $this->description); // Hook into WooCommerce actions add_action('wp_enqueue_scripts', array($this, 'alatpay_enqueue_scripts')); add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); // add_action('woocommerce_checkout_order_processed', array($this, 'clear_pending_order_session'), 20, 1); add_action('admin_notices', array($this, 'maybe_display_missing_credentials_notice')); add_action('admin_footer', array($this, 'output_webhook_notice_script')); } public function init_form_fields() { $this->form_fields = array( 'enabled' => array( 'title' => esc_html__('Enable/Disable', 'alatpay'), 'type' => 'checkbox', 'label' => esc_html__('Enable ALATPay Payments', 'alatpay'), 'default' => 'no', ), 'test_mode' => array( 'title' => esc_html__('Enable/Disable', 'alatpay'), 'type' => 'checkbox', 'label' => esc_html__('Enable Test Mode', 'alatpay'), 'default' => 'no', ), 'title' => array( 'title' => esc_html__('Gateway Title', 'alatpay'), 'type' => 'text', 'default' => esc_html__('ALATPay', 'alatpay'), 'desc_tip' => true, 'description' => esc_html__('This title is displayed to customers at checkout.', 'alatpay'), ), 'description' => array( 'title' => esc_html__('Description', 'alatpay'), 'type' => 'textarea', 'default' => esc_html__('Pay securely using ALATPay.', 'alatpay'), 'desc_tip' => true, 'description' => esc_html__('This description is displayed to customers at checkout.', 'alatpay'), ), 'api_key' => array( 'title' => esc_html__('API Key', 'alatpay'), 'type' => 'text', 'default' => '', 'description' => esc_html__('Your ALATPay API key. This field is required.', 'alatpay'), 'custom_attributes' => array( 'required' => 'required', ), ), 'business_id' => array( 'title' => esc_html__('Business ID', 'alatpay'), 'type' => 'text', 'default' => '', 'description' => esc_html__('Your ALATPay business ID. This field is required.', 'alatpay'), 'custom_attributes' => array( 'required' => 'required', ), ), 'auto_complete_order' => array( 'title' => esc_html__('Auto Complete Order', 'alatpay'), 'type' => 'checkbox', 'label' => esc_html__('Enable', 'alatpay'), 'description' => esc_html__('Enable automatic order completion after successful payment.', 'alatpay'), 'default' => 'no', ), 'webhook_secret' => array( 'title' => esc_html__('Webhook Secret', 'alatpay'), 'type' => 'text', 'default' => '', 'description' => esc_html__('Used to verify incoming webhooks from ALATPay. This must match the secret in your ALATPay merchant dashboard and is required.', 'alatpay'), 'custom_attributes' => array( 'required' => 'required', ), ), ); } // Process the payment public function process_payment($order_id) { $order = wc_get_order($order_id); // Check if the order exists if (! $order) { return array( 'result' => 'failure', 'redirect' => esc_url(wc_get_checkout_url() . '?result=fail'), ); } // Check if the order has already been completed if ($order->has_status(array('processing', 'completed'))) { return array( 'result' => 'failure', 'redirect' => esc_url($order->get_checkout_order_received_url()), ); } // Redirect to custom payment page. return array( 'result' => 'success', 'redirect' => esc_url($order->get_checkout_payment_url(true)), ); } public function alatpay_enqueue_scripts() { // Test Mode if ($this->test_mode == 'yes') { wp_enqueue_script('alatpay-js', esc_url('https://alatpay-client.azurewebsites.net/js/alatpay.js'), array(), '1.0.0', true); wp_enqueue_style('alatpay-style', esc_url(plugins_url('/assets/styles/alatpay-style.css', __FILE__)), array(), '1.0.0'); return; } wp_enqueue_script('alatpay-js', esc_url('https://web.alatpay.ng/js/alatpay.js'), array(), '1.0.0', true); wp_enqueue_style('alatpay-style', esc_url(plugins_url('/assets/styles/alatpay-style.css', __FILE__)), array(), '1.0.0'); } public function process_admin_options() { $post_data = $this->get_post_data(); $required_fields = array( 'api_key' => esc_html__('API Key', 'alatpay'), 'business_id' => esc_html__('Business ID', 'alatpay'), 'webhook_secret' => esc_html__('Webhook Secret', 'alatpay'), ); $missing = array(); foreach ($required_fields as $field_key => $label) { $post_key = $this->get_field_key($field_key); $raw_value = isset($post_data[$post_key]) ? $post_data[$post_key] : ''; $value = is_string($raw_value) ? trim(wp_unslash($raw_value)) : ''; if ('' === $value) { $missing[] = $label; } } if (! empty($missing)) { $list = function_exists('wc_format_list_of_items') ? wc_format_list_of_items($missing) : implode(', ', $missing); WC_Admin_Settings::add_error( sprintf( /* translators: %s list of required credentials. */ esc_html__('Please provide the following ALATPay credentials before saving: %s.', 'alatpay'), esc_html($list) ) ); return false; } $result = parent::process_admin_options(); if ($result) { $this->settings['api_key'] = sanitize_text_field($this->settings['api_key']); $this->settings['business_id'] = sanitize_text_field($this->settings['business_id']); $this->settings['webhook_secret'] = sanitize_text_field($this->settings['webhook_secret']); } return $result; } public function maybe_display_missing_credentials_notice() { if (! is_admin() || ! current_user_can('manage_woocommerce')) { return; } if (! function_exists('get_current_screen')) { return; } $screen = get_current_screen(); if (! $screen || false === strpos($screen->id, 'woocommerce')) { return; } $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : ''; $tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : ''; $section = isset($_GET['section']) ? sanitize_text_field(wp_unslash($_GET['section'])) : ''; if ('wc-settings' !== $page || 'checkout' !== $tab || $this->id !== $section) { return; } if (self::$credentials_notice_displayed) { return; } $missing = array(); if (! $this->business_id) { $missing[] = esc_html__('Business ID', 'alatpay'); } if (! $this->api_key) { $missing[] = esc_html__('API Key', 'alatpay'); } if (! $this->get_option('webhook_secret')) { $missing[] = esc_html__('Webhook Secret', 'alatpay'); } if (! $missing) { return; } $message = sprintf( /* translators: %s list of missing credentials. */ esc_html__('ALATPay requires the following settings: %s. Please configure them under WooCommerce → Settings → Payments → ALATPay.', 'alatpay'), esc_html(is_callable('wc_format_list_of_items') ? wc_format_list_of_items($missing) : implode(', ', $missing)) ); self::$credentials_notice_displayed = true; echo '

' . $message . '

'; } public function get_admin_method_description() { $current_site_webhook_url = trailingslashit(home_url('/')) . 'wp-json/alatpay/v1/webhook'; $has_credentials = '' !== trim((string) $this->get_option('api_key')) && '' !== trim((string) $this->get_option('business_id')); $notice_style = $has_credentials ? '' : 'display:none;'; $base_description = sprintf( esc_html__('ALATPay provides a seamless way to accept payments from customers worldwide. %1$s and %2$s.', 'alatpay'), '' . esc_html__('Create an ALATPay account', 'alatpay') . '', '' . esc_html__('retrieve your API keys', 'alatpay') . '' ); $webhook_instructions = sprintf( /* translators: 1: Link to ALATPay webhook documentation, 2: The user's webhook URL. */ __('To ensure that you can verify transactions regardless of network issues, kindly set your webhook URL as provided below. Steps to configure your webhook URL on the ALATPay dashboard are explained %1$s.
Webhook URL: %2$s', 'alatpay'), '' . esc_html__('here', 'alatpay') . '', '' . esc_html($current_site_webhook_url) . '' ); ob_start(); ?>

id, 'woocommerce')) { return; } $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : ''; $tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : ''; $section = isset($_GET['section']) ? sanitize_text_field(wp_unslash($_GET['section'])) : ''; if ('wc-settings' !== $page || 'checkout' !== $tab || $this->id !== $section) { return; } ?> get_user_id() !== $current_user_id) { alatpay_log('warning', 'Unauthorized attempt to update order status. Order ID: ' . $order_id . ', User ID: ' . $current_user_id); wp_die(esc_html__('Unauthorized action.', 'alatpay')); } // Validate status. $allowed_statuses = array('completed', 'processing'); if (! in_array($status, $allowed_statuses, true)) { alatpay_log('warning', 'Invalid order status. Order ID: ' . $order_id . ', Status: ' . $status); wp_die(esc_html__('Invalid status.', 'alatpay')); } // Enforce gateway configuration: only allow "completed" when auto-complete is enabled. $gateway = new Alatpay_Payment_Gateway(); $auto_complete_order = $gateway->get_option('auto_complete_order'); if ('completed' === $status && 'yes' !== $auto_complete_order) { $status = 'processing'; } $needs_save = false; if (! empty($transaction_id)) { $order->set_transaction_id($transaction_id); $order->add_order_note(sprintf(__('ALATPay transaction ID: %s', 'alatpay'), $transaction_id)); $needs_save = true; } // Require transaction_id and verify it server-side before finalizing the order. if (empty($transaction_id)) { alatpay_log('warning', 'Missing transaction ID on checkout callback. Order ID: ' . $order_id); wp_die(esc_html__('Missing transaction ID.', 'alatpay')); } if (function_exists('alatpay_is_transaction_processed') && alatpay_is_transaction_processed($order, $transaction_id)) { wp_safe_redirect(esc_url($order->get_checkout_order_received_url())); exit; } $requery_data = function_exists('alatpay_requery_transaction') ? alatpay_requery_transaction($transaction_id, $gateway) : new WP_Error('requery_unavailable', 'Requery handler unavailable'); if (is_wp_error($requery_data)) { alatpay_log('error', 'ALATPay requery failed on checkout callback. Order ID: ' . $order_id . ', Error: ' . $requery_data->get_error_message()); wp_die(esc_html__('Unable to verify transaction. Please contact support.', 'alatpay')); } $verified_transaction_id = function_exists('alatpay_get_transaction_id_from_payload') ? alatpay_get_transaction_id_from_payload($requery_data) : ''; if (! empty($verified_transaction_id) && $verified_transaction_id !== $transaction_id) { wp_die(esc_html__('Transaction verification failed.', 'alatpay')); } $verified_status = function_exists('alatpay_get_payload_status') ? alatpay_get_payload_status($requery_data) : strtolower((string) ($requery_data['Status'] ?? $requery_data['status'] ?? '')); if ('completed' !== $verified_status) { wp_die(esc_html__('Transaction is not completed.', 'alatpay')); } $verified_order_id = function_exists('alatpay_extract_order_id_from_payload') ? alatpay_extract_order_id_from_payload($requery_data) : 0; if ($verified_order_id && $verified_order_id !== $order_id) { wp_die(esc_html__('Transaction does not match order.', 'alatpay')); } $stored_order_token = sanitize_text_field((string) $order->get_meta('_alatpay_order_token', true)); $verified_order_token = function_exists('alatpay_extract_order_token_from_payload') ? alatpay_extract_order_token_from_payload($requery_data) : ''; if ($stored_order_token && $verified_order_token && $stored_order_token !== $verified_order_token) { wp_die(esc_html__('Order verification failed.', 'alatpay')); } $order_currency = strtoupper((string) $order->get_currency()); $payment_currency = function_exists('alatpay_get_payload_currency') ? alatpay_get_payload_currency($requery_data) : strtoupper((string) ($requery_data['Currency'] ?? $requery_data['currency'] ?? '')); if ($order_currency !== $payment_currency) { wp_die(esc_html__('Order currency mismatch.', 'alatpay')); } $amount_paid = function_exists('alatpay_get_payload_amount') ? alatpay_get_payload_amount($requery_data) : floatval($requery_data['Amount'] ?? $requery_data['amount'] ?? 0); $order_total = floatval($order->get_total()); if ($amount_paid + 0.00001 < $order_total) { $order->update_status('on-hold', __('ALATPay payment amount is lower than order total. Manual review required.', 'alatpay')); $order->add_order_note( sprintf( __('ALATPay amount mismatch. Paid: %1$s %2$s, Order total: %3$s %4$s.', 'alatpay'), number_format($amount_paid, 2, '.', ''), $payment_currency, number_format($order_total, 2, '.', ''), $order_currency ) ); $order->save(); wp_safe_redirect(esc_url($order->get_checkout_order_received_url())); exit; } // Update order status only after successful server-side verification. $order->update_status($status); if (function_exists('alatpay_add_success_note_if_needed')) { if (alatpay_add_success_note_if_needed($order)) { $needs_save = true; } } if ($needs_save) { if (function_exists('alatpay_mark_transaction_processed')) { alatpay_mark_transaction_processed($order, $transaction_id); } $order->update_meta_data('_alatpay_last_verified_transaction_id', $transaction_id); $order->save(); } $transaction_message = $transaction_id ? sprintf('Transaction ID %s', $transaction_id) : 'Transaction ID not provided'; alatpay_transaction_log( 'info', sprintf( 'Order %d updated to %s via checkout callback. %s.', $order_id, $status, $transaction_message ), array( 'order_id' => $order_id, 'status' => $status, 'transaction_id' => $transaction_id, ) ); // Redirect to thank you page wp_safe_redirect(esc_url($order->get_checkout_order_received_url())); exit; } function alatpay_record_popup_closed() { $order_id = isset($_POST['order_id']) ? absint(wp_unslash($_POST['order_id'])) : 0; $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; if (! $order_id || ! $nonce) { wp_send_json_error( array( 'message' => esc_html__('Missing required parameters.', 'alatpay'), ), 400 ); } if (! wp_verify_nonce($nonce, 'alatpay_popup_closed_' . $order_id)) { wp_send_json_error( array( 'message' => esc_html__('Invalid request.', 'alatpay'), ), 403 ); } $order = wc_get_order($order_id); if (! $order) { wp_send_json_error( array( 'message' => esc_html__('Invalid order.', 'alatpay'), ), 404 ); } alatpay_log('info', 'Customer closed the payment popup before completing payment for order ' . $order_id); $note = sprintf( /* translators: %s: order number. */ esc_html__('ALATPay: Customer closed the payment popup before completing payment for order %s.', 'alatpay'), $order->get_order_number() ); $order->add_order_note($note); wp_send_json_success(); } function alatpay_record_failed_payment() { $order_id = isset($_POST['order_id']) ? absint(wp_unslash($_POST['order_id'])) : 0; $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; if (! $order_id || ! $nonce) { wp_send_json_error( array( 'message' => esc_html__('Missing required parameters.', 'alatpay'), ), 400 ); } if (! wp_verify_nonce($nonce, 'alatpay_payment_failed_' . $order_id)) { wp_send_json_error( array( 'message' => esc_html__('Invalid request.', 'alatpay'), ), 403 ); } $order = wc_get_order($order_id); if (! $order) { wp_send_json_error( array( 'message' => esc_html__('Invalid order.', 'alatpay'), ), 404 ); } alatpay_log('warning', 'ALATPay payment failed for order ' . $order_id); alatpay_transaction_log( 'warning', sprintf('Payment failed for order %d via checkout popup.', $order_id), array( 'order_id' => $order_id, ) ); $needs_save = false; if (function_exists('alatpay_add_failure_note_if_needed')) { if (alatpay_add_failure_note_if_needed($order)) { $needs_save = true; } } else { $order->add_order_note(__('Payment failed via ALATPay (Failed).', 'alatpay')); } if ($needs_save) { $order->save(); } wp_send_json_success(); } function alatpay_record_payment_initiated() { $order_id = isset($_POST['order_id']) ? absint(wp_unslash($_POST['order_id'])) : 0; $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; if (! $order_id || ! $nonce) { wp_send_json_error( array( 'message' => esc_html__('Missing required parameters.', 'alatpay'), ), 400 ); } if (! wp_verify_nonce($nonce, 'alatpay_payment_initiated_' . $order_id)) { wp_send_json_error( array( 'message' => esc_html__('Invalid request.', 'alatpay'), ), 403 ); } $order = wc_get_order($order_id); if (! $order) { wp_send_json_error( array( 'message' => esc_html__('Invalid order.', 'alatpay'), ), 404 ); } $needs_save = false; if (function_exists('alatpay_add_initiated_note_if_needed')) { if (alatpay_add_initiated_note_if_needed($order)) { $needs_save = true; } } else { $order->add_order_note(__('Customer clicked "Pay with ALATPay". Awaiting payment confirmation.', 'alatpay')); } if ($needs_save) { $order->save(); } alatpay_transaction_log( 'info', sprintf('Customer initiated ALATPay payment for order %d.', $order_id), array( 'order_id' => $order_id, ) ); wp_send_json_success(); }