Skip to content

Commit

Permalink
feature: email notification when ordering (#115)
Browse files Browse the repository at this point in the history
feature: email notification when ordering
  • Loading branch information
masnann authored Dec 25, 2023
2 parents baeb340 + f3e3aff commit 92249f3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 78 deletions.
4 changes: 2 additions & 2 deletions module/feature/order/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions module/feature/order/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
53 changes: 6 additions & 47 deletions module/feature/order/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
36 changes: 9 additions & 27 deletions utils/payment/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,35 @@ 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{
OrderID: orderID,
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
}

0 comments on commit 92249f3

Please sign in to comment.