完善商福通接口通知字段;提供一个环境变量以决定是否打印原始 http 报文

This commit is contained in:
2025-06-23 11:27:28 +08:00
parent 546e81fee3
commit fda1a2de0e
2 changed files with 45 additions and 9 deletions

30
pkg/env/env.go vendored
View File

@@ -148,7 +148,8 @@ func loadRedis() {
// region log // region log
var ( var (
LogLevel = slog.LevelDebug LogLevel = slog.LevelDebug
LogHttpDump = false // 是否打印HTTP请求和响应的原始数据
) )
func loadLog() { func loadLog() {
@@ -163,6 +164,17 @@ func loadLog() {
case "error": case "error":
LogLevel = slog.LevelError LogLevel = slog.LevelError
} }
_LogHttpDump := os.Getenv("LOG_HTTP_DUMP")
if _LogHttpDump != "" {
value, err := strconv.ParseBool(_LogHttpDump)
if err != nil {
panic("环境变量 LOG_HTTP_DUMP 的值不是布尔值")
}
LogHttpDump = value
} else {
LogHttpDump = false // 默认不打印HTTP请求和响应的原始数据
}
} }
// endregion // endregion
@@ -355,6 +367,8 @@ var (
SftPayRouteId string SftPayRouteId string
SftPayAppPrivateKey string SftPayAppPrivateKey string
SftPayPublicKey string SftPayPublicKey string
SftReturnUrl *string
SftNotifyUrl *string
) )
func loadSftPay() { func loadSftPay() {
@@ -394,6 +408,20 @@ func loadSftPay() {
} else { } else {
SftPayPublicKey = value SftPayPublicKey = value
} }
value = os.Getenv("SFTPAY_RETURN_URL")
if value != "" {
SftReturnUrl = &value
} else {
SftReturnUrl = nil
}
value = os.Getenv("SFTPAY_NOTIFY_URL")
if value != "" {
SftNotifyUrl = &value
} else {
SftNotifyUrl = nil
}
} }
// endregion // endregion

View File

@@ -82,12 +82,16 @@ func initSft() {
func (s *SftClient) PaymentScanPay(req *PaymentScanPayReq) (*PaymentScanPayResp, error) { func (s *SftClient) PaymentScanPay(req *PaymentScanPayReq) (*PaymentScanPayResp, error) {
const url = "https://pay.rscygroup.com/api/open/payment/scanpay" const url = "https://pay.rscygroup.com/api/open/payment/scanpay"
req.ReturnUrl = env.SftReturnUrl
req.NotifyUrl = env.SftNotifyUrl
req.RouteNo = u.P(s.routeId) req.RouteNo = u.P(s.routeId)
return call[PaymentScanPayResp](s, url, req) return call[PaymentScanPayResp](s, url, req)
} }
func (s *SftClient) PaymentH5Pay(req *PaymentH5PayReq) (*PaymentH5PayResp, error) { func (s *SftClient) PaymentH5Pay(req *PaymentH5PayReq) (*PaymentH5PayResp, error) {
const url = "https://pay.rscygroup.com/api/open/payment/h5pay" const url = "https://pay.rscygroup.com/api/open/payment/h5pay"
req.ReturnUrl = env.SftReturnUrl
req.NotifyUrl = env.SftNotifyUrl
req.RouteNo = u.P(s.routeId) req.RouteNo = u.P(s.routeId)
return call[PaymentH5PayResp](s, url, req) return call[PaymentH5PayResp](s, url, req)
} }
@@ -265,22 +269,26 @@ func call[T any](s *SftClient, url string, req any) (*T, error) {
} }
request.Header.Set("Content-Type", "application/json") request.Header.Set("Content-Type", "application/json")
reqDump, err := httputil.DumpRequest(request, true) if env.LogHttpDump == true {
if err != nil { reqDump, err := httputil.DumpRequest(request, true)
return nil, fmt.Errorf("请求内容转储失败:%w", err) if err != nil {
return nil, fmt.Errorf("请求内容转储失败:%w", err)
}
println(string(reqDump) + "\n\n")
} }
println(string(reqDump) + "\n\n")
response, err := http.DefaultClient.Do(request) response, err := http.DefaultClient.Do(request)
if err != nil { if err != nil {
return nil, fmt.Errorf("请求失败:%w", err) return nil, fmt.Errorf("请求失败:%w", err)
} }
respDump, err := httputil.DumpResponse(response, true) if env.LogHttpDump == true {
if err != nil { respDump, err := httputil.DumpResponse(response, true)
return nil, fmt.Errorf("响应内容转储失败:%w", err) if err != nil {
return nil, fmt.Errorf("响应内容转储失败:%w", err)
}
println(string(respDump) + "\n\n")
} }
println(string(respDump) + "\n\n")
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("请求响应失败:%d", response.StatusCode) return nil, fmt.Errorf("请求响应失败:%d", response.StatusCode)