PaymentCenter/app/http/controllers/front/psbc.go

169 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package front
import (
"PaymentCenter/app/constants/common"
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/http/entities/front"
"PaymentCenter/app/models/paychannelmodel"
"PaymentCenter/app/services"
"PaymentCenter/app/services/thirdpay/thirdpay_notify"
"PaymentCenter/app/third/paymentService"
"fmt"
"github.com/gin-gonic/gin"
"github.com/goccy/go-json"
"github.com/qit-team/snow-core/log/logger"
"io"
"net/http"
"strconv"
)
// PsbcCallback 邮储支付回调
func PsbcCallback(c *gin.Context) {
notifyRsp := front.YouChuOrderNotifyResponse{}
logger.Info(c, "PsbcCallback-回调数据", c.Request)
payChannelId := c.Param("payChannelId")
logger.Info(c, "PsbcCallback-回调数据payChannelId", payChannelId)
if payChannelId == "" {
notifyRsp.RespCode = "000001"
notifyRsp.RespMsg = "支付渠道ID为空"
c.JSON(http.StatusOK, notifyRsp)
return
}
body, err := io.ReadAll(c.Request.Body)
if err != nil {
logger.Error(c, "PsbcCallback-读取请求体失败", err.Error())
notifyRsp.RespCode = "000001"
notifyRsp.RespMsg = "读取请求体失败"
c.JSON(http.StatusOK, notifyRsp)
return
}
bodyStr := string(body)
logger.Info(c, "PsbcCallback-回调数据body", bodyStr)
var payChannelModel paychannelmodel.PayChannel
payChannelIdInt, _ := strconv.Atoi(payChannelId)
payChannelModel.Id = int64(payChannelIdInt)
code := services.PayChannelGet(&payChannelModel)
if code == errorcode.PayChannelNotFound {
logger.Error(c, "PsbcCallback-回调数据未获取到支付配置404")
notifyRsp.RespCode = "000001"
notifyRsp.RespMsg = "未获取到支付配置"
c.JSON(http.StatusOK, notifyRsp)
return
}
var psbcConfig paymentService.PsbcPay
err = json.Unmarshal([]byte(payChannelModel.ExtJson), &psbcConfig)
if err != nil {
logger.Error(c, "PsbcCallback-回调数据解析支付配置错误", fmt.Sprintf("错误原因:%s", err.Error()))
notifyRsp.RespCode = "000001"
notifyRsp.RespMsg = "解析支付配置错误"
c.JSON(http.StatusOK, notifyRsp)
return
}
if psbcConfig.MchtNo == "" || psbcConfig.PrivateKey == "" || psbcConfig.BankKey == "" {
logger.Error(c, "PsbcCallback-回调数据解析支付配置错误,解析出来的信息为空")
notifyRsp.RespCode = "000001"
notifyRsp.RespMsg = "解析支付配置错误"
c.JSON(http.StatusOK, notifyRsp)
return
}
psbcConfig.AppID = payChannelModel.AppId
result, err := paymentService.PsbcVerifyAndParseNotify(bodyStr, psbcConfig)
if err != nil {
logger.Error(c, "PsbcCallback-回调数据验签失败", err.Error())
notifyRsp.RespCode = "000001"
notifyRsp.RespMsg = "验签失败"
c.JSON(http.StatusOK, notifyRsp)
return
}
reqTraceId := result.ReqTraceId
var amountTotal int64
var payerTotal int64
var status string
if result.TxnAmt != "" {
amountFloat, err := strconv.ParseFloat(result.TxnAmt, 64)
if err == nil {
amountTotal = int64(amountFloat * 100)
payerTotal = amountTotal
}
}
orderSta := ""
if result.OrderSta != "" {
orderSta = result.OrderSta
}
switch orderSta {
case "03":
status = "SUCCESS"
case "04", "05":
status = "FAIL"
default:
status = "NOTPAY"
}
logger.Info(c, "PsbcCallback-解析邮储回调结果", fmt.Sprintf("reqTraceId:%s, amountTotal:%d, payerTotal:%d, status:%s", reqTraceId, amountTotal, payerTotal, status))
var orderStatus int
var errCode int
var msg string
switch status {
case "SUCCESS":
orderStatus = common.ORDER_STATUS_PAYED
errCode = errorcode.Success
msg = "支付成功"
case "FAIL":
orderStatus = common.ORDER_STATUS_FAILED
errCode = errorcode.ParamError
msg = "支付失败"
default:
logger.Error(c, "PsbcCallback-订单状态异常", status)
notifyRsp.RespCode = "000001"
notifyRsp.RespMsg = "订单状态异常"
c.JSON(http.StatusOK, notifyRsp)
return
}
orderId, err := strconv.ParseInt(reqTraceId, 10, 64)
if err != nil {
logger.Error(c, "PsbcCallback 发生错误", fmt.Sprintf("订单号转换失败,错误信息:%s", err.Error()))
notifyRsp.RespCode = "000001"
notifyRsp.RespMsg = "订单号转换失败"
c.JSON(http.StatusOK, notifyRsp)
return
}
notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderId, errCode, orderStatus, int(amountTotal), int(payerTotal), msg, result.OrderNo)
notifyResultJson, _ := json.Marshal(notifyResult)
go func() {
payCallback := bodyStr
payParam := "{}"
merchantCallback := string(notifyResultJson)
logStatus := common.THIRD_ORDER_LOG_STATUS_SUCCESS
if notifyResult.ErrCode != errorcode.Success {
logStatus = common.THIRD_ORDER_LOG_STATUS_FAIL
}
paymentService.SaveLog(orderId, common.THIRD_ORDER_TYPE_CALL_BACK, payCallback, payParam, merchantCallback, logStatus)
}()
if notifyResult.ErrCode != errorcode.Success {
logger.Error(c, "PsbcCallback-回调执行失败,失败原因:", fmt.Sprintf("%+v", notifyResult))
notifyRsp.RespCode = "000001"
notifyRsp.RespMsg = "交易失败" + errorcode.GetMsg(notifyResult.ErrCode, "")
c.JSON(http.StatusOK, notifyRsp)
return
}
c.JSON(http.StatusOK, front.YouChuOrderNotifyResponse{
RespCode: "000000",
RespMsg: "交易成功",
})
return
}