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

112 lines
3.6 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/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) {
logger.Info(c, "PsbcCallback-回调数据", c.Request)
payChannelId := c.Param("payChannelId")
logger.Info(c, "PsbcCallback-回调数据payChannelId", payChannelId)
if payChannelId == "" {
c.String(http.StatusBadRequest, "%s", "fail")
return
}
body, err := io.ReadAll(c.Request.Body)
if err != nil {
logger.Error(c, "PsbcCallback-读取请求体失败", err.Error())
c.String(http.StatusBadRequest, "%s", "fail")
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")
c.String(http.StatusBadRequest, "%s", "fail")
return
}
var psbcConfig paymentService.PsbcPay
err = json.Unmarshal([]byte(payChannelModel.ExtJson), &psbcConfig)
if err != nil {
logger.Error(c, "PsbcCallback-回调数据解析支付配置错误", fmt.Sprintf("错误原因:%s", err.Error()))
c.String(http.StatusBadRequest, "%s", "fail")
return
}
if psbcConfig.MchtNo == "" || psbcConfig.PrivateKey == "" || psbcConfig.BankKey == "" {
logger.Error(c, "PsbcCallback-回调数据解析支付配置错误,解析出来的信息为空")
c.String(http.StatusBadRequest, "%s", "fail")
return
}
psbcConfig.AppID = payChannelModel.AppId
orderId, amountTotal, payerTotal, status, err := paymentService.PsbcVerifyAndParseNotify(bodyStr, psbcConfig)
if err != nil {
logger.Error(c, "PsbcCallback-回调数据验签失败", err.Error())
c.String(http.StatusBadRequest, "%s", "fail")
return
}
logger.Info(c, "PsbcCallback-解析邮储回调结果", fmt.Sprintf("orderId:%d, amountTotal:%d, payerTotal:%d, status:%s", orderId, 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)
c.String(http.StatusBadRequest, "%s", "fail")
return
}
notifyResult := thirdpay_notify.NewOrderNotifyWithHandle(orderId, errCode, orderStatus, int(amountTotal), int(payerTotal), msg)
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))
c.String(http.StatusBadRequest, "%s", "fail")
return
}
c.String(http.StatusOK, "%s", "success")
return
}