From 5bc453b09f139c01619353e49a5c16b3fc26ed9e Mon Sep 17 00:00:00 2001 From: shanshanzhong Date: Tue, 16 Dec 2025 01:53:36 -0800 Subject: [PATCH] =?UTF-8?q?fix(iap/apple):=20=E6=B7=BB=E5=8A=A0=E7=BC=BA?= =?UTF-8?q?=E5=A4=B1=E7=9A=84IssuerID=E9=BB=98=E8=AE=A4=E5=80=BC=E5=B9=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=B5=8B=E8=AF=95=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当IssuerID缺失或为默认值时,使用硬编码值作为回退方案 更新测试文件中的IssuerID和BundleID为实际值 --- cmd/test_apple_iap/main.go | 52 +++++++++---------- .../iap/apple/attachTransactionByIdLogic.go | 5 ++ 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/cmd/test_apple_iap/main.go b/cmd/test_apple_iap/main.go index 511eafa..17547b7 100644 --- a/cmd/test_apple_iap/main.go +++ b/cmd/test_apple_iap/main.go @@ -1,34 +1,34 @@ package main import ( - "fmt" - "log" - "time" - "net/http" - "io" - "encoding/json" - "encoding/base64" - "crypto/x509" - "encoding/pem" "crypto/ecdsa" "crypto/rand" "crypto/sha256" + "crypto/x509" + "encoding/base64" + "encoding/json" + "encoding/pem" + "fmt" + "io" + "log" + "net/http" + "time" ) // 配置区域 - 请在此处填入您的真实信息进行测试 const ( // 必填:您的 Key ID (从 App Store Connect 获取) - KeyID = "2C4X3HVPM8" - + KeyID = "2C4X3HVPM8" + // 必填:您的 Issuer ID (从 App Store Connect 获取,通常是一个 UUID) - IssuerID = "69a6de70-03db-47e3-e053-5b8c7c11a4d1" // 请替换为您真实的 Issuer ID - + IssuerID = "34f54810-5118-4b7f-8069-c8c1e012b7a9" // 请替换为您真实的 Issuer ID + // 必填:您的 Bundle ID (App 的包名) - BundleID = "com.hifastvpn.ios" // 请替换为您真实的 Bundle ID - + BundleID = "com.taw.hifastvpn" // 请替换为您真实的 Bundle ID + // 必填:用于测试的 Transaction ID (任意一个真实的交易 ID) - TestTransactionID = "2000001083318819" - + TestTransactionID = "2000001083318819" + // 必填:是否为沙盒环境 IsSandbox = true ) @@ -65,11 +65,11 @@ func main() { if IsSandbox { host = "https://api.storekit-sandbox.itunes.apple.com" } - + url := fmt.Sprintf("%s/inApps/v1/transactions/%s", host, TestTransactionID) req, _ := http.NewRequest("GET", url, nil) req.Header.Set("Authorization", "Bearer "+token) - + log.Printf("正在请求: %s", url) start := time.Now() resp, err := http.DefaultClient.Do(req) @@ -77,13 +77,13 @@ func main() { log.Fatalf("请求失败: %v", err) } defer resp.Body.Close() - + duration := time.Since(start) body, _ := io.ReadAll(resp.Body) - + log.Printf("请求耗时: %v", duration) log.Printf("状态码: %d", resp.StatusCode) - + if resp.StatusCode == 200 { log.Println("✅ 测试成功!API 调用正常。") log.Printf("响应内容: %s", string(body)) @@ -122,12 +122,12 @@ func buildAPIToken() (string, error) { hb, _ := json.Marshal(header) pb, _ := json.Marshal(payload) - + enc := func(b []byte) string { return base64.RawURLEncoding.EncodeToString(b) } unsigned := fmt.Sprintf("%s.%s", enc(hb), enc(pb)) - + block, _ := pem.Decode([]byte(PrivateKeyPEM)) if block == nil { return "", fmt.Errorf("invalid private key") @@ -140,13 +140,13 @@ func buildAPIToken() (string, error) { if !ok { return "", fmt.Errorf("private key is not ECDSA") } - + digest := sha256Sum([]byte(unsigned)) r, s, err := ecdsa.Sign(rand.Reader, priv, digest) if err != nil { return "", err } - + curveBits := priv.Curve.Params().BitSize keyBytes := curveBits / 8 if curveBits%8 > 0 { diff --git a/internal/logic/public/iap/apple/attachTransactionByIdLogic.go b/internal/logic/public/iap/apple/attachTransactionByIdLogic.go index 16b4192..fbf8843 100644 --- a/internal/logic/public/iap/apple/attachTransactionByIdLogic.go +++ b/internal/logic/public/iap/apple/attachTransactionByIdLogic.go @@ -99,6 +99,11 @@ SIRDAVLcWemp0fMlnfDE4EHmqcD58arEJWsr3aWEhc4BHocOUIGjko0cVWGchrFa return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "apple server api credential missing") } + // Hardcode IssuerID as fallback (since it was missing in config) + if apiCfg.IssuerID == "" || apiCfg.IssuerID == "some_issuer_id" { + apiCfg.IssuerID = "34f54810-5118-4b7f-8069-c8c1e012b7a9" + } + // Try to get BundleID from Site CustomData if not set if apiCfg.BundleID == "" { var customData struct {