Browse Source

Feat(booking): New payment mode - KTMWallet

master
Samuel Pua 3 years ago
parent
commit
9c456b72b2
  1. 2
      backend/docs/docs.go
  2. 2
      backend/docs/swagger.json
  3. 2
      backend/docs/swagger.yaml
  4. 74
      backend/internal/ktmtrainbot/backgroundbookingjob.go
  5. 2
      backend/internal/user/profilemodel.go

2
backend/docs/docs.go

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

2
backend/docs/swagger.json

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

2
backend/docs/swagger.yaml

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

74
backend/internal/ktmtrainbot/backgroundbookingjob.go

@ -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,41 +275,47 @@ 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:
}
// Exits if context cancelled
select {
case <-timerCtx.Done():
browser.MustClose()
return false
default:
}
expiryMonth := strings.Split(creditCardExpiry, "/")[0]
expiryYear := strings.Split(creditCardExpiry, "/")[1]
expiryMonth := strings.Split(creditCardExpiry, "/")[0]
expiryYear := strings.Split(creditCardExpiry, "/")[1]
page = makePayment(page, creditCardType, creditCard, expiryMonth, expiryYear, creditCardCVV)
log.Println("Payment made.")
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.")
}
// // Start debug screenshots
// debugScreenshotCtx, cancelDebugScreenshot := context.WithCancel(context.Background())
@ -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()

2
backend/internal/user/profilemodel.go

@ -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"`

Loading…
Cancel
Save