Add Transaction in Payment Callback

This commit is contained in:
aditya.siregar 2024-08-28 00:05:47 +07:00
parent 986e0b67a0
commit 6528196d23
2 changed files with 44 additions and 17 deletions

View File

@ -15,6 +15,9 @@ type Transaction struct {
Amount float64 `gorm:"not null"` Amount float64 `gorm:"not null"`
CreatedAt time.Time `gorm:"autoCreateTime"` CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"` UpdatedAt time.Time `gorm:"autoUpdateTime"`
SiteID *int64
Fee float64
Total float64
} }
type TransactionDB struct { type TransactionDB struct {

View File

@ -24,14 +24,15 @@ type Config interface {
} }
type OrderService struct { type OrderService struct {
repo repository.Order repo repository.Order
crypt repository.Crypto crypt repository.Crypto
product repository.Product product repository.Product
midtrans repository.Midtrans midtrans repository.Midtrans
payment repository.Payment payment repository.Payment
txmanager repository.TransactionManager transaction repository.TransactionRepository
wallet repository.WalletRepository txmanager repository.TransactionManager
cfg Config wallet repository.WalletRepository
cfg Config
} }
func NewOrderService( func NewOrderService(
@ -39,16 +40,19 @@ func NewOrderService(
product repository.Product, crypt repository.Crypto, product repository.Product, crypt repository.Crypto,
midtrans repository.Midtrans, payment repository.Payment, midtrans repository.Midtrans, payment repository.Payment,
txmanager repository.TransactionManager, txmanager repository.TransactionManager,
wallet repository.WalletRepository, cfg Config) *OrderService { wallet repository.WalletRepository, cfg Config,
transaction repository.TransactionRepository,
) *OrderService {
return &OrderService{ return &OrderService{
repo: repo, repo: repo,
product: product, product: product,
crypt: crypt, crypt: crypt,
midtrans: midtrans, midtrans: midtrans,
payment: payment, payment: payment,
txmanager: txmanager, txmanager: txmanager,
wallet: wallet, wallet: wallet,
cfg: cfg, 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) 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 { if err := s.updateOrderStatus(ctx, tx, existingPayment.State, existingPayment.OrderID); err != nil {
return fmt.Errorf("failed to update order status: %w", err) 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 { if err := s.updateWalletBalance(ctx, tx, existingPayment.PartnerID, existingPayment.Amount); err != nil {
return fmt.Errorf("failed to update wallet balance: %w", err) 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 return nil
} }