OnePay.NetCore 1.0.0

dotnet add package OnePay.NetCore --version 1.0.0
                    
NuGet\Install-Package OnePay.NetCore -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OnePay.NetCore" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OnePay.NetCore" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="OnePay.NetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OnePay.NetCore --version 1.0.0
                    
#r "nuget: OnePay.NetCore, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=OnePay.NetCore&version=1.0.0
                    
Install OnePay.NetCore as a Cake Addin
#tool nuget:?package=OnePay.NetCore&version=1.0.0
                    
Install OnePay.NetCore as a Cake Tool

OnePay.NetCore

OnePay.NetCore là một thư viện được thiết kế để tích hợp cổng thanh toán OnePay vào các ứng dụng .NET Core của bạn. Nó giúp đơn giản hóa quá trình tạo liên kết thanh toán, xử lý callback (ReturnURL, IPN) và xử lý phản hồi thanh toán.

Tính năng

  • Tích hợp Dependency Injection: Dễ dàng cấu hình OnePayOptionsOnePayService với Dependency Injection.
  • Dịch vụ Thanh toán Tùy chỉnh: Cho phép sử dụng các triển khai tùy chỉnh của interface IOnePayService để xử lý thanh toán.
  • Xử lý Callback: Hỗ trợ tự động cấu hình các endpoints để xử lý các callback từ OnePay (ReturnURL, IPN).
  • Tạo Liên kết Thanh toán: Cho phép tạo liên kết thanh toán cho các giao dịch OnePay.
  • Tích hợp Processor: Hỗ trợ thêm các processor tùy chỉnh để xử lý dữ liệu trả về từ OnePay.

Cài đặt

Để cài đặt OnePay.NetCore, sử dụng NuGet Package Manager hoặc .NET CLI:

dotnet add package OnePay.NetCore

Cấu hình và Thiết lập

1. Cấu hình OnePay trong Startup.cs hoặc Program.cs

Trong Startup.cs hoặc Program.cs của ứng dụng, đăng ký các dịch vụ của OnePay và cấu hình các tùy chọn.

public void ConfigureServices(IServiceCollection services)
{
    services.AddOnePay(options => Configuration.GetSection("OnePay").Bind(options));
    services.AddTransient<IOnePayProcessor, TestOnePayProcessor>();
}

2. Định tuyến các Endpoints của OnePay

Trong phương thức Configure, cấu hình các endpoint để xử lý các phản hồi ReturnURL và IPN từ OnePay.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.MapOnePay();
}

3. Tạo Liên kết Thanh toán

Sử dụng OnePayService để tạo một liên kết thanh toán cho giao dịch.

public class SomeClass
{
    private readonly IOnePayService _onePayService;

    public SomeClass(IOnePayService onePayService)
    {
        _onePayService = onePayService;
    }

    public async Task CreateLink(OnePayRequest request, string returnUrl)
    {
        var url = await _onePayService.CreatePaymentLink(TestOnePayProcessor.TYPE, request, returnUrl);
    }
}

Tùy chỉnh Dịch vụ Thanh toán

Bạn có thể ghi đè OnePayService hoặc triển khai dịch vụ IOnePayService của riêng mình và đăng ký nó trong DI container. Dưới đây là ví dụ:

public class CustomOnePayService : OnePayService
{
    // Ghi đè các phương thức xử lý yêu cầu ở đây
}

Sau đó, đăng ký dịch vụ trong DI container:

services.AddOnePay<CustomOnePayService>(options => Configuration.GetSection("OnePay").Bind(options));

Xử lý Callback từ OnePay

Thư viện tự động cấu hình hai endpoint để xử lý callback URLs từ OnePay, bao gồm ReturnURL và IPN:

  1. ReturnURL: Xử lý qua endpoint MapGet(options.ReturnURL).
  2. IPN: Xử lý qua endpoint MapGet(options.IPNURL).

Bạn có thể tùy chỉnh cách xử lý phản hồi bằng cách triển khai interface IOnePayProcessor và đăng ký các processor tùy chỉnh.

Ví dụ về một processor đơn giản:

public class TestOnePayProcessor : IOnePayProcessor
{
    public const string TYPE = nameof(TestOnePayProcessor);
    private readonly ILogger<TestOnePayProcessor> _logger;

    public string Type => TYPE;

    public TestOnePayProcessor(ILogger<TestOnePayProcessor> logger)
    {
        _logger = logger;
    }

    public Task ProcessReturnURL(OnePayResponse response)
    {
        _logger.LogInformation("Đang xử lý ReturnURL cho mã yêu cầu: {0}", response.RequestCode);
        return Task.CompletedTask;
    }

    public Task ProcessIPN(OnePayResponse response)
    {
        _logger.LogInformation("Đang xử lý IPN cho mã yêu cầu: {0}", response.RequestCode);
        return Task.CompletedTask;
    }
}

Đăng ký Processor Tùy chỉnh

Đăng ký processor tùy chỉnh như sau:

services.AddTransient<IOnePayProcessor, TestOnePayProcessor>();

Cấu hình Ví dụ

{
  "OnePay": {
    "ApiUrl": "https://mtf.onepay.vn/",
    "AccessCode": "6BEB2546",
    "Merchant": "TESTONEPAY",
    "HashKey": "6D0870CDE5F24F34F3915FB0045120DB"
  }
}

Giấy phép

OnePay.NetCore là một thư viện mã nguồn mở và được cấp phép dưới Giấy phép MIT. Bạn có thể tự do sử dụng, sửa đổi và phân phối thư viện trong các dự án của mình.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 122 12/14/2024