From f3e3aff9f4b437eaf18d98280ea158bb1df18da1 Mon Sep 17 00:00:00 2001 From: Anan Widianto Date: Mon, 25 Dec 2023 12:33:20 +0700 Subject: [PATCH] feature: email notification when ordering --- module/feature/order/interface.go | 4 +- module/feature/order/repository/repository.go | 4 +- module/feature/order/service/service.go | 53 +++---------------- utils/payment/request.go | 36 ++++--------- 4 files changed, 19 insertions(+), 78 deletions(-) diff --git a/module/feature/order/interface.go b/module/feature/order/interface.go index c4bd3c9..01158b6 100644 --- a/module/feature/order/interface.go +++ b/module/feature/order/interface.go @@ -15,7 +15,7 @@ type RepositoryOrderInterface interface { GetOrderById(orderID string) (*entities.OrderModels, error) CreateOrder(newOrder *entities.OrderModels) (*entities.OrderModels, error) ConfirmPayment(orderID string, orderStatus, paymentStatus string) error - ProcessGatewayPayment(totalAmountPaid uint64, orderID string, paymentMethod string) (interface{}, error) + ProcessGatewayPayment(totalAmountPaid uint64, orderID string, paymentMethod, name, email string) (interface{}, error) CheckTransaction(orderID string) (dto.Status, error) UpdateOrderStatus(req *dto.UpdateOrderStatus) error GetAllOrdersByUserID(userID uint64) ([]*entities.OrderModels, error) @@ -72,7 +72,7 @@ type ServiceOrderInterface interface { GetOrderByDateRangeAndPaymentStatus(filterType, status string, page, perPage int) ([]*entities.OrderModels, int64, error) GetOrderByPaymentStatus(orderStatus string, page, perPage int) ([]*entities.OrderModels, int64, error) ProcessManualPayment(orderID string) (*entities.OrderModels, error) - ProcessGatewayPayment(totalAmountPaid uint64, orderID string, paymentMethod string) (interface{}, error) + ProcessGatewayPayment(totalAmountPaid uint64, orderID string, paymentMethod, name, email string) (interface{}, error) SendNotificationOrder(request dto.SendNotificationOrderRequest) (string, error) SendNotificationPayment(request dto.SendNotificationPaymentRequest) (string, error) } diff --git a/module/feature/order/repository/repository.go b/module/feature/order/repository/repository.go index 99c1fed..d311181 100644 --- a/module/feature/order/repository/repository.go +++ b/module/feature/order/repository/repository.go @@ -124,7 +124,7 @@ func (r *OrderRepository) ConfirmPayment(orderID, orderStatus, paymentStatus str return nil } -func (r *OrderRepository) ProcessGatewayPayment(totalAmountPaid uint64, orderID string, paymentMethod string) (interface{}, error) { +func (r *OrderRepository) ProcessGatewayPayment(totalAmountPaid uint64, orderID string, paymentMethod, name, email string) (interface{}, error) { var paymentType coreapi.CoreapiPaymentType switch paymentMethod { @@ -137,7 +137,7 @@ func (r *OrderRepository) ProcessGatewayPayment(totalAmountPaid uint64, orderID } coreClient := r.coreClient - resp, err := payment.CreateCoreAPIPaymentRequest(coreClient, orderID, int64(totalAmountPaid), paymentType) + resp, err := payment.CreateCoreAPIPaymentRequest(coreClient, orderID, int64(totalAmountPaid), paymentType, name, email) if err != nil { logrus.Error(err) return nil, errors.New("gagal membuat permintaan pembayaran") diff --git a/module/feature/order/service/service.go b/module/feature/order/service/service.go index 38fd988..a62f4d1 100644 --- a/module/feature/order/service/service.go +++ b/module/feature/order/service/service.go @@ -262,7 +262,7 @@ func (s *OrderService) CreateOrder(userID uint64, request *dto.CreateOrderReques case "whatsapp", "telegram": return s.ProcessManualPayment(orderID) case "qris", "bank_transfer", "gopay", "shopepay": - return s.ProcessGatewayPayment(totalAmountPaid, createdOrder.ID, request.PaymentMethod) + return s.ProcessGatewayPayment(totalAmountPaid, createdOrder.ID, request.PaymentMethod, user.Name, user.Email) default: return nil, errors.New("jenis pembayaran tidak valid") } @@ -421,7 +421,7 @@ func (s *OrderService) CreateOrderFromCart(userID uint64, request *dto.CreateOrd case "whatsapp", "telegram": return s.ProcessManualPayment(orderID) case "qris", "bank_transfer", "gopay", "shopepay": - return s.ProcessGatewayPayment(totalAmountPaid, createdOrder.ID, request.PaymentMethod) + return s.ProcessGatewayPayment(totalAmountPaid, createdOrder.ID, request.PaymentMethod, user.Name, user.Email) default: return nil, errors.New("jenis pembayaran tidak valid") } @@ -435,8 +435,8 @@ func (s *OrderService) ProcessManualPayment(orderID string) (*entities.OrderMode return result, nil } -func (s *OrderService) ProcessGatewayPayment(totalAmountPaid uint64, orderID string, paymentMethod string) (interface{}, error) { - result, err := s.repo.ProcessGatewayPayment(totalAmountPaid, orderID, paymentMethod) +func (s *OrderService) ProcessGatewayPayment(totalAmountPaid uint64, orderID string, paymentMethod, name, email string) (interface{}, error) { + result, err := s.repo.ProcessGatewayPayment(totalAmountPaid, orderID, paymentMethod, name, email) if err != nil { return nil, err } @@ -459,55 +459,14 @@ func (s *OrderService) CallBack(notifPayload map[string]interface{}) error { return errors.New("transaction data not found") } - user, err := s.userService.GetUsersById(transaction.UserID) - if err != nil { - return errors.New("pengguna tidak ditemukan") - } - if status.PaymentStatus == "Konfirmasi" { - user.Exp += transaction.GrandTotalExp - if _, err := s.userService.UpdateUserExp(user.ID, user.Exp); err != nil { - return err - } - - user.TotalGram += transaction.GrandTotalGramPlastic - if _, err := s.userService.UpdateUserContribution(user.ID, user.TotalGram); err != nil { - return err - } - notificationRequest := dto.SendNotificationPaymentRequest{ - OrderID: orderID, - UserID: user.ID, - PaymentStatus: "Konfirmasi", - Token: user.DeviceToken, - } - - _, err = s.SendNotificationPayment(notificationRequest) - if err != nil { - logrus.Error("Gagal mengirim notifikasi: ", err) + if err := s.ConfirmPayment(transaction.ID); err != nil { return err } - } else if status.PaymentStatus == "Gagal" { - - for _, orderDetail := range transaction.OrderDetails { - if err := s.productService.IncreaseStock(orderDetail.ProductID, orderDetail.Quantity); err != nil { - return errors.New("failed to increase product stock") - } - } - - notificationRequest := dto.SendNotificationPaymentRequest{ - OrderID: orderID, - UserID: user.ID, - PaymentStatus: "Gagal", - Token: user.DeviceToken, - } - - _, err = s.SendNotificationPayment(notificationRequest) - if err != nil { - logrus.Error("Gagal mengirim notifikasi: ", err) + if err := s.CancelPayment(transaction.ID); err != nil { return err } - } return nil diff --git a/utils/payment/request.go b/utils/payment/request.go index d1ce2ca..9833f01 100644 --- a/utils/payment/request.go +++ b/utils/payment/request.go @@ -7,11 +7,11 @@ import ( "github.com/midtrans/midtrans-go/coreapi" ) -func CreateCoreAPIPaymentRequest(coreClient coreapi.Client, orderID string, totalAmountPaid int64, paymentType coreapi.CoreapiPaymentType) (*coreapi.ChargeResponse, error) { +func CreateCoreAPIPaymentRequest(coreClient coreapi.Client, orderID string, totalAmountPaid int64, paymentType coreapi.CoreapiPaymentType, name string, email string) (*coreapi.ChargeResponse, error) { var paymentRequest *coreapi.ChargeReq switch paymentType { - case coreapi.PaymentTypeQris: + case coreapi.PaymentTypeQris, coreapi.PaymentTypeBankTransfer, coreapi.PaymentTypeGopay, coreapi.PaymentTypeShopeepay: paymentRequest = &coreapi.ChargeReq{ PaymentType: paymentType, TransactionDetails: midtrans.TransactionDetails{ @@ -19,41 +19,23 @@ func CreateCoreAPIPaymentRequest(coreClient coreapi.Client, orderID string, tota GrossAmt: totalAmountPaid, }, } - case coreapi.PaymentTypeBankTransfer: - paymentRequest = &coreapi.ChargeReq{ - PaymentType: paymentType, - TransactionDetails: midtrans.TransactionDetails{ - OrderID: orderID, - GrossAmt: totalAmountPaid, - }, - } - case coreapi.PaymentTypeGopay: - paymentRequest = &coreapi.ChargeReq{ - PaymentType: paymentType, - TransactionDetails: midtrans.TransactionDetails{ - OrderID: orderID, - GrossAmt: totalAmountPaid, - }, - } - case coreapi.PaymentTypeShopeepay: - paymentRequest = &coreapi.ChargeReq{ - PaymentType: paymentType, - TransactionDetails: midtrans.TransactionDetails{ - OrderID: orderID, - GrossAmt: totalAmountPaid, - }, - } - default: return nil, errors.New("Jenis pembayaran tidak valid") } + paymentRequest.CustomerDetails = &midtrans.CustomerDetails{ + FName: name, + Email: email, + } + resp, err := coreClient.ChargeTransaction(paymentRequest) if err != nil { fmt.Println("Error creating payment request:", err.GetMessage()) return nil, err } + fmt.Println("Menyimpan data pembayaran: OrderID=", orderID, " Name=", name, " Email=", email) fmt.Println("Payment request created successfully:", resp) + return resp, nil }