Feat(booking): New payment mode - KTMWallet
All checks were successful
ktm-booking-bot/ktm-booking-bot/pipeline/head This commit looks good

This commit is contained in:
2022-10-10 22:39:46 +08:00
parent dcfa34176e
commit 9c456b72b2
5 changed files with 49 additions and 35 deletions

View File

@@ -583,7 +583,7 @@ const docTemplate = `{
},
"ktmTrainCreditCardType": {
"type": "string",
"example": "Visa|Mastercard"
"example": "Visa|Mastercard|KTMWallet"
},
"ktmTrainPassword": {
"type": "string",

View File

@@ -575,7 +575,7 @@
},
"ktmTrainCreditCardType": {
"type": "string",
"example": "Visa|Mastercard"
"example": "Visa|Mastercard|KTMWallet"
},
"ktmTrainPassword": {
"type": "string",

View File

@@ -106,7 +106,7 @@ definitions:
example: 01/2022
type: string
ktmTrainCreditCardType:
example: Visa|Mastercard
example: Visa|Mastercard|KTMWallet
type: string
ktmTrainPassword:
example: password

View File

@@ -77,7 +77,7 @@ func (env *Env) BackgroundJobRunner() {
} else { // if there's job to do
// Create next run where it's not the past (either from old NextRun or now())
timeNow := time.Now()
startTime := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 10, 0, 0, timeNow.Location())
startTime := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 22, 34, 0, 0, timeNow.Location())
endTime := startTime.Add(10 * time.Minute)
if timeNow.After(startTime) && timeNow.Before(endTime) {
@@ -105,7 +105,7 @@ func (env *Env) BackgroundJobRunner() {
success := env.startBooking(&jobToDo, username, password, creditCardType, creditCard, creditCardCVV, creditCardExpiry)
if success {
jobToDo.Status = "success"
env.DB.Save(jobToDo)
env.DB.Save(&jobToDo)
fmt.Println("Successfully made a booking.")
} else {
// Sleep for 10 minutes before setting status to pending
@@ -275,42 +275,48 @@ func (env *Env) startBooking(job *Booking, username string, password string, cre
default:
}
page = choosePayment(page)
log.Println("Payment method chosen.")
if strings.ToUpper(creditCardType) == "VISA" || strings.ToUpper(creditCardType) == "MASTERCARD" {
page = choosePaymentCreditCard(page)
log.Println("Credit card payment method chosen.")
// Wait 5 seconds for payment gateway to load
time.Sleep(time.Second * 5)
// Wait 5 seconds for payment gateway to load
time.Sleep(time.Second * 5)
for _, currPage := range browser.MustPages() {
currPage.MustWaitLoad()
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") {
page = currPage
}
}
if strings.Contains(currTitle, "Payment Acceptance") {
page = currPage
// Exits if context cancelled
select {
case <-timerCtx.Done():
browser.MustClose()
return false
default:
}
expiryMonth := strings.Split(creditCardExpiry, "/")[0]
expiryYear := strings.Split(creditCardExpiry, "/")[1]
page = makePayment(page, creditCardType, creditCard, expiryMonth, expiryYear, creditCardCVV)
log.Println("Credit card payment made.")
} else if strings.ToUpper(creditCardType) == "KTMWALLET" {
page = choosePaymentKTMWallet(page)
log.Println("KTM wallet payment method chosen.")
log.Println("KTM wallet payment made.")
}
// Exits if context cancelled
select {
case <-timerCtx.Done():
browser.MustClose()
return false
default:
}
expiryMonth := strings.Split(creditCardExpiry, "/")[0]
expiryYear := strings.Split(creditCardExpiry, "/")[1]
page = makePayment(page, creditCardType, creditCard, expiryMonth, expiryYear, creditCardCVV)
log.Println("Payment made.")
// // Start debug screenshots
// debugScreenshotCtx, cancelDebugScreenshot := context.WithCancel(context.Background())
// go takeDebugScreenshots(debugScreenshotCtx, courtPage)
@@ -499,7 +505,15 @@ func fillPassengerDetails(page *rod.Page, name string, gender string, passport s
return page
}
func choosePayment(page *rod.Page) *rod.Page {
func choosePaymentKTMWallet(page *rod.Page) *rod.Page {
creditCardButton := page.MustElement(".btn-ktmb-ewallet")
creditCardButton.Eval(`this.click()`)
page.MustWaitLoad()
return page
}
func choosePaymentCreditCard(page *rod.Page) *rod.Page {
creditCardButton := page.MustElement(".btn-public-bank")
creditCardButton.Eval(`this.click()`)
page.MustWaitLoad()

View File

@@ -24,7 +24,7 @@ type Profile struct {
type ProfileRequest struct {
KtmTrainUsername string `json:"ktmTrainUsername" swaggertype:"string" example:"user@gmail.com"`
KtmTrainPassword string `json:"ktmTrainPassword" swaggertype:"string" example:"password"`
KtmTrainCreditCardType string `json:"ktmTrainCreditCardType" swaggertype:"string" example:"Visa|Mastercard"`
KtmTrainCreditCardType string `json:"ktmTrainCreditCardType" swaggertype:"string" example:"Visa|Mastercard|KTMWallet"`
KtmTrainCreditCard string `json:"ktmTrainCreditCard" swaggertype:"string" example:"1234123412341234"`
KtmTrainCreditCardExpiry string `json:"ktmTrainCreditCardExpiry" swaggertype:"string" example:"01/2022"`
KtmTrainCreditCardCVV string `json:"ktmTrainCreditCardCVV" swaggertype:"string" example:"123"`