integration wordpress
This commit is contained in:
160
integration/wordpress/includes/class-m2pool-eth-api.php
Normal file
160
integration/wordpress/includes/class-m2pool-eth-api.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
/**
|
||||
* M2Pool ETH API 客户端
|
||||
*/
|
||||
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class M2Pool_ETH_API {
|
||||
|
||||
/**
|
||||
* API 基础 URL
|
||||
*/
|
||||
private $api_url;
|
||||
|
||||
/**
|
||||
* API 密钥
|
||||
*/
|
||||
private $api_key;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->api_url = get_option('m2pool_eth_api_url', 'http://localhost:8080');
|
||||
$this->api_key = get_option('m2pool_eth_api_key', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成签名
|
||||
*/
|
||||
private function generate_sign($timestamp) {
|
||||
if (empty($this->api_key)) {
|
||||
return '';
|
||||
}
|
||||
return hash('sha256', dechex($timestamp) . $this->api_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送 HTTP 请求
|
||||
*/
|
||||
private function request($endpoint, $method = 'GET', $data = array()) {
|
||||
$url = rtrim($this->api_url, '/') . '/' . ltrim($endpoint, '/');
|
||||
|
||||
$args = array(
|
||||
'method' => $method,
|
||||
'timeout' => 30,
|
||||
'headers' => array(
|
||||
'Content-Type' => 'application/json',
|
||||
),
|
||||
);
|
||||
|
||||
if (!empty($data)) {
|
||||
$args['body'] = json_encode($data);
|
||||
}
|
||||
|
||||
$response = wp_remote_request($url, $args);
|
||||
|
||||
if (is_wp_error($response)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$body = wp_remote_retrieve_body($response);
|
||||
$code = wp_remote_retrieve_response_code($response);
|
||||
|
||||
if ($code >= 200 && $code < 300) {
|
||||
$data = json_decode($body, true);
|
||||
return $data;
|
||||
} else {
|
||||
return new WP_Error('api_error', sprintf(__('API 错误: %s', 'm2pool-eth-payment'), $body), array('status' => $code));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建支付请求
|
||||
*
|
||||
* 注意:这个方法假设有一个 HTTP API 接口。
|
||||
* 如果后端系统只支持 RabbitMQ,需要创建一个中间 API 服务。
|
||||
*/
|
||||
public function create_payment($queue_id, $from_address, $to_address, $amount, $chain = 'ETH', $symbol = 'ETH') {
|
||||
$timestamp = time();
|
||||
$sign = $this->generate_sign($timestamp);
|
||||
|
||||
$data = array(
|
||||
'queue_id' => $queue_id,
|
||||
'chain' => $chain,
|
||||
'symbol' => $symbol,
|
||||
'from_address' => $from_address,
|
||||
'to_address' => $to_address,
|
||||
'amount' => $amount,
|
||||
'fee' => 0,
|
||||
'timestamp' => $timestamp,
|
||||
'sign' => $sign,
|
||||
);
|
||||
|
||||
// 发送到支付 API
|
||||
// 注意:这里需要根据实际的后端 API 接口调整
|
||||
// 如果后端只支持 RabbitMQ,需要创建一个中间 API 服务来处理
|
||||
$result = $this->request('/api/payment/create', 'POST', $data);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询支付状态
|
||||
*/
|
||||
public function get_payment_status($queue_id) {
|
||||
$result = $this->request('/api/payment/status/' . $queue_id, 'GET');
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建充值监听请求
|
||||
*/
|
||||
public function create_topup($queue_id, $address, $chain = 'ETH', $symbol = 'ETH') {
|
||||
$timestamp = time();
|
||||
$sign = $this->generate_sign($timestamp);
|
||||
|
||||
$data = array(
|
||||
'queue_id' => $queue_id,
|
||||
'chain' => $chain,
|
||||
'symbol' => $symbol,
|
||||
'address' => $address,
|
||||
'timestamp' => $timestamp,
|
||||
'sign' => $sign,
|
||||
);
|
||||
|
||||
$result = $this->request('/api/topup/create', 'POST', $data);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询充值状态
|
||||
*/
|
||||
public function get_topup_status($queue_id) {
|
||||
$result = $this->request('/api/topup/status/' . $queue_id, 'GET');
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
320
integration/wordpress/includes/class-m2pool-eth-gateway.php
Normal file
320
integration/wordpress/includes/class-m2pool-eth-gateway.php
Normal file
@@ -0,0 +1,320 @@
|
||||
<?php
|
||||
/**
|
||||
* M2Pool ETH 支付网关类
|
||||
*/
|
||||
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class M2Pool_ETH_Gateway extends WC_Payment_Gateway {
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->id = 'm2pool_eth';
|
||||
$this->icon = apply_filters('woocommerce_m2pool_eth_icon', '');
|
||||
$this->has_fields = false;
|
||||
$this->method_title = __('M2Pool ETH 支付', 'm2pool-eth-payment');
|
||||
$this->method_description = __('接受以太坊 (ETH) 支付', 'm2pool-eth-payment');
|
||||
$this->supports = array(
|
||||
'products',
|
||||
);
|
||||
|
||||
// 加载设置
|
||||
$this->init_form_fields();
|
||||
$this->init_settings();
|
||||
|
||||
// 定义用户可见的设置
|
||||
$this->title = $this->get_option('title', __('ETH 支付', 'm2pool-eth-payment'));
|
||||
$this->description = $this->get_option('description', __('使用以太坊进行支付', 'm2pool-eth-payment'));
|
||||
$this->enabled = $this->get_option('enabled', 'no');
|
||||
|
||||
// 保存设置
|
||||
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
|
||||
|
||||
// 支付完成页面
|
||||
add_action('woocommerce_thankyou_' . $this->id, array($this, 'thankyou_page'));
|
||||
|
||||
// 订单详情页面
|
||||
add_action('woocommerce_order_details_after_order_table', array($this, 'order_details'), 10, 1);
|
||||
|
||||
// 确保在结账页面显示
|
||||
add_filter('woocommerce_available_payment_gateways', array($this, 'ensure_available'), 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保支付网关可用(强制显示)
|
||||
*/
|
||||
public function ensure_available($available_gateways) {
|
||||
// 如果已启用且配置正确,确保在列表中
|
||||
if ($this->enabled === 'yes') {
|
||||
$to_address = $this->get_option('to_address', get_option('m2pool_eth_to_address', ''));
|
||||
if (!empty($to_address) && $this->is_available()) {
|
||||
// 确保网关在可用列表中
|
||||
if (!isset($available_gateways[$this->id])) {
|
||||
$available_gateways[$this->id] = $this;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $available_gateways;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查支付网关是否可用
|
||||
*/
|
||||
public function is_available() {
|
||||
// 检查是否启用
|
||||
if ($this->enabled !== 'yes') {
|
||||
if (defined('WP_DEBUG') && WP_DEBUG) {
|
||||
error_log('M2Pool ETH Gateway: Not enabled');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否有接收地址
|
||||
$to_address = $this->get_option('to_address', get_option('m2pool_eth_to_address', ''));
|
||||
if (empty($to_address)) {
|
||||
if (defined('WP_DEBUG') && WP_DEBUG) {
|
||||
error_log('M2Pool ETH Gateway: No receive address configured');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查是否有订单(在结账页面)
|
||||
if (is_checkout() || is_checkout_pay_page()) {
|
||||
// 在结账页面,确保有订单
|
||||
$order_id = absint(get_query_var('order-pay'));
|
||||
if ($order_id > 0) {
|
||||
$order = wc_get_order($order_id);
|
||||
if (!$order || $order->get_total() <= 0) {
|
||||
if (defined('WP_DEBUG') && WP_DEBUG) {
|
||||
error_log('M2Pool ETH Gateway: Invalid order or zero amount');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 调用父类方法
|
||||
$parent_available = parent::is_available();
|
||||
|
||||
// 如果父类返回 false,记录原因(用于调试)
|
||||
if (!$parent_available && defined('WP_DEBUG') && WP_DEBUG) {
|
||||
$order = WC()->cart;
|
||||
$total = $order ? $order->get_total() : 0;
|
||||
error_log(sprintf(
|
||||
'M2Pool ETH Gateway: Parent is_available() returned false. Cart total: %s, Enabled: %s, Address: %s',
|
||||
$total,
|
||||
$this->enabled,
|
||||
!empty($to_address) ? 'set' : 'empty'
|
||||
));
|
||||
}
|
||||
|
||||
return $parent_available;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化表单字段
|
||||
*/
|
||||
public function init_form_fields() {
|
||||
$this->form_fields = array(
|
||||
'enabled' => array(
|
||||
'title' => __('启用/禁用', 'm2pool-eth-payment'),
|
||||
'type' => 'checkbox',
|
||||
'label' => __('启用 M2Pool ETH 支付', 'm2pool-eth-payment'),
|
||||
'default' => 'no',
|
||||
),
|
||||
'title' => array(
|
||||
'title' => __('标题', 'm2pool-eth-payment'),
|
||||
'type' => 'text',
|
||||
'description' => __('客户在结账时看到的支付方式标题', 'm2pool-eth-payment'),
|
||||
'default' => __('ETH 支付', 'm2pool-eth-payment'),
|
||||
'desc_tip' => true,
|
||||
),
|
||||
'description' => array(
|
||||
'title' => __('描述', 'm2pool-eth-payment'),
|
||||
'type' => 'textarea',
|
||||
'description' => __('客户在结账时看到的支付方式描述', 'm2pool-eth-payment'),
|
||||
'default' => __('使用以太坊 (ETH) 进行支付', 'm2pool-eth-payment'),
|
||||
'desc_tip' => true,
|
||||
),
|
||||
'from_address' => array(
|
||||
'title' => __('发送地址', 'm2pool-eth-payment'),
|
||||
'type' => 'text',
|
||||
'description' => __('用于接收支付的以太坊地址', 'm2pool-eth-payment'),
|
||||
'default' => get_option('m2pool_eth_from_address', ''),
|
||||
'desc_tip' => true,
|
||||
),
|
||||
'to_address' => array(
|
||||
'title' => __('接收地址', 'm2pool-eth-payment'),
|
||||
'type' => 'text',
|
||||
'description' => __('用于接收支付的以太坊地址', 'm2pool-eth-payment'),
|
||||
'default' => get_option('m2pool_eth_to_address', ''),
|
||||
'desc_tip' => true,
|
||||
),
|
||||
'chain' => array(
|
||||
'title' => __('链名称', 'm2pool-eth-payment'),
|
||||
'type' => 'select',
|
||||
'options' => array(
|
||||
'ETH' => 'Ethereum',
|
||||
),
|
||||
'default' => 'ETH',
|
||||
),
|
||||
'symbol' => array(
|
||||
'title' => __('代币符号', 'm2pool-eth-payment'),
|
||||
'type' => 'select',
|
||||
'options' => array(
|
||||
'ETH' => 'ETH',
|
||||
'USDT' => 'USDT',
|
||||
),
|
||||
'default' => 'ETH',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理支付
|
||||
*/
|
||||
public function process_payment($order_id) {
|
||||
$order = wc_get_order($order_id);
|
||||
|
||||
if (!$order) {
|
||||
wc_add_notice(__('订单不存在', 'm2pool-eth-payment'), 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取 API 客户端
|
||||
$api = new M2Pool_ETH_API();
|
||||
|
||||
// 准备支付请求
|
||||
$amount = floatval($order->get_total());
|
||||
$from_address = $this->get_option('from_address', get_option('m2pool_eth_from_address', ''));
|
||||
$to_address = $this->get_option('to_address', get_option('m2pool_eth_to_address', ''));
|
||||
$chain = $this->get_option('chain', 'ETH');
|
||||
$symbol = $this->get_option('symbol', 'ETH');
|
||||
|
||||
if (empty($to_address)) {
|
||||
wc_add_notice(__('支付网关配置错误:接收地址未设置', 'm2pool-eth-payment'), 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// 生成队列 ID
|
||||
$queue_id = 'wp_' . $order_id . '_' . time();
|
||||
|
||||
// 发送支付请求
|
||||
$result = $api->create_payment($queue_id, $from_address, $to_address, $amount, $chain, $symbol);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
wc_add_notice($result->get_error_message(), 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// 保存支付信息到数据库
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'm2pool_eth_payments';
|
||||
|
||||
$wpdb->insert(
|
||||
$table_name,
|
||||
array(
|
||||
'order_id' => $order_id,
|
||||
'queue_id' => $queue_id,
|
||||
'from_address' => $from_address,
|
||||
'to_address' => $to_address,
|
||||
'amount' => $amount,
|
||||
'fee' => isset($result['fee']) ? $result['fee'] : 0,
|
||||
'chain' => $chain,
|
||||
'symbol' => $symbol,
|
||||
'status' => 0, // 待支付
|
||||
),
|
||||
array('%d', '%s', '%s', '%s', '%f', '%f', '%s', '%s', '%d')
|
||||
);
|
||||
|
||||
// 更新订单元数据
|
||||
$order->update_meta_data('_m2pool_queue_id', $queue_id);
|
||||
$order->update_meta_data('_m2pool_payment_address', $to_address);
|
||||
$order->update_meta_data('_m2pool_payment_amount', $amount);
|
||||
$order->update_meta_data('_m2pool_payment_chain', $chain);
|
||||
$order->update_meta_data('_m2pool_payment_symbol', $symbol);
|
||||
$order->save();
|
||||
|
||||
// 将订单状态设置为待支付
|
||||
$order->update_status('pending', __('等待 ETH 支付', 'm2pool-eth-payment'));
|
||||
|
||||
// 启动支付监听
|
||||
$listener = new M2Pool_ETH_Listener();
|
||||
$listener->start_listening($order_id);
|
||||
|
||||
// 返回成功
|
||||
return array(
|
||||
'result' => 'success',
|
||||
'redirect' => $this->get_return_url($order),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 感谢页面
|
||||
*/
|
||||
public function thankyou_page($order_id) {
|
||||
$order = wc_get_order($order_id);
|
||||
|
||||
if (!$order || $order->get_payment_method() !== $this->id) {
|
||||
return;
|
||||
}
|
||||
|
||||
$payment_address = $order->get_meta('_m2pool_payment_address');
|
||||
$payment_amount = $order->get_meta('_m2pool_payment_amount');
|
||||
$payment_symbol = $order->get_meta('_m2pool_payment_symbol');
|
||||
$queue_id = $order->get_meta('_m2pool_queue_id');
|
||||
|
||||
if (!$payment_address || !$payment_amount) {
|
||||
return;
|
||||
}
|
||||
|
||||
include M2POOL_ETH_PLUGIN_DIR . 'templates/payment-instructions.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情页面
|
||||
*/
|
||||
public function order_details($order) {
|
||||
if ($order->get_payment_method() !== $this->id) {
|
||||
return;
|
||||
}
|
||||
|
||||
$payment_address = $order->get_meta('_m2pool_payment_address');
|
||||
$payment_amount = $order->get_meta('_m2pool_payment_amount');
|
||||
$payment_symbol = $order->get_meta('_m2pool_payment_symbol');
|
||||
$tx_hash = $order->get_meta('_m2pool_tx_hash');
|
||||
|
||||
if (!$payment_address || !$payment_amount) {
|
||||
return;
|
||||
}
|
||||
|
||||
?>
|
||||
<section class="woocommerce-order-details m2pool-eth-payment-details">
|
||||
<h2 class="woocommerce-order-details__title"><?php echo esc_html__('ETH 支付信息', 'm2pool-eth-payment'); ?></h2>
|
||||
<table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th><?php echo esc_html__('支付地址', 'm2pool-eth-payment'); ?>:</th>
|
||||
<td><code><?php echo esc_html($payment_address); ?></code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><?php echo esc_html__('支付金额', 'm2pool-eth-payment'); ?>:</th>
|
||||
<td><?php echo esc_html($payment_amount); ?> <?php echo esc_html($payment_symbol); ?></td>
|
||||
</tr>
|
||||
<?php if ($tx_hash): ?>
|
||||
<tr>
|
||||
<th><?php echo esc_html__('交易哈希', 'm2pool-eth-payment'); ?>:</th>
|
||||
<td><a href="https://etherscan.io/tx/<?php echo esc_attr($tx_hash); ?>" target="_blank"><?php echo esc_html($tx_hash); ?></a></td>
|
||||
</tr>
|
||||
<?php endif; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
255
integration/wordpress/includes/class-m2pool-eth-listener.php
Normal file
255
integration/wordpress/includes/class-m2pool-eth-listener.php
Normal file
@@ -0,0 +1,255 @@
|
||||
<?php
|
||||
/**
|
||||
* M2Pool ETH 支付监听器
|
||||
*/
|
||||
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class M2Pool_ETH_Listener {
|
||||
|
||||
/**
|
||||
* API 客户端
|
||||
*/
|
||||
private $api;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->api = new M2Pool_ETH_API();
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始监听订单支付
|
||||
*/
|
||||
public function start_listening($order_id) {
|
||||
// 创建定时任务检查支付状态
|
||||
if (!wp_next_scheduled('m2pool_eth_check_payments')) {
|
||||
$interval = get_option('m2pool_eth_listen_interval', 30);
|
||||
wp_schedule_event(time(), 'm2pool_eth_interval', 'm2pool_eth_check_payments');
|
||||
}
|
||||
|
||||
// 添加自定义间隔
|
||||
add_filter('cron_schedules', array($this, 'add_cron_interval'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加自定义 Cron 间隔
|
||||
*/
|
||||
public function add_cron_interval($schedules) {
|
||||
$interval = get_option('m2pool_eth_listen_interval', 30);
|
||||
|
||||
$schedules['m2pool_eth_interval'] = array(
|
||||
'interval' => $interval,
|
||||
'display' => sprintf(__('每 %d 秒', 'm2pool-eth-payment'), $interval),
|
||||
);
|
||||
|
||||
return $schedules;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查支付状态
|
||||
*/
|
||||
public function check_payment_status($order_id) {
|
||||
$order = wc_get_order($order_id);
|
||||
|
||||
if (!$order) {
|
||||
return array(
|
||||
'success' => false,
|
||||
'message' => __('订单不存在', 'm2pool-eth-payment'),
|
||||
);
|
||||
}
|
||||
|
||||
$queue_id = $order->get_meta('_m2pool_queue_id');
|
||||
|
||||
if (empty($queue_id)) {
|
||||
return array(
|
||||
'success' => false,
|
||||
'message' => __('未找到支付队列 ID', 'm2pool-eth-payment'),
|
||||
);
|
||||
}
|
||||
|
||||
// 从数据库获取支付记录
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'm2pool_eth_payments';
|
||||
$payment = $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT * FROM $table_name WHERE order_id = %d AND queue_id = %s",
|
||||
$order_id,
|
||||
$queue_id
|
||||
));
|
||||
|
||||
if (!$payment) {
|
||||
return array(
|
||||
'success' => false,
|
||||
'message' => __('未找到支付记录', 'm2pool-eth-payment'),
|
||||
);
|
||||
}
|
||||
|
||||
// 如果已经成功,直接返回
|
||||
if ($payment->status == 1) {
|
||||
return array(
|
||||
'success' => true,
|
||||
'status' => 'completed',
|
||||
'message' => __('支付已完成', 'm2pool-eth-payment'),
|
||||
);
|
||||
}
|
||||
|
||||
// 查询 API 获取最新状态
|
||||
$result = $this->api->get_payment_status($queue_id);
|
||||
|
||||
if (is_wp_error($result)) {
|
||||
return array(
|
||||
'success' => false,
|
||||
'message' => $result->get_error_message(),
|
||||
);
|
||||
}
|
||||
|
||||
// 更新支付状态
|
||||
$this->update_payment_status($order_id, $result);
|
||||
|
||||
return array(
|
||||
'success' => true,
|
||||
'status' => $this->get_status_text($result['status']),
|
||||
'data' => $result,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 Webhook
|
||||
*/
|
||||
public function process_webhook($data) {
|
||||
if (!isset($data['queue_id'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$queue_id = $data['queue_id'];
|
||||
|
||||
// 从数据库查找订单
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'm2pool_eth_payments';
|
||||
$payment = $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT * FROM $table_name WHERE queue_id = %s",
|
||||
$queue_id
|
||||
));
|
||||
|
||||
if (!$payment) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 更新支付状态
|
||||
$this->update_payment_status($payment->order_id, $data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新支付状态
|
||||
*/
|
||||
private function update_payment_status($order_id, $data) {
|
||||
$order = wc_get_order($order_id);
|
||||
|
||||
if (!$order) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'm2pool_eth_payments';
|
||||
|
||||
$status = isset($data['status']) ? intval($data['status']) : 0;
|
||||
$tx_hash = isset($data['tx_hash']) ? $data['tx_hash'] : null;
|
||||
$block_height = isset($data['block_height']) ? $data['block_height'] : null;
|
||||
$amount = isset($data['amount']) ? floatval($data['amount']) : null;
|
||||
|
||||
// 更新数据库
|
||||
$update_data = array(
|
||||
'status' => $status,
|
||||
);
|
||||
|
||||
if ($tx_hash) {
|
||||
$update_data['tx_hash'] = $tx_hash;
|
||||
}
|
||||
|
||||
if ($block_height) {
|
||||
$update_data['block_height'] = $block_height;
|
||||
}
|
||||
|
||||
if ($amount !== null) {
|
||||
$update_data['amount'] = $amount;
|
||||
}
|
||||
|
||||
$wpdb->update(
|
||||
$table_name,
|
||||
$update_data,
|
||||
array('order_id' => $order_id),
|
||||
array('%d', '%s', '%d', '%f'),
|
||||
array('%d')
|
||||
);
|
||||
|
||||
// 更新订单状态
|
||||
if ($status == 1) {
|
||||
// 支付成功
|
||||
$order->update_meta_data('_m2pool_tx_hash', $tx_hash);
|
||||
$order->payment_complete();
|
||||
$order->add_order_note(__('ETH 支付成功', 'm2pool-eth-payment'));
|
||||
} elseif ($status == 2) {
|
||||
// 待确认
|
||||
$order->update_status('on-hold', __('ETH 支付待确认', 'm2pool-eth-payment'));
|
||||
if ($tx_hash) {
|
||||
$order->update_meta_data('_m2pool_tx_hash', $tx_hash);
|
||||
$order->add_order_note(sprintf(__('ETH 支付待确认,交易哈希: %s', 'm2pool-eth-payment'), $tx_hash));
|
||||
}
|
||||
} elseif ($status == 0 || $status == 3) {
|
||||
// 支付失败
|
||||
$order->update_status('failed', __('ETH 支付失败', 'm2pool-eth-payment'));
|
||||
$order->add_order_note(__('ETH 支付失败', 'm2pool-eth-payment'));
|
||||
}
|
||||
|
||||
$order->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取状态文本
|
||||
*/
|
||||
private function get_status_text($status) {
|
||||
$statuses = array(
|
||||
0 => 'pending',
|
||||
1 => 'completed',
|
||||
2 => 'processing',
|
||||
3 => 'failed',
|
||||
);
|
||||
|
||||
return isset($statuses[$status]) ? $statuses[$status] : 'unknown';
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量检查所有待支付订单
|
||||
*/
|
||||
public function check_all_pending_payments() {
|
||||
global $wpdb;
|
||||
$table_name = $wpdb->prefix . 'm2pool_eth_payments';
|
||||
|
||||
// 获取所有待支付和待确认的订单
|
||||
$payments = $wpdb->get_results(
|
||||
"SELECT * FROM $table_name WHERE status IN (0, 2) ORDER BY created_at ASC LIMIT 50"
|
||||
);
|
||||
|
||||
foreach ($payments as $payment) {
|
||||
$this->check_payment_status($payment->order_id);
|
||||
|
||||
// 避免请求过快
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 注册定时任务处理函数
|
||||
add_action('m2pool_eth_check_payments', 'm2pool_eth_check_all_payments');
|
||||
|
||||
// 定时任务处理函数
|
||||
function m2pool_eth_check_all_payments() {
|
||||
$listener = new M2Pool_ETH_Listener();
|
||||
$listener->check_all_pending_payments();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user