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"`
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
SiteID *int64
Fee float64
Total float64
}
type TransactionDB struct {

View File

@ -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
}