Docker Build (Gitea) / Build and Push Docker Image (push) Successful in 13s
之前刻意保留的三处,在没有订单数据的前提下一起改掉: - 表前缀 bep_ → vupay_(6 张表 + gormigrate 的 vupay_migration) - api_type 取值 "epusdt"/"epusdt_order" → "vupay"/"vupay_order" - handler 包 app/handler/epusdt → app/handler/vupay、类型 Epusdt → Vupay、EpusdtSign → VupaySign 顺带清理: - 删 docs/faq/vupay.md(整篇是「本项目和 EPusdt 的关系」的上游叙事) - README 去掉「缘起于/原项目已转手」说明与上游致谢条目,联系邮箱不再指向 vupay.online - 修 app/task/testenv_test.go 的 model.Init 调用(上一版加 MySQL 支持时漏了第三个参数,go build 不编译测试所以没暴露) 验证:go build ./... / go vet ./... / go test ./app/... 全通过;全仓 grep -i epusdt 归零
249 lines
6.5 KiB
Go
249 lines
6.5 KiB
Go
package notify
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"strings"
|
||
"time"
|
||
|
||
"git.viaeon.com/admin/vupay/app"
|
||
"git.viaeon.com/admin/vupay/app/log"
|
||
"git.viaeon.com/admin/vupay/app/model"
|
||
"git.viaeon.com/admin/vupay/app/notifier"
|
||
"git.viaeon.com/admin/vupay/app/utils"
|
||
"github.com/spf13/cast"
|
||
|
||
"github.com/v03413/go-cache"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
type EpNotify struct {
|
||
TradeId string `json:"trade_id"` // 本地订单号
|
||
OrderId string `json:"order_id"` // 客户交易id
|
||
Amount float64 `json:"amount"` // 订单金额 CNY
|
||
ActualAmount string `json:"actual_amount"` // USDT 交易数额
|
||
Token string `json:"token"` // 收款钱包地址
|
||
BlockTransactionId string `json:"block_transaction_id"` // 区块id
|
||
Signature string `json:"signature"` // 签名
|
||
Status int `json:"status"` // 1:等待支付,2:支付成功,3:订单超时
|
||
}
|
||
|
||
func Handle(order model.Order) error {
|
||
if order.Status != model.OrderStatusSuccess {
|
||
|
||
return errors.New("订单未支付 无法回调")
|
||
}
|
||
|
||
var ctx, cancel = context.WithTimeout(context.Background(), time.Second*10)
|
||
defer cancel()
|
||
|
||
var err error
|
||
if order.ApiType == model.OrderApiTypeEpay {
|
||
err = epay(ctx, order)
|
||
} else {
|
||
err = vupay(ctx, order)
|
||
}
|
||
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
log.Info("订单回调成功:", order.TradeId)
|
||
|
||
return nil
|
||
}
|
||
|
||
func epay(ctx context.Context, order model.Order) error {
|
||
var client = http.Client{Timeout: time.Second * 5}
|
||
var notifyUrl = fmt.Sprintf("%s?%s", order.NotifyUrl, order.BuildNotifyParams())
|
||
|
||
postReq, err2 := http.NewRequestWithContext(ctx, "GET", notifyUrl, nil)
|
||
if err2 != nil {
|
||
return err2
|
||
}
|
||
|
||
postReq.Header.Set("Powered-By", "https://git.viaeon.com/admin/vupay")
|
||
resp, err := client.Do(postReq)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
defer resp.Body.Close()
|
||
if resp.StatusCode != 200 {
|
||
markNotifyFail(order, fmt.Sprintf("resp.StatusCode != 200"))
|
||
|
||
return fmt.Errorf("商户系统返回状态码错误:%d(必须是200)", resp.StatusCode)
|
||
}
|
||
|
||
all, err := io.ReadAll(resp.Body)
|
||
if err != nil {
|
||
markNotifyFail(order, fmt.Sprintf("io.ReadAll(resp.Body) Error: %v", err))
|
||
|
||
return err
|
||
}
|
||
|
||
var bodyStr = strings.ToLower(strings.TrimSpace(string(all)))
|
||
|
||
// 判断是否包含 success 或 ok
|
||
if !strings.Contains(bodyStr, "success") && !strings.Contains(bodyStr, "ok") {
|
||
markNotifyFail(order, "商户系统必须响应 success 或 ok 才会认定回调成功")
|
||
|
||
return fmt.Errorf("商户系统必须响应 success 或 ok 才会认定回调成功,实际响应:%s", string(all))
|
||
}
|
||
|
||
if err = order.SetNotifyState(model.OrderNotifyStateSucc); err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
func vupay(ctx context.Context, order model.Order) error {
|
||
var data = make(map[string]interface{})
|
||
var body = EpNotify{
|
||
TradeId: order.TradeId,
|
||
OrderId: order.OrderId,
|
||
Amount: cast.ToFloat64(order.Money),
|
||
ActualAmount: order.Amount,
|
||
Token: order.Address,
|
||
BlockTransactionId: order.RefHash,
|
||
Status: order.Status,
|
||
}
|
||
var jsonBody, err = json.Marshal(body)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
if err = json.Unmarshal(jsonBody, &data); err != nil {
|
||
return err
|
||
}
|
||
|
||
// 签名
|
||
body.Signature = utils.VupaySign(data, model.AuthToken())
|
||
|
||
// 再次序列化
|
||
jsonBody, err = json.Marshal(body)
|
||
var client = http.Client{Timeout: time.Second * 5}
|
||
var postReq, err2 = http.NewRequestWithContext(ctx, "POST", order.NotifyUrl, strings.NewReader(string(jsonBody)))
|
||
if err2 != nil {
|
||
markNotifyFail(order, err2.Error())
|
||
|
||
return err2
|
||
}
|
||
|
||
postReq.Header.Set("Content-Type", "application/json")
|
||
postReq.Header.Set("Powered-By", "https://git.viaeon.com/admin/vupay")
|
||
postReq.Header.Set("User-Agent", "VUPAY/"+app.Version)
|
||
resp, err := client.Do(postReq)
|
||
if err != nil {
|
||
markNotifyFail(order, err.Error())
|
||
|
||
return err
|
||
}
|
||
|
||
defer resp.Body.Close()
|
||
if resp.StatusCode != 200 {
|
||
markNotifyFail(order, fmt.Sprintf("商户系统返回状态码错误:%d(必须是200)", resp.StatusCode))
|
||
|
||
return fmt.Errorf("商户系统返回状态码错误:%d(必须是200)", resp.StatusCode)
|
||
}
|
||
|
||
if err = order.SetNotifyState(model.OrderNotifyStateSucc); err != nil {
|
||
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
func Vupay(o model.Order) {
|
||
if o.ApiType != model.OrderApiTypeVupay && o.ApiType != model.OrderApiTypeVupayOrder {
|
||
|
||
return
|
||
}
|
||
|
||
var authToken = model.AuthToken()
|
||
var client = &http.Client{Timeout: time.Second * 5}
|
||
go func() {
|
||
if err := deliverVupayStatusUpdate(model.Db, client, authToken, o); err != nil {
|
||
log.Warn("notify VUPAY Error:", err.Error())
|
||
}
|
||
}()
|
||
}
|
||
|
||
func deliverVupayStatusUpdate(db *gorm.DB, client *http.Client, authToken string, o model.Order) error {
|
||
if client == nil {
|
||
client = &http.Client{Timeout: time.Second * 5}
|
||
}
|
||
|
||
var current model.Order
|
||
tx := db.Where("trade_id = ? and status = ?", o.TradeId, o.Status).Limit(1).Find(¤t)
|
||
if tx.Error != nil {
|
||
return tx.Error
|
||
}
|
||
if tx.RowsAffected == 0 {
|
||
return nil
|
||
}
|
||
|
||
var key = fmt.Sprintf("vupay_notify_%d_%s", current.Status, current.TradeId)
|
||
if _, ok := cache.Get(key); ok {
|
||
return nil
|
||
}
|
||
|
||
cache.Set(key, true, time.Minute)
|
||
|
||
var data = make(map[string]interface{})
|
||
var body = EpNotify{
|
||
TradeId: current.TradeId,
|
||
OrderId: current.OrderId,
|
||
Amount: cast.ToFloat64(current.Money),
|
||
ActualAmount: current.Amount,
|
||
Token: current.Address,
|
||
BlockTransactionId: current.RefHash,
|
||
Status: current.Status,
|
||
}
|
||
jsonBody, err := json.Marshal(body)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
if err = json.Unmarshal(jsonBody, &data); err != nil {
|
||
return err
|
||
}
|
||
|
||
body.Signature = utils.VupaySign(data, authToken)
|
||
|
||
jsonBody, _ = json.Marshal(body)
|
||
req, err := http.NewRequest("POST", current.NotifyUrl, strings.NewReader(string(jsonBody)))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
req.Header.Set("Content-Type", "application/json")
|
||
req.Header.Set("Powered-By", "https://git.viaeon.com/admin/vupay")
|
||
resp, err := client.Do(req)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer resp.Body.Close()
|
||
|
||
if resp.StatusCode != 200 {
|
||
return fmt.Errorf("resp.StatusCode != 200")
|
||
}
|
||
|
||
all, _ := io.ReadAll(resp.Body)
|
||
log.Info(fmt.Sprintf("订单回调成功[%d]:%s %s", current.Status, current.TradeId, string(all)))
|
||
|
||
return nil
|
||
}
|
||
|
||
func markNotifyFail(o model.Order, reason string) {
|
||
log.Warn(fmt.Sprintf("订单回调失败(%v):%s %v", o.TradeId, reason, o.SetNotifyState(model.OrderNotifyStateFail)))
|
||
|
||
notifier.NotifyFail(o, reason)
|
||
}
|