f
This commit is contained in:
@@ -95,6 +95,112 @@ func (l *PaymentCheckLogic) PaymentCheck(req *types.PaymentCheckReq) (resp *type
|
||||
}
|
||||
}
|
||||
|
||||
// 如果订单状态是 pending 且支付平台是云印签支付,主动查询云印签订单状态
|
||||
if order.Status == "pending" && (order.PaymentPlatform == "yunyinSignPay" || order.PaymentPlatform == "yunyinSignPay_wechat" || order.PaymentPlatform == "yunyinSignPay_alipay") {
|
||||
// 检查云印签支付服务是否启用
|
||||
if l.svcCtx.YunYinSignPayService != nil {
|
||||
// 主动查询云印签订单状态
|
||||
queryResp, queryErr := l.svcCtx.YunYinSignPayService.QueryPayeeBill(l.ctx, req.OrderNo)
|
||||
if queryErr != nil {
|
||||
logx.Errorf("主动查询云印签订单状态失败,订单号: %s, 错误: %v", req.OrderNo, queryErr)
|
||||
// 查询失败不影响返回,继续返回当前订单状态
|
||||
} else {
|
||||
// 根据云印签返回的支付状态更新本地订单状态
|
||||
// 云印签 payStatus: 0-订单生成, 1-支付中, 2-支付成功, 3-支付失败, 4-已退款
|
||||
// 我们的订单状态: pending, paid, failed, refunded
|
||||
var newOrderStatus string
|
||||
var shouldUpdate bool
|
||||
|
||||
switch queryResp.PayStatus {
|
||||
case 2: // 支付成功
|
||||
newOrderStatus = "paid"
|
||||
shouldUpdate = true
|
||||
case 3: // 支付失败
|
||||
newOrderStatus = "failed"
|
||||
shouldUpdate = true
|
||||
case 4: // 已退款
|
||||
newOrderStatus = "refunded"
|
||||
shouldUpdate = true
|
||||
case 0, 1: // 订单生成或支付中,保持 pending
|
||||
// 不更新,继续返回 pending
|
||||
}
|
||||
|
||||
if shouldUpdate {
|
||||
logx.Infof("主动查询发现云印签订单状态已变更,订单号: %s, 支付状态: %d, 新订单状态: %s", req.OrderNo, queryResp.PayStatus, newOrderStatus)
|
||||
|
||||
// 重新查询订单(获取最新版本号)
|
||||
order, err = l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
logx.Errorf("更新订单状态前重新查询订单失败: %v", err)
|
||||
} else {
|
||||
// 更新云印签订单表的支付状态(无论订单状态如何,都要同步云印签的状态)
|
||||
yunyinOrder, findYunyinErr := l.svcCtx.YunyinSignPayOrderModel.FindOneByOrderId(l.ctx, order.Id)
|
||||
if findYunyinErr == nil && yunyinOrder != nil {
|
||||
// 更新支付状态
|
||||
// 云印签 payStatus: 0-订单生成, 1-支付中, 2-支付成功, 3-支付失败, 4-已退款
|
||||
// 我们的 payStatus: 0-待支付, 1-已支付, 2-已退款
|
||||
var newPayStatus int64
|
||||
if queryResp.PayStatus == 2 {
|
||||
newPayStatus = 1 // 已支付
|
||||
} else if queryResp.PayStatus == 4 {
|
||||
newPayStatus = 2 // 已退款
|
||||
} else if queryResp.PayStatus == 3 {
|
||||
// 支付失败,保持待支付状态
|
||||
newPayStatus = 0
|
||||
}
|
||||
|
||||
if newPayStatus != yunyinOrder.PayStatus {
|
||||
yunyinOrder.PayStatus = newPayStatus
|
||||
if updateYunyinErr := l.svcCtx.YunyinSignPayOrderModel.Update(l.ctx, yunyinOrder); updateYunyinErr != nil {
|
||||
logx.Errorf("更新云印签订单支付状态失败: %v", updateYunyinErr)
|
||||
} else {
|
||||
logx.Infof("成功更新云印签订单支付状态,订单ID: %s, 新支付状态: %d", order.Id, newPayStatus)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 只有在订单状态是 pending 时才更新订单状态
|
||||
if order.Status == "pending" {
|
||||
// 更新订单状态
|
||||
order.Status = newOrderStatus
|
||||
if newOrderStatus == "paid" {
|
||||
order.PayTime = lzUtils.TimeToNullTime(time.Now())
|
||||
if queryResp.ChannelOrderNo != "" {
|
||||
order.PlatformOrderId = lzUtils.StringToNullString(queryResp.ChannelOrderNo)
|
||||
}
|
||||
} else if newOrderStatus == "refunded" {
|
||||
order.RefundTime = lzUtils.TimeToNullTime(time.Now())
|
||||
}
|
||||
|
||||
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
|
||||
logx.Errorf("主动查询后更新订单状态失败: %v", updateErr)
|
||||
} else {
|
||||
logx.Infof("主动查询后成功更新订单状态,订单号: %s, 新状态: %s", req.OrderNo, newOrderStatus)
|
||||
|
||||
// 如果订单已支付,发送异步任务处理后续流程
|
||||
if newOrderStatus == "paid" {
|
||||
if asyncErr := l.svcCtx.AsynqService.SendQueryTask(order.Id); asyncErr != nil {
|
||||
logx.Errorf("主动查询后发送异步任务失败: %v", asyncErr)
|
||||
}
|
||||
}
|
||||
|
||||
// 返回更新后的状态
|
||||
return &types.PaymentCheckResp{
|
||||
Type: "query",
|
||||
Status: newOrderStatus,
|
||||
}, nil
|
||||
}
|
||||
} else {
|
||||
// 订单状态已经不是 pending,说明可能已经被其他流程处理
|
||||
// 但仍然返回当前状态
|
||||
logx.Infof("订单状态已不是 pending,当前状态: %s,跳过更新", order.Status)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &types.PaymentCheckResp{
|
||||
Type: "query",
|
||||
Status: order.Status,
|
||||
|
||||
@@ -3,13 +3,16 @@ package pay
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"jnc-server/app/main/api/internal/service"
|
||||
"jnc-server/app/main/api/internal/svc"
|
||||
"jnc-server/app/main/api/internal/types"
|
||||
"jnc-server/app/main/model"
|
||||
"jnc-server/common/ctxdata"
|
||||
"jnc-server/common/xerr"
|
||||
"jnc-server/pkg/lzkit/crypto"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -31,6 +34,8 @@ type PaymentTypeResp struct {
|
||||
outTradeNo string
|
||||
description string
|
||||
orderID string // 订单ID,用于开发环境测试支付模式
|
||||
userName string // 用户姓名(从查询缓存中获取)
|
||||
userMobile string // 用户手机号(从查询缓存中获取)
|
||||
}
|
||||
|
||||
func NewPaymentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PaymentLogic {
|
||||
@@ -111,6 +116,83 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
}
|
||||
} else if req.PayMethod == "appleiap" {
|
||||
prepayData = l.svcCtx.ApplePayService.GetIappayAppID(paymentTypeResp.outTradeNo)
|
||||
} else if req.PayMethod == "yunyinSignPay" || req.PayMethod == "yunyinSignPay_wechat" || req.PayMethod == "yunyinSignPay_alipay" {
|
||||
if l.svcCtx.YunYinSignPayService == nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "云印签支付服务未启用")
|
||||
}
|
||||
|
||||
// 从查询缓存中获取用户姓名和手机号
|
||||
if paymentTypeResp.userMobile == "" {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 查询缓存中未找到用户手机号,无法使用云印签支付")
|
||||
}
|
||||
|
||||
// 获取用户ID
|
||||
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if getUidErr != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取用户ID失败: %+v", getUidErr)
|
||||
}
|
||||
|
||||
// 根据支付方式确定支付类型
|
||||
payType := 0 // 默认微信支付
|
||||
if req.PayMethod == "yunyinSignPay_alipay" {
|
||||
payType = 1 // 支付宝支付
|
||||
}
|
||||
|
||||
// 查询用户是否有未完成的签署(待签署且待支付)
|
||||
var yunYinSignPayResult *service.CreateYunYinSignPayOrderResult
|
||||
unfinishedOrder, findUnfinishedErr := l.svcCtx.YunyinSignPayOrderModel.FindUnfinishedByUserId(l.ctx, userID)
|
||||
if findUnfinishedErr == nil && unfinishedOrder != nil {
|
||||
// 复用未完成的签署,只获取新的支付链接
|
||||
logx.Infof("复用未完成的云印签签署,任务ID: %s, 参与者ID: %s", unfinishedOrder.TaskId, unfinishedOrder.ParticipantId)
|
||||
|
||||
// 获取token和操作ID(带缓存)
|
||||
accessToken, tokenErr := l.svcCtx.YunYinSignPayService.GetAccessToken(l.ctx)
|
||||
if tokenErr != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取云印签token失败: %+v", tokenErr)
|
||||
}
|
||||
|
||||
operationUserId, userIdErr := l.svcCtx.YunYinSignPayService.GetUserId(l.ctx, accessToken)
|
||||
if userIdErr != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取云印签操作ID失败: %+v", userIdErr)
|
||||
}
|
||||
|
||||
// 获取新的支付链接
|
||||
payURL, payURLErr := l.svcCtx.YunYinSignPayService.GetPaymentURL(l.ctx, accessToken, operationUserId, unfinishedOrder.ParticipantId, payType)
|
||||
if payURLErr != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取云印签支付链接失败: %+v", payURLErr)
|
||||
}
|
||||
|
||||
yunYinSignPayResult = &service.CreateYunYinSignPayOrderResult{
|
||||
PayURL: payURL,
|
||||
ParticipantID: unfinishedOrder.ParticipantId,
|
||||
TaskID: unfinishedOrder.TaskId,
|
||||
}
|
||||
} else {
|
||||
// 没有未完成的签署,创建新的签署流程
|
||||
var createOrderErr error
|
||||
yunYinSignPayResult, createOrderErr = l.svcCtx.YunYinSignPayService.CreateYunYinSignPayOrder(
|
||||
l.ctx,
|
||||
paymentTypeResp.userMobile,
|
||||
paymentTypeResp.userName,
|
||||
paymentTypeResp.amount,
|
||||
paymentTypeResp.outTradeNo,
|
||||
payType,
|
||||
)
|
||||
if createOrderErr != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 创建云印签支付订单失败: %+v", createOrderErr)
|
||||
}
|
||||
}
|
||||
|
||||
prepayData = yunYinSignPayResult.PayURL
|
||||
|
||||
// 将云印签信息存储到context中,后续创建订单和云印签订单记录时使用
|
||||
ctx = context.WithValue(ctx, "yunyin_sign_pay_result", yunYinSignPayResult)
|
||||
ctx = context.WithValue(ctx, "yunyin_sign_pay_user_id", userID)
|
||||
ctx = context.WithValue(ctx, "yunyin_sign_pay_user_mobile", paymentTypeResp.userMobile)
|
||||
ctx = context.WithValue(ctx, "yunyin_sign_pay_user_name", paymentTypeResp.userName)
|
||||
ctx = context.WithValue(ctx, "yunyin_sign_pay_amount", paymentTypeResp.amount)
|
||||
ctx = context.WithValue(ctx, "yunyin_sign_pay_pay_type", payType)
|
||||
l.ctx = ctx
|
||||
}
|
||||
if createOrderErr != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 创建支付订单失败: %+v", createOrderErr)
|
||||
@@ -204,6 +286,27 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 解析缓存内容失败, %v", err)
|
||||
}
|
||||
|
||||
// 解析查询参数,获取用户姓名和手机号(用于云印签支付)
|
||||
var userName, userMobile string
|
||||
if data.Params != "" {
|
||||
secretKey := l.svcCtx.Config.Encrypt.SecretKey
|
||||
key, decodeErr := hex.DecodeString(secretKey)
|
||||
if decodeErr == nil {
|
||||
decryptedData, decryptErr := crypto.AesDecrypt(data.Params, key)
|
||||
if decryptErr == nil {
|
||||
var params map[string]interface{}
|
||||
if unmarshalErr := json.Unmarshal(decryptedData, ¶ms); unmarshalErr == nil {
|
||||
if name, ok := params["name"].(string); ok {
|
||||
userName = name
|
||||
}
|
||||
if mobile, ok := params["mobile"].(string); ok {
|
||||
userMobile = mobile
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
product, err := l.svcCtx.ProductModel.FindOneByProductEn(l.ctx, data.Product)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 查找产品错误: %v", err)
|
||||
@@ -252,6 +355,40 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses
|
||||
}
|
||||
orderID := order.Id
|
||||
|
||||
// 如果是云印签支付,创建云印签订单记录
|
||||
if req.PayMethod == "yunyinSignPay" || req.PayMethod == "yunyinSignPay_wechat" || req.PayMethod == "yunyinSignPay_alipay" {
|
||||
yunYinSignPayResult, ok := l.ctx.Value("yunyin_sign_pay_result").(*service.CreateYunYinSignPayOrderResult)
|
||||
if ok && yunYinSignPayResult != nil {
|
||||
userID, _ := l.ctx.Value("yunyin_sign_pay_user_id").(string)
|
||||
userMobile, _ := l.ctx.Value("yunyin_sign_pay_user_mobile").(string)
|
||||
userName, _ := l.ctx.Value("yunyin_sign_pay_user_name").(string)
|
||||
amount, _ := l.ctx.Value("yunyin_sign_pay_amount").(float64)
|
||||
payType, _ := l.ctx.Value("yunyin_sign_pay_pay_type").(int)
|
||||
|
||||
yunyinSignPayOrder := model.YunyinSignPayOrder{
|
||||
Id: uuid.NewString(),
|
||||
OrderId: orderID,
|
||||
UserId: userID,
|
||||
TaskId: yunYinSignPayResult.TaskID,
|
||||
ParticipantId: yunYinSignPayResult.ParticipantID,
|
||||
Amount: amount,
|
||||
PayType: int64(payType),
|
||||
SignStatus: 0, // 待签署
|
||||
PayStatus: 0, // 待支付
|
||||
SourceOrderCode: outTradeNo,
|
||||
UserMobile: sql.NullString{String: userMobile, Valid: userMobile != ""},
|
||||
UserName: sql.NullString{String: userName, Valid: userName != ""},
|
||||
DelState: 0,
|
||||
Version: 0,
|
||||
}
|
||||
|
||||
_, insertYunYinErr := l.svcCtx.YunyinSignPayOrderModel.InsertWithSession(l.ctx, session, &yunyinSignPayOrder)
|
||||
if insertYunYinErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存云印签订单失败: %+v", insertYunYinErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是代理推广订单,创建完整的代理订单记录
|
||||
if data.AgentIdentifier != "" && agentLinkModel != nil {
|
||||
// 获取产品配置(必须存在)
|
||||
@@ -302,5 +439,12 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存代理订单失败: %+v", agentOrderInsert)
|
||||
}
|
||||
}
|
||||
return &PaymentTypeResp{amount: amount, outTradeNo: outTradeNo, description: product.ProductName, orderID: orderID}, nil
|
||||
return &PaymentTypeResp{
|
||||
amount: amount,
|
||||
outTradeNo: outTradeNo,
|
||||
description: product.ProductName,
|
||||
orderID: orderID,
|
||||
userName: userName,
|
||||
userMobile: userMobile,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user