Feat(booking): New payment mode - KTMWallet
All checks were successful
ktm-booking-bot/ktm-booking-bot/pipeline/head This commit looks good
All checks were successful
ktm-booking-bot/ktm-booking-bot/pipeline/head This commit looks good
This commit is contained in:
@@ -583,7 +583,7 @@ const docTemplate = `{
|
|||||||
},
|
},
|
||||||
"ktmTrainCreditCardType": {
|
"ktmTrainCreditCardType": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"example": "Visa|Mastercard"
|
"example": "Visa|Mastercard|KTMWallet"
|
||||||
},
|
},
|
||||||
"ktmTrainPassword": {
|
"ktmTrainPassword": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
|
|||||||
@@ -575,7 +575,7 @@
|
|||||||
},
|
},
|
||||||
"ktmTrainCreditCardType": {
|
"ktmTrainCreditCardType": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"example": "Visa|Mastercard"
|
"example": "Visa|Mastercard|KTMWallet"
|
||||||
},
|
},
|
||||||
"ktmTrainPassword": {
|
"ktmTrainPassword": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ definitions:
|
|||||||
example: 01/2022
|
example: 01/2022
|
||||||
type: string
|
type: string
|
||||||
ktmTrainCreditCardType:
|
ktmTrainCreditCardType:
|
||||||
example: Visa|Mastercard
|
example: Visa|Mastercard|KTMWallet
|
||||||
type: string
|
type: string
|
||||||
ktmTrainPassword:
|
ktmTrainPassword:
|
||||||
example: password
|
example: password
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ func (env *Env) BackgroundJobRunner() {
|
|||||||
} else { // if there's job to do
|
} else { // if there's job to do
|
||||||
// Create next run where it's not the past (either from old NextRun or now())
|
// Create next run where it's not the past (either from old NextRun or now())
|
||||||
timeNow := time.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)
|
endTime := startTime.Add(10 * time.Minute)
|
||||||
|
|
||||||
if timeNow.After(startTime) && timeNow.Before(endTime) {
|
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)
|
success := env.startBooking(&jobToDo, username, password, creditCardType, creditCard, creditCardCVV, creditCardExpiry)
|
||||||
if success {
|
if success {
|
||||||
jobToDo.Status = "success"
|
jobToDo.Status = "success"
|
||||||
env.DB.Save(jobToDo)
|
env.DB.Save(&jobToDo)
|
||||||
fmt.Println("Successfully made a booking.")
|
fmt.Println("Successfully made a booking.")
|
||||||
} else {
|
} else {
|
||||||
// Sleep for 10 minutes before setting status to pending
|
// 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:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
page = choosePayment(page)
|
if strings.ToUpper(creditCardType) == "VISA" || strings.ToUpper(creditCardType) == "MASTERCARD" {
|
||||||
log.Println("Payment method chosen.")
|
page = choosePaymentCreditCard(page)
|
||||||
|
log.Println("Credit card payment method chosen.")
|
||||||
|
|
||||||
// Wait 5 seconds for payment gateway to load
|
// Wait 5 seconds for payment gateway to load
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
|
|
||||||
for _, currPage := range browser.MustPages() {
|
for _, currPage := range browser.MustPages() {
|
||||||
currPage.MustWaitLoad()
|
currPage.MustWaitLoad()
|
||||||
|
|
||||||
var currTitle string
|
var currTitle string
|
||||||
err := rod.Try(func() {
|
err := rod.Try(func() {
|
||||||
currTitle = currPage.Timeout(100 * time.Millisecond).MustElement("title").MustText()
|
currTitle = currPage.Timeout(100 * time.Millisecond).MustElement("title").MustText()
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
currTitle = ""
|
currTitle = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(currTitle, "Payment Acceptance") {
|
||||||
|
page = currPage
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.Contains(currTitle, "Payment Acceptance") {
|
// Exits if context cancelled
|
||||||
page = currPage
|
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
|
// // Start debug screenshots
|
||||||
// debugScreenshotCtx, cancelDebugScreenshot := context.WithCancel(context.Background())
|
// debugScreenshotCtx, cancelDebugScreenshot := context.WithCancel(context.Background())
|
||||||
// go takeDebugScreenshots(debugScreenshotCtx, courtPage)
|
// go takeDebugScreenshots(debugScreenshotCtx, courtPage)
|
||||||
@@ -499,7 +505,15 @@ func fillPassengerDetails(page *rod.Page, name string, gender string, passport s
|
|||||||
return page
|
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 := page.MustElement(".btn-public-bank")
|
||||||
creditCardButton.Eval(`this.click()`)
|
creditCardButton.Eval(`this.click()`)
|
||||||
page.MustWaitLoad()
|
page.MustWaitLoad()
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type Profile struct {
|
|||||||
type ProfileRequest struct {
|
type ProfileRequest struct {
|
||||||
KtmTrainUsername string `json:"ktmTrainUsername" swaggertype:"string" example:"user@gmail.com"`
|
KtmTrainUsername string `json:"ktmTrainUsername" swaggertype:"string" example:"user@gmail.com"`
|
||||||
KtmTrainPassword string `json:"ktmTrainPassword" swaggertype:"string" example:"password"`
|
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"`
|
KtmTrainCreditCard string `json:"ktmTrainCreditCard" swaggertype:"string" example:"1234123412341234"`
|
||||||
KtmTrainCreditCardExpiry string `json:"ktmTrainCreditCardExpiry" swaggertype:"string" example:"01/2022"`
|
KtmTrainCreditCardExpiry string `json:"ktmTrainCreditCardExpiry" swaggertype:"string" example:"01/2022"`
|
||||||
KtmTrainCreditCardCVV string `json:"ktmTrainCreditCardCVV" swaggertype:"string" example:"123"`
|
KtmTrainCreditCardCVV string `json:"ktmTrainCreditCardCVV" swaggertype:"string" example:"123"`
|
||||||
|
|||||||
Reference in New Issue
Block a user