.NET Core SDK
thông tin
Tài liệu đầy đủ
Để biết thêm chi tiết về các phương thức, tham số và tính năng nâng cao (async/await support, auto-pagination, ILogger integration, v.v.), vui lòng xem GitHub Repository.
Cài đặt
Cài đặt package payOS thông qua dotnet CLI:
dotnet add package payOS
Khởi tạo
Khởi tạo đối tượng PayOSClient với Client ID, API Key và Checksum Key:
using PayOS;
var payOS = new PayOSClient(
Environment.GetEnvironmentVariable("PAYOS_CLIENT_ID"),
Environment.GetEnvironmentVariable("PAYOS_API_KEY"),
Environment.GetEnvironmentVariable("PAYOS_CHECKSUM_KEY")
);
Tạo link thanh toán
Sử dụng phương thức PaymentRequests.CreateAsync() để tạo link thanh toán:
using PayOS.Models;
var paymentRequest = new CreatePaymentLinkRequest
{
OrderCode = 123,
Amount = 2000,
Description = "Thanh toán đơn hàng",
CancelUrl = "https://your-domain.com/cancel",
ReturnUrl = "https://your-domain.com/success"
};
var paymentLink = await payOS.PaymentRequests.CreateAsync(paymentRequest);
Console.WriteLine(paymentLink.CheckoutUrl);
Xác minh webhook
Sử dụng phương thức Webhooks.VerifyAsync() để xác thực dữ liệu webhook:
using Microsoft.AspNetCore.Mvc;
[HttpPost("webhook")]
public async Task<IActionResult> HandleWebhook([FromBody] WebhookData webhookData)
{
try
{
var verifiedData = await payOS.Webhooks.VerifyAsync(webhookData);
Console.WriteLine($"Thanh toán thành công: {verifiedData.OrderCode}");
return Ok("OK");
}
catch (Exception ex)
{
Console.WriteLine($"Webhook không hợp lệ: {ex.Message}");
return BadRequest("Invalid webhook");
}
}
Tạo payout
Sử dụng phương thức Payouts.Batch.CreateAsync() để tạo payout theo lô:
using PayOS.Models;
var referenceId = $"payout_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
var payoutRequest = new PayoutBatchRequest
{
ReferenceId = referenceId,
Category = new List<string> { "salary" },
ValidateDestination = true,
Payouts = new List<PayoutBatchItem>
{
new PayoutBatchItem
{
ReferenceId = $"{referenceId}_1",
Amount = 2000,
Description = "Thanh toán lương",
ToBin = "970422",
ToAccountNumber = "0123456789"
},
new PayoutBatchItem
{
ReferenceId = $"{referenceId}_2",
Amount = 3000,
Description = "Thanh toán thưởng",
ToBin = "970422",
ToAccountNumber = "0987654321"
}
}
};
var payout = await payOS.Payouts.Batch.CreateAsync(payoutRequest);
Console.WriteLine($"Payout ID: {payout.Id}");