Ana içeriğe geç

.NET

.NET ile Paylox API entegrasyonu. HttpClient kullanarak temel işlemleri gösterir.

Gereksinimler

  • .NET 6.0+
  • System.Text.Json (dahili)

Servis Sınıfı

using System.Text;
using System.Text.Json;

public class PayloxService
{
private readonly HttpClient _client;
private readonly string _apiKey;

public PayloxService(HttpClient client, IConfiguration config)
{
_client = client;
_client.BaseAddress = new Uri(
config["Paylox:ApiUrl"] ?? "https://api.jetcheckout.com/api/v1"
);
_apiKey = config["Paylox:ApiKey"]!;
}

public async Task<JsonElement> CreateSessionAsync(int amount, object customer, string successUrl, string failUrl)
{
var body = new
{
merchant_api_key = _apiKey,
amount,
currency = "TRY",
customer,
success_url = successUrl,
fail_url = failUrl
};

var content = new StringContent(
JsonSerializer.Serialize(body),
Encoding.UTF8,
"application/json"
);

var response = await _client.PostAsync("/embedded/session", content);
response.EnsureSuccessStatusCode();

var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<JsonElement>(json);
}

public async Task<JsonElement> GetPaymentStatusAsync(string orderId, string sessionId)
{
var response = await _client.GetAsync(
$"/payment/status/{orderId}?session_id={sessionId}"
);
response.EnsureSuccessStatusCode();

var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<JsonElement>(json);
}
}

Dependency Injection

builder.Services.AddHttpClient<PayloxService>();

Controller Örneği

[ApiController]
[Route("api/[controller]")]
public class CheckoutController : ControllerBase
{
private readonly PayloxService _paylox;

public CheckoutController(PayloxService paylox)
{
_paylox = paylox;
}

[HttpPost]
public async Task<IActionResult> Create([FromBody] CheckoutRequest request)
{
var session = await _paylox.CreateSessionAsync(
request.Amount,
new { name = request.Name, surname = request.Surname, email = request.Email },
$"{Request.Scheme}://{Request.Host}/payment/success",
$"{Request.Scheme}://{Request.Host}/payment/fail"
);

return Ok(session);
}
}

Yapılandırma

{
"Paylox": {
"ApiUrl": "https://api.jetcheckout.com/api/v1",
"ApiKey": "YOUR_API_KEY"
}
}
uyarı

API Key'i appsettings.json'a değil, User Secrets veya environment variable olarak saklayın.