完善商福通接口通知字段;提供一个环境变量以决定是否打印原始 http 报文
This commit is contained in:
30
pkg/env/env.go
vendored
30
pkg/env/env.go
vendored
@@ -148,7 +148,8 @@ func loadRedis() {
|
||||
// region log
|
||||
|
||||
var (
|
||||
LogLevel = slog.LevelDebug
|
||||
LogLevel = slog.LevelDebug
|
||||
LogHttpDump = false // 是否打印HTTP请求和响应的原始数据
|
||||
)
|
||||
|
||||
func loadLog() {
|
||||
@@ -163,6 +164,17 @@ func loadLog() {
|
||||
case "error":
|
||||
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
|
||||
@@ -355,6 +367,8 @@ var (
|
||||
SftPayRouteId string
|
||||
SftPayAppPrivateKey string
|
||||
SftPayPublicKey string
|
||||
SftReturnUrl *string
|
||||
SftNotifyUrl *string
|
||||
)
|
||||
|
||||
func loadSftPay() {
|
||||
@@ -394,6 +408,20 @@ func loadSftPay() {
|
||||
} else {
|
||||
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
|
||||
|
||||
@@ -82,12 +82,16 @@ func initSft() {
|
||||
|
||||
func (s *SftClient) PaymentScanPay(req *PaymentScanPayReq) (*PaymentScanPayResp, error) {
|
||||
const url = "https://pay.rscygroup.com/api/open/payment/scanpay"
|
||||
req.ReturnUrl = env.SftReturnUrl
|
||||
req.NotifyUrl = env.SftNotifyUrl
|
||||
req.RouteNo = u.P(s.routeId)
|
||||
return call[PaymentScanPayResp](s, url, req)
|
||||
}
|
||||
|
||||
func (s *SftClient) PaymentH5Pay(req *PaymentH5PayReq) (*PaymentH5PayResp, error) {
|
||||
const url = "https://pay.rscygroup.com/api/open/payment/h5pay"
|
||||
req.ReturnUrl = env.SftReturnUrl
|
||||
req.NotifyUrl = env.SftNotifyUrl
|
||||
req.RouteNo = u.P(s.routeId)
|
||||
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")
|
||||
|
||||
reqDump, err := httputil.DumpRequest(request, true)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求内容转储失败:%w", err)
|
||||
if env.LogHttpDump == true {
|
||||
reqDump, err := httputil.DumpRequest(request, true)
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求失败:%w", err)
|
||||
}
|
||||
|
||||
respDump, err := httputil.DumpResponse(response, true)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("响应内容转储失败:%w", err)
|
||||
if env.LogHttpDump == true {
|
||||
respDump, err := httputil.DumpResponse(response, true)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("响应内容转储失败:%w", err)
|
||||
}
|
||||
println(string(respDump) + "\n\n")
|
||||
}
|
||||
println(string(respDump) + "\n\n")
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("请求响应失败:%d", response.StatusCode)
|
||||
|
||||
Reference in New Issue
Block a user