5 分钟快速接入加密货币支付!
优点: 自动确认,无需手动操作 时间: 15 分钟 难度: ⭐⭐
优点: 无需注册,立即可用 时间: 5 分钟 难度: ⭐
- 访问 https://commerce.coinbase.com
- 使用 Coinbase 账号登录 (没有则注册)
- 进入 Settings → API Keys
- 创建新的 API Key
- 保存 API Key 和 Webhook Secret
# 复制配置文件
cp .env.crypto.example .env.crypto
# 编辑配置
vim .env.crypto填入:
COINBASE_COMMERCE_API_KEY=your_api_key_here
COINBASE_WEBHOOK_SECRET=your_webhook_secret_here在 Coinbase Commerce 后台:
- 进入 Settings → Webhook subscriptions
- 添加 Webhook URL:
https://your-domain.com/api/payment/crypto/webhook/coinbase - 选择事件:
charge:confirmed,charge:failed - 保存
# 启动服务
go run main.go
# 测试支付
./test_crypto_payment.sh✅ 完成! 用户现在可以使用加密货币支付了。
准备以下钱包地址:
- BTC 地址 (如
bc1q...) - ETH 地址 (如
0x...) - USDT 地址 (ERC20, 如
0x...) - USDC 地址 (ERC20, 如
0x...)
💡 提示: 可以使用同一个 ETH 地址接收 ETH, USDT, USDC。
编辑 saas/crypto_payment_service.go:
walletAddresses: map[string]string{
"BTC": "bc1q...", // 你的 BTC 地址
"ETH": "0x...", // 你的 ETH 地址
"USDT": "0x...", // 你的 USDT (ERC20) 地址
"USDC": "0x...", // 你的 USDC 地址
},或者使用环境变量:
export WALLET_ADDRESS_BTC="bc1q..."
export WALLET_ADDRESS_ETH="0x..."
export WALLET_ADDRESS_USDT="0x..."
export WALLET_ADDRESS_USDC="0x..."go run main.go# 创建支付
curl -X POST http://localhost:8080/api/payment/crypto/direct/create \
-H "Content-Type: application/json" \
-d '{
"plan": "professional",
"email": "test@example.com",
"crypto_currency": "USDT"
}'响应:
{
"payment_id": 1,
"crypto_currency": "USDT",
"crypto_amount": 199.0,
"payment_address": "0x...",
"amount_usd": 199.00,
"message": "请向指定地址转账,并保存交易哈希"
}用户转账后:
# 确认支付
curl -X POST http://localhost:8080/api/payment/crypto/1/confirm \
-H "Content-Type: application/json" \
-d '{
"transaction_hash": "0xabc..."
}'✅ 完成! 支付已确认,订阅已激活。
import (
"quantmesh/saas"
"quantmesh/web"
)
func main() {
// ... 其他初始化代码 ...
// 初始化加密货币支付服务
cryptoPaymentService := saas.NewCryptoPaymentService(
db,
os.Getenv("COINBASE_COMMERCE_API_KEY"),
)
// 初始化数据库
if err := cryptoPaymentService.InitDatabase(); err != nil {
log.Fatal(err)
}
// 设置到 web 服务
web.SetCryptoPaymentService(cryptoPaymentService)
// ... 启动服务器 ...
}// 创建支付
async function createCryptoPayment(plan, email, method = 'coinbase') {
const endpoint = method === 'coinbase'
? '/api/payment/crypto/coinbase/create'
: '/api/payment/crypto/direct/create';
const data = {
plan: plan,
email: email,
};
if (method === 'direct') {
data.crypto_currency = 'USDT'; // 或让用户选择
}
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify(data),
});
const result = await response.json();
if (method === 'coinbase') {
// 跳转到 Coinbase 支付页面
window.location.href = result.payment_url;
} else {
// 显示支付信息
showPaymentInfo(result);
}
}
// 显示支付信息
function showPaymentInfo(payment) {
alert(`
请向以下地址转账:
地址: ${payment.payment_address}
金额: ${payment.crypto_amount} ${payment.crypto_currency}
转账后请提交交易哈希
`);
}
// 查询支付状态
async function checkPaymentStatus(paymentId) {
const response = await fetch(`/api/payment/crypto/${paymentId}`, {
headers: {
'Authorization': `Bearer ${token}`,
},
});
const payment = await response.json();
return payment.status; // pending/completed/expired
}SELECT * FROM crypto_payments
ORDER BY created_at DESC
LIMIT 20;SELECT id, user_id, email, plan, crypto_amount, crypto_currency,
payment_address, created_at
FROM crypto_payments
WHERE status = 'pending'
AND payment_method = 'direct'
ORDER BY created_at DESC;UPDATE crypto_payments
SET status = 'completed',
transaction_hash = '0xabc...',
completed_at = NOW()
WHERE id = 123;SELECT
DATE(completed_at) as date,
COUNT(*) as count,
SUM(amount) as total_usd,
SUM(crypto_amount) as total_crypto,
crypto_currency
FROM crypto_payments
WHERE status = 'completed'
GROUP BY DATE(completed_at), crypto_currency
ORDER BY date DESC;向用户推荐 USDT 或 USDC:
- 价格稳定
- 金额精确
- 转账快速
- Coinbase: 1 小时 (自动)
- 直接支付: 24 小时
- 小额支付 (< $1000): 1 个区块确认即可
- 大额支付 (≥ $1000): 等待 6 个以上确认
// 定时清理过期支付
ticker := time.NewTicker(1 * time.Hour)
go func() {
for range ticker.C {
cryptoPaymentService.CleanupExpiredPayments()
}
}()支付完成后发送邮件:
func (s *CryptoPaymentService) completePayment(chargeID string) error {
// ... 更新数据库 ...
// 发送邮件
sendEmail(payment.Email, "支付成功", fmt.Sprintf(
"您的 %s 订阅已激活!",
payment.Plan,
))
return nil
}A: 不需要。Coinbase Commerce 和直接支付都不需要 KYC。
A:
- Coinbase Commerce: 1% 手续费
- 直接支付: 仅网络 Gas 费 (用户承担)
A: 全球通用,无地域限制。
A:
- 查询原支付记录
- 向原地址退款
- 更新数据库状态
A:
- Coinbase: 使用 Coinbase 实时汇率
- 直接支付: 使用 CoinGecko API
- 📧 Email: tech@quantmesh.io
- 💬 Discord: https://discord.gg/quantmesh
- 📚 完整文档:
docs/CRYPTO_PAYMENT_GUIDE.md
恭喜! 🎉 你已成功接入加密货币支付系统!