From 8be27ce2149a1565c358f5b38de066f468ec8561 Mon Sep 17 00:00:00 2001 From: Samuel Pua Date: Fri, 28 Oct 2022 00:51:00 +0800 Subject: [PATCH] Feat(booking-job): Loop detect payment merchant --- .../ktmtrainbot/backgroundbookingjob.go | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/backend/internal/ktmtrainbot/backgroundbookingjob.go b/backend/internal/ktmtrainbot/backgroundbookingjob.go index 8794a30..2c7d6f8 100644 --- a/backend/internal/ktmtrainbot/backgroundbookingjob.go +++ b/backend/internal/ktmtrainbot/backgroundbookingjob.go @@ -322,29 +322,37 @@ func chooseAndMakePayment(timerCtx context.Context, creditCardType, creditCard, log.Println("Waiting for payment gateway to load.") time.Sleep(time.Second * 5) - for _, currPage := range browser.MustPages() { - currPage.MustWaitLoad() + foundPage := false + + for !foundPage { + for _, currPage := range browser.MustPages() { + currPage.MustWaitLoad() + + var currTitle string + err := rod.Try(func() { + currTitle = currPage.Timeout(100 * time.Millisecond).MustElement("title").MustText() + }) + if err != nil { + currTitle = "" + } - var currTitle string - err := rod.Try(func() { - currTitle = currPage.Timeout(100 * time.Millisecond).MustElement("title").MustText() - }) - if err != nil { - currTitle = "" + if strings.Contains(currTitle, "Payment Acceptance") { + log.Println("Page found.") + foundPage = true + page = currPage + } } - if strings.Contains(currTitle, "Payment Acceptance") { - page = currPage + // Exits if context cancelled + select { + case <-timerCtx.Done(): + browser.MustClose() + cancelRefresh() + return false + default: } - } - // Exits if context cancelled - select { - case <-timerCtx.Done(): - browser.MustClose() - cancelRefresh() - return false - default: + time.Sleep(time.Second * 1) } expiryMonth := strings.Split(creditCardExpiry, "/")[0]