payment gateways wallet service fixes

This commit is contained in:
Yared Yemane 2025-10-29 15:12:06 +03:00
parent 7575f29386
commit 1e39d75239
2 changed files with 19 additions and 19 deletions

View File

@ -135,24 +135,24 @@ func (s *Service) InitiateWithdrawal(ctx context.Context, userID int64, req doma
}
// Get user's wallet
wallets, err := s.walletStore.GetWalletsByUser(ctx, userID)
wallet, err := s.walletStore.GetCustomerWallet(ctx, userID)
if err != nil {
return nil, fmt.Errorf("failed to get user wallets: %w", err)
}
var withdrawWallet domain.Wallet
for _, wallet := range wallets {
if wallet.IsWithdraw {
withdrawWallet = wallet
break
}
}
// var withdrawWallet domain.Wallet
// for _, wallet := range wallets {
// if wallet.IsWithdraw {
// withdrawWallet = wallet
// break
// }
// }
if withdrawWallet.ID == 0 {
return nil, errors.New("withdrawal wallet not found")
}
// if withdrawWallet.ID == 0 {
// return nil, errors.New("withdrawal wallet not found")
// }
// Check balance
if withdrawWallet.Balance < domain.Currency(amount) {
if float64(wallet.RegularBalance) < float64(amount) {
return nil, domain.ErrInsufficientBalance
}
@ -164,7 +164,7 @@ func (s *Service) InitiateWithdrawal(ctx context.Context, userID int64, req doma
Amount: domain.Currency(amount),
Type: domain.WITHDRAW,
SenderWalletID: domain.ValidInt64{
Value: withdrawWallet.ID,
Value: wallet.RegularID,
Valid: true,
},
Status: string(domain.PaymentStatusPending),
@ -205,8 +205,8 @@ func (s *Service) InitiateWithdrawal(ctx context.Context, userID int64, req doma
return nil, fmt.Errorf("failed to update withdrawal status: %w", err)
}
// Deduct from wallet (or wait for webhook confirmation depending on your flow)
newBalance := withdrawWallet.Balance - domain.Currency(amount)
if err := s.walletStore.UpdateBalance(ctx, withdrawWallet.ID, newBalance); err != nil {
newBalance := float64(wallet.RegularBalance) - float64(amount)
if err := s.walletStore.UpdateBalance(ctx, wallet.RegularID, domain.Currency(newBalance)); err != nil {
return nil, fmt.Errorf("failed to update wallet balance: %w", err)
}

View File

@ -134,16 +134,16 @@ func (s *SantimPayService) ProcessCallback(ctx context.Context, payload domain.S
return fmt.Errorf("invalid ThirdPartyId '%s': %w", payload.ThirdPartyId, err)
}
wallets, err := s.walletSvc.GetWalletsByUser(ctx, userID)
wallet, err := s.walletSvc.GetCustomerWallet(ctx, userID)
if err != nil {
return fmt.Errorf("failed to get wallets for user %d: %w", userID, err)
return fmt.Errorf("failed to get wallets for customer %d: %w", userID, err)
}
// Optionally, credit user wallet
if transfer.Type == domain.DEPOSIT {
if _, err := s.walletSvc.AddToWallet(
ctx,
wallets[0].ID,
wallet.RegularID,
domain.Currency(amount),
domain.ValidInt64{},
domain.TRANSFER_SANTIMPAY,