优化结构

This commit is contained in:
Yvan
2026-08-28 00:29:40 +08:00
parent 04ff85116c
commit 03a4131f6e
118 changed files with 758 additions and 834 deletions
+27 -10
View File
@@ -15,7 +15,7 @@ import (
"strings"
"time"
"kra/pkg/paymentkit"
"kra/internal/paymentkit"
)
type vendorProfile string
@@ -133,8 +133,13 @@ func (a *vendorPaymentAdapter) call(ctx context.Context, endpointKey string, pay
}
result := &bizpayment.PaymentResult{Provider: a.provider, Status: "created", TradeNo: tradeNo, Payload: ensureJSON(body)}
if endpointKey != "query_url" {
if object := jsonObject(body); object != nil {
operation := strings.TrimSuffix(endpointKey, "_url")
operation := strings.TrimSuffix(endpointKey, "_url")
object := jsonObject(body)
if object == nil {
if operation == "refund" {
return nil, fmt.Errorf("%s 退款响应不是 JSON 对象", a.provider)
}
} else {
statusField := text(c, operation+"_status_field")
if statusField == "" {
statusField = text(c, "query_status_field")
@@ -158,8 +163,20 @@ func (a *vendorPaymentAdapter) call(ctx context.Context, endpointKey string, pay
case containsFold(configuredValues(c, operation+"_success_values"), state):
result.Status = "created"
default:
result.Status = normalizePaymentStatus(state, "created")
if operation == "refund" {
normalized := normalizePaymentStatus(state, "")
switch normalized {
case "success", "pending", "failed":
result.Status = normalized
default:
return nil, fmt.Errorf("%s 退款响应状态无法识别: %s", a.provider, state)
}
} else {
result.Status = normalizePaymentStatus(state, "created")
}
}
} else if operation == "refund" {
return nil, fmt.Errorf("%s 退款响应缺少业务状态", a.provider)
}
}
return result, nil
@@ -273,18 +290,18 @@ func (a *vendorPaymentAdapter) signRequest(request *http.Request, payload map[st
digest := sha256.Sum256([]byte(appID + timestamp + nonceValue + string(raw) + secret))
request.Header.Set("Authorization", "OPEN-BODY-SIG AppId="+appID+", Timestamp="+timestamp+", Nonce="+nonceValue+", Signature="+hex.EncodeToString(digest[:]))
case vendorSFT:
request.Header.Set("X-SFT-Sign", paymentutil.MD5Canonical(payload, secret))
request.Header.Set("X-SFT-Sign", paymentkit.MD5Canonical(payload, secret))
case vendorWechatGame:
request.Header.Set("X-Wechat-Game-Sign", paymentutil.HMACSHA256Hex(raw, secret, false))
request.Header.Set("X-Wechat-Game-Sign", paymentkit.HMACSHA256Hex(raw, secret, false))
if token := text(c, "access_token"); token != "" {
query := request.URL.Query()
query.Set("access_token", token)
request.URL.RawQuery = query.Encode()
}
case vendorDouyinGame:
request.Header.Set("X-TT-Pay-Sign", paymentutil.MD5Canonical(payload, secret))
request.Header.Set("X-TT-Pay-Sign", paymentkit.MD5Canonical(payload, secret))
default:
request.Header.Set("X-Payment-Sign", paymentutil.HMACSHA256Hex(raw, secret, false))
request.Header.Set("X-Payment-Sign", paymentkit.HMACSHA256Hex(raw, secret, false))
}
}
func (a *vendorPaymentAdapter) verify(fields map[string]string, raw []byte, headers map[string]string, c map[string]any) error {
@@ -327,9 +344,9 @@ func (a *vendorPaymentAdapter) verify(fields map[string]string, raw []byte, head
values[key] = value
}
}
actual = paymentutil.MD5Canonical(values, secret)
actual = paymentkit.MD5Canonical(values, secret)
default:
actual = paymentutil.HMACSHA256Hex(raw, secret, false)
actual = paymentkit.HMACSHA256Hex(raw, secret, false)
}
if !hmac.Equal([]byte(strings.ToLower(expected)), []byte(strings.ToLower(actual))) {
return errors.New("支付回调签名校验失败")