package apple import ( "context" "strings" "github.com/perfect-panel/server/internal/model/payment" "github.com/perfect-panel/server/internal/model/user" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "github.com/perfect-panel/server/pkg/constant" iapapple "github.com/perfect-panel/server/pkg/iap/apple" "github.com/perfect-panel/server/pkg/logger" "github.com/perfect-panel/server/pkg/xerr" "github.com/pkg/errors" ) type AttachTransactionByIdLogic struct { logger.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewAttachTransactionByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AttachTransactionByIdLogic { return &AttachTransactionByIdLogic{ Logger: logger.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *AttachTransactionByIdLogic) AttachById(req *types.AttachAppleTransactionByIdRequest) (*types.AttachAppleTransactionResponse, error) { l.Infow("attach by transaction id start", logger.Field("orderNo", req.OrderNo), logger.Field("transactionId", req.TransactionId)) u, ok := l.ctx.Value(constant.CtxKeyUser).(*user.User) if !ok || u == nil { l.Errorw("attach by id invalid access") return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "invalid access") } ord, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo) if err != nil { l.Errorw("attach by id order not exist", logger.Field("orderNo", req.OrderNo)) return nil, errors.Wrapf(xerr.NewErrCode(xerr.OrderNotExist), "order not exist") } pay, err := l.svcCtx.PaymentModel.FindOne(l.ctx, ord.PaymentId) if err != nil { l.Errorw("attach by id payment not found", logger.Field("paymentId", ord.PaymentId)) return nil, errors.Wrapf(xerr.NewErrCode(xerr.PaymentMethodNotFound), "payment not found") } hasKey := false if pay.Config != "" && (strings.Contains(pay.Config, "-----BEGIN PRIVATE KEY-----") || strings.Contains(pay.Config, "BEGIN PRIVATE KEY")) { hasKey = true } l.Infow("attach by id payment config meta", logger.Field("paymentId", pay.Id), logger.Field("platform", pay.Platform), logger.Field("config_len", len(pay.Config)), logger.Field("has_private_key", hasKey)) if pay.Config == "" { l.Errorw("attach by id iap config empty", logger.Field("paymentId", pay.Id)) return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "iap config is empty") } var cfg payment.AppleIAPConfig if err := cfg.Unmarshal([]byte(pay.Config)); err != nil { l.Errorw("attach by id iap config error", logger.Field("error", err.Error()), logger.Field("paymentId", pay.Id), logger.Field("platform", pay.Platform), logger.Field("config_len", len(pay.Config)), logger.Field("has_private_key", hasKey)) return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "iap config error") } apiCfg := iapapple.ServerAPIConfig{ KeyID: cfg.KeyID, IssuerID: cfg.IssuerID, PrivateKey: cfg.PrivateKey, Sandbox: cfg.Sandbox, } if req.Sandbox != nil { apiCfg.Sandbox = *req.Sandbox } 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") } jws, err := iapapple.GetTransactionInfo(apiCfg, req.TransactionId) if err != nil { l.Errorw("fetch transaction info error", logger.Field("error", err.Error())) return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "fetch transaction info error") } // reuse existing attach logic with JWS attach := NewAttachTransactionLogic(l.ctx, l.svcCtx) resp, e := attach.Attach(&types.AttachAppleTransactionRequest{ SignedTransactionJWS: jws, SubscribeId: 0, DurationDays: 0, Tier: "", OrderNo: req.OrderNo, }) if e != nil { l.Errorw("attach by id commit error", logger.Field("error", e.Error())) return nil, e } l.Infow("attach by transaction id ok", logger.Field("orderNo", req.OrderNo), logger.Field("transactionId", req.TransactionId), logger.Field("expiresAt", resp.ExpiresAt)) return resp, nil }