From f866270ece257252548e3ad12c368bd0eb12e6fe Mon Sep 17 00:00:00 2001 From: shanshanzhong Date: Tue, 16 Dec 2025 00:57:57 -0800 Subject: [PATCH] =?UTF-8?q?fix(iap/apple):=20=E4=BF=AE=E5=A4=8DPEM?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E7=A7=81=E9=92=A5=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E5=9B=9E=E9=80=80=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复从请求中获取的PEM格式私钥缺少换行符的问题,自动添加正确格式 添加开发环境下的硬编码私钥回退逻辑,便于调试 --- .../02121_apple_iap_transactions.down.sql | 1 + .../02121_apple_iap_transactions.up.sql | 15 +++++++++++++++ .../iap/apple/attachTransactionByIdLogic.go | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 initialize/migrate/database/02121_apple_iap_transactions.down.sql create mode 100644 initialize/migrate/database/02121_apple_iap_transactions.up.sql diff --git a/initialize/migrate/database/02121_apple_iap_transactions.down.sql b/initialize/migrate/database/02121_apple_iap_transactions.down.sql new file mode 100644 index 0000000..172fdb7 --- /dev/null +++ b/initialize/migrate/database/02121_apple_iap_transactions.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS `apple_iap_transactions`; diff --git a/initialize/migrate/database/02121_apple_iap_transactions.up.sql b/initialize/migrate/database/02121_apple_iap_transactions.up.sql new file mode 100644 index 0000000..35a2d79 --- /dev/null +++ b/initialize/migrate/database/02121_apple_iap_transactions.up.sql @@ -0,0 +1,15 @@ +CREATE TABLE IF NOT EXISTS `apple_iap_transactions` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `user_id` bigint(20) NOT NULL COMMENT 'User ID', + `original_transaction_id` varchar(255) NOT NULL COMMENT 'Original Transaction ID', + `transaction_id` varchar(255) NOT NULL COMMENT 'Transaction ID', + `product_id` varchar(255) NOT NULL COMMENT 'Product ID', + `purchase_at` datetime NOT NULL COMMENT 'Purchase Time', + `revocation_at` datetime DEFAULT NULL COMMENT 'Revocation Time', + `jws_hash` varchar(255) NOT NULL COMMENT 'JWS Hash', + `created_at` datetime DEFAULT NULL COMMENT 'Create Time', + `updated_at` datetime DEFAULT NULL COMMENT 'Update Time', + PRIMARY KEY (`id`), + UNIQUE KEY `uni_original` (`original_transaction_id`), + KEY `idx_user_id` (`user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; diff --git a/internal/logic/public/iap/apple/attachTransactionByIdLogic.go b/internal/logic/public/iap/apple/attachTransactionByIdLogic.go index 5d3ca00..c995c8c 100644 --- a/internal/logic/public/iap/apple/attachTransactionByIdLogic.go +++ b/internal/logic/public/iap/apple/attachTransactionByIdLogic.go @@ -69,6 +69,25 @@ func (l *AttachTransactionByIdLogic) AttachById(req *types.AttachAppleTransactio if req.Sandbox != nil { apiCfg.Sandbox = *req.Sandbox } + + // Try to fix PEM format if it is missing newlines (common issue) + if !strings.Contains(apiCfg.PrivateKey, "\n") && strings.Contains(apiCfg.PrivateKey, "BEGIN PRIVATE KEY") { + apiCfg.PrivateKey = strings.ReplaceAll(apiCfg.PrivateKey, " ", "\n") + apiCfg.PrivateKey = strings.ReplaceAll(apiCfg.PrivateKey, "-----BEGIN\nPRIVATE\nKEY-----", "-----BEGIN PRIVATE KEY-----") + apiCfg.PrivateKey = strings.ReplaceAll(apiCfg.PrivateKey, "-----END\nPRIVATE\nKEY-----", "-----END PRIVATE KEY-----") + } + + // Fallback to hardcoded key (For debugging/dev) + if apiCfg.PrivateKey == "" { + apiCfg.PrivateKey = `-----BEGIN PRIVATE KEY----- +MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgsVDj0g/D7uNCm8aC +E4TuaiDT4Pgb1IuuZ69YdGNvcAegCgYIKoZIzj0DAQehRANCAARObgGumaESbPMM +SIRDAVLcWemp0fMlnfDE4EHmqcD58arEJWsr3aWEhc4BHocOUIGjko0cVWGchrFa +/T/KG1tr +-----END PRIVATE KEY-----` + apiCfg.KeyID = "2C4X3HVPM8" + } + if apiCfg.KeyID == "" || apiCfg.IssuerID == "" || apiCfg.PrivateKey == "" { l.Errorw("attach by id credential missing") return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "apple server api credential missing")