@@ -227,7 +227,7 @@ func (s *ApiServer) handleCodepayCreateHelper(sessionId string, w http.ResponseW
227227 if err != nil {
228228 // print error
229229 slog .Error ("failed to generate qr code" , "error" , err )
230- resBuf , err := json .MarshalIndent (map [string ]interface {} {
230+ resBuf , err := json .MarshalIndent (map [string ]any {
231231 "success" : false ,
232232 "message" : "failed to generate qr code: server internal error" ,
233233 }, "" , " " )
@@ -319,12 +319,12 @@ func (s *ApiServer) handleCodepayQueryHelper(sessionId string, w http.ResponseWr
319319 return
320320 }
321321
322- var response map [string ]interface {}
322+ var response map [string ]any
323323 // check if monDealCur exists
324324 if _ , ok := res ["monDealCur" ]; ok {
325325 // monDealCur exists, it's a completed deal
326326 delete (codepayInstances , code )
327- response = map [string ]interface {} {
327+ response = map [string ]any {
328328 "status" : 1 ,
329329 "message" : "payment completed" ,
330330 "money" : res ["monDealCur" ],
@@ -335,13 +335,13 @@ func (s *ApiServer) handleCodepayQueryHelper(sessionId string, w http.ResponseWr
335335 if time .Now ().Unix ()- codepay .Creation > 30 {
336336 // 30s limit exceeded, remove the codepay instance
337337 delete (codepayInstances , code )
338- response = map [string ]interface {} {
338+ response = map [string ]any {
339339 "status" : 2 ,
340340 "message" : "payment code expired" ,
341341 }
342342 } else {
343343 // 30s limit not exceeded, keep the codepay instance
344- response = map [string ]interface {} {
344+ response = map [string ]any {
345345 "status" : 0 ,
346346 "message" : "pending" ,
347347 }
@@ -388,6 +388,63 @@ func (s *ApiServer) handleCodepayQueryPath(w http.ResponseWriter, r *http.Reques
388388 s .handleCodepayQueryHelper (sessionId , w , r )
389389}
390390
391+ func (s * ApiServer ) handleRecentTransactions (w http.ResponseWriter , r * http.Request ) {
392+ w .Header ().Set ("Access-Control-Allow-Origin" , "*" )
393+ if r .Method == http .MethodOptions {
394+ return
395+ }
396+
397+ vars := mux .Vars (r )
398+ sessionId := vars ["sessionId" ]
399+ if sessionId == "" {
400+ http .Error (w , "sessionId required in path" , http .StatusBadRequest )
401+ return
402+ }
403+
404+ user := s .cfg .SelectUserFromSessionId (sessionId )
405+ if user == nil {
406+ http .Error (w , "user with sessionId=" + sessionId + " not found" , http .StatusNotFound )
407+ return
408+ }
409+
410+ _ , transactions , err := xfb .CardQuerynoPage (user .SessionId , user .YmUserId , time .Now ())
411+ if err != nil {
412+ http .Error (w , "unable to fetch recent transactions: " + err .Error (), http .StatusInternalServerError )
413+ return
414+ }
415+
416+ // Limit to at most 3 transactions
417+ if len (transactions ) > 3 {
418+ transactions = transactions [len (transactions )- 3 :]
419+ }
420+
421+ resBuf , err := json .MarshalIndent (transactions , "" , " " )
422+ if err != nil {
423+ http .Error (w , err .Error (), http .StatusInternalServerError )
424+ return
425+ }
426+
427+ w .Header ().Set ("Content-Type" , "application/json" )
428+ w .WriteHeader (http .StatusOK )
429+ w .Write (resBuf )
430+ }
431+
432+ func (s * ApiServer ) handleRecentTransactionsPath (w http.ResponseWriter , r * http.Request ) {
433+ w .Header ().Set ("Access-Control-Allow-Origin" , "*" )
434+ if r .Method == http .MethodOptions {
435+ return
436+ }
437+
438+ vars := mux .Vars (r )
439+ sessionId := vars ["sessionId" ]
440+ if sessionId == "" {
441+ http .Error (w , "sessionId required in path" , http .StatusBadRequest )
442+ return
443+ }
444+
445+ s .handleRecentTransactions (w , r )
446+ }
447+
391448func CreateApiServer (cfg * Config ) * mux.Router {
392449 r := mux .NewRouter ()
393450 s := & ApiServer {
@@ -405,10 +462,12 @@ func CreateApiServer(cfg *Config) *mux.Router {
405462 // Codepay endpoints
406463 r .HandleFunc ("/api/v1/codepay/create" , s .handleCodepayCreate ).Methods (http .MethodPost , http .MethodOptions )
407464 r .HandleFunc ("/api/v1/codepay/query" , s .handleCodepayQuery ).Methods (http .MethodGet , http .MethodOptions )
465+ r .HandleFunc ("/api/v1/codepay/recentTransactions" , s .handleRecentTransactions ).Methods (http .MethodGet , http .MethodOptions )
408466
409467 // Codepay endpoints with sessionId embedded in path
410468 r .HandleFunc ("/api/v1/codepay/{sessionId}/create" , s .handleCodepayCreatePath ).Methods (http .MethodGet , http .MethodPost , http .MethodOptions )
411469 r .HandleFunc ("/api/v1/codepay/{sessionId}/query" , s .handleCodepayQueryPath ).Methods (http .MethodGet , http .MethodOptions )
470+ r .HandleFunc ("/api/v1/codepay/{sessionId}/recentTransactions" , s .handleRecentTransactionsPath ).Methods (http .MethodGet , http .MethodOptions )
412471
413472 r .Use (mux .CORSMethodMiddleware (r ))
414473 return r
0 commit comments