using GB5Shared.DTO.Framework.Login; using Microsoft.Extensions.Logging; using System.Net.Http.Json; namespace GoodBooks.PAY.PAYBLL.Integration { public class SalesCouponService : ISalesCouponService { private readonly IHttpClientFactory _httpClientFactory; private readonly ILogger _logger; public SalesCouponService( IHttpClientFactory httpClientFactory, ILogger logger) { _httpClientFactory = httpClientFactory; _logger = logger; } public async Task ValidateCouponAsync( string couponCode, int customerId, decimal orderAmt, LoginDTO login, CancellationToken ct) { try { var client = _httpClientFactory.CreateClient("SalesModule"); var payload = new { CouponCode = couponCode, CustomerId = customerId, OrderAmt = orderAmt, ClientId = login.ClientId, DatabaseName = login.DatabaseName }; var response = await client .PostAsJsonAsync("/Sales/Coupon/ValidateCoupon", payload, ct) .ConfigureAwait(false); if (!response.IsSuccessStatusCode) return null; return await response.Content .ReadFromJsonAsync(cancellationToken: ct) .ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "ValidateCoupon failed for CouponCode {CouponCode}", couponCode); return null; } } public async Task RecordCouponUsageAsync( string couponCode, int customerId, int payOrderId, LoginDTO login, CancellationToken ct) { try { var client = _httpClientFactory.CreateClient("SalesModule"); var payload = new { CouponCode = couponCode, CustomerId = customerId, PayOrderId = payOrderId, ClientId = login.ClientId, DatabaseName = login.DatabaseName }; await client .PostAsJsonAsync("/Sales/Coupon/RecordCouponUsage", payload, ct) .ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning(ex, "RecordCouponUsage failed for CouponCode {CouponCode} PayOrder {PayOrderId}", couponCode, payOrderId); // Non-fatal — fire-and-forget; caller does not need to handle } } } }