Compare commits
No commits in common. "8c4f98a2d5c7cda7c4908d3b663ee9bc97e11b1a" and "1ecac190983be8635380221319fb6219f248adfe" have entirely different histories.
8c4f98a2d5
...
1ecac19098
@ -61,6 +61,3 @@ Ali:
|
|||||||
Code: "d55b58829efb41c8aa8e86769cba4844"
|
Code: "d55b58829efb41c8aa8e86769cba4844"
|
||||||
SystemConfig:
|
SystemConfig:
|
||||||
ThreeVerify: false
|
ThreeVerify: false
|
||||||
WechatH5:
|
|
||||||
AppID: "wxa581992dc74d860e"
|
|
||||||
AppSecret: "ba37510206df321279222cecb8614e00"
|
|
||||||
|
@ -62,6 +62,3 @@ Ali:
|
|||||||
Code: "d55b58829efb41c8aa8e86769cba4844"
|
Code: "d55b58829efb41c8aa8e86769cba4844"
|
||||||
SystemConfig:
|
SystemConfig:
|
||||||
ThreeVerify: true
|
ThreeVerify: true
|
||||||
WechatH5:
|
|
||||||
AppID: "wxa581992dc74d860e"
|
|
||||||
AppSecret: "ba37510206df321279222cecb8614e00"
|
|
||||||
|
@ -19,7 +19,6 @@ type Config struct {
|
|||||||
WestConfig WestConfig
|
WestConfig WestConfig
|
||||||
YushanConfig YushanConfig
|
YushanConfig YushanConfig
|
||||||
SystemConfig SystemConfig
|
SystemConfig SystemConfig
|
||||||
WechatH5 WechatH5Config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// JwtAuth 用于 JWT 鉴权配置
|
// JwtAuth 用于 JWT 鉴权配置
|
||||||
@ -88,7 +87,3 @@ type YushanConfig struct {
|
|||||||
type SystemConfig struct {
|
type SystemConfig struct {
|
||||||
ThreeVerify bool
|
ThreeVerify bool
|
||||||
}
|
}
|
||||||
type WechatH5Config struct {
|
|
||||||
AppID string
|
|
||||||
AppSecret string
|
|
||||||
}
|
|
||||||
|
@ -70,7 +70,7 @@ func (l *ApplyForAgentLogic) ApplyForAgent(req *types.AgentApplyReq) (resp *type
|
|||||||
// }
|
// }
|
||||||
insertResult, userInsertErr := l.svcCtx.UserModel.Insert(transCtx, session, user)
|
insertResult, userInsertErr := l.svcCtx.UserModel.Insert(transCtx, session, user)
|
||||||
if userInsertErr != nil {
|
if userInsertErr != nil {
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 数据库插入新用户失败, mobile%s, err: %+v", encryptedMobile, userInsertErr)
|
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 数据库插入新用户失败, mobile%s, err: %+v", encryptedMobile, err)
|
||||||
}
|
}
|
||||||
lastId, lastInsertIdErr := insertResult.LastInsertId()
|
lastId, lastInsertIdErr := insertResult.LastInsertId()
|
||||||
if lastInsertIdErr != nil {
|
if lastInsertIdErr != nil {
|
||||||
|
@ -2,6 +2,7 @@ package pay
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@ -162,7 +163,7 @@ func (l *AlipayCallbackLogic) handleAgentVipOrderPayment(w http.ResponseWriter,
|
|||||||
} else {
|
} else {
|
||||||
logx.Infof("代理会员新购或升级成功,会员ID:%d,等级:%s", agentModel.Id, agentModel.LevelName)
|
logx.Infof("代理会员新购或升级成功,会员ID:%d,等级:%s", agentModel.Id, agentModel.LevelName)
|
||||||
}
|
}
|
||||||
agentModel.MembershipExpiryTime = lzUtils.RenewMembership(agentModel.MembershipExpiryTime)
|
agentModel.MembershipExpiryTime = RenewMembership(agentModel.MembershipExpiryTime)
|
||||||
|
|
||||||
if updateErr := l.svcCtx.AgentModel.UpdateWithVersion(l.ctx, nil, agentModel); updateErr != nil {
|
if updateErr := l.svcCtx.AgentModel.UpdateWithVersion(l.ctx, nil, agentModel); updateErr != nil {
|
||||||
return fmt.Errorf("修改代理信息失败: %+v", updateErr)
|
return fmt.Errorf("修改代理信息失败: %+v", updateErr)
|
||||||
@ -182,6 +183,24 @@ func (l *AlipayCallbackLogic) handleAgentVipOrderPayment(w http.ResponseWriter,
|
|||||||
alipay.ACKNotification(w)
|
alipay.ACKNotification(w)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func RenewMembership(expiry sql.NullTime) sql.NullTime {
|
||||||
|
// 确定基准时间
|
||||||
|
var baseTime time.Time
|
||||||
|
if expiry.Valid {
|
||||||
|
baseTime = expiry.Time
|
||||||
|
} else {
|
||||||
|
baseTime = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 增加一年(自动处理闰年)
|
||||||
|
newTime := baseTime.AddDate(1, 0, 0)
|
||||||
|
|
||||||
|
// 返回始终有效的 NullTime
|
||||||
|
return sql.NullTime{
|
||||||
|
Time: newTime,
|
||||||
|
Valid: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (l *AlipayCallbackLogic) handleRefund(order *model.AgentMembershipRechargeOrder) error {
|
func (l *AlipayCallbackLogic) handleRefund(order *model.AgentMembershipRechargeOrder) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
@ -2,19 +2,14 @@ package pay
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
"tydata-server/app/user/cmd/api/internal/service"
|
"tydata-server/app/user/cmd/api/internal/service"
|
||||||
"tydata-server/app/user/model"
|
|
||||||
"tydata-server/pkg/lzkit/lzUtils"
|
"tydata-server/pkg/lzkit/lzUtils"
|
||||||
|
|
||||||
"tydata-server/app/user/cmd/api/internal/svc"
|
"tydata-server/app/user/cmd/api/internal/svc"
|
||||||
|
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type WechatPayCallbackLogic struct {
|
type WechatPayCallbackLogic struct {
|
||||||
@ -37,23 +32,6 @@ func (l *WechatPayCallbackLogic) WechatPayCallback(w http.ResponseWriter, r *htt
|
|||||||
logx.Errorf("微信支付回调,%v", err)
|
logx.Errorf("微信支付回调,%v", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据订单号前缀判断订单类型
|
|
||||||
orderNo := *notification.OutTradeNo
|
|
||||||
if strings.HasPrefix(orderNo, "Q_") {
|
|
||||||
// 查询订单处理
|
|
||||||
return l.handleQueryOrderPayment(w, notification)
|
|
||||||
} else if strings.HasPrefix(orderNo, "A_") {
|
|
||||||
// 代理会员订单处理
|
|
||||||
return l.handleAgentVipOrderPayment(w, notification)
|
|
||||||
} else {
|
|
||||||
// 兼容旧订单,假设没有前缀的是查询订单
|
|
||||||
return l.handleQueryOrderPayment(w, notification)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理查询订单支付
|
|
||||||
func (l *WechatPayCallbackLogic) handleQueryOrderPayment(w http.ResponseWriter, notification *payments.Transaction) error {
|
|
||||||
order, findOrderErr := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, *notification.OutTradeNo)
|
order, findOrderErr := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, *notification.OutTradeNo)
|
||||||
if findOrderErr != nil {
|
if findOrderErr != nil {
|
||||||
logx.Errorf("微信支付回调,查找订单信息失败: %+v", findOrderErr)
|
logx.Errorf("微信支付回调,查找订单信息失败: %+v", findOrderErr)
|
||||||
@ -65,13 +43,11 @@ func (l *WechatPayCallbackLogic) handleQueryOrderPayment(w http.ResponseWriter,
|
|||||||
logx.Errorf("微信支付回调,金额不一致")
|
logx.Errorf("微信支付回调,金额不一致")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if order.Status != "pending" {
|
if order.Status != "pending" {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, _ = w.Write([]byte("success"))
|
_, _ = w.Write([]byte("success")) // 确保只写入一次响应
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch *notification.TradeState {
|
switch *notification.TradeState {
|
||||||
case service.TradeStateSuccess:
|
case service.TradeStateSuccess:
|
||||||
order.Status = "paid"
|
order.Status = "paid"
|
||||||
@ -84,13 +60,11 @@ func (l *WechatPayCallbackLogic) handleQueryOrderPayment(w http.ResponseWriter,
|
|||||||
default:
|
default:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
order.PlatformOrderId = lzUtils.StringToNullString(*notification.TransactionId)
|
order.PlatformOrderId = lzUtils.StringToNullString(*notification.TransactionId)
|
||||||
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
|
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
|
||||||
logx.Errorf("微信支付回调,更新订单失败%+v", updateErr)
|
logx.Errorf("微信支付回调,更新订单失败%+v", updateErr)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if order.Status == "paid" {
|
if order.Status == "paid" {
|
||||||
if asyncErr := l.svcCtx.AsynqService.SendQueryTask(order.Id); asyncErr != nil {
|
if asyncErr := l.svcCtx.AsynqService.SendQueryTask(order.Id); asyncErr != nil {
|
||||||
logx.Errorf("异步任务调度失败: %v", asyncErr)
|
logx.Errorf("异步任务调度失败: %v", asyncErr)
|
||||||
@ -98,118 +72,8 @@ func (l *WechatPayCallbackLogic) handleQueryOrderPayment(w http.ResponseWriter,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 响应微信回调成功
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, _ = w.Write([]byte("success"))
|
_, _ = w.Write([]byte("success")) // 确保只写入一次响应
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理代理会员订单支付
|
|
||||||
func (l *WechatPayCallbackLogic) handleAgentVipOrderPayment(w http.ResponseWriter, notification *payments.Transaction) error {
|
|
||||||
agentOrder, findAgentOrderErr := l.svcCtx.AgentMembershipRechargeOrderModel.FindOneByOrderNo(l.ctx, *notification.OutTradeNo)
|
|
||||||
if findAgentOrderErr != nil {
|
|
||||||
logx.Errorf("微信支付回调,查找代理会员订单失败: %+v", findAgentOrderErr)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if agentOrder.Status != "pending" {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_, _ = w.Write([]byte("success"))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
user, err := l.svcCtx.UserModel.FindOne(l.ctx, agentOrder.UserId)
|
|
||||||
if err != nil {
|
|
||||||
logx.Errorf("微信支付回调,查找用户失败: %+v", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
amount := lzUtils.ToWechatAmount(agentOrder.Amount)
|
|
||||||
if user.Inside != 1 {
|
|
||||||
if amount != *notification.Amount.Total {
|
|
||||||
logx.Errorf("微信支付回调,金额不一致")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch *notification.TradeState {
|
|
||||||
case service.TradeStateSuccess:
|
|
||||||
agentOrder.Status = "paid"
|
|
||||||
default:
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if agentOrder.Status == "paid" {
|
|
||||||
err = l.svcCtx.AgentModel.Trans(l.ctx, func(transCtx context.Context, session sqlx.Session) error {
|
|
||||||
agentModel, err := l.svcCtx.AgentModel.FindOne(transCtx, agentOrder.AgentId)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("查找代理信息失败: %+v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
agentOrder.PlatformOrderId = lzUtils.StringToNullString(*notification.TransactionId)
|
|
||||||
if updateErr := l.svcCtx.AgentMembershipRechargeOrderModel.UpdateWithVersion(l.ctx, nil, agentOrder); updateErr != nil {
|
|
||||||
return fmt.Errorf("修改代理会员订单信息失败: %+v", updateErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置会员等级
|
|
||||||
agentModel.LevelName = agentOrder.LevelName
|
|
||||||
|
|
||||||
// 延长会员时间
|
|
||||||
isRenewal := agentModel.LevelName == agentOrder.LevelName && agentModel.MembershipExpiryTime.Valid
|
|
||||||
if isRenewal {
|
|
||||||
logx.Infof("代理会员续费成功,会员ID:%d,等级:%s", agentModel.Id, agentModel.LevelName)
|
|
||||||
} else {
|
|
||||||
logx.Infof("代理会员新购或升级成功,会员ID:%d,等级:%s", agentModel.Id, agentModel.LevelName)
|
|
||||||
}
|
|
||||||
agentModel.MembershipExpiryTime = lzUtils.RenewMembership(agentModel.MembershipExpiryTime)
|
|
||||||
|
|
||||||
if updateErr := l.svcCtx.AgentModel.UpdateWithVersion(l.ctx, nil, agentModel); updateErr != nil {
|
|
||||||
return fmt.Errorf("修改代理信息失败: %+v", updateErr)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
logx.Errorf("微信支付回调,处理代理会员订单失败: %+v", err)
|
|
||||||
refundErr := l.handleRefund(agentOrder)
|
|
||||||
if refundErr != nil {
|
|
||||||
logx.Errorf("微信支付回调,退款失败: %+v", refundErr)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_, _ = w.Write([]byte("success"))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *WechatPayCallbackLogic) handleRefund(order *model.AgentMembershipRechargeOrder) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
// 退款
|
|
||||||
if order.PaymentMethod == "wechat" {
|
|
||||||
refundErr := l.svcCtx.WechatPayService.WeChatRefund(ctx, order.OrderNo, order.Amount, order.Amount)
|
|
||||||
if refundErr != nil {
|
|
||||||
return refundErr
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
refund, refundErr := l.svcCtx.AlipayService.AliRefund(ctx, order.OrderNo, order.Amount)
|
|
||||||
if refundErr != nil {
|
|
||||||
return refundErr
|
|
||||||
}
|
|
||||||
if refund.IsSuccess() {
|
|
||||||
logx.Errorf("支付宝退款成功, orderID: %d", order.Id)
|
|
||||||
// 更新订单状态为退款
|
|
||||||
order.Status = "refunded"
|
|
||||||
updateOrderErr := l.svcCtx.AgentMembershipRechargeOrderModel.UpdateWithVersion(ctx, nil, order)
|
|
||||||
if updateOrderErr != nil {
|
|
||||||
logx.Errorf("更新订单状态失败,订单ID: %d, 错误: %v", order.Id, updateOrderErr)
|
|
||||||
return fmt.Errorf("更新订单状态失败: %v", updateOrderErr)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
logx.Errorf("支付宝退款失败:%v", refundErr)
|
|
||||||
return refundErr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ func NewWxH5AuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WxH5Auth
|
|||||||
|
|
||||||
func (l *WxH5AuthLogic) WxH5Auth(req *types.WXH5AuthReq) (resp *types.WXH5AuthResp, err error) {
|
func (l *WxH5AuthLogic) WxH5Auth(req *types.WXH5AuthReq) (resp *types.WXH5AuthResp, err error) {
|
||||||
// Step 1: 使用code获取access_token
|
// Step 1: 使用code获取access_token
|
||||||
accessTokenResp, err := l.GetAccessToken(req.Code)
|
accessTokenResp, err := GetAccessToken(req.Code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取access_token失败: %v", err)
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取access_token失败: %v", err)
|
||||||
}
|
}
|
||||||
@ -108,9 +108,9 @@ type AccessTokenResp struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAccessToken 通过code获取access_token
|
// GetAccessToken 通过code获取access_token
|
||||||
func (l *WxH5AuthLogic) GetAccessToken(code string) (*AccessTokenResp, error) {
|
func GetAccessToken(code string) (*AccessTokenResp, error) {
|
||||||
appID := l.svcCtx.Config.WechatH5.AppID
|
appID := "wxa581992dc74d860e"
|
||||||
appSecret := l.svcCtx.Config.WechatH5.AppSecret
|
appSecret := "cfca484cf8cebcccb1f16fbaf1d9fe2c"
|
||||||
|
|
||||||
url := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appID, appSecret, code)
|
url := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", appID, appSecret, code)
|
||||||
|
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
package lzUtils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RenewMembership 延长会员有效期
|
|
||||||
func RenewMembership(expiry sql.NullTime) sql.NullTime {
|
|
||||||
// 确定基准时间
|
|
||||||
var baseTime time.Time
|
|
||||||
if expiry.Valid {
|
|
||||||
baseTime = expiry.Time
|
|
||||||
} else {
|
|
||||||
baseTime = time.Now()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 增加一年(自动处理闰年)
|
|
||||||
newTime := baseTime.AddDate(1, 0, 0)
|
|
||||||
|
|
||||||
// 返回始终有效的 NullTime
|
|
||||||
return sql.NullTime{
|
|
||||||
Time: newTime,
|
|
||||||
Valid: true,
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user