// ── نمونه اتصال C#‎ (.NET 6+) — درگاه پرداخت اعتباری راتک‌لند ──────────────── using System.Net.Http.Headers; using System.Text; using System.Text.Json; public class RatekCreditGatewayClient { private static readonly HttpClient Http = new() { Timeout = TimeSpan.FromSeconds(30) }; private const string BaseUrl = "https://your-rateklend-host.example.com"; private readonly string _clientId, _clientSecret, _username, _password; public RatekCreditGatewayClient(string clientId, string clientSecret, string username, string password) => (_clientId, _clientSecret, _username, _password) = (clientId, clientSecret, username, password); // ۱) دریافت Access Token public async Task GetAccessTokenAsync() { var basic = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_clientId}:{_clientSecret}")); var req = new HttpRequestMessage(HttpMethod.Post, $"{BaseUrl}/api/v1/auth/token"); req.Headers.Authorization = new AuthenticationHeaderValue("Basic", basic); req.Content = new FormUrlEncodedContent(new Dictionary { ["grant_type"] = "password", ["scope"] = "credit-gateway", ["username"] = _username, ["password"] = _password, }); var res = await Http.SendAsync(req); var json = JsonDocument.Parse(await res.Content.ReadAsStringAsync()); res.EnsureSuccessStatusCode(); return json.RootElement.GetProperty("data").GetProperty("access_token").GetString()!; } // ۲) بررسی امکان پرداخت public async Task CheckEligibilityAsync(string jwt, long amountIrr) { var req = new HttpRequestMessage(HttpMethod.Get, $"{BaseUrl}/api/v1/credit-offers/eligibility?amount={amountIrr}"); req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt); var res = await Http.SendAsync(req); var json = JsonDocument.Parse(await res.Content.ReadAsStringAsync()); return json.RootElement.GetProperty("data").Clone(); } // ۳) ایجاد جلسهٔ پرداخت public async Task InitiatePaymentAsync( string jwt, long amountIrr, string mobile, string callbackUrl, string merchantOrderId) { var payload = new { amount = amountIrr, discountAmount = 0, otherSourceAmount = 0, mobile, callbackUrl, merchantOrderId, orderLines = new[] { new { lineId = 1, shippingIncluded = true, taxIncluded = true, shippingFee = 0, tax = 0, lineTotal = amountIrr, items = new[] { new { id = 101, name = "سفارش", category = "general", count = 1, amount = amountIrr, feeCategory = 100 }, }, }, }, }; return await PostJsonAsync(jwt, "initiate", payload); } // ۴) confirm / finalize / reverse / void public Task ConfirmAsync(string jwt, string reference) => PostJsonAsync(jwt, "confirm", new { paymentReference = reference }); public Task FinalizeAsync(string jwt, string reference) => PostJsonAsync(jwt, "finalize", new { paymentReference = reference }); public Task VoidAsync(string jwt, string reference) => PostJsonAsync(jwt, "void", new { paymentReference = reference }); // ۵) استعلام وضعیت public async Task GetStatusAsync(string jwt, string reference) { var req = new HttpRequestMessage(HttpMethod.Get, $"{BaseUrl}/api/v1/credit-payments/status?paymentReference={Uri.EscapeDataString(reference)}"); req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt); var res = await Http.SendAsync(req); var json = JsonDocument.Parse(await res.Content.ReadAsStringAsync()); return json.RootElement.GetProperty("data").Clone(); } private async Task PostJsonAsync(string jwt, string action, object payload) { var req = new HttpRequestMessage(HttpMethod.Post, $"{BaseUrl}/api/v1/credit-payments/{action}"); req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt); req.Content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"); var res = await Http.SendAsync(req); return JsonDocument.Parse(await res.Content.ReadAsStringAsync()).RootElement.Clone(); } }