203 lines
4.5 KiB
Markdown
203 lines
4.5 KiB
Markdown
# M2Pool ETH Payment Gateway for WordPress
|
||
|
||
WordPress 支付网关插件,支持以太坊 (ETH) 交易的支付、监听和返回支付结果。
|
||
|
||
## 功能特性
|
||
|
||
- ✅ 集成 WooCommerce 支付网关
|
||
- ✅ 支持 ETH 和 USDT 支付
|
||
- ✅ 自动监听支付状态
|
||
- ✅ 支持 Webhook 回调
|
||
- ✅ 支付状态实时更新
|
||
- ✅ 完整的订单管理
|
||
|
||
## 安装要求
|
||
|
||
- WordPress 5.0+
|
||
- WooCommerce 3.0+
|
||
- PHP 7.4+
|
||
|
||
## 安装步骤
|
||
|
||
1. 将插件文件夹上传到 `/wp-content/plugins/` 目录
|
||
2. 在 WordPress 后台激活插件
|
||
3. 进入 `设置 > M2Pool ETH 支付` 配置 API 地址和密钥
|
||
4. 在 `WooCommerce > 设置 > 支付` 中启用并配置支付网关
|
||
|
||
## 配置说明
|
||
|
||
### 基本设置
|
||
|
||
1. **API 地址**: 支付系统的 API 地址(例如: `http://localhost:8080`)
|
||
2. **API 密钥**: 用于签名验证的密钥(与后端系统的 `msgKey` 对应)
|
||
3. **接收地址**: 用于接收支付的以太坊地址
|
||
4. **监听间隔**: 检查支付状态的间隔时间(秒)
|
||
|
||
### Webhook 配置
|
||
|
||
Webhook URL: `https://your-site.com/wp-json/m2pool-eth/v1/webhook`
|
||
|
||
将此 URL 配置到您的支付系统中,以便接收支付状态更新。
|
||
|
||
## API 接口说明
|
||
|
||
插件需要与后端支付系统通信。如果后端系统只支持 RabbitMQ,您需要创建一个中间 API 服务。
|
||
|
||
### 需要的 API 接口
|
||
|
||
#### 1. 创建支付请求
|
||
|
||
```
|
||
POST /api/payment/create
|
||
```
|
||
|
||
请求体:
|
||
```json
|
||
{
|
||
"queue_id": "wp_123_1234567890",
|
||
"chain": "ETH",
|
||
"symbol": "ETH",
|
||
"from_address": "0x...",
|
||
"to_address": "0x...",
|
||
"amount": 0.1,
|
||
"fee": 0,
|
||
"timestamp": 1234567890,
|
||
"sign": "signature_hash"
|
||
}
|
||
```
|
||
|
||
响应:
|
||
```json
|
||
{
|
||
"success": true,
|
||
"queue_id": "wp_123_1234567890"
|
||
}
|
||
```
|
||
|
||
#### 2. 查询支付状态
|
||
|
||
```
|
||
GET /api/payment/status/{queue_id}
|
||
```
|
||
|
||
响应:
|
||
```json
|
||
{
|
||
"queue_id": "wp_123_1234567890",
|
||
"status": 1,
|
||
"tx_hash": "0x...",
|
||
"block_height": 12345,
|
||
"amount": 0.1
|
||
}
|
||
```
|
||
|
||
状态码说明:
|
||
- `0`: 待支付
|
||
- `1`: 支付成功
|
||
- `2`: 待确认
|
||
- `3`: 支付失败
|
||
|
||
## 中间 API 服务
|
||
|
||
如果您的后端系统只支持 RabbitMQ,可以创建一个简单的 HTTP API 服务来桥接 WordPress 和 RabbitMQ。
|
||
|
||
示例代码(Go):
|
||
|
||
```go
|
||
package main
|
||
|
||
import (
|
||
"encoding/json"
|
||
"log"
|
||
"net/http"
|
||
"github.com/streadway/amqp"
|
||
)
|
||
|
||
func createPaymentHandler(w http.ResponseWriter, r *http.Request) {
|
||
var req PaymentRequest
|
||
json.NewDecoder(r.Body).Decode(&req)
|
||
|
||
// 发送到 RabbitMQ
|
||
conn, _ := amqp.Dial("amqp://guest:guest@localhost:5672/")
|
||
ch, _ := conn.Channel()
|
||
|
||
body, _ := json.Marshal(req)
|
||
ch.Publish("pay.exchange", "pay.auto.routing.key", false, false, amqp.Publishing{
|
||
ContentType: "application/json",
|
||
Body: body,
|
||
})
|
||
|
||
w.Header().Set("Content-Type", "application/json")
|
||
json.NewEncoder(w).Encode(map[string]bool{"success": true})
|
||
}
|
||
|
||
func main() {
|
||
http.HandleFunc("/api/payment/create", createPaymentHandler)
|
||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||
}
|
||
```
|
||
|
||
## 数据库结构
|
||
|
||
插件会自动创建以下数据库表:
|
||
|
||
- `wp_m2pool_eth_payments`: 存储支付记录
|
||
|
||
表结构:
|
||
- `id`: 主键
|
||
- `order_id`: WooCommerce 订单 ID
|
||
- `queue_id`: 支付队列 ID
|
||
- `from_address`: 发送地址
|
||
- `to_address`: 接收地址
|
||
- `amount`: 支付金额
|
||
- `fee`: 手续费
|
||
- `chain`: 链名称
|
||
- `symbol`: 代币符号
|
||
- `tx_hash`: 交易哈希
|
||
- `block_height`: 区块高度
|
||
- `status`: 支付状态
|
||
- `created_at`: 创建时间
|
||
- `updated_at`: 更新时间
|
||
|
||
## 使用流程
|
||
|
||
1. 客户选择 ETH 支付方式
|
||
2. 系统生成支付地址和金额
|
||
3. 客户向指定地址支付
|
||
4. 系统自动监听支付状态
|
||
5. 支付确认后自动更新订单状态
|
||
|
||
## 开发说明
|
||
|
||
### 文件结构
|
||
|
||
```
|
||
m2pool-eth-payment/
|
||
├── m2pool-eth-payment.php # 主插件文件
|
||
├── includes/
|
||
│ ├── class-m2pool-eth-gateway.php # 支付网关类
|
||
│ ├── class-m2pool-eth-api.php # API 客户端
|
||
│ └── class-m2pool-eth-listener.php # 支付监听器
|
||
├── templates/
|
||
│ ├── settings.php # 设置页面模板
|
||
│ └── payment-instructions.php # 支付说明模板
|
||
└── README.md
|
||
```
|
||
|
||
### 扩展开发
|
||
|
||
要添加新的功能,可以:
|
||
|
||
1. 扩展 `M2Pool_ETH_Gateway` 类添加新的支付方式
|
||
2. 扩展 `M2Pool_ETH_API` 类添加新的 API 接口
|
||
3. 修改 `M2Pool_ETH_Listener` 类自定义监听逻辑
|
||
|
||
## 许可证
|
||
|
||
MIT License
|
||
|
||
## 支持
|
||
|
||
如有问题,请提交 Issue 或联系开发团队。
|
||
|