C# Sample
This C# example is based on the Laravel framework and uses the HttpClient library to make requests.
Signature implemention
csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
public static class Signature
{
public static string Sign(Dictionary<string, string> parameters, string apiToken)
{
var sorted = parameters
.Where(kv => kv.Key != "sign" && !string.IsNullOrEmpty(kv.Value))
.OrderBy(kv => kv.Key);
var sb = new StringBuilder();
sb.Append(apiToken);
foreach (var kv in sorted)
{
sb.Append($"&{kv.Key}={kv.Value}");
}
using (var md5 = MD5.Create())
{
var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()));
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
}Create collection order
The following shows how to create a collection order. The payout request is similar.
csharp
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class OrderRequest
{
private static readonly HttpClient client = new HttpClient();
public static async Task Main()
{
var parameters = new Dictionary<string, string>
{
{ "mch_id", "2050" }, // Merchant ID
{ "trans_id", "E1763923463" }, // Transaction ID of your system
{ "channel", "bank" }, // Channel code
{ "amount", "200000.00" }, // Order amount
{ "currency", "VND" }, // Currency
{ "callback_url", "https://api.blackhole.com" }, // Webhook url
{ "remarks", "callme" }, // Remarks
{ "nonce", Guid.NewGuid().ToString().Substring(0, 8) }, // Random string
{ "timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString() } // UNIX timestamp
};
string apiToken = "0xFAKE_TOKENx0";
parameters["sign"] = Signature.Sign(parameters, apiToken);
var jsonContent = JsonSerializer.Serialize(parameters);
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
try
{
var response = await client.PostAsync("http://domain/api/v1/mch/pmt-orders", content);
var responseBody = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"HTTP Error: {response.StatusCode}");
}
else
{
var result = JsonSerializer.Deserialize<Dictionary<string, object>>(responseBody);
if (result != null && result.TryGetValue("code", out var code) && Convert.ToInt32(code) == 200)
{
Console.WriteLine("Success");
}
else
{
Console.WriteLine($"Failed: {result?["message"]}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
}