Compare commits
2 Commits
9f509924b3
...
9e10b5f2e9
| Author | SHA1 | Date | |
|---|---|---|---|
| 9e10b5f2e9 | |||
| b1a9f60bb0 |
@@ -118,18 +118,23 @@ 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" {
|
||||
logx.Infof("[云印签支付] 开始处理支付请求,支付方式: %s, 订单号: %s", req.PayMethod, paymentTypeResp.outTradeNo)
|
||||
|
||||
if l.svcCtx.YunYinSignPayService == nil {
|
||||
logx.Errorf("[云印签支付] 支付服务未启用")
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "云印签支付服务未启用")
|
||||
}
|
||||
|
||||
// 从查询缓存中获取用户姓名和手机号
|
||||
if paymentTypeResp.userMobile == "" {
|
||||
logx.Errorf("[云印签支付] 查询缓存中未找到用户手机号,订单号: %s", paymentTypeResp.outTradeNo)
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 查询缓存中未找到用户手机号,无法使用云印签支付")
|
||||
}
|
||||
|
||||
// 获取用户ID
|
||||
userID, getUidErr := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if getUidErr != nil {
|
||||
logx.Errorf("[云印签支付] 获取用户ID失败,订单号: %s, 错误: %+v", paymentTypeResp.outTradeNo, getUidErr)
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取用户ID失败: %+v", getUidErr)
|
||||
}
|
||||
|
||||
@@ -138,10 +143,13 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
if req.PayMethod == "yunyinSignPay_alipay" {
|
||||
payType = 1 // 支付宝支付
|
||||
}
|
||||
logx.Infof("[云印签支付] 支付参数,订单号: %s, 用户ID: %s, 用户手机: %s, 用户姓名: %s, 金额: %.2f, 支付类型: %d(0=微信,1=支付宝)",
|
||||
paymentTypeResp.outTradeNo, userID, paymentTypeResp.userMobile, paymentTypeResp.userName, paymentTypeResp.amount, payType)
|
||||
|
||||
// 查询用户是否有未完成的签署(待签署且待支付)
|
||||
var yunYinSignPayResult *service.CreateYunYinSignPayOrderResult
|
||||
// 使用 SelectBuilder 查询未完成的签署订单(del_state 由系统自动处理)
|
||||
logx.Infof("[云印签支付] 查询用户未完成的签署订单,用户ID: %s", userID)
|
||||
unfinishedBuilder := l.svcCtx.YunyinSignPayOrderModel.SelectBuilder().
|
||||
Where(squirrel.Eq{
|
||||
"user_id": userID,
|
||||
@@ -154,35 +162,60 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
var unfinishedOrder *model.YunyinSignPayOrder
|
||||
if findUnfinishedErr == nil && len(unfinishedOrders) > 0 {
|
||||
unfinishedOrder = unfinishedOrders[0]
|
||||
logx.Infof("[云印签支付] 找到未完成的签署订单,订单号: %s, 任务ID: %s, 参与者ID: %s, 金额: %.2f",
|
||||
paymentTypeResp.outTradeNo, unfinishedOrder.TaskId, unfinishedOrder.ParticipantId, unfinishedOrder.Amount)
|
||||
} else {
|
||||
if findUnfinishedErr != nil {
|
||||
logx.Infof("[云印签支付] 查询未完成签署订单失败,用户ID: %s, 错误: %v", userID, findUnfinishedErr)
|
||||
} else {
|
||||
logx.Infof("[云印签支付] 未找到未完成的签署订单,用户ID: %s, 将创建新签署流程", userID)
|
||||
}
|
||||
}
|
||||
|
||||
if unfinishedOrder != nil {
|
||||
// 复用未完成的签署,只获取新的支付链接
|
||||
logx.Infof("复用未完成的云印签签署,任务ID: %s, 参与者ID: %s", unfinishedOrder.TaskId, unfinishedOrder.ParticipantId)
|
||||
logx.Infof("[云印签支付] 复用未完成的签署,订单号: %s, 任务ID: %s, 参与者ID: %s",
|
||||
paymentTypeResp.outTradeNo, unfinishedOrder.TaskId, unfinishedOrder.ParticipantId)
|
||||
|
||||
// 获取token和操作ID(带缓存)
|
||||
logx.Infof("[云印签支付] 开始获取AccessToken,订单号: %s", paymentTypeResp.outTradeNo)
|
||||
accessToken, tokenErr := l.svcCtx.YunYinSignPayService.GetAccessToken(l.ctx)
|
||||
if tokenErr != nil {
|
||||
logx.Errorf("[云印签支付] 获取AccessToken失败,订单号: %s, 错误: %+v", paymentTypeResp.outTradeNo, tokenErr)
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取云印签token失败: %+v", tokenErr)
|
||||
}
|
||||
logx.Infof("[云印签支付] 获取AccessToken成功,订单号: %s", paymentTypeResp.outTradeNo)
|
||||
|
||||
logx.Infof("[云印签支付] 开始获取操作ID,订单号: %s", paymentTypeResp.outTradeNo)
|
||||
operationUserId, userIdErr := l.svcCtx.YunYinSignPayService.GetUserId(l.ctx, accessToken)
|
||||
if userIdErr != nil {
|
||||
logx.Errorf("[云印签支付] 获取操作ID失败,订单号: %s, 错误: %+v", paymentTypeResp.outTradeNo, userIdErr)
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取云印签操作ID失败: %+v", userIdErr)
|
||||
}
|
||||
logx.Infof("[云印签支付] 获取操作ID成功,订单号: %s, 操作ID: %s", paymentTypeResp.outTradeNo, operationUserId)
|
||||
|
||||
// 获取新的支付链接
|
||||
logx.Infof("[云印签支付] 开始获取支付链接,订单号: %s, 参与者ID: %s, 支付类型: %d",
|
||||
paymentTypeResp.outTradeNo, unfinishedOrder.ParticipantId, payType)
|
||||
payURL, payURLErr := l.svcCtx.YunYinSignPayService.GetPaymentURL(l.ctx, accessToken, operationUserId, unfinishedOrder.ParticipantId, payType)
|
||||
if payURLErr != nil {
|
||||
logx.Errorf("[云印签支付] 获取支付链接失败,订单号: %s, 参与者ID: %s, 错误: %+v",
|
||||
paymentTypeResp.outTradeNo, unfinishedOrder.ParticipantId, payURLErr)
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 获取云印签支付链接失败: %+v", payURLErr)
|
||||
}
|
||||
logx.Infof("[云印签支付] 获取支付链接成功,订单号: %s, 支付链接: %s", paymentTypeResp.outTradeNo, payURL)
|
||||
|
||||
yunYinSignPayResult = &service.CreateYunYinSignPayOrderResult{
|
||||
PayURL: payURL,
|
||||
ParticipantID: unfinishedOrder.ParticipantId,
|
||||
TaskID: unfinishedOrder.TaskId,
|
||||
}
|
||||
logx.Infof("[云印签支付] 复用签署流程完成,订单号: %s, 任务ID: %s, 参与者ID: %s, 支付链接: %s",
|
||||
paymentTypeResp.outTradeNo, yunYinSignPayResult.TaskID, yunYinSignPayResult.ParticipantID, yunYinSignPayResult.PayURL)
|
||||
} else {
|
||||
// 没有未完成的签署,创建新的签署流程
|
||||
logx.Infof("[云印签支付] 开始创建新签署流程,订单号: %s, 用户手机: %s, 用户姓名: %s, 金额: %.2f, 支付类型: %d",
|
||||
paymentTypeResp.outTradeNo, paymentTypeResp.userMobile, paymentTypeResp.userName, paymentTypeResp.amount, payType)
|
||||
var createOrderErr error
|
||||
yunYinSignPayResult, createOrderErr = l.svcCtx.YunYinSignPayService.CreateYunYinSignPayOrder(
|
||||
l.ctx,
|
||||
@@ -193,11 +226,15 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
payType,
|
||||
)
|
||||
if createOrderErr != nil {
|
||||
logx.Errorf("[云印签支付] 创建新签署流程失败,订单号: %s, 错误: %+v", paymentTypeResp.outTradeNo, createOrderErr)
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 创建云印签支付订单失败: %+v", createOrderErr)
|
||||
}
|
||||
logx.Infof("[云印签支付] 创建新签署流程成功,订单号: %s, 任务ID: %s, 参与者ID: %s, 支付链接: %s",
|
||||
paymentTypeResp.outTradeNo, yunYinSignPayResult.TaskID, yunYinSignPayResult.ParticipantID, yunYinSignPayResult.PayURL)
|
||||
}
|
||||
|
||||
prepayData = yunYinSignPayResult.PayURL
|
||||
logx.Infof("[云印签支付] 支付链接获取成功,订单号: %s, 支付链接: %s", paymentTypeResp.outTradeNo, yunYinSignPayResult.PayURL)
|
||||
|
||||
// 将云印签信息存储到context中,后续创建订单和云印签订单记录时使用
|
||||
ctx = context.WithValue(ctx, "yunyin_sign_pay_result", yunYinSignPayResult)
|
||||
@@ -207,6 +244,8 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
ctx = context.WithValue(ctx, "yunyin_sign_pay_amount", paymentTypeResp.amount)
|
||||
ctx = context.WithValue(ctx, "yunyin_sign_pay_pay_type", payType)
|
||||
l.ctx = ctx
|
||||
logx.Infof("[云印签支付] 云印签信息已存储到context,订单号: %s, 任务ID: %s, 参与者ID: %s",
|
||||
paymentTypeResp.outTradeNo, yunYinSignPayResult.TaskID, yunYinSignPayResult.ParticipantID)
|
||||
}
|
||||
if createOrderErr != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 创建支付订单失败: %+v", createOrderErr)
|
||||
@@ -371,6 +410,7 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses
|
||||
|
||||
// 如果是云印签支付,创建云印签订单记录
|
||||
if req.PayMethod == "yunyinSignPay" || req.PayMethod == "yunyinSignPay_wechat" || req.PayMethod == "yunyinSignPay_alipay" {
|
||||
logx.Infof("[云印签支付] 开始创建云印签订单记录,订单号: %s, 订单ID: %s", outTradeNo, orderID)
|
||||
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)
|
||||
@@ -379,6 +419,9 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses
|
||||
amount, _ := l.ctx.Value("yunyin_sign_pay_amount").(float64)
|
||||
payType, _ := l.ctx.Value("yunyin_sign_pay_pay_type").(int)
|
||||
|
||||
logx.Infof("[云印签支付] 云印签订单数据,订单号: %s, 订单ID: %s, 用户ID: %s, 任务ID: %s, 参与者ID: %s, 金额: %.2f, 支付类型: %d",
|
||||
outTradeNo, orderID, userID, yunYinSignPayResult.TaskID, yunYinSignPayResult.ParticipantID, amount, payType)
|
||||
|
||||
yunyinSignPayOrder := model.YunyinSignPayOrder{
|
||||
Id: uuid.NewString(),
|
||||
OrderId: orderID,
|
||||
@@ -398,8 +441,13 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses
|
||||
|
||||
_, insertYunYinErr := l.svcCtx.YunyinSignPayOrderModel.Insert(l.ctx, session, &yunyinSignPayOrder)
|
||||
if insertYunYinErr != nil {
|
||||
logx.Errorf("[云印签支付] 保存云印签订单失败,订单号: %s, 订单ID: %s, 错误: %+v", outTradeNo, orderID, insertYunYinErr)
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存云印签订单失败: %+v", insertYunYinErr)
|
||||
}
|
||||
logx.Infof("[云印签支付] 云印签订单记录创建成功,订单号: %s, 订单ID: %s, 云印签订单ID: %s, 任务ID: %s, 参与者ID: %s",
|
||||
outTradeNo, orderID, yunyinSignPayOrder.Id, yunyinSignPayOrder.TaskId, yunyinSignPayOrder.ParticipantId)
|
||||
} else {
|
||||
logx.Errorf("[云印签支付] 未找到云印签支付结果,订单号: %s, 订单ID: %s", outTradeNo, orderID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,21 +27,26 @@ func NewYunYinSignPayCallbackLogic(ctx context.Context, svcCtx *svc.ServiceConte
|
||||
|
||||
// YunYinSignPayCallback 云印签支付回调处理
|
||||
func (l *YunYinSignPayCallbackLogic) YunYinSignPayCallback(w http.ResponseWriter, r *http.Request) error {
|
||||
logx.Infof("[云印签回调] 收到回调请求,Method: %s, URL: %s, RemoteAddr: %s", r.Method, r.URL.String(), r.RemoteAddr)
|
||||
logx.Infof("[云印签回调] 请求Header,Content-Type: %s, User-Agent: %s", r.Header.Get("Content-Type"), r.Header.Get("User-Agent"))
|
||||
|
||||
// 检查云印签支付服务是否启用
|
||||
if l.svcCtx.YunYinSignPayService == nil {
|
||||
logx.Errorf("云印签支付服务未启用")
|
||||
logx.Errorf("[云印签回调] 云印签支付服务未启用")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
|
||||
// 解析表单数据
|
||||
logx.Infof("[云印签回调] 开始解析表单数据")
|
||||
if err := r.ParseForm(); err != nil {
|
||||
logx.Errorf("云印签回调,解析表单数据失败: %v", err)
|
||||
logx.Errorf("[云印签回调] 解析表单数据失败: %v", err)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
logx.Infof("[云印签回调] 表单数据解析成功,Form参数数量: %d", len(r.Form))
|
||||
|
||||
// 获取回调参数
|
||||
tradeState := r.FormValue("tradeState")
|
||||
@@ -53,73 +58,100 @@ func (l *YunYinSignPayCallbackLogic) YunYinSignPayCallback(w http.ResponseWriter
|
||||
channelOrderNo := r.FormValue("channelOrderNo")
|
||||
paySuccessTime := r.FormValue("paySuccessTime")
|
||||
|
||||
// 记录所有回调参数
|
||||
logx.Infof("[云印签回调] 回调参数详情:")
|
||||
logx.Infof("[云印签回调] - tradeState: %s", tradeState)
|
||||
logx.Infof("[云印签回调] - orderType: %s", orderType)
|
||||
logx.Infof("[云印签回调] - payOrderNo: %s", payOrderNo)
|
||||
logx.Infof("[云印签回调] - payAmount: %s", payAmount)
|
||||
logx.Infof("[云印签回调] - flowId: %s", flowId)
|
||||
logx.Infof("[云印签回调] - sourceOrderCode: %s", sourceOrderCode)
|
||||
logx.Infof("[云印签回调] - channelOrderNo: %s", channelOrderNo)
|
||||
logx.Infof("[云印签回调] - paySuccessTime: %s", paySuccessTime)
|
||||
|
||||
// 记录回调日志
|
||||
logx.Infof("云印签回调通知,订单号: %s, 支付状态: %s, 订单类型: %s, 支付单号: %s, 金额: %s, 流程ID: %s",
|
||||
logx.Infof("[云印签回调] 回调通知摘要,订单号: %s, 支付状态: %s, 订单类型: %s, 支付单号: %s, 金额: %s, 流程ID: %s",
|
||||
sourceOrderCode, tradeState, orderType, payOrderNo, payAmount, flowId)
|
||||
|
||||
// 只处理付款通知(orderType == "pay")
|
||||
logx.Infof("[云印签回调] 检查订单类型,orderType: %s", orderType)
|
||||
if orderType != "pay" {
|
||||
logx.Infof("云印签回调,非付款通知,订单类型: %s, 订单号: %s", orderType, sourceOrderCode)
|
||||
logx.Infof("[云印签回调] 非付款通知,跳过处理,订单类型: %s, 订单号: %s", orderType, sourceOrderCode)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
logx.Infof("[云印签回调] 确认为付款通知,继续处理")
|
||||
|
||||
// 如果没有 sourceOrderCode,无法关联订单,直接返回成功
|
||||
if sourceOrderCode == "" {
|
||||
logx.Errorf("云印签回调,缺少 sourceOrderCode 参数")
|
||||
logx.Errorf("[云印签回调] 缺少 sourceOrderCode 参数,无法关联订单")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
logx.Infof("[云印签回调] sourceOrderCode 存在,订单号: %s", sourceOrderCode)
|
||||
|
||||
// 根据 sourceOrderCode 查找订单
|
||||
logx.Infof("[云印签回调] 开始查找订单,订单号: %s", sourceOrderCode)
|
||||
order, findOrderErr := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, sourceOrderCode)
|
||||
if findOrderErr != nil {
|
||||
logx.Errorf("云印签回调,查找订单失败,订单号: %s, 错误: %v", sourceOrderCode, findOrderErr)
|
||||
logx.Errorf("[云印签回调] 查找订单失败,订单号: %s, 错误: %v", sourceOrderCode, findOrderErr)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
logx.Infof("[云印签回调] 订单查找成功,订单号: %s, 订单ID: %s, 订单状态: %s, 订单金额: %.2f, 支付平台: %s",
|
||||
sourceOrderCode, order.Id, order.Status, order.Amount, order.PaymentPlatform)
|
||||
|
||||
// 幂等性检查:如果订单已经不是 pending 状态,说明已经处理过,直接返回成功
|
||||
logx.Infof("[云印签回调] 幂等性检查,订单状态: %s", order.Status)
|
||||
if order.Status != "pending" {
|
||||
logx.Infof("云印签回调,订单已处理,订单号: %s, 当前状态: %s", sourceOrderCode, order.Status)
|
||||
logx.Infof("[云印签回调] 订单已处理,跳过重复处理,订单号: %s, 当前状态: %s", sourceOrderCode, order.Status)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
logx.Infof("[云印签回调] 订单状态为pending,继续处理")
|
||||
|
||||
// 验证支付状态:tradeState == "00000" 表示支付成功
|
||||
logx.Infof("[云印签回调] 验证支付状态,tradeState: %s", tradeState)
|
||||
if tradeState != "00000" {
|
||||
logx.Infof("云印签回调,订单未支付成功,订单号: %s, 支付状态: %s", sourceOrderCode, tradeState)
|
||||
logx.Infof("[云印签回调] 订单未支付成功,跳过处理,订单号: %s, 支付状态: %s", sourceOrderCode, tradeState)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
logx.Infof("[云印签回调] 支付状态验证通过,tradeState: %s 表示支付成功", tradeState)
|
||||
|
||||
// 验证金额
|
||||
logx.Infof("[云印签回调] 开始验证金额,回调金额字符串: %s, 订单金额: %.2f", payAmount, order.Amount)
|
||||
payAmountFloat, parseAmountErr := strconv.ParseFloat(payAmount, 64)
|
||||
if parseAmountErr != nil {
|
||||
logx.Errorf("云印签回调,金额解析失败,订单号: %s, 金额: %s, 错误: %v", sourceOrderCode, payAmount, parseAmountErr)
|
||||
logx.Errorf("[云印签回调] 金额解析失败,订单号: %s, 金额字符串: %s, 错误: %v", sourceOrderCode, payAmount, parseAmountErr)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
logx.Infof("[云印签回调] 金额解析成功,回调金额: %.2f, 订单金额: %.2f", payAmountFloat, order.Amount)
|
||||
|
||||
// 金额比较(允许小数点误差)
|
||||
amountDiff := payAmountFloat - order.Amount
|
||||
if amountDiff < 0 {
|
||||
amountDiff = -amountDiff
|
||||
}
|
||||
logx.Infof("[云印签回调] 金额差异: %.2f", amountDiff)
|
||||
if amountDiff > 0.01 { // 允许1分钱的误差
|
||||
logx.Errorf("云印签回调,金额不一致,订单号: %s, 订单金额: %.2f, 回调金额: %.2f", sourceOrderCode, order.Amount, payAmountFloat)
|
||||
logx.Errorf("[云印签回调] 金额不一致,订单号: %s, 订单金额: %.2f, 回调金额: %.2f, 差异: %.2f",
|
||||
sourceOrderCode, order.Amount, payAmountFloat, amountDiff)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
logx.Infof("[云印签回调] 金额验证通过,差异在允许范围内")
|
||||
|
||||
// 解析支付成功时间
|
||||
logx.Infof("[云印签回调] 开始解析支付成功时间,时间字符串: %s", paySuccessTime)
|
||||
var payTime time.Time
|
||||
if paySuccessTime != "" {
|
||||
// 尝试多种时间格式
|
||||
@@ -130,61 +162,87 @@ func (l *YunYinSignPayCallbackLogic) YunYinSignPayCallback(w http.ResponseWriter
|
||||
"2006-01-02T15:04:05.000Z",
|
||||
}
|
||||
parsed := false
|
||||
for _, format := range timeFormats {
|
||||
for i, format := range timeFormats {
|
||||
if t, err := time.Parse(format, paySuccessTime); err == nil {
|
||||
payTime = t
|
||||
parsed = true
|
||||
logx.Infof("[云印签回调] 支付时间解析成功,使用格式[%d]: %s, 解析结果: %s", i, format, payTime.Format("2006-01-02 15:04:05"))
|
||||
break
|
||||
}
|
||||
}
|
||||
if !parsed {
|
||||
// 如果解析失败,使用当前时间
|
||||
payTime = time.Now()
|
||||
logx.Infof("云印签回调,支付时间解析失败,使用当前时间,订单号: %s, 时间字符串: %s", sourceOrderCode, paySuccessTime)
|
||||
logx.Infof("[云印签回调] 支付时间解析失败,使用当前时间,订单号: %s, 时间字符串: %s, 当前时间: %s",
|
||||
sourceOrderCode, paySuccessTime, payTime.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
} else {
|
||||
payTime = time.Now()
|
||||
logx.Infof("[云印签回调] 支付时间字符串为空,使用当前时间: %s", payTime.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
|
||||
// 更新订单状态
|
||||
logx.Infof("[云印签回调] 开始更新订单状态,订单号: %s, 订单ID: %s, 原状态: %s, 新状态: paid, 支付时间: %s, 渠道单号: %s",
|
||||
sourceOrderCode, order.Id, order.Status, payTime.Format("2006-01-02 15:04:05"), channelOrderNo)
|
||||
order.Status = "paid"
|
||||
order.PayTime = lzUtils.TimeToNullTime(payTime)
|
||||
if channelOrderNo != "" {
|
||||
order.PlatformOrderId = lzUtils.StringToNullString(channelOrderNo)
|
||||
logx.Infof("[云印签回调] 设置渠道单号,订单号: %s, 渠道单号: %s", sourceOrderCode, channelOrderNo)
|
||||
}
|
||||
|
||||
logx.Infof("[云印签回调] 调用UpdateWithVersion更新订单,订单号: %s, 订单ID: %s, Version: %d",
|
||||
sourceOrderCode, order.Id, order.Version)
|
||||
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
|
||||
logx.Errorf("云印签回调,更新订单状态失败,订单号: %s, 错误: %v", sourceOrderCode, updateErr)
|
||||
logx.Errorf("[云印签回调] 更新订单状态失败,订单号: %s, 订单ID: %s, 错误: %v", sourceOrderCode, order.Id, updateErr)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
logx.Infof("[云印签回调] 订单状态更新成功,订单号: %s, 订单ID: %s", sourceOrderCode, order.Id)
|
||||
|
||||
// 更新云印签订单表的支付状态
|
||||
logx.Infof("[云印签回调] 开始查找云印签订单记录,订单ID: %s, 流程ID: %s", order.Id, flowId)
|
||||
yunyinOrder, findYunyinErr := l.svcCtx.YunyinSignPayOrderModel.FindOneByOrderId(l.ctx, order.Id)
|
||||
if findYunyinErr == nil && yunyinOrder != nil {
|
||||
logx.Infof("[云印签回调] 找到云印签订单记录,订单ID: %s, 云印签订单ID: %s, 任务ID: %s, 参与者ID: %s, 当前支付状态: %d",
|
||||
order.Id, yunyinOrder.Id, yunyinOrder.TaskId, yunyinOrder.ParticipantId, yunyinOrder.PayStatus)
|
||||
// 更新支付状态为已支付(1)
|
||||
if yunyinOrder.PayStatus != 1 {
|
||||
logx.Infof("[云印签回调] 需要更新云印签订单支付状态,订单ID: %s, 原支付状态: %d, 新支付状态: 1", order.Id, yunyinOrder.PayStatus)
|
||||
yunyinOrder.PayStatus = 1
|
||||
if _, updateYunyinErr := l.svcCtx.YunyinSignPayOrderModel.Update(l.ctx, nil, yunyinOrder); updateYunyinErr != nil {
|
||||
logx.Errorf("云印签回调,更新云印签订单支付状态失败,订单ID: %s, 错误: %v", order.Id, updateYunyinErr)
|
||||
logx.Errorf("[云印签回调] 更新云印签订单支付状态失败,订单ID: %s, 云印签订单ID: %s, 错误: %v",
|
||||
order.Id, yunyinOrder.Id, updateYunyinErr)
|
||||
} else {
|
||||
logx.Infof("云印签回调,成功更新云印签订单支付状态,订单ID: %s", order.Id)
|
||||
logx.Infof("[云印签回调] 成功更新云印签订单支付状态,订单ID: %s, 云印签订单ID: %s, 支付状态: 1",
|
||||
order.Id, yunyinOrder.Id)
|
||||
}
|
||||
} else {
|
||||
logx.Infof("[云印签回调] 云印签订单支付状态已是已支付,无需更新,订单ID: %s, 支付状态: %d", order.Id, yunyinOrder.PayStatus)
|
||||
}
|
||||
} else {
|
||||
logx.Infof("云印签回调,未找到对应的云印签订单记录,订单ID: %s, 流程ID: %s", order.Id, flowId)
|
||||
if findYunyinErr != nil {
|
||||
logx.Infof("[云印签回调] 查找云印签订单记录失败,订单ID: %s, 流程ID: %s, 错误: %v", order.Id, flowId, findYunyinErr)
|
||||
} else {
|
||||
logx.Infof("[云印签回调] 未找到对应的云印签订单记录,订单ID: %s, 流程ID: %s", order.Id, flowId)
|
||||
}
|
||||
}
|
||||
|
||||
// 发送异步任务处理后续流程
|
||||
logx.Infof("[云印签回调] 开始发送异步任务,订单ID: %s", order.Id)
|
||||
if asyncErr := l.svcCtx.AsynqService.SendQueryTask(order.Id); asyncErr != nil {
|
||||
logx.Errorf("云印签回调,异步任务调度失败,订单ID: %s, 错误: %v", order.Id, asyncErr)
|
||||
logx.Errorf("[云印签回调] 异步任务调度失败,订单ID: %s, 错误: %v", order.Id, asyncErr)
|
||||
// 不返回错误,因为订单已经更新成功
|
||||
} else {
|
||||
logx.Infof("[云印签回调] 异步任务发送成功,订单ID: %s", order.Id)
|
||||
}
|
||||
|
||||
logx.Infof("云印签回调处理成功,订单号: %s, 支付单号: %s, 渠道单号: %s", sourceOrderCode, payOrderNo, channelOrderNo)
|
||||
logx.Infof("[云印签回调] 回调处理完成,订单号: %s, 订单ID: %s, 支付单号: %s, 渠道单号: %s, 支付金额: %.2f",
|
||||
sourceOrderCode, order.Id, payOrderNo, channelOrderNo, payAmountFloat)
|
||||
|
||||
// 返回 success,云印签要求返回纯字符串 success
|
||||
logx.Infof("[云印签回调] 返回success响应给云印签")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
|
||||
@@ -61,15 +61,20 @@ type AccessTokenData struct {
|
||||
|
||||
// GetAccessToken 获取访问token(带缓存)
|
||||
func (y *YunYinSignPayService) GetAccessToken(ctx context.Context) (string, error) {
|
||||
logx.Infof("[云印签API] GetAccessToken: 开始获取AccessToken,Redis Key: %s", YunYinSignPayTokenKey)
|
||||
|
||||
// 先从Redis获取
|
||||
token, err := y.redis.GetCtx(ctx, YunYinSignPayTokenKey)
|
||||
if err == nil && token != "" {
|
||||
logx.Infof("从Redis获取云印签token成功")
|
||||
logx.Infof("[云印签API] GetAccessToken: 从Redis获取token成功,Token长度: %d", len(token))
|
||||
return token, nil
|
||||
}
|
||||
if err != nil {
|
||||
logx.Infof("[云印签API] GetAccessToken: Redis查询失败或token不存在,错误: %v,将从API获取", err)
|
||||
}
|
||||
|
||||
// Redis中没有,调用API获取
|
||||
logx.Infof("从API获取云印签token")
|
||||
logx.Infof("[云印签API] GetAccessToken: 从API获取token,API地址: %s/service/getAccessToken, AppID: %s", y.config.ApiURL, y.config.AppID)
|
||||
reqBody := GetAccessTokenRequest{
|
||||
AppID: y.config.AppID,
|
||||
AppSecret: y.config.AppSecret,
|
||||
@@ -77,51 +82,68 @@ func (y *YunYinSignPayService) GetAccessToken(ctx context.Context) (string, erro
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetAccessToken: 序列化请求参数失败: %v", err)
|
||||
return "", fmt.Errorf("序列化请求参数失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] GetAccessToken: 请求体: %s", string(jsonData))
|
||||
|
||||
url := fmt.Sprintf("%s/service/getAccessToken", y.config.ApiURL)
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetAccessToken: 创建请求失败: %v", err)
|
||||
return "", fmt.Errorf("创建请求失败: %v", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
logx.Infof("[云印签API] GetAccessToken: 发送请求,URL: %s, Method: POST", url)
|
||||
|
||||
resp, err := y.client.Do(req)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetAccessToken: HTTP请求失败: %v", err)
|
||||
return "", fmt.Errorf("请求失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
logx.Infof("[云印签API] GetAccessToken: 收到响应,状态码: %d", resp.StatusCode)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetAccessToken: 读取响应体失败: %v", err)
|
||||
return "", fmt.Errorf("读取响应失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] GetAccessToken: 响应体: %s", string(body))
|
||||
|
||||
var tokenResp GetAccessTokenResponse
|
||||
if err := json.Unmarshal(body, &tokenResp); err != nil {
|
||||
logx.Errorf("[云印签API] GetAccessToken: 解析响应失败: %v, 响应内容: %s", err, string(body))
|
||||
return "", fmt.Errorf("解析响应失败: %v, 响应内容: %s", err, string(body))
|
||||
}
|
||||
|
||||
logx.Infof("[云印签API] GetAccessToken: 响应解析成功,Code: %v, Msg: %s", tokenResp.Code, tokenResp.Msg)
|
||||
|
||||
if tokenResp.Code != 0 {
|
||||
logx.Errorf("[云印签API] GetAccessToken: API返回错误,Code: %v, Msg: %s", tokenResp.Code, tokenResp.Msg)
|
||||
return "", fmt.Errorf("获取token失败: %s", tokenResp.Msg)
|
||||
}
|
||||
|
||||
if tokenResp.Data == nil || tokenResp.Data.AccessToken == "" {
|
||||
logx.Errorf("[云印签API] GetAccessToken: token数据为空,响应: %+v", tokenResp)
|
||||
return "", fmt.Errorf("token数据为空")
|
||||
}
|
||||
|
||||
accessToken := tokenResp.Data.AccessToken
|
||||
logx.Infof("[云印签API] GetAccessToken: 获取token成功,Token长度: %d, ExpireTime: %d", len(accessToken), tokenResp.Data.ExpireTime)
|
||||
|
||||
// 存储到Redis,2小时过期
|
||||
err = y.redis.SetexCtx(ctx, YunYinSignPayTokenKey, accessToken, int(TokenCacheExpire.Seconds()))
|
||||
if err != nil {
|
||||
logx.Errorf("存储token到Redis失败: %v", err)
|
||||
logx.Errorf("[云印签API] GetAccessToken: 存储token到Redis失败: %v", err)
|
||||
// 不返回错误,因为token已经获取成功
|
||||
} else {
|
||||
logx.Infof("[云印签API] GetAccessToken: token已缓存到Redis,过期时间: %d秒", int(TokenCacheExpire.Seconds()))
|
||||
}
|
||||
|
||||
logx.Infof("获取云印签token成功,已缓存到Redis")
|
||||
logx.Infof("[云印签API] GetAccessToken: 完成,返回token")
|
||||
return accessToken, nil
|
||||
}
|
||||
|
||||
@@ -144,69 +166,91 @@ type UserIdData struct {
|
||||
|
||||
// GetUserId 获取操作ID(带缓存)
|
||||
func (y *YunYinSignPayService) GetUserId(ctx context.Context, accessToken string) (string, error) {
|
||||
logx.Infof("[云印签API] GetUserId: 开始获取操作ID,Redis Key: %s, Mobile: %s", YunYinSignPayUserIdKey, y.config.Mobile)
|
||||
|
||||
// 先从Redis获取
|
||||
userId, err := y.redis.GetCtx(ctx, YunYinSignPayUserIdKey)
|
||||
if err == nil && userId != "" {
|
||||
logx.Infof("从Redis获取云印签操作ID成功")
|
||||
logx.Infof("[云印签API] GetUserId: 从Redis获取操作ID成功,UserID: %s", userId)
|
||||
return userId, nil
|
||||
}
|
||||
if err != nil {
|
||||
logx.Infof("[云印签API] GetUserId: Redis查询失败或操作ID不存在,错误: %v,将从API获取", err)
|
||||
}
|
||||
|
||||
// Redis中没有,调用API获取
|
||||
logx.Infof("从API获取云印签操作ID")
|
||||
logx.Infof("[云印签API] GetUserId: 从API获取操作ID,API地址: %s/member/infoByCorpId, Mobile: %s", y.config.ApiURL, y.config.Mobile)
|
||||
reqBody := GetUserIdRequest{
|
||||
Mobile: y.config.Mobile,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetUserId: 序列化请求参数失败: %v", err)
|
||||
return "", fmt.Errorf("序列化请求参数失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] GetUserId: 请求体: %s", string(jsonData))
|
||||
|
||||
url := fmt.Sprintf("%s/member/infoByCorpId", y.config.ApiURL)
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetUserId: 创建请求失败: %v", err)
|
||||
return "", fmt.Errorf("创建请求失败: %v", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("appId", y.config.AppID)
|
||||
req.Header.Set("accessToken", accessToken)
|
||||
logx.Infof("[云印签API] GetUserId: 发送请求,URL: %s, Method: POST, AppID: %s, AccessToken长度: %d", url, y.config.AppID, len(accessToken))
|
||||
|
||||
resp, err := y.client.Do(req)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetUserId: HTTP请求失败: %v", err)
|
||||
return "", fmt.Errorf("请求失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
logx.Infof("[云印签API] GetUserId: 收到响应,状态码: %d", resp.StatusCode)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetUserId: 读取响应体失败: %v", err)
|
||||
return "", fmt.Errorf("读取响应失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] GetUserId: 响应体: %s", string(body))
|
||||
|
||||
var userIdResp GetUserIdResponse
|
||||
if err := json.Unmarshal(body, &userIdResp); err != nil {
|
||||
logx.Errorf("[云印签API] GetUserId: 解析响应失败: %v, 响应内容: %s", err, string(body))
|
||||
return "", fmt.Errorf("解析响应失败: %v, 响应内容: %s", err, string(body))
|
||||
}
|
||||
|
||||
logx.Infof("[云印签API] GetUserId: 响应解析成功,Code: %v, Msg: %s", userIdResp.Code, userIdResp.Msg)
|
||||
|
||||
if userIdResp.Code != 0 {
|
||||
logx.Errorf("[云印签API] GetUserId: API返回错误,Code: %v, Msg: %s", userIdResp.Code, userIdResp.Msg)
|
||||
return "", fmt.Errorf("获取操作ID失败: %s", userIdResp.Msg)
|
||||
}
|
||||
|
||||
if userIdResp.Data == nil || userIdResp.Data.UserId == "" {
|
||||
logx.Errorf("[云印签API] GetUserId: 操作ID数据为空,响应: %+v", userIdResp)
|
||||
return "", fmt.Errorf("操作ID数据为空")
|
||||
}
|
||||
|
||||
// 获取操作ID
|
||||
operationUserId := userIdResp.Data.UserId
|
||||
logx.Infof("[云印签API] GetUserId: 获取操作ID成功,UserID: %s", operationUserId)
|
||||
|
||||
// 存储到Redis,2小时过期
|
||||
err = y.redis.SetexCtx(ctx, YunYinSignPayUserIdKey, operationUserId, int(UserIdCacheExpire.Seconds()))
|
||||
if err != nil {
|
||||
logx.Errorf("存储操作ID到Redis失败: %v", err)
|
||||
logx.Errorf("[云印签API] GetUserId: 存储操作ID到Redis失败: %v", err)
|
||||
// 不返回错误,因为操作ID已经获取成功
|
||||
} else {
|
||||
logx.Infof("[云印签API] GetUserId: 操作ID已缓存到Redis,过期时间: %d秒", int(UserIdCacheExpire.Seconds()))
|
||||
}
|
||||
|
||||
logx.Infof("获取云印签操作ID成功,已缓存到Redis")
|
||||
logx.Infof("[云印签API] GetUserId: 完成,返回操作ID: %s", operationUserId)
|
||||
return operationUserId, nil
|
||||
}
|
||||
|
||||
@@ -303,14 +347,28 @@ func (r *StartSignFlowResponse) GetCodeInt() int {
|
||||
|
||||
// StartSignFlow 发起签署流程
|
||||
func (y *YunYinSignPayService) StartSignFlow(ctx context.Context, accessToken, userId string, req *StartSignFlowRequest) (*StartSignFlowResult, error) {
|
||||
logx.Infof("[云印签API] StartSignFlow: 开始发起签署流程,订单号: %s, 模板代码: %s, 模板名称: %s, 用户ID: %s",
|
||||
req.SourceOrderCode, req.TemplateCode, req.TemplateName, userId)
|
||||
logx.Infof("[云印签API] StartSignFlow: 参与者列表,数量: %d", len(req.ParticipantList))
|
||||
for i, p := range req.ParticipantList {
|
||||
logx.Infof("[云印签API] StartSignFlow: 参与者[%d],标志: %s, 账号: %s, 姓名: %s, 类型: %d, 企业名: %s",
|
||||
i, p.ParticipantFlag, p.PsnAccount, p.PsnName, p.ParticipantType, p.ParticipantCorpName)
|
||||
if p.Payee != nil {
|
||||
logx.Infof("[云印签API] StartSignFlow: 参与者[%d]收款信息,金额: %.2f, 优先级: %s", i, p.Payee.Amount, p.Payee.Priority)
|
||||
}
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] StartSignFlow: 序列化请求参数失败: %v", err)
|
||||
return nil, fmt.Errorf("序列化请求参数失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] StartSignFlow: 请求体: %s", string(jsonData))
|
||||
|
||||
url := fmt.Sprintf("%s/signTask/startSignFlow", y.config.ApiURL)
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] StartSignFlow: 创建请求失败: %v", err)
|
||||
return nil, fmt.Errorf("创建请求失败: %v", err)
|
||||
}
|
||||
|
||||
@@ -319,47 +377,67 @@ func (y *YunYinSignPayService) StartSignFlow(ctx context.Context, accessToken, u
|
||||
httpReq.Header.Set("accessToken", accessToken)
|
||||
httpReq.Header.Set("userId", userId)
|
||||
httpReq.Header.Set("source", "pc")
|
||||
logx.Infof("[云印签API] StartSignFlow: 发送请求,URL: %s, Method: POST, AppID: %s, UserID: %s, AccessToken长度: %d",
|
||||
url, y.config.AppID, userId, len(accessToken))
|
||||
|
||||
resp, err := y.client.Do(httpReq)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] StartSignFlow: HTTP请求失败: %v", err)
|
||||
return nil, fmt.Errorf("请求失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
logx.Infof("[云印签API] StartSignFlow: 收到响应,状态码: %d", resp.StatusCode)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] StartSignFlow: 读取响应体失败: %v", err)
|
||||
return nil, fmt.Errorf("读取响应失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] StartSignFlow: 响应体: %s", string(body))
|
||||
|
||||
var signFlowResp StartSignFlowResponse
|
||||
if err := json.Unmarshal(body, &signFlowResp); err != nil {
|
||||
logx.Errorf("[云印签API] StartSignFlow: 解析响应失败: %v, 响应内容: %s", err, string(body))
|
||||
return nil, fmt.Errorf("解析响应失败: %v, 响应内容: %s", err, string(body))
|
||||
}
|
||||
|
||||
logx.Infof("[云印签API] StartSignFlow: 响应解析成功,Code: %v, Msg: %s", signFlowResp.Code, signFlowResp.Msg)
|
||||
|
||||
// 检查响应码(可能是字符串"200"或数字200)
|
||||
codeInt := signFlowResp.GetCodeInt()
|
||||
if codeInt != 200 {
|
||||
logx.Errorf("[云印签API] StartSignFlow: API返回错误,Code: %v, Msg: %s", signFlowResp.Code, signFlowResp.Msg)
|
||||
return nil, fmt.Errorf("发起签署失败: %s", signFlowResp.Msg)
|
||||
}
|
||||
|
||||
if signFlowResp.Data == nil {
|
||||
logx.Errorf("[云印签API] StartSignFlow: 签署数据为空")
|
||||
return nil, fmt.Errorf("签署数据为空")
|
||||
}
|
||||
|
||||
logx.Infof("[云印签API] StartSignFlow: 签署数据,流程ID: %s, 流程状态: %d, 流程描述: %s, 参与者数量: %d",
|
||||
signFlowResp.Data.FlowID, signFlowResp.Data.FlowStatus, signFlowResp.Data.FlowDesc, len(signFlowResp.Data.ParticipantList))
|
||||
|
||||
// 从 participantList 中找到签署方2的 participantId
|
||||
var participantID2 string
|
||||
for _, participant := range signFlowResp.Data.ParticipantList {
|
||||
for i, participant := range signFlowResp.Data.ParticipantList {
|
||||
logx.Infof("[云印签API] StartSignFlow: 响应参与者[%d],标志: %s, 参与者ID: %s, 类型: %d",
|
||||
i, participant.ParticipantFlag, participant.ParticipantID, participant.ParticipantType)
|
||||
if participant.ParticipantFlag == "签署方2" {
|
||||
participantID2 = participant.ParticipantID
|
||||
logx.Infof("[云印签API] StartSignFlow: 找到签署方2,参与者ID: %s", participantID2)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if participantID2 == "" {
|
||||
logx.Errorf("[云印签API] StartSignFlow: 未找到签署方2的参与者ID,参与者列表: %+v", signFlowResp.Data.ParticipantList)
|
||||
return nil, fmt.Errorf("未找到签署方2的参与者ID")
|
||||
}
|
||||
|
||||
logx.Infof("发起云印签签署流程成功,订单号: %s, 流程ID: %s, 签署方2参与者ID: %s", req.SourceOrderCode, signFlowResp.Data.FlowID, participantID2)
|
||||
logx.Infof("[云印签API] StartSignFlow: 发起签署流程成功,订单号: %s, 流程ID: %s, 签署方2参与者ID: %s",
|
||||
req.SourceOrderCode, signFlowResp.Data.FlowID, participantID2)
|
||||
|
||||
// 返回结果,包含签署方2的参与者ID和流程ID
|
||||
return &StartSignFlowResult{
|
||||
@@ -389,6 +467,9 @@ type PaymentURLData struct {
|
||||
|
||||
// GetPaymentURL 获取支付链接
|
||||
func (y *YunYinSignPayService) GetPaymentURL(ctx context.Context, accessToken, userId string, participantID string, payType int) (string, error) {
|
||||
logx.Infof("[云印签API] GetPaymentURL: 开始获取支付链接,参与者ID: %s, 支付类型: %d(0=微信,1=支付宝), 用户ID: %s",
|
||||
participantID, payType, userId)
|
||||
|
||||
reqBody := GetPaymentURLRequest{
|
||||
ParticipantID: participantID,
|
||||
Type: payType,
|
||||
@@ -396,12 +477,15 @@ func (y *YunYinSignPayService) GetPaymentURL(ctx context.Context, accessToken, u
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetPaymentURL: 序列化请求参数失败: %v", err)
|
||||
return "", fmt.Errorf("序列化请求参数失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] GetPaymentURL: 请求体: %s", string(jsonData))
|
||||
|
||||
url := fmt.Sprintf("%s/signTask/contract/appletForParticipant", y.config.ApiURL)
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetPaymentURL: 创建请求失败: %v", err)
|
||||
return "", fmt.Errorf("创建请求失败: %v", err)
|
||||
}
|
||||
|
||||
@@ -410,28 +494,40 @@ func (y *YunYinSignPayService) GetPaymentURL(ctx context.Context, accessToken, u
|
||||
httpReq.Header.Set("accessToken", accessToken)
|
||||
httpReq.Header.Set("userId", userId)
|
||||
httpReq.Header.Set("source", "pc")
|
||||
logx.Infof("[云印签API] GetPaymentURL: 发送请求,URL: %s, Method: POST, AppID: %s, UserID: %s, AccessToken长度: %d",
|
||||
url, y.config.AppID, userId, len(accessToken))
|
||||
|
||||
resp, err := y.client.Do(httpReq)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetPaymentURL: HTTP请求失败: %v", err)
|
||||
return "", fmt.Errorf("请求失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
logx.Infof("[云印签API] GetPaymentURL: 收到响应,状态码: %d", resp.StatusCode)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] GetPaymentURL: 读取响应体失败: %v", err)
|
||||
return "", fmt.Errorf("读取响应失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] GetPaymentURL: 响应体: %s", string(body))
|
||||
|
||||
var paymentURLResp GetPaymentURLResponse
|
||||
if err := json.Unmarshal(body, &paymentURLResp); err != nil {
|
||||
logx.Errorf("[云印签API] GetPaymentURL: 解析响应失败: %v, 响应内容: %s", err, string(body))
|
||||
return "", fmt.Errorf("解析响应失败: %v, 响应内容: %s", err, string(body))
|
||||
}
|
||||
|
||||
logx.Infof("[云印签API] GetPaymentURL: 响应解析成功,Code: %d, Msg: %s", paymentURLResp.Code, paymentURLResp.Msg)
|
||||
|
||||
if paymentURLResp.Code != 0 {
|
||||
logx.Errorf("[云印签API] GetPaymentURL: API返回错误,Code: %d, Msg: %s", paymentURLResp.Code, paymentURLResp.Msg)
|
||||
return "", fmt.Errorf("获取支付链接失败: %s", paymentURLResp.Msg)
|
||||
}
|
||||
|
||||
if paymentURLResp.Data == nil {
|
||||
logx.Errorf("[云印签API] GetPaymentURL: 支付链接数据为空")
|
||||
return "", fmt.Errorf("支付链接数据为空")
|
||||
}
|
||||
|
||||
@@ -439,13 +535,15 @@ func (y *YunYinSignPayService) GetPaymentURL(ctx context.Context, accessToken, u
|
||||
payURL := paymentURLResp.Data.PayURL
|
||||
if payURL == "" {
|
||||
payURL = paymentURLResp.Data.URL
|
||||
logx.Infof("[云印签API] GetPaymentURL: PayURL为空,使用URL字段: %s", payURL)
|
||||
}
|
||||
|
||||
if payURL == "" {
|
||||
logx.Errorf("[云印签API] GetPaymentURL: 支付链接为空,响应数据: %+v", paymentURLResp.Data)
|
||||
return "", fmt.Errorf("支付链接为空")
|
||||
}
|
||||
|
||||
logx.Infof("获取云印签支付链接成功,参与者ID: %s", participantID)
|
||||
logx.Infof("[云印签API] GetPaymentURL: 获取支付链接成功,参与者ID: %s, 支付链接: %s", participantID, payURL)
|
||||
return payURL, nil
|
||||
}
|
||||
|
||||
@@ -458,18 +556,28 @@ type CreateYunYinSignPayOrderResult struct {
|
||||
|
||||
// CreateYunYinSignPayOrder 创建云印签支付订单(封装完整流程)
|
||||
func (y *YunYinSignPayService) CreateYunYinSignPayOrder(ctx context.Context, userMobile, userName string, amount float64, outTradeNo string, payType int) (*CreateYunYinSignPayOrderResult, error) {
|
||||
logx.Infof("[云印签API] CreateYunYinSignPayOrder: 开始创建支付订单,订单号: %s, 用户手机: %s, 用户姓名: %s, 金额: %.2f, 支付类型: %d",
|
||||
outTradeNo, userMobile, userName, amount, payType)
|
||||
|
||||
// 1. 获取token和操作ID(带缓存)
|
||||
logx.Infof("[云印签API] CreateYunYinSignPayOrder: 步骤1-获取AccessToken,订单号: %s", outTradeNo)
|
||||
accessToken, err := y.GetAccessToken(ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] CreateYunYinSignPayOrder: 获取AccessToken失败,订单号: %s, 错误: %v", outTradeNo, err)
|
||||
return nil, fmt.Errorf("获取云印签token失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] CreateYunYinSignPayOrder: AccessToken获取成功,订单号: %s", outTradeNo)
|
||||
|
||||
logx.Infof("[云印签API] CreateYunYinSignPayOrder: 步骤2-获取操作ID,订单号: %s", outTradeNo)
|
||||
operationUserId, err := y.GetUserId(ctx, accessToken)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] CreateYunYinSignPayOrder: 获取操作ID失败,订单号: %s, 错误: %v", outTradeNo, err)
|
||||
return nil, fmt.Errorf("获取云印签操作ID失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] CreateYunYinSignPayOrder: 操作ID获取成功,订单号: %s, 操作ID: %s", outTradeNo, operationUserId)
|
||||
|
||||
// 2. 构建参与者列表
|
||||
logx.Infof("[云印签API] CreateYunYinSignPayOrder: 步骤3-构建参与者列表,订单号: %s", outTradeNo)
|
||||
participantList := []ParticipantInfo{
|
||||
// 签署方1:我方
|
||||
{
|
||||
@@ -497,8 +605,11 @@ func (y *YunYinSignPayService) CreateYunYinSignPayOrder(ctx context.Context, use
|
||||
},
|
||||
},
|
||||
}
|
||||
logx.Infof("[云印签API] CreateYunYinSignPayOrder: 参与者列表构建完成,订单号: %s, 签署方1: %s(%s), 签署方2: %s(%s), 金额: %.2f",
|
||||
outTradeNo, y.config.Name, y.config.Mobile, userName, userMobile, amount)
|
||||
|
||||
// 3. 发起签署流程
|
||||
logx.Infof("[云印签API] CreateYunYinSignPayOrder: 步骤4-发起签署流程,订单号: %s, 模板代码: %s", outTradeNo, y.config.TemplateCode)
|
||||
startSignFlowReq := &StartSignFlowRequest{
|
||||
TemplateCode: y.config.TemplateCode,
|
||||
TemplateName: y.config.TemplateName,
|
||||
@@ -510,24 +621,35 @@ func (y *YunYinSignPayService) CreateYunYinSignPayOrder(ctx context.Context, use
|
||||
|
||||
signFlowData, err := y.StartSignFlow(ctx, accessToken, operationUserId, startSignFlowReq)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] CreateYunYinSignPayOrder: 发起签署流程失败,订单号: %s, 错误: %v", outTradeNo, err)
|
||||
return nil, fmt.Errorf("发起云印签签署失败: %v", err)
|
||||
}
|
||||
|
||||
if signFlowData.ParticipantID == "" {
|
||||
logx.Errorf("[云印签API] CreateYunYinSignPayOrder: 签署流程返回的参与者ID为空,订单号: %s", outTradeNo)
|
||||
return nil, fmt.Errorf("签署流程返回的参与者ID为空")
|
||||
}
|
||||
logx.Infof("[云印签API] CreateYunYinSignPayOrder: 签署流程发起成功,订单号: %s, 任务ID: %s, 参与者ID: %s",
|
||||
outTradeNo, signFlowData.TaskID, signFlowData.ParticipantID)
|
||||
|
||||
// 4. 获取支付链接
|
||||
logx.Infof("[云印签API] CreateYunYinSignPayOrder: 步骤5-获取支付链接,订单号: %s, 参与者ID: %s, 支付类型: %d",
|
||||
outTradeNo, signFlowData.ParticipantID, payType)
|
||||
payURL, err := y.GetPaymentURL(ctx, accessToken, operationUserId, signFlowData.ParticipantID, payType)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] CreateYunYinSignPayOrder: 获取支付链接失败,订单号: %s, 错误: %v", outTradeNo, err)
|
||||
return nil, fmt.Errorf("获取云印签支付链接失败: %v", err)
|
||||
}
|
||||
|
||||
return &CreateYunYinSignPayOrderResult{
|
||||
result := &CreateYunYinSignPayOrderResult{
|
||||
PayURL: payURL,
|
||||
ParticipantID: signFlowData.ParticipantID,
|
||||
TaskID: signFlowData.TaskID,
|
||||
}, nil
|
||||
}
|
||||
logx.Infof("[云印签API] CreateYunYinSignPayOrder: 创建支付订单完成,订单号: %s, 任务ID: %s, 参与者ID: %s, 支付链接: %s",
|
||||
outTradeNo, result.TaskID, result.ParticipantID, result.PayURL)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// QueryPayeeBillRequest 查询收款单请求
|
||||
@@ -607,18 +729,25 @@ func (r *QueryPayeeBillResponse) GetCodeInt() int {
|
||||
|
||||
// QueryPayeeBill 查询收款单(根据sourceOrderCode查询)
|
||||
func (y *YunYinSignPayService) QueryPayeeBill(ctx context.Context, sourceOrderCode string) (*QueryPayeeBillResult, error) {
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 开始查询收款单,订单号: %s", sourceOrderCode)
|
||||
|
||||
// 1. 获取token和操作ID(带缓存)
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 步骤1-获取AccessToken,订单号: %s", sourceOrderCode)
|
||||
accessToken, err := y.GetAccessToken(ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] QueryPayeeBill: 获取AccessToken失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return nil, fmt.Errorf("获取云印签token失败: %v", err)
|
||||
}
|
||||
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 步骤2-获取操作ID,订单号: %s", sourceOrderCode)
|
||||
operationUserId, err := y.GetUserId(ctx, accessToken)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] QueryPayeeBill: 获取操作ID失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return nil, fmt.Errorf("获取云印签操作ID失败: %v", err)
|
||||
}
|
||||
|
||||
// 2. 构建查询请求
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 步骤3-构建查询请求,订单号: %s", sourceOrderCode)
|
||||
reqBody := QueryPayeeBillRequest{
|
||||
SourceOrderCode: sourceOrderCode,
|
||||
ListPageNo: 1,
|
||||
@@ -627,13 +756,16 @@ func (y *YunYinSignPayService) QueryPayeeBill(ctx context.Context, sourceOrderCo
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] QueryPayeeBill: 序列化请求参数失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return nil, fmt.Errorf("序列化请求参数失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 请求体: %s", string(jsonData))
|
||||
|
||||
// 3. 调用查询API
|
||||
url := fmt.Sprintf("%s/signFlowBill/payeeBillList", y.config.ApiURL)
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] QueryPayeeBill: 创建请求失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return nil, fmt.Errorf("创建请求失败: %v", err)
|
||||
}
|
||||
|
||||
@@ -642,46 +774,61 @@ func (y *YunYinSignPayService) QueryPayeeBill(ctx context.Context, sourceOrderCo
|
||||
httpReq.Header.Set("accessToken", accessToken)
|
||||
httpReq.Header.Set("userId", operationUserId)
|
||||
httpReq.Header.Set("source", "pc")
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 发送请求,URL: %s, Method: POST, 订单号: %s", url, sourceOrderCode)
|
||||
|
||||
resp, err := y.client.Do(httpReq)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] QueryPayeeBill: HTTP请求失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return nil, fmt.Errorf("请求失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 收到响应,订单号: %s, 状态码: %d", sourceOrderCode, resp.StatusCode)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] QueryPayeeBill: 读取响应体失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return nil, fmt.Errorf("读取响应失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 响应体,订单号: %s, 内容: %s", sourceOrderCode, string(body))
|
||||
|
||||
var queryResp QueryPayeeBillResponse
|
||||
if err := json.Unmarshal(body, &queryResp); err != nil {
|
||||
logx.Errorf("[云印签API] QueryPayeeBill: 解析响应失败,订单号: %s, 错误: %v, 响应内容: %s", sourceOrderCode, err, string(body))
|
||||
return nil, fmt.Errorf("解析响应失败: %v, 响应内容: %s", err, string(body))
|
||||
}
|
||||
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 响应解析成功,订单号: %s, Code: %v, Msg: %s, 总记录数: %d, 总页数: %d",
|
||||
sourceOrderCode, queryResp.Code, queryResp.Msg, queryResp.TotalCount, queryResp.ListPageCount)
|
||||
|
||||
// 4. 检查响应码
|
||||
codeInt := queryResp.GetCodeInt()
|
||||
if codeInt != 200 {
|
||||
logx.Errorf("[云印签API] QueryPayeeBill: API返回错误,订单号: %s, Code: %v, Msg: %s", sourceOrderCode, queryResp.Code, queryResp.Msg)
|
||||
return nil, fmt.Errorf("查询收款单失败: %s", queryResp.Msg)
|
||||
}
|
||||
|
||||
// 5. 查找匹配的记录(根据sourceOrderCode)
|
||||
if len(queryResp.Data) == 0 {
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 未找到匹配的收款单记录,订单号: %s", sourceOrderCode)
|
||||
return nil, fmt.Errorf("未找到匹配的收款单记录")
|
||||
}
|
||||
|
||||
// 取第一条记录(因为我们已经用sourceOrderCode精确查询)
|
||||
billItem := queryResp.Data[0]
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 找到收款单记录,订单号: %s, 支付单号: %s, 支付状态: %d, 金额: %.2f, 退款金额: %.2f, 渠道单号: %s, 参与方ID: %d",
|
||||
sourceOrderCode, billItem.PayOrderCode, billItem.PayStatus, billItem.Amount, billItem.RefundAmount, billItem.ChannelOrderNo, billItem.ParticipateID)
|
||||
|
||||
logx.Infof("查询云印签收款单成功,订单号: %s, 支付状态: %d", sourceOrderCode, billItem.PayStatus)
|
||||
|
||||
return &QueryPayeeBillResult{
|
||||
result := &QueryPayeeBillResult{
|
||||
PayStatus: billItem.PayStatus,
|
||||
PayOrderCode: billItem.PayOrderCode,
|
||||
ChannelOrderNo: billItem.ChannelOrderNo,
|
||||
Amount: billItem.Amount,
|
||||
RefundAmount: billItem.RefundAmount,
|
||||
}, nil
|
||||
}
|
||||
logx.Infof("[云印签API] QueryPayeeBill: 查询收款单完成,订单号: %s, 支付状态: %d", sourceOrderCode, result.PayStatus)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// RefundPayeeBillRequest 退款请求
|
||||
@@ -718,19 +865,27 @@ func (r *RefundPayeeBillResponse) GetCodeInt() int {
|
||||
|
||||
// RefundPayeeBill 发起退款
|
||||
func (y *YunYinSignPayService) RefundPayeeBill(ctx context.Context, sourceOrderCode string, participateId int64, refundAmount float64, refundReason string) error {
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 开始发起退款,订单号: %s, 参与方ID: %d, 退款金额: %.2f, 退款原因: %s",
|
||||
sourceOrderCode, participateId, refundAmount, refundReason)
|
||||
|
||||
// 1. 获取token和操作ID(带缓存)
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 步骤1-获取AccessToken,订单号: %s", sourceOrderCode)
|
||||
accessToken, err := y.GetAccessToken(ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 获取AccessToken失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return fmt.Errorf("获取云印签token失败: %v", err)
|
||||
}
|
||||
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 步骤2-获取操作ID,订单号: %s", sourceOrderCode)
|
||||
operationUserId, err := y.GetUserId(ctx, accessToken)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 获取操作ID失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return fmt.Errorf("获取云印签操作ID失败: %v", err)
|
||||
}
|
||||
|
||||
// 2. 如果只提供了 sourceOrderCode,需要先查询收款单获取 participateId
|
||||
if participateId == 0 && sourceOrderCode != "" {
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 参与方ID为空,通过订单号查询,订单号: %s", sourceOrderCode)
|
||||
// 查询收款单列表获取 participateId
|
||||
reqBody := QueryPayeeBillRequest{
|
||||
SourceOrderCode: sourceOrderCode,
|
||||
@@ -740,12 +895,15 @@ func (y *YunYinSignPayService) RefundPayeeBill(ctx context.Context, sourceOrderC
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 序列化查询请求失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return fmt.Errorf("序列化查询请求失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 查询请求体: %s", string(jsonData))
|
||||
|
||||
url := fmt.Sprintf("%s/signFlowBill/payeeBillList", y.config.ApiURL)
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 创建查询请求失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return fmt.Errorf("创建查询请求失败: %v", err)
|
||||
}
|
||||
|
||||
@@ -754,48 +912,65 @@ func (y *YunYinSignPayService) RefundPayeeBill(ctx context.Context, sourceOrderC
|
||||
httpReq.Header.Set("accessToken", accessToken)
|
||||
httpReq.Header.Set("userId", operationUserId)
|
||||
httpReq.Header.Set("source", "pc")
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 发送查询请求,URL: %s, 订单号: %s", url, sourceOrderCode)
|
||||
|
||||
resp, err := y.client.Do(httpReq)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 查询请求失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return fmt.Errorf("查询请求失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 收到查询响应,订单号: %s, 状态码: %d", sourceOrderCode, resp.StatusCode)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 读取查询响应失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return fmt.Errorf("读取查询响应失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 查询响应体,订单号: %s, 内容: %s", sourceOrderCode, string(body))
|
||||
|
||||
var queryResp QueryPayeeBillResponse
|
||||
if err := json.Unmarshal(body, &queryResp); err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 解析查询响应失败,订单号: %s, 错误: %v, 响应内容: %s", sourceOrderCode, err, string(body))
|
||||
return fmt.Errorf("解析查询响应失败: %v, 响应内容: %s", err, string(body))
|
||||
}
|
||||
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 查询响应解析成功,订单号: %s, Code: %v, Msg: %s, 记录数: %d",
|
||||
sourceOrderCode, queryResp.Code, queryResp.Msg, len(queryResp.Data))
|
||||
|
||||
codeInt := queryResp.GetCodeInt()
|
||||
if codeInt != 200 {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 查询收款单失败,订单号: %s, Code: %v, Msg: %s", sourceOrderCode, queryResp.Code, queryResp.Msg)
|
||||
return fmt.Errorf("查询收款单失败: %s", queryResp.Msg)
|
||||
}
|
||||
|
||||
if len(queryResp.Data) == 0 {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 未找到匹配的收款单记录,订单号: %s", sourceOrderCode)
|
||||
return fmt.Errorf("未找到匹配的收款单记录,订单号: %s", sourceOrderCode)
|
||||
}
|
||||
|
||||
// 获取第一条记录的 participateId
|
||||
billItem := queryResp.Data[0]
|
||||
participateId = billItem.ParticipateID
|
||||
logx.Infof("通过订单号查询到参与方ID: %d", participateId)
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 通过订单号查询到参与方ID,订单号: %s, 参与方ID: %d, 支付单号: %s",
|
||||
sourceOrderCode, participateId, billItem.PayOrderCode)
|
||||
}
|
||||
|
||||
// 3. 验证参数
|
||||
if participateId == 0 {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 参与方ID不能为空,订单号: %s", sourceOrderCode)
|
||||
return fmt.Errorf("参与方ID不能为空,请提供 participateId 或 sourceOrderCode")
|
||||
}
|
||||
|
||||
if refundAmount <= 0 {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 退款金额必须大于0,订单号: %s, 退款金额: %.2f", sourceOrderCode, refundAmount)
|
||||
return fmt.Errorf("退款金额必须大于0")
|
||||
}
|
||||
|
||||
// 4. 构建退款请求
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 步骤3-构建退款请求,订单号: %s, 参与方ID: %d, 退款金额: %.2f",
|
||||
sourceOrderCode, participateId, refundAmount)
|
||||
refundReq := RefundPayeeBillRequest{
|
||||
RefundAmount: refundAmount,
|
||||
RefundReason: refundReason,
|
||||
@@ -804,20 +979,25 @@ func (y *YunYinSignPayService) RefundPayeeBill(ctx context.Context, sourceOrderC
|
||||
// 优先使用 participateId
|
||||
if participateId > 0 {
|
||||
refundReq.ParticipateId = participateId
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 使用参与方ID,订单号: %s, 参与方ID: %d", sourceOrderCode, participateId)
|
||||
} else if sourceOrderCode != "" {
|
||||
// 如果 participateId 仍然为0,使用 sourceOrderCode
|
||||
refundReq.SourceOrderCode = sourceOrderCode
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 使用订单号,订单号: %s", sourceOrderCode)
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(refundReq)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 序列化退款请求失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return fmt.Errorf("序列化退款请求失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 退款请求体: %s", string(jsonData))
|
||||
|
||||
// 5. 调用退款API
|
||||
url := fmt.Sprintf("%s/signFlowBill/refund", y.config.ApiURL)
|
||||
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 创建退款请求失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return fmt.Errorf("创建退款请求失败: %v", err)
|
||||
}
|
||||
|
||||
@@ -826,29 +1006,41 @@ func (y *YunYinSignPayService) RefundPayeeBill(ctx context.Context, sourceOrderC
|
||||
httpReq.Header.Set("accessToken", accessToken)
|
||||
httpReq.Header.Set("userId", operationUserId)
|
||||
httpReq.Header.Set("source", "pc")
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 发送退款请求,URL: %s, Method: POST, 订单号: %s, 参与方ID: %d, 退款金额: %.2f",
|
||||
url, sourceOrderCode, participateId, refundAmount)
|
||||
|
||||
resp, err := y.client.Do(httpReq)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: HTTP请求失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return fmt.Errorf("退款请求失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 收到退款响应,订单号: %s, 状态码: %d", sourceOrderCode, resp.StatusCode)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 读取退款响应失败,订单号: %s, 错误: %v", sourceOrderCode, err)
|
||||
return fmt.Errorf("读取退款响应失败: %v", err)
|
||||
}
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 退款响应体,订单号: %s, 内容: %s", sourceOrderCode, string(body))
|
||||
|
||||
var refundResp RefundPayeeBillResponse
|
||||
if err := json.Unmarshal(body, &refundResp); err != nil {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: 解析退款响应失败,订单号: %s, 错误: %v, 响应内容: %s", sourceOrderCode, err, string(body))
|
||||
return fmt.Errorf("解析退款响应失败: %v, 响应内容: %s", err, string(body))
|
||||
}
|
||||
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 退款响应解析成功,订单号: %s, Code: %v, Msg: %s", sourceOrderCode, refundResp.Code, refundResp.Msg)
|
||||
|
||||
// 6. 检查响应码
|
||||
codeInt := refundResp.GetCodeInt()
|
||||
if codeInt != 200 {
|
||||
logx.Errorf("[云印签API] RefundPayeeBill: API返回错误,订单号: %s, Code: %v, Msg: %s", sourceOrderCode, refundResp.Code, refundResp.Msg)
|
||||
return fmt.Errorf("退款失败: %s", refundResp.Msg)
|
||||
}
|
||||
|
||||
logx.Infof("云印签退款成功,参与方ID: %d, 退款金额: %.2f, 退款原因: %s", participateId, refundAmount, refundReason)
|
||||
logx.Infof("[云印签API] RefundPayeeBill: 退款成功,订单号: %s, 参与方ID: %d, 退款金额: %.2f, 退款原因: %s",
|
||||
sourceOrderCode, participateId, refundAmount, refundReason)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user