@@ -3,7 +3,6 @@ package subscriptions
33import (
44 "database/sql"
55 "errors"
6- "fmt"
76 "time"
87 "website-pb/config"
98
@@ -60,15 +59,17 @@ func (s *SubscriptionService) CreateCheckoutSession(user *core.Record, plan stri
6059 return "" , ErrPlanInvalid
6160 }
6261
63- _ , err := s .CheckValidSubscription (user .Original ())
64-
65- if ! errors .Is (err , sql .ErrNoRows ) {
62+ sub , err := s .CheckValidSubscription (user .Original ())
6663
64+ // 情况 A: 找到了有效订阅 (err == nil)
65+ if err == nil && sub != nil {
6766 return "" , ErrAlreadySubscribed
6867 }
6968
70- if err != nil {
71- s .app .Logger ().Warn ("Failed to create checkout session" , err )
69+ // 情况 B: 发生了真正的数据库错误 (不是“没找到”)
70+ if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
71+ // 结构化日志修复:使用 key-value
72+ s .app .Logger ().Error ("Failed to check subscription" , "error" , err .Error (), "userId" , user .Id )
7273 return "" , errors .New ("check subscription failed" )
7374 }
7475
@@ -77,11 +78,8 @@ func (s *SubscriptionService) CreateCheckoutSession(user *core.Record, plan stri
7778 CancelURL : stripe .String (frontendURL ),
7879 Mode : stripe .String (string (stripe .CheckoutSessionModeSubscription )),
7980 ClientReferenceID : stripe .String (user .Id ),
80- SubscriptionData : & stripe.CheckoutSessionSubscriptionDataParams {
81- Metadata : map [string ]string {"plan" : "pro" }, // 存到订阅对象里
82- },
83- Metadata : map [string ]string {"plan" : plan },
84- LineItems : []* stripe.CheckoutSessionLineItemParams {{Price : stripe .String (priceID ), Quantity : stripe .Int64 (1 )}},
81+ Metadata : map [string ]string {"plan" : plan },
82+ LineItems : []* stripe.CheckoutSessionLineItemParams {{Price : stripe .String (priceID ), Quantity : stripe .Int64 (1 )}},
8583 }
8684
8785 // 关联已有 Stripe Customer ID
@@ -122,21 +120,16 @@ func (s *SubscriptionService) HandleInvoicePaid(inv stripe.Invoice) error {
122120
123121 record := core .NewRecord (collection )
124122
125- fmt .Println (inv .Lines .Data [0 ].Pricing .PriceDetails .Price )
126-
127123 priceID := inv .Lines .Data [0 ].Pricing .PriceDetails .Price
128124
129125 priceIDMap := s .cfg .PriceToPlan [priceID ]
130- fmt .Println (priceID )
131126
132127 if priceIDMap == "" {
133128 s .app .Logger ().Warn ("No prices found for invoice " , inv )
134129 return errors .New ("invalid price" )
135130 }
136131
137132 expiresAt := time .Unix (inv .Lines .Data [0 ].Period .End , 0 ).UTC ()
138- fmt .Println ("Expires at:" , expiresAt )
139- fmt .Println (inv .Lines .Data [0 ].Period .End )
140133
141134 record .Set ("user_id" , user .Id )
142135 record .Set ("plan" , priceIDMap )
0 commit comments