feat: 微信h5支付回跳支付中心地址再跳转下游地址,收银台逻辑对应调整
This commit is contained in:
parent
b20417cb60
commit
79cc31ee71
|
@ -7,6 +7,7 @@ import (
|
|||
"PaymentCenter/app/http/entities"
|
||||
"PaymentCenter/app/http/entities/backend"
|
||||
"PaymentCenter/app/models/ordersmodel"
|
||||
"PaymentCenter/app/services/thirdpay"
|
||||
"PaymentCenter/app/services/thirdpay/thirdpay_notify"
|
||||
"PaymentCenter/app/third/paymentService"
|
||||
"PaymentCenter/app/third/paymentService/payCommon"
|
||||
|
@ -130,75 +131,84 @@ func queryOrder() {
|
|||
orderInfo := order[index]
|
||||
|
||||
// 发起查询上游支付
|
||||
go func(orderInfo ordersmodel.OrdersLeftPayChannelList) {
|
||||
go func(order ordersmodel.OrdersLeftPayChannelList) {
|
||||
defer func() {
|
||||
<-ch
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
query := paymentService.PayOrderQueryRequest{
|
||||
OrderId: orderInfo.Id,
|
||||
}
|
||||
switch utils.PayType(orderInfo.ChannelType) {
|
||||
case common.PAY_CHANNLE_TYPE_WECHAT:
|
||||
wx := backend.WechatPayChannel{}
|
||||
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &wx)
|
||||
query.PayChannel = payCommon.PAY_CHANNLE_TYPE_WECHAT
|
||||
query.Wx = paymentService.WxPay{
|
||||
AppId: orderInfo.AppId,
|
||||
MchId: wx.MchId,
|
||||
SerialNo: wx.SerialNo,
|
||||
ApiV3Key: wx.ApiV3Key,
|
||||
PrivateKey: wx.PrivateKey,
|
||||
}
|
||||
case common.PAY_CHANNLE_TYPE_ZFB:
|
||||
ali := backend.AliPayChannel{}
|
||||
query.PayChannel = payCommon.PAY_CHANNLE_TYPE_ZFB
|
||||
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &ali)
|
||||
query.Ali = paymentService.AliPay{
|
||||
AppId: orderInfo.AppId,
|
||||
PrivateKey: ali.PrivateKey,
|
||||
AppPublicCert: ali.AppPublicCert,
|
||||
AlipayRootCert: ali.AlipayRootCert,
|
||||
AlipayPublicCert: ali.AlipayPublicCert,
|
||||
}
|
||||
default:
|
||||
utils.Log(nil, "查询订单,支付渠道不支持", orderInfo.ChannelType)
|
||||
return
|
||||
}
|
||||
|
||||
// 发起查询
|
||||
result := paymentService.PayOrderQuery(ctx, query)
|
||||
utils.Log(nil, "主动查询订单支付状态,上游返回数据", result)
|
||||
// 查询成功,校验状态
|
||||
var status int
|
||||
if result.Code == payCommon.PAY_SUCCESS_CODE {
|
||||
var msg string
|
||||
switch result.Result.TradeState {
|
||||
case "SUCCESS":
|
||||
// 成功
|
||||
status = common.ORDER_STATUS_PAYED
|
||||
msg = "支付成功"
|
||||
case "REFUND":
|
||||
// 退款 订单支付完成才能退款,所以支付单的状态是支付完成
|
||||
status = common.ORDER_STATUS_PAYED
|
||||
msg = "已支付,发生退款"
|
||||
case "NOTPAY":
|
||||
// 未支付
|
||||
return
|
||||
case "CLOSED":
|
||||
// 关闭
|
||||
status = common.ORDER_STATUS_CLOSE
|
||||
msg = "订单关闭"
|
||||
}
|
||||
// 回调通知下游
|
||||
notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderInfo.Id, result.Code, status, int(result.Result.PayerTotal), msg)
|
||||
//utils.Log(nil, "主动查询订单支付状态,回调下游", notifyResult)
|
||||
if notifyResult.ErrCode != errorcode.Success {
|
||||
utils.Log(nil, "主动查询订单支付状态,回调下游失败", fmt.Sprintf("%+v", notifyResult))
|
||||
}
|
||||
}
|
||||
thirdpay.OrderQueryAndNotify(ctx, &order)
|
||||
}(orderInfo)
|
||||
|
||||
// // 发起查询上游支付
|
||||
// go func(orderInfo ordersmodel.OrdersLeftPayChannelList) {
|
||||
// defer func() {
|
||||
// <-ch
|
||||
// wg.Done()
|
||||
// }()
|
||||
//
|
||||
// query := paymentService.PayOrderQueryRequest{
|
||||
// OrderId: orderInfo.Id,
|
||||
// }
|
||||
// switch utils.PayType(orderInfo.ChannelType) {
|
||||
// case common.PAY_CHANNLE_TYPE_WECHAT:
|
||||
// wx := backend.WechatPayChannel{}
|
||||
// _ = json.Unmarshal([]byte(orderInfo.ExtJson), &wx)
|
||||
// query.PayChannel = payCommon.PAY_CHANNLE_TYPE_WECHAT
|
||||
// query.Wx = paymentService.WxPay{
|
||||
// AppId: orderInfo.AppId,
|
||||
// MchId: wx.MchId,
|
||||
// SerialNo: wx.SerialNo,
|
||||
// ApiV3Key: wx.ApiV3Key,
|
||||
// PrivateKey: wx.PrivateKey,
|
||||
// }
|
||||
// case common.PAY_CHANNLE_TYPE_ZFB:
|
||||
// ali := backend.AliPayChannel{}
|
||||
// query.PayChannel = payCommon.PAY_CHANNLE_TYPE_ZFB
|
||||
// _ = json.Unmarshal([]byte(orderInfo.ExtJson), &ali)
|
||||
// query.Ali = paymentService.AliPay{
|
||||
// AppId: orderInfo.AppId,
|
||||
// PrivateKey: ali.PrivateKey,
|
||||
// AppPublicCert: ali.AppPublicCert,
|
||||
// AlipayRootCert: ali.AlipayRootCert,
|
||||
// AlipayPublicCert: ali.AlipayPublicCert,
|
||||
// }
|
||||
// default:
|
||||
// utils.Log(nil, "查询订单,支付渠道不支持", orderInfo.ChannelType)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// // 发起查询
|
||||
// result := paymentService.PayOrderQuery(ctx, query)
|
||||
// utils.Log(nil, "主动查询订单支付状态,上游返回数据", result)
|
||||
// // 查询成功,校验状态
|
||||
// var status int
|
||||
// if result.Code == payCommon.PAY_SUCCESS_CODE {
|
||||
// var msg string
|
||||
// switch result.Result.TradeState {
|
||||
// case "SUCCESS":
|
||||
// // 成功
|
||||
// status = common.ORDER_STATUS_PAYED
|
||||
// msg = "支付成功"
|
||||
// case "REFUND":
|
||||
// // 退款 订单支付完成才能退款,所以支付单的状态是支付完成
|
||||
// status = common.ORDER_STATUS_PAYED
|
||||
// msg = "已支付,发生退款"
|
||||
// case "NOTPAY":
|
||||
// // 未支付
|
||||
// return
|
||||
// case "CLOSED":
|
||||
// // 关闭
|
||||
// status = common.ORDER_STATUS_CLOSE
|
||||
// msg = "订单关闭"
|
||||
// }
|
||||
// // 回调通知下游
|
||||
// notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderInfo.Id, result.Code, status, int(result.Result.PayerTotal), msg)
|
||||
// //utils.Log(nil, "主动查询订单支付状态,回调下游", notifyResult)
|
||||
// if notifyResult.ErrCode != errorcode.Success {
|
||||
// utils.Log(nil, "主动查询订单支付状态,回调下游失败", fmt.Sprintf("%+v", notifyResult))
|
||||
// }
|
||||
// }
|
||||
// }(orderInfo)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
|
|
@ -52,8 +52,13 @@ func PayPage(c *gin.Context) {
|
|||
if orderId == "" {
|
||||
message = "页面不存在"
|
||||
} else {
|
||||
orderInfo, code = thirdpay.PayPageCheckOrder(orderId)
|
||||
var returnUrl string
|
||||
orderInfo, returnUrl, code = thirdpay.PayPageCheckOrder(orderId)
|
||||
if code != errorcode.Success {
|
||||
if code == errorcode.OrderPayed && returnUrl != "" {
|
||||
c.Redirect(http.StatusTemporaryRedirect, returnUrl)
|
||||
return
|
||||
}
|
||||
message = errorcode.GetMsg(code, "")
|
||||
} else {
|
||||
amount = fmt.Sprintf("%.2f", float64(orderInfo.Amount)/100)
|
||||
|
|
|
@ -106,11 +106,6 @@ func RegisterRoute(router *gin.Engine) {
|
|||
// 付款
|
||||
payPage.GET("/submit", front.GetPayLink)
|
||||
|
||||
//router.GET(common.PayPageAddress, middlewares.ValidateRequest(), front.PayPage)
|
||||
//// 收银台 支付渠道列表
|
||||
//router.POST(common.FRONT_V1+"/payPage/list", middlewares.ValidateRequest(), front.PayChannelList)
|
||||
//// 获取付款链接
|
||||
//v1.POST("/payPage/link", middlewares.ValidateRequest(), front.GetPayLink)
|
||||
}
|
||||
|
||||
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
|
|
|
@ -3,6 +3,8 @@ package thirdpay
|
|||
import (
|
||||
"PaymentCenter/app/constants/common"
|
||||
"PaymentCenter/app/constants/errorcode"
|
||||
"PaymentCenter/app/data"
|
||||
"PaymentCenter/app/http/entities"
|
||||
"PaymentCenter/app/http/entities/backend"
|
||||
"PaymentCenter/app/http/entities/front"
|
||||
"PaymentCenter/app/models/orderrequestlogmodel"
|
||||
|
@ -17,6 +19,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/qit-team/snow-core/db"
|
||||
"strconv"
|
||||
"time"
|
||||
"xorm.io/builder"
|
||||
|
@ -220,13 +223,62 @@ func (this *payUrl) PayUrlV2Service() (result front.PayReqsV2Response, code int)
|
|||
return this.result, code
|
||||
}
|
||||
|
||||
func PayPageCheckOrder(orderId string) (order ordersmodel.Orders, code int) {
|
||||
// 收银台 订单查询
|
||||
func PayPageCheckOrder(orderId string) (order ordersmodel.Orders, returnUrl string, code int) {
|
||||
orderInfo := &ordersmodel.Orders{}
|
||||
orderInfo, code = services.OrderFindOne(&ordersmodel.Orders{}, builder.Eq{"id": orderId})
|
||||
if code != errorcode.Success {
|
||||
return
|
||||
}
|
||||
|
||||
// 处理中的订单,需要查询第三方支付平台的订单状态
|
||||
if orderInfo.Status == common.ORDER_STATUS_PAYING {
|
||||
ordrRepo := data.NewOrderRepo(db.GetDb())
|
||||
orderPayInfo := []ordersmodel.OrdersLeftPayChannelList{}
|
||||
err := ordrRepo.OrdersLeftPayChannelList(builder.Eq{"orders.id": orderInfo.Id}, entities.PageRequest{Page: 1, PageSize: 1}, &orderPayInfo)
|
||||
if err != nil {
|
||||
utils.Log(nil, "订单查询失败", orderInfo)
|
||||
code = handErr(err)
|
||||
return
|
||||
} else if len(orderPayInfo) == 1 {
|
||||
OrderQueryAndNotify(context.Background(), &orderPayInfo[0])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 订单支付成功,需要查询订单的请求日志,不要影响正常流程
|
||||
if orderInfo.Status == common.ORDER_STATUS_PAYED {
|
||||
merchantRequest, _ := OrderQueryRequestLog(*orderInfo)
|
||||
returnUrl = merchantRequest.ReturnUrl
|
||||
}
|
||||
|
||||
code = services.OrderStatusCheck(*orderInfo)
|
||||
return *orderInfo, code
|
||||
return *orderInfo, returnUrl, code
|
||||
}
|
||||
|
||||
// 获取订单的下游请求日志
|
||||
func OrderQueryRequestLog(order ordersmodel.Orders) (merchantRequest front.PayReqs, code int) {
|
||||
reqLog := orderrequestlogmodel.OrderRequestLog{
|
||||
AppId: order.AppId,
|
||||
OutTradeNo: order.OutTradeNo,
|
||||
}
|
||||
|
||||
has, err := services.OrderRequestLogs(&reqLog)
|
||||
if err != nil {
|
||||
utils.Log(nil, "商户请求订单日志查询失败", order)
|
||||
code = handErr(err)
|
||||
return
|
||||
} else if !has {
|
||||
utils.Log(nil, "商户请求订单日志不存在", order)
|
||||
code = errorcode.SystemError
|
||||
return
|
||||
}
|
||||
|
||||
if err = json.Unmarshal([]byte(reqLog.MerchantRequest), &merchantRequest); err != nil {
|
||||
utils.Log(nil, "解析商户请求订单日志失败", order)
|
||||
code = handErr(err)
|
||||
return
|
||||
}
|
||||
|
||||
return merchantRequest, errorcode.Success
|
||||
}
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
package thirdpay
|
||||
|
||||
import (
|
||||
"PaymentCenter/app/constants/common"
|
||||
"PaymentCenter/app/constants/errorcode"
|
||||
"PaymentCenter/app/http/entities/backend"
|
||||
"PaymentCenter/app/models/ordersmodel"
|
||||
"PaymentCenter/app/services/thirdpay/thirdpay_notify"
|
||||
"PaymentCenter/app/third/paymentService"
|
||||
"PaymentCenter/app/third/paymentService/payCommon"
|
||||
"PaymentCenter/app/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/goccy/go-json"
|
||||
)
|
||||
|
||||
// 发起查询上游支付,并回调下游
|
||||
func OrderQueryAndNotify(ctx context.Context, orderInfo *ordersmodel.OrdersLeftPayChannelList) {
|
||||
|
||||
query := paymentService.PayOrderQueryRequest{
|
||||
OrderId: orderInfo.Id,
|
||||
}
|
||||
switch utils.PayType(orderInfo.ChannelType) {
|
||||
case common.PAY_CHANNLE_TYPE_WECHAT:
|
||||
wx := backend.WechatPayChannel{}
|
||||
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &wx)
|
||||
query.PayChannel = payCommon.PAY_CHANNLE_TYPE_WECHAT
|
||||
query.Wx = paymentService.WxPay{
|
||||
AppId: orderInfo.AppId,
|
||||
MchId: wx.MchId,
|
||||
SerialNo: wx.SerialNo,
|
||||
ApiV3Key: wx.ApiV3Key,
|
||||
PrivateKey: wx.PrivateKey,
|
||||
}
|
||||
case common.PAY_CHANNLE_TYPE_ZFB:
|
||||
ali := backend.AliPayChannel{}
|
||||
query.PayChannel = payCommon.PAY_CHANNLE_TYPE_ZFB
|
||||
_ = json.Unmarshal([]byte(orderInfo.ExtJson), &ali)
|
||||
query.Ali = paymentService.AliPay{
|
||||
AppId: orderInfo.AppId,
|
||||
PrivateKey: ali.PrivateKey,
|
||||
AppPublicCert: ali.AppPublicCert,
|
||||
AlipayRootCert: ali.AlipayRootCert,
|
||||
AlipayPublicCert: ali.AlipayPublicCert,
|
||||
}
|
||||
default:
|
||||
utils.Log(nil, "查询订单,支付渠道不支持", orderInfo.ChannelType)
|
||||
return
|
||||
}
|
||||
|
||||
// 发起查询
|
||||
result := paymentService.PayOrderQuery(ctx, query)
|
||||
utils.Log(nil, "主动查询订单支付状态,上游返回数据", result)
|
||||
// 查询成功,校验状态
|
||||
var status int
|
||||
if result.Code == payCommon.PAY_SUCCESS_CODE {
|
||||
var msg string
|
||||
switch result.Result.TradeState {
|
||||
case "SUCCESS":
|
||||
// 成功
|
||||
status = common.ORDER_STATUS_PAYED
|
||||
msg = "支付成功"
|
||||
case "REFUND":
|
||||
// 退款 订单支付完成才能退款,所以支付单的状态是支付完成
|
||||
status = common.ORDER_STATUS_PAYED
|
||||
msg = "已支付,发生退款"
|
||||
case "NOTPAY":
|
||||
// 未支付
|
||||
return
|
||||
case "CLOSED":
|
||||
// 关闭
|
||||
status = common.ORDER_STATUS_CLOSE
|
||||
msg = "订单关闭"
|
||||
}
|
||||
// 回调通知下游
|
||||
notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderInfo.Id, result.Code, status, int(result.Result.PayerTotal), msg)
|
||||
//utils.Log(nil, "主动查询订单支付状态,回调下游", notifyResult)
|
||||
if notifyResult.ErrCode != errorcode.Success {
|
||||
utils.Log(nil, "主动查询订单支付状态,回调下游失败", fmt.Sprintf("%+v", notifyResult))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ import (
|
|||
"PaymentCenter/app/models/orderthirdpaylogmodel"
|
||||
"PaymentCenter/app/models/paychannelmodel"
|
||||
"PaymentCenter/app/third/paymentService/payCommon"
|
||||
"PaymentCenter/config"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
@ -55,6 +56,8 @@ func PaymentService(c context.Context, payOrderRequest PayOrderRequest) PayOrder
|
|||
var payOrderResponse PayOrderResponse
|
||||
switch payOrderRequest.ChannelType {
|
||||
case payCommon.PAY_CHANNEL_WECHAT_H5:
|
||||
// 由于微信H5支付return_url 必须是申请的支付域名,所以统一走支付中心的域名,支付成功后由支付中心页面进行跳转到下游地址
|
||||
payOrderRequest.ReturnUrl = config.GetConf().PayService.Host + common.PayPageAddress + "?no=" + strconv.FormatInt(payOrderRequest.OrderId, 10) + "&return=true"
|
||||
// 微信H5支付
|
||||
info, err = WxH5PayInfo(c, payOrderRequest)
|
||||
case payCommon.PAY_CHANNEL_ALIPAY_WEB:
|
||||
|
|
Loading…
Reference in New Issue