187 lines
6.0 KiB
Go
187 lines
6.0 KiB
Go
package payment
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"hfb_sys/backend/internal/integrations/payment/lakala"
|
|
"hfb_sys/backend/internal/integrations/payment/leshua"
|
|
"hfb_sys/backend/internal/integrations/payment/saobei"
|
|
"hfb_sys/backend/internal/integrations/payment/shuncheng"
|
|
"hfb_sys/backend/internal/middleware"
|
|
"hfb_sys/backend/internal/modules/paymentconfig"
|
|
"hfb_sys/backend/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func (h *Handler) StartConfigTestPayment(c *gin.Context) {
|
|
configID, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req StartConfigTestPaymentRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "测试金额必须大于0")
|
|
return
|
|
}
|
|
adminID, _ := configTestAdminID(c)
|
|
item, err := h.service.StartConfigTestPayment(c.Request.Context(), configID, req, adminID, c.ClientIP())
|
|
if err != nil {
|
|
writeConfigTestError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
func (h *Handler) AdminListConfigTestPayments(c *gin.Context) {
|
|
query := ConfigTestPaymentQuery{
|
|
Provider: strings.TrimSpace(c.Query("provider")),
|
|
PayWay: strings.TrimSpace(c.Query("pay_way")),
|
|
Status: strings.TrimSpace(c.Query("status")),
|
|
TestNo: strings.TrimSpace(c.Query("test_no")),
|
|
}
|
|
if raw := strings.TrimSpace(c.Query("payment_config_id")); raw != "" {
|
|
value, err := strconv.ParseUint(raw, 10, 64)
|
|
if err != nil || value == 0 {
|
|
response.BadRequest(c, "支付配置ID不正确")
|
|
return
|
|
}
|
|
query.PaymentConfigID = value
|
|
}
|
|
query.Page, _ = strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
query.PageSize, _ = strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
result, err := h.service.ListConfigTestPayments(c.Request.Context(), query)
|
|
if err != nil {
|
|
writeConfigTestError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, result)
|
|
}
|
|
|
|
func (h *Handler) AdminGetConfigTestPayment(c *gin.Context) {
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
item, err := h.service.GetConfigTestPayment(c.Request.Context(), id)
|
|
if err != nil {
|
|
writeConfigTestError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
func (h *Handler) AdminRefreshConfigTestPayment(c *gin.Context) {
|
|
id, ok := parseID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
item, err := h.service.RefreshConfigTestPayment(c.Request.Context(), id)
|
|
if err != nil {
|
|
writeConfigTestError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, item)
|
|
}
|
|
|
|
// ConfigTestNotify 是独立测试单回调,不读取或更新 payment_orders / rental_orders。
|
|
func (h *Handler) ConfigTestNotify(c *gin.Context) {
|
|
provider := strings.ToLower(strings.TrimSpace(c.Param("provider")))
|
|
notifyToken := strings.TrimSpace(c.Param("token"))
|
|
body, err := io.ReadAll(io.LimitReader(c.Request.Body, 1<<20))
|
|
if err != nil {
|
|
h.recordConfigTestNotifyAck(c, provider, notifyToken, false)
|
|
writeConfigTestNotifyFailure(c, provider)
|
|
return
|
|
}
|
|
var params map[string]string
|
|
switch provider {
|
|
case "leshua":
|
|
params, err = leshua.ParsePayload(body)
|
|
case "lakala":
|
|
params, err = lakala.ParsePayload(body)
|
|
case "shuncheng":
|
|
params, err = shuncheng.ParsePayload(body)
|
|
case "saobei":
|
|
params, err = saobei.ParsePayload(body)
|
|
default:
|
|
err = ErrPaymentNotFound
|
|
}
|
|
if err != nil {
|
|
h.recordConfigTestNotifyAck(c, provider, notifyToken, false)
|
|
writeConfigTestNotifyFailure(c, provider)
|
|
return
|
|
}
|
|
rawPayload := string(body)
|
|
contentType := c.GetHeader("Content-Type")
|
|
authorization := c.GetHeader("Authorization")
|
|
result, err := h.service.HandleConfigTestNotify(c.Request.Context(), provider, notifyToken, params, rawPayload, contentType, authorization)
|
|
if err != nil || result == nil || !result.OK {
|
|
h.log().Warn("支付配置测试回调处理失败", notifyLogFields(c.Request.Context(), provider, params, contentType, len(body), zap.Error(err))...)
|
|
h.recordConfigTestNotifyAck(c, provider, notifyToken, false)
|
|
writeConfigTestNotifyFailure(c, provider)
|
|
return
|
|
}
|
|
h.recordConfigTestNotifyAck(c, provider, notifyToken, true)
|
|
h.log().Info("支付配置测试回调处理完成", notifyLogFields(c.Request.Context(), provider, params, contentType, len(body))...)
|
|
switch provider {
|
|
case "lakala":
|
|
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS", "message": "执行成功"})
|
|
case "saobei":
|
|
c.JSON(http.StatusOK, gin.H{"return_code": "01", "return_msg": "success"})
|
|
default:
|
|
c.String(http.StatusOK, result.Message)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) recordConfigTestNotifyAck(c *gin.Context, provider string, notifyToken string, success bool) {
|
|
ack := configTestNotifyAck(provider, success)
|
|
if err := h.service.RecordConfigTestNotifyAck(c.Request.Context(), provider, notifyToken, ack); err != nil {
|
|
h.log().Warn("支付配置测试回调应答保存失败",
|
|
notifyLogFields(c.Request.Context(), provider, nil, "", 0, zap.Error(err))...)
|
|
}
|
|
}
|
|
|
|
func writeConfigTestNotifyFailure(c *gin.Context, provider string) {
|
|
switch provider {
|
|
case "lakala":
|
|
c.JSON(http.StatusOK, gin.H{"code": "FAIL", "message": "失败"})
|
|
case "saobei":
|
|
c.JSON(http.StatusOK, gin.H{"return_code": "02", "return_msg": "失败"})
|
|
default:
|
|
c.String(http.StatusOK, "FAIL")
|
|
}
|
|
}
|
|
|
|
func writeConfigTestError(c *gin.Context, err error) {
|
|
response.RecordError(c, err)
|
|
switch {
|
|
case errors.Is(err, ErrConfigTestPaymentInvalid):
|
|
response.BadRequest(c, "测试金额必须大于0")
|
|
case errors.Is(err, ErrDependencyUnavailable):
|
|
response.ServiceUnavailable(c, "数据库未连接")
|
|
case errors.Is(err, ErrPaymentNotFound), errors.Is(err, paymentconfig.ErrConfigNotFound):
|
|
response.NotFound(c, "测试支付记录或支付配置不存在")
|
|
case errors.Is(err, ErrPaymentVerifyFailed):
|
|
response.Error(c, http.StatusForbidden, "payment_verify_failed", "支付回调验签失败")
|
|
case errors.Is(err, ErrPaymentUnavailable):
|
|
response.Error(c, http.StatusBadGateway, "payment_unavailable", "支付渠道暂不可用")
|
|
default:
|
|
response.Error(c, http.StatusBadGateway, "config_test_payment_failed", "测试支付处理失败,请查看测试记录")
|
|
}
|
|
}
|
|
|
|
func configTestAdminID(c *gin.Context) (uint64, bool) {
|
|
value, ok := c.Get(middleware.ContextAdminID)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
id, ok := value.(uint64)
|
|
return id, ok && id > 0
|
|
}
|