Skip to content

Commit

Permalink
updte webhok
Browse files Browse the repository at this point in the history
  • Loading branch information
lendral3n committed Feb 13, 2024
1 parent 7aa5076 commit 6643cdb
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 81 deletions.
81 changes: 50 additions & 31 deletions features/booking/data/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,27 @@ type Booking struct {
DeletedAt gorm.DeletedAt `gorm:"index"`
User ud.User `gorm:"foreignKey:UserId"`
BoardingHouse kd.BoardingHouse `gorm:"foreignKey:BoardingHouseId"`
Payment Payment `gorm:"embedded;embeddedPrefix:payment_"`
Method string `gorm:"column:method; type:varchar(20);"`
Bank string `gorm:"column:bank; type:varchar(20);"`
VirtualNumber string `gorm:"column:virtual_number; type:varchar(50);"`
BillKey string `gorm:"column:bill_key; type:varchar(50);"`
BillCode string `gorm:"column:bill_code; type:varchar(50);"`
CreatedAt time.Time `gorm:"index"`
ExpiredAt *time.Time `gorm:"nullable"`
PaidAt *time.Time `gorm:"default:null;"`
// Payment Payment `gorm:"embedded;embeddedPrefix:payment_"`
}

type Payment struct {
Method string `gorm:"column:method; type:varchar(20);"`
Bank string `gorm:"column:bank; type:varchar(20);"`
VirtualNumber string `gorm:"column:virtual_number; type:varchar(50);"`
BillKey string `gorm:"column:bill_key; type:varchar(50);"`
BillCode string `gorm:"column:bill_code; type:varchar(50);"`
CreatedAt time.Time `gorm:"index"`
ExpiredAt *time.Time `gorm:"nullable"`
PaidAt *time.Time `gorm:"default:null;"`
}
// type Payment struct {
// Method string `gorm:"column:method; type:varchar(20);"`
// Bank string `gorm:"column:bank; type:varchar(20);"`
// VirtualNumber string `gorm:"column:virtual_number; type:varchar(50);"`
// BillKey string `gorm:"column:bill_key; type:varchar(50);"`
// BillCode string `gorm:"column:bill_code; type:varchar(50);"`
// CreatedAt time.Time `gorm:"index"`
// ExpiredAt *time.Time `gorm:"nullable"`
// PaidAt *time.Time `gorm:"default:null;"`
// }

// type MonthCount struct {
// Month int
Expand All @@ -46,21 +54,26 @@ func CoreToModelBook(input booking.BookingCore) Booking {
Total: input.Total,
BookedAt: input.BookedAt,
Status: input.Status,
Payment: CoreToModelPay(input.Payment),
Method: input.Method,
Bank: input.Bank,
VirtualNumber: input.VirtualNumber,
ExpiredAt: &input.ExpiredAt,
PaidAt: &input.PaidAt,
// Payment: CoreToModelPay(input.Payment),
}
}

func CoreToModelPay(input booking.PaymentCore) Payment {
return Payment{
Method: input.Method,
Bank: input.Bank,
VirtualNumber: input.VirtualNumber,
BillKey: input.BillKey,
BillCode: input.BillCode,
ExpiredAt: &input.ExpiredAt,
PaidAt: &input.PaidAt,
}
}
// func CoreToModelPay(input booking.PaymentCore) Payment {
// return Payment{
// Method: input.Method,
// Bank: input.Bank,
// VirtualNumber: input.VirtualNumber,
// BillKey: input.BillKey,
// BillCode: input.BillCode,
// ExpiredAt: &input.ExpiredAt,
// PaidAt: &input.PaidAt,
// }
// }

