Add Transaction in Payment Callback
This commit is contained in:
parent
986e0b67a0
commit
6528196d23
@ -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 {
|
||||
|
||||
@ -29,6 +29,7 @@ type OrderService struct {
|
||||
product repository.Product
|
||||
midtrans repository.Midtrans
|
||||
payment repository.Payment
|
||||
transaction repository.TransactionRepository
|
||||
txmanager repository.TransactionManager
|
||||
wallet repository.WalletRepository
|
||||
cfg Config
|
||||
@ -39,7 +40,9 @@ 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,
|
||||
@ -49,6 +52,7 @@ func NewOrderService(
|
||||
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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user