using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GoodBooks.PAY.PAYDAL.DTO.Gateway; using GoodBooks.PAY.PAYDAL.Query.Gateway; using Newtonsoft.Json; namespace GoodBooks.PAY.PAYDAL.CustomCode.Gateway { public class PayGatewayConfigDAL : IPayGatewayConfigDAL { private readonly IQueryExecutor _QueryExecutor; public PayGatewayConfigDAL(IQueryExecutor queryExecutor) { _QueryExecutor = queryExecutor; } // ── Get single config by Id ─────────────────────────────────────── // Filters by PAYGATEWAYCONFID + TENANTID. public async Task GetPayGatewayConfig(int payGatewayConfId, LoginDTO loginDTO) { try { var param = new { PayGatewayConfId = payGatewayConfId, TenantId = loginDTO.ClientId }; var result = await _QueryExecutor.QuerySingleAsync( loginDTO, PayGatewayConfigQB.GET_PAY_GATEWAY_CONFIG, param).ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } // ── Get all configs for a gateway within the tenant ─────────────── public async Task GetPayGatewayConfigByGatewayId(int payGatewayId, LoginDTO loginDTO) { try { var param = new { PayGatewayId = payGatewayId, TenantId = loginDTO.ClientId }; var result = await _QueryExecutor.QueryAsync( loginDTO, PayGatewayConfigQB.GET_PAY_GATEWAY_CONFIG_BY_GATEWAY_ID, param).ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } // ── Webhook-specific lookup — returns typed DTO, not JSON ───────── // Used by webhook BLL before a full LoginDTO is built. // Returns PayGatewayConfigDTO? (nullable) so the caller can check for null // and return 404 / reject the webhook without leaking internals. public async Task GetPayGatewayConfigForWebhook( string gatewayCode, int tenantId, LoginDTO loginDTO) { try { var param = new { GatewayCode = gatewayCode, TenantId = tenantId }; var result = await _QueryExecutor.QuerySingleAsync( loginDTO, PayGatewayConfigQB.GET_PAY_GATEWAY_CONFIG_FOR_WEBHOOK, param).ConfigureAwait(false); return result; } catch (Exception) { throw; } } // ── Insert new gateway config ───────────────────────────────────── public async Task SavePayGatewayConfig(PayGatewayConfigDTO dto, LoginDTO loginDTO) { try { int rows = await _QueryExecutor.ExecuteAsync( loginDTO, PayGatewayConfigQB.SAVE_PAY_GATEWAY_CONFIG, dto).ConfigureAwait(false); return rows; } catch (Exception) { throw; } } // ── Update existing gateway config ──────────────────────────────── public async Task UpdatePayGatewayConfig(PayGatewayConfigDTO dto, LoginDTO loginDTO) { try { int rows = await _QueryExecutor.ExecuteAsync( loginDTO, PayGatewayConfigQB.UPDATE_PAY_GATEWAY_CONFIG, dto).ConfigureAwait(false); return rows; } catch (Exception) { throw; } } } }