修复拉卡拉支付时间偏差
This commit is contained in:
@@ -11,6 +11,9 @@ import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/timeutil"
|
||||
)
|
||||
|
||||
func TestVerifyNotifyDetail(t *testing.T) {
|
||||
@@ -90,6 +93,41 @@ func TestCreatePaymentSetsCounterParamWhenPayModeConfigured(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreatePaymentUsesShanghaiTimeWhenLocalIsUTC(t *testing.T) {
|
||||
oldLocal := time.Local
|
||||
time.Local = time.UTC
|
||||
defer func() {
|
||||
time.Local = oldLocal
|
||||
}()
|
||||
|
||||
loc := timeutil.ShanghaiLocation()
|
||||
before := time.Now().In(loc)
|
||||
body := captureCreatePaymentBody(t, "")
|
||||
after := time.Now().In(loc)
|
||||
reqData := body["req_data"].(map[string]any)
|
||||
|
||||
reqTime := parseLakalaTestTime(t, body["req_time"].(string))
|
||||
if reqTime.Before(before.Add(-2*time.Second)) || reqTime.After(after.Add(2*time.Second)) {
|
||||
t.Fatalf("req_time = %s, want between %s and %s", reqTime, before, after)
|
||||
}
|
||||
|
||||
efficientTime := parseLakalaTestTime(t, reqData["order_efficient_time"].(string))
|
||||
wantMin := before.Add(15*time.Minute - 2*time.Second)
|
||||
wantMax := after.Add(15*time.Minute + 2*time.Second)
|
||||
if efficientTime.Before(wantMin) || efficientTime.After(wantMax) {
|
||||
t.Fatalf("order_efficient_time = %s, want between %s and %s", efficientTime, wantMin, wantMax)
|
||||
}
|
||||
}
|
||||
|
||||
func parseLakalaTestTime(t *testing.T, value string) time.Time {
|
||||
t.Helper()
|
||||
parsed, err := time.ParseInLocation("20060102150405", value, timeutil.ShanghaiLocation())
|
||||
if err != nil {
|
||||
t.Fatalf("parse lakala time %q: %v", value, err)
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
func testKeyPairPEM(t *testing.T, key *rsa.PrivateKey) (string, string) {
|
||||
t.Helper()
|
||||
privateDER := x509.MarshalPKCS1PrivateKey(key)
|
||||
|
||||
Reference in New Issue
Block a user