feat(payment): enhance TwentyCard payment integration and update configuration
Build and Deploy to Kubernetes / build-and-deploy (push) Has been cancelled
Build and Deploy to Kubernetes / build-and-deploy (push) Has been cancelled
- Added support for optional Email and NationalId fields in PaymentRequest for TwentyCard transactions. - Updated TwentyCardPaymentService to handle multipart/form-data requests and improved error handling. - Modified appsettings and configuration files for both production and staging environments to include updated BaseUrl and credentials for TwentyCard. - Updated README to reflect the new documentation location.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
@@ -11,7 +10,7 @@ namespace CMSMicroservice.Infrastructure.Services.Payment;
|
||||
|
||||
/// <summary>
|
||||
/// درگاه پرداخت بیستکارت (20Card IPG Merchant API).
|
||||
/// مبلغ ورودی به تومان است (مثل بقیهٔ سیستم).
|
||||
/// مبلغ ورودی به تومان است. درخواستها بهصورت multipart/form-data ارسال میشوند.
|
||||
/// </summary>
|
||||
public class TwentyCardPaymentService : IPaymentGatewayService
|
||||
{
|
||||
@@ -55,12 +54,15 @@ public class TwentyCardPaymentService : IPaymentGatewayService
|
||||
{
|
||||
try
|
||||
{
|
||||
var amount = (long)request.Amount;
|
||||
var amount = ((long)request.Amount).ToString();
|
||||
var payerName = string.IsNullOrWhiteSpace(request.Name)
|
||||
? (string.IsNullOrWhiteSpace(request.Mobile) ? "کاربر" : request.Mobile)
|
||||
: request.Name.Trim();
|
||||
var invoice = string.IsNullOrWhiteSpace(request.Invoice)
|
||||
? request.UserId.ToString()
|
||||
: request.Invoice;
|
||||
|
||||
var body = new Dictionary<string, object?>
|
||||
using var content = BuildForm(new Dictionary<string, string>
|
||||
{
|
||||
["terminal_id"] = _terminalId,
|
||||
["terminal_username"] = _terminalUsername,
|
||||
@@ -68,19 +70,19 @@ public class TwentyCardPaymentService : IPaymentGatewayService
|
||||
["amount"] = amount,
|
||||
["name"] = payerName,
|
||||
["mobile"] = request.Mobile ?? string.Empty,
|
||||
["invoice"] = string.IsNullOrWhiteSpace(request.Invoice)
|
||||
? request.UserId.ToString()
|
||||
: request.Invoice,
|
||||
["invoice"] = invoice,
|
||||
["callback"] = request.CallbackUrl,
|
||||
["description"] = request.Description
|
||||
};
|
||||
["description"] = request.Description ?? string.Empty
|
||||
});
|
||||
|
||||
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");
|
||||
|
||||
_logger.LogInformation(
|
||||
"TwentyCard payment request: Amount={Amount} Toman, User={UserId}, Invoice={Invoice}",
|
||||
amount, request.UserId, body["invoice"]);
|
||||
|
||||
using var content = new StringContent(
|
||||
JsonSerializer.Serialize(body, JsonOptions), Encoding.UTF8, "application/json");
|
||||
amount, request.UserId, invoice);
|
||||
|
||||
var response = await _httpClient.PostAsync("api/pgw/merchant/request", content, cancellationToken);
|
||||
var responseBody = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
@@ -108,7 +110,9 @@ public class TwentyCardPaymentService : IPaymentGatewayService
|
||||
return new PaymentInitiateResult
|
||||
{
|
||||
IsSuccess = false,
|
||||
ErrorMessage = result?.Message ?? "ایجاد تراکنش ۲۰کارت ناموفق بود"
|
||||
ErrorMessage = result?.Message
|
||||
?? result?.Error?.Message
|
||||
?? "ایجاد تراکنش ۲۰کارت ناموفق بود"
|
||||
};
|
||||
}
|
||||
|
||||
@@ -155,17 +159,14 @@ public class TwentyCardPaymentService : IPaymentGatewayService
|
||||
};
|
||||
}
|
||||
|
||||
var body = new Dictionary<string, object?>
|
||||
using var content = BuildForm(new Dictionary<string, string>
|
||||
{
|
||||
["terminal_id"] = _terminalId,
|
||||
["terminal_username"] = _terminalUsername,
|
||||
["terminal_password"] = _terminalPassword,
|
||||
["authority"] = refId,
|
||||
["amount"] = (long)amountInToman
|
||||
};
|
||||
|
||||
using var content = new StringContent(
|
||||
JsonSerializer.Serialize(body, JsonOptions), Encoding.UTF8, "application/json");
|
||||
["amount"] = ((long)amountInToman).ToString()
|
||||
});
|
||||
|
||||
var response = await _httpClient.PostAsync("api/pgw/merchant/verify", content, cancellationToken);
|
||||
var responseBody = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
@@ -236,10 +237,25 @@ public class TwentyCardPaymentService : IPaymentGatewayService
|
||||
});
|
||||
}
|
||||
|
||||
private static MultipartFormDataContent BuildForm(Dictionary<string, string> fields)
|
||||
{
|
||||
var form = new MultipartFormDataContent();
|
||||
foreach (var (key, value) in fields)
|
||||
form.Add(new StringContent(value ?? string.Empty), key);
|
||||
return form;
|
||||
}
|
||||
|
||||
private sealed class TwentyCardRequestResponse
|
||||
{
|
||||
public string? Message { get; set; }
|
||||
public TwentyCardRequestData? Data { get; set; }
|
||||
public TwentyCardErrorBody? Error { get; set; }
|
||||
}
|
||||
|
||||
private sealed class TwentyCardErrorBody
|
||||
{
|
||||
public string? Code { get; set; }
|
||||
public string? Message { get; set; }
|
||||
}
|
||||
|
||||
private sealed class TwentyCardRequestData
|
||||
|
||||
Reference in New Issue
Block a user