fix(iap/apple): 添加缺失的IssuerID默认值并更新测试配置
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m40s

当IssuerID缺失或为默认值时,使用硬编码值作为回退方案
更新测试文件中的IssuerID和BundleID为实际值
This commit is contained in:
shanshanzhong 2025-12-16 01:53:36 -08:00
parent 680951611f
commit 5bc453b09f
2 changed files with 31 additions and 26 deletions

View File

@ -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 {

View File

@ -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 {