321 lines
12 KiB
PHP
321 lines
12 KiB
PHP
<?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
|
||
}
|
||
}
|
||
|