diff --git a/internal/entity/transaction.go b/internal/entity/transaction.go index 094406b..7e28546 100644 --- a/internal/entity/transaction.go +++ b/internal/entity/transaction.go @@ -15,6 +15,9 @@ type Transaction struct { Amount float64 `gorm:"not null"` CreatedAt time.Time `gorm:"autoCreateTime"` UpdatedAt time.Time `gorm:"autoUpdateTime"` + SiteID *int64 + Fee float64 + Total float64 } type TransactionDB struct { diff --git a/internal/services/order/order.go b/internal/services/order/order.go index 02e6923..2761a32 100644 --- a/internal/services/order/order.go +++ b/internal/services/order/order.go @@ -24,14 +24,15 @@ type Config interface { } type OrderService struct { - repo repository.Order - crypt repository.Crypto - product repository.Product - midtrans repository.Midtrans - payment repository.Payment - txmanager repository.TransactionManager - wallet repository.WalletRepository - cfg Config + repo repository.Order + crypt repository.Crypto + product repository.Product + midtrans repository.Midtrans + payment repository.Payment + transaction repository.TransactionRepository + txmanager repository.TransactionManager + wallet repository.WalletRepository + cfg Config } func NewOrderService( @@ -39,16 +40,19 @@ func NewOrderService( product repository.Product, crypt repository.Crypto, midtrans repository.Midtrans, payment repository.Payment, txmanager repository.TransactionManager, - wallet repository.WalletRepository, cfg Config) *OrderService { + wallet repository.WalletRepository, cfg Config, + transaction repository.TransactionRepository, +) *OrderService { return &OrderService{ - repo: repo, - product: product, - crypt: crypt, - midtrans: midtrans, - payment: payment, - txmanager: txmanager, - wallet: wallet, - cfg: cfg, + repo: repo, + product: product, + crypt: crypt, + midtrans: midtrans, + payment: payment, + txmanager: txmanager, + wallet: wallet, + cfg: cfg, + transaction: transaction, } } @@ -439,6 +443,11 @@ func (s *OrderService) processPayment(ctx context.Context, tx *gorm.DB, req *ent return fmt.Errorf("failed to update payment: %w", err) } + order, err := s.repo.FindByID(ctx, existingPayment.OrderID) + if err != nil { + return fmt.Errorf("failed to get order: %w", err) + } + if err := s.updateOrderStatus(ctx, tx, existingPayment.State, existingPayment.OrderID); err != nil { return fmt.Errorf("failed to update order status: %w", err) } @@ -447,6 +456,21 @@ func (s *OrderService) processPayment(ctx context.Context, tx *gorm.DB, req *ent if err := s.updateWalletBalance(ctx, tx, existingPayment.PartnerID, existingPayment.Amount); err != nil { return fmt.Errorf("failed to update wallet balance: %w", err) } + + transaction := &entity.Transaction{ + PartnerID: existingPayment.PartnerID, + TransactionType: "PAYMENT_RECEIVED", + ReferenceID: existingPayment.ReferenceID, + Status: "SUCCESS", + CreatedBy: 0, + Amount: existingPayment.Amount, + Fee: order.Fee, + Total: order.Total, + SiteID: order.SiteID, + } + if _, err = s.transaction.Create(ctx, tx, transaction); err != nil { + return fmt.Errorf("failed to update transaction: %w", err) + } } return nil }