func CoreToModelBookCancel(input booking.BookingCore) Booking {
return Booking{
Expand All @@ -75,23 +88,29 @@ func ModelToCoreBook(model Booking) booking.BookingCore {
UserId: model.UserId,
Status: model.Status,
BoardingHouse: model.BoardingHouse.ModelToCoreKos(),
Payment: PaymentModelToCore(model.Payment),
}
}

func PaymentModelToCore(model Payment) booking.PaymentCore {
return booking.PaymentCore{
Method: model.Method,
Bank: model.Bank,
VirtualNumber: model.VirtualNumber,
BillKey: model.BillKey,
BillCode: model.BillCode,
CreatedAt: model.CreatedAt,
ExpiredAt: *model.ExpiredAt,
PaidAt: *model.PaidAt,
// Payment: PaymentModelToCore(model.Payment),
}
}

// func PaymentModelToCore(model Payment) booking.PaymentCore {
// return booking.PaymentCore{
// Method: model.Method,
// Bank: model.Bank,
// VirtualNumber: model.VirtualNumber,
// BillKey: model.BillKey,
// BillCode: model.BillCode,
// CreatedAt: model.CreatedAt,
// ExpiredAt: *model.ExpiredAt,
// PaidAt: *model.PaidAt,
// }
// }

func WebhoocksCoreToModel(reqNotif booking.BookingCore) Booking {
return Booking{
Code: reqNotif.Code,
Expand Down
19 changes: 9 additions & 10 deletions features/booking/data/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ func (repo *bookQuery) Insert(userIdLogin int, input booking.BookingCore) (*book
return nil, errPay
}

bookModel.Payment.Method = payment.Method
bookModel.Payment.Bank = payment.Bank
bookModel.Payment.VirtualNumber = payment.VirtualNumber
bookModel.Payment.BillKey = payment.BillKey
bookModel.Payment.BillCode = payment.BillCode
bookModel.Payment.ExpiredAt = &payment.ExpiredAt
bookModel.Payment.PaidAt = &payment.PaidAt
bookModel.Method = payment.Method
bookModel.Bank = payment.Bank
bookModel.VirtualNumber = payment.VirtualNumber
bookModel.Status= payment.Status
bookModel.ExpiredAt = &payment.ExpiredAt
bookModel.PaidAt = &payment.PaidAt

log.Println("input bookmodel", bookModel)

Expand All @@ -66,9 +65,9 @@ func (repo *bookQuery) Insert(userIdLogin int, input booking.BookingCore) (*book
}

bookCore := ModelToCoreBook(bookModel)
if payment != nil {
bookCore.Payment = *payment
}
// if payment != nil {
// bookCore = *payment
// }

return &bookCore, nil
}
Expand Down
38 changes: 23 additions & 15 deletions features/booking/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,40 @@ type BookingCore struct {
Total float64
BookedAt time.Time
DeletedAt gorm.DeletedAt
Status string
UserId uint
User ud.Core
BoardingHouseId uint
BoardingHouse kd.Core
Payment PaymentCore
Method string
Bank string
VirtualNumber string
BookingCode int
BookingTotal float64
Status string
CreatedAt time.Time
ExpiredAt time.Time
PaidAt time.Time
// Payment PaymentCore
}

type MonthCount struct {
Month int
Count int
}


type PaymentCore struct {
Method string
Bank string
VirtualNumber string
BillKey string
BillCode string
BookingCode int
BookingTotal float64
CreatedAt time.Time
ExpiredAt time.Time
PaidAt time.Time
}
// type PaymentCore struct {
// Method string
// Bank string
// VirtualNumber string
// BillKey string
// BillCode string
// BookingCode int
// BookingTotal float64
// Status string
// CreatedAt time.Time
// ExpiredAt time.Time
// PaidAt time.Time
// }

type BookDataInterface interface {
Insert(userIdLogin int, input BookingCore) (*BookingCore, error)
Expand Down
8 changes: 5 additions & 3 deletions features/booking/handler/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ func RequestToCoreBook(input BookRequest, userIdLogin uint) booking.BookingCore
Code: uuid.New().String(),
UserId: userIdLogin,
BoardingHouseId: input.BoardingHouseId,
Payment: booking.PaymentCore{
Method: input.Method,
Method: input.Method,
Bank: input.Bank,
},
// Payment: booking.PaymentCore{
// Method: input.Method,
// Bank: input.Bank,
// },
}
}

Expand Down
8 changes: 3 additions & 5 deletions features/booking/handler/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ func CoreToResponseBook(core *booking.BookingCore) BookingResponse {
Code: core.Code,
Status: core.Status,
Total: core.Total,
PaymentBank: core.Payment.Bank,
PaymentVirtualNumber: core.Payment.VirtualNumber,
PaymentBillKey: core.Payment.BillKey,
PaymentBillCode: core.Payment.BillCode,
PaymentExpiredAt: &core.Payment.ExpiredAt,
PaymentBank: core.Bank,
PaymentVirtualNumber: core.VirtualNumber,
PaymentExpiredAt: &core.ExpiredAt,
}
}

Expand Down
26 changes: 9 additions & 17 deletions utils/externalapi/midtrans.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type MidtransInterface interface {
NewOrderPayment(book booking.BookingCore) (*booking.PaymentCore, error)
NewOrderPayment(book booking.BookingCore) (*booking.BookingCore, error)
CancelOrderPayment(bookingId string) error
}

Expand All @@ -32,14 +32,14 @@ func New() MidtransInterface {
}

// NewOrderPayment implements Midtrans.
func (pay *midtrans) NewOrderPayment(book booking.BookingCore) (*booking.PaymentCore, error) {
func (pay *midtrans) NewOrderPayment(book booking.BookingCore) (*booking.BookingCore, error) {
req := new(coreapi.ChargeReq)
req.TransactionDetails = mid.TransactionDetails{
OrderID: book.Code,
GrossAmt: int64(book.Total),
}

switch book.Payment.Bank {
switch book.Bank {
case "bca":
req.PaymentType = coreapi.PaymentTypeBankTransfer
req.BankTransfer = &coreapi.BankTransferDetails{
Expand Down Expand Up @@ -72,24 +72,16 @@ func (pay *midtrans) NewOrderPayment(book booking.BookingCore) (*booking.Payment
return nil, errors.New(res.StatusMessage)
}

if res.BillKey != "" {
book.Payment.BillKey = res.BillKey
}

if res.BillerCode != "" {
book.Payment.BillCode = res.BillerCode
}

if len(res.VaNumbers) == 1 {
book.Payment.VirtualNumber = res.VaNumbers[0].VANumber
book.VirtualNumber = res.VaNumbers[0].VANumber
}

if res.PermataVaNumber != "" {
book.Payment.VirtualNumber = res.PermataVaNumber
book.VirtualNumber = res.PermataVaNumber
}

if res.PaymentType != "" {
book.Payment.Method = res.PaymentType
book.Method = res.PaymentType
}

if res.TransactionStatus != "" {
Expand All @@ -99,12 +91,12 @@ func (pay *midtrans) NewOrderPayment(book booking.BookingCore) (*booking.Payment
if expiredAt, err := time.Parse("2006-01-02 15:04:05", res.ExpiryTime); err != nil {
return nil, err
} else {
book.Payment.ExpiredAt = expiredAt
book.ExpiredAt = expiredAt
}

book.Payment.BookingTotal = book.Total
book.BookingTotal = book.Total

return &book.Payment, nil
return &book, nil
}

func (pay *midtrans) CancelOrderPayment(bookingId string) error {
Expand Down

0 comments on commit 6643cdb

Please sign in to comment.