This commit is contained in:
2024-10-15 17:19:23 +08:00
parent 8f903b457f
commit c76451788c
42 changed files with 1600 additions and 237 deletions

View File

@@ -11,6 +11,7 @@ type Config struct {
CacheRedis cache.CacheConf // 缓存配置,使用 go-zero 自带的缓存配置结构体
Alipay AlipayConfig
TopUp TopUpConfig
UserRpc zrpc.RpcClientConf
}
type AlipayConfig struct {
AppID string
@@ -20,4 +21,7 @@ type AlipayConfig struct {
}
type TopUpConfig struct {
MaxTopUpAmount int64
Subject string
NotifyURL string
ReturnURL string
}

View File

@@ -37,12 +37,12 @@ func (l *AliTopUpLogic) AliTopUp(in *sentinel.AliTopUpRequest) (*sentinel.AliTop
// 构造支付请求
var p = alipay.TradePagePay{
Trade: alipay.Trade{
Subject: "天远数据API使用额度",
Subject: l.svcCtx.Config.TopUp.Subject,
OutTradeNo: outTradeNo,
TotalAmount: totalAmount,
ProductCode: "FAST_INSTANT_TRADE_PAY",
NotifyURL: "https://console.tianyuanapi.com/api/console/", // 异步回调通知地址
ReturnURL: "https://console.tianyuanapi.com/charge/recharge", // 支付成功后的跳转地址
NotifyURL: l.svcCtx.Config.TopUp.NotifyURL, // 异步回调通知地址
ReturnURL: l.svcCtx.Config.TopUp.ReturnURL, // 支付成功后的跳转地址
},
}
_, inserErr := l.svcCtx.PayOrderModel.Insert(l.ctx, &model.PayOrder{

View File

@@ -7,6 +7,7 @@ import (
"math"
"net/url"
"strconv"
"tianyuan-api/apps/user/user"
"tianyuan-api/apps/sentinel/internal/svc"
"tianyuan-api/apps/sentinel/sentinel"
@@ -63,7 +64,20 @@ func (l *AliTopUpNotifyLogic) AliTopUpNotify(in *sentinel.AliTopUpNotifyRequest)
logx.Infof("回调金额:%v订单金额%v", notify.TotalAmount, payOrder.Amount)
// 比较支付宝返回的金额与数据库存储的金额
if math.Abs(notifyAmount-payOrder.Amount) < 1e-6 {
// 金额匹配,继续处理
payOrder.Status = 1
updateErr := l.svcCtx.PayOrderModel.Update(l.ctx, payOrder)
if updateErr != nil {
return nil, globalErr
}
_, rechargeWallet := l.svcCtx.WalletRpc.RechargeWallet(l.ctx, &user.RechargeWalletRequest{
UserId: payOrder.UserId,
OutTradeNo: payOrder.OutTradeNo,
Amount: int64(payOrder.Amount),
PaymentMethod: 0,
})
if rechargeWallet != nil {
return nil, globalErr
}
} else {
return nil, globalErr
}

View File

@@ -5,8 +5,10 @@ import (
"github.com/smartwalle/alipay/v3"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/zrpc"
"tianyuan-api/apps/sentinel/internal/config"
"tianyuan-api/apps/sentinel/internal/model"
"tianyuan-api/apps/user/user"
)
type ServiceContext struct {
@@ -18,6 +20,7 @@ type ServiceContext struct {
ProductsModel model.ProductsModel
UserProductsModel model.UserProductsModel
PayOrderModel model.PayOrderModel
WalletRpc user.WalletServiceClient
}
func NewServiceContext(c config.Config) *ServiceContext {
@@ -41,6 +44,8 @@ func NewServiceContext(c config.Config) *ServiceContext {
if err != nil {
panic(fmt.Sprintf("加载支付宝公钥失败: %v", err))
}
walletRpc := user.NewWalletServiceClient(zrpc.MustNewClient(c.UserRpc).Conn())
return &ServiceContext{
Config: c,
Redis: rds,
@@ -50,5 +55,6 @@ func NewServiceContext(c config.Config) *ServiceContext {
ProductsModel: model.NewProductsModel(db, c.CacheRedis),
UserProductsModel: model.NewUserProductsModel(rds, db, c.CacheRedis),
PayOrderModel: model.NewPayOrderModel(db, c.CacheRedis),
WalletRpc: walletRpc,
}
}