Files
m2pool_payment/integration/wordpress/includes/class-m2pool-eth-api.php
2025-11-18 17:26:07 +08:00

161 lines
4.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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;
}
}