From 6ad1341754f6a2e53c2a1a4b23cc00efc312061c Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Tue, 22 Sep 2026 00:08:09 +0330 Subject: [PATCH] feat(config): add DefaultEmail to configuration files for payment processing - Introduced DefaultEmail field in cms-config.yaml, appsettings.json, and appsettings.Staging.json to standardize email handling in payment requests. - Updated TwentyCardPaymentService to utilize DefaultEmail when no email is provided in the request, enhancing error handling and user experience. --- k8s/production/cms-config.yaml | 1 + k8s/staging/cms-config.yaml | 1 + .../Payment/TwentyCardPaymentService.cs | 31 ++++++++++++++++--- .../appsettings.Staging.json | 1 + src/CMSMicroservice.WebApi/appsettings.json | 1 + 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/k8s/production/cms-config.yaml b/k8s/production/cms-config.yaml index 0835075..a1a0674 100644 --- a/k8s/production/cms-config.yaml +++ b/k8s/production/cms-config.yaml @@ -30,6 +30,7 @@ stringData: "TerminalUsername": "ea5y1biehi", "TerminalPassword": "SaLamAT@1404!", "CreditPortalUrl": "https://userpanel.20card.ir/", + "DefaultEmail": "info@kbs2.ir", "Packages": { "4": { "GatewayAmountToman": 9700000, diff --git a/k8s/staging/cms-config.yaml b/k8s/staging/cms-config.yaml index 6a7e80c..fa9b9e3 100644 --- a/k8s/staging/cms-config.yaml +++ b/k8s/staging/cms-config.yaml @@ -31,6 +31,7 @@ stringData: "TerminalPassword": "SaLamAT@1404!", "CreditPortalUrl": "https://userpanel.20card.ir/", "CallbackBaseUrl": "https://kbs2.ir", + "DefaultEmail": "info@kbs2.ir", "Packages": { "4": { "GatewayAmountToman": 1000, diff --git a/src/CMSMicroservice.Infrastructure/Services/Payment/TwentyCardPaymentService.cs b/src/CMSMicroservice.Infrastructure/Services/Payment/TwentyCardPaymentService.cs index 9fec515..f086906 100644 --- a/src/CMSMicroservice.Infrastructure/Services/Payment/TwentyCardPaymentService.cs +++ b/src/CMSMicroservice.Infrastructure/Services/Payment/TwentyCardPaymentService.cs @@ -19,6 +19,7 @@ public class TwentyCardPaymentService : IPaymentGatewayService private readonly string _terminalId; private readonly string _terminalUsername; private readonly string _terminalPassword; + private readonly string _defaultEmail; private static readonly JsonSerializerOptions JsonOptions = new() { @@ -43,8 +44,11 @@ public class TwentyCardPaymentService : IPaymentGatewayService ?? throw new InvalidOperationException("TwentyCard:TerminalUsername is not configured."); _terminalPassword = configuration["TwentyCard:TerminalPassword"] ?? throw new InvalidOperationException("TwentyCard:TerminalPassword is not configured."); + _defaultEmail = configuration["TwentyCard:DefaultEmail"] ?? "info@kbs2.ir"; _httpClient.BaseAddress = new Uri(baseUrl.TrimEnd('/') + "/"); + if (!_httpClient.DefaultRequestHeaders.Contains("Accept")) + _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json"); _logger.LogInformation("TwentyCard payment service initialized. BaseUrl={BaseUrl}", baseUrl); } @@ -72,13 +76,13 @@ public class TwentyCardPaymentService : IPaymentGatewayService ["mobile"] = request.Mobile ?? string.Empty, ["invoice"] = invoice, ["callback"] = request.CallbackUrl, - ["description"] = request.Description ?? string.Empty + ["description"] = request.Description ?? string.Empty, + // بدون email درگاه ۲۰کارت به‌جای validation، HTTP 500 برمی‌گرداند + ["email"] = ResolveEmail(request.Email) }); - if (!string.IsNullOrWhiteSpace(request.Email)) - content.Add(new StringContent(request.Email), "email"); if (!string.IsNullOrWhiteSpace(request.NationalId)) - content.Add(new StringContent(request.NationalId), "national_id"); + AddFormField(content, "national_id", request.NationalId); _logger.LogInformation( "TwentyCard payment request: Amount={Amount} Toman, User={UserId}, Invoice={Invoice}", @@ -241,10 +245,27 @@ public class TwentyCardPaymentService : IPaymentGatewayService { var form = new MultipartFormDataContent(); foreach (var (key, value) in fields) - form.Add(new StringContent(value ?? string.Empty), key); + AddFormField(form, key, value ?? string.Empty); return form; } + /// + /// شبیه curl --form: بدون Content-Type روی هر part ( بعضی بک‌اندهای PHP با charset=utf-8 می‌شکنند). + /// + private static void AddFormField(MultipartFormDataContent form, string name, string value) + { + var part = new StringContent(value ?? string.Empty); + part.Headers.ContentType = null; + form.Add(part, name); + } + + private string ResolveEmail(string? email) + { + if (!string.IsNullOrWhiteSpace(email) && email.Contains('@', StringComparison.Ordinal)) + return email.Trim(); + return _defaultEmail; + } + private sealed class TwentyCardRequestResponse { public string? Message { get; set; } diff --git a/src/CMSMicroservice.WebApi/appsettings.Staging.json b/src/CMSMicroservice.WebApi/appsettings.Staging.json index 140cbfe..557dd00 100644 --- a/src/CMSMicroservice.WebApi/appsettings.Staging.json +++ b/src/CMSMicroservice.WebApi/appsettings.Staging.json @@ -11,6 +11,7 @@ "TerminalPassword": "SaLamAT@1404!", "CreditPortalUrl": "https://userpanel.20card.ir/", "CallbackBaseUrl": "https://kbs2.ir", + "DefaultEmail": "info@kbs2.ir", "Packages": { "4": { "GatewayAmountToman": 1000, diff --git a/src/CMSMicroservice.WebApi/appsettings.json b/src/CMSMicroservice.WebApi/appsettings.json index fc929bb..22c19d1 100644 --- a/src/CMSMicroservice.WebApi/appsettings.json +++ b/src/CMSMicroservice.WebApi/appsettings.json @@ -10,6 +10,7 @@ "TerminalUsername": "ea5y1biehi", "TerminalPassword": "SaLamAT@1404!", "CreditPortalUrl": "https://userpanel.20card.ir/", + "DefaultEmail": "info@kbs2.ir", "Packages": { "4": { "GatewayAmountToman": 9700000,