Skip to content

Instantly share code, notes, and snippets.

@GigaOrts
Last active April 15, 2024 20:42
Show Gist options
  • Save GigaOrts/99dbb07095c1c1c6649618f7f88f7b72 to your computer and use it in GitHub Desktop.
Save GigaOrts/99dbb07095c1c1c6649618f7f88f7b72 to your computer and use it in GitHub Desktop.
using System;
using System.Security.Cryptography;
using System.Text;
namespace PaymentSystem;
internal static class Program
{
private static void Main(string[] args)
{
//Выведите платёжные ссылки для трёх разных систем платежа:
//pay.system1.ru/order?amount=12000RUB&hash={MD5 хеш ID заказа}
//order.system2.ru/pay?hash={MD5 хеш ID заказа + сумма заказа}
//system3.com/pay?amount=12000&currency=RUB&hash={SHA-1 хеш сумма заказа + ID заказа + секретный ключ от системы}
Order order = new Order("100", 12000);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(new RubPaymentSystem(new Md5HashService(), "pay.system1.ru/order?amount={0}RUB&hash={1}").GetPayingLink(order));
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(new RubAmountPaymentSystem(new Md5HashService(), "order.system2.ru/pay?hash={0}").GetPayingLink(order));
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(new EurPaymentSystem(new Sha1HashService(), "system3.com/pay?amount={0}&currency=RUB&hash={1}", "secret").GetPayingLink(order));
}
}
public class Order(string id, int amount)
{
public string Id { get; } = id;
public int Amount { get; } = amount;
}
public interface IPaymentSystem
{
string GetPayingLink(Order order);
}
public interface IHashService
{
string Hash(string data);
}
public class Md5HashService : IHashService
{
public string Hash(string data)
{
if (string.IsNullOrWhiteSpace(data))
{
throw new ArgumentException($"\"{nameof(data)}\" не может быть пустым или содержать только пробел.", nameof(data));
}
byte[] bytes = MD5.HashData(Encoding.UTF8.GetBytes(data));
return bytes.ToHex(true);
}
}
public class Sha1HashService : IHashService
{
public string Hash(string data)
{
if (string.IsNullOrWhiteSpace(data))
{
throw new ArgumentException($"\"{nameof(data)}\" не может быть пустым или содержать только пробел.", nameof(data));
}
byte[] bytes = SHA1.HashData(Encoding.UTF8.GetBytes(data));
return bytes.ToHex(true);
}
}
public class RubPaymentSystem(IHashService hashService, string link) : IPaymentSystem
{
private readonly IHashService _hashService = hashService ?? throw new ArgumentNullException(nameof(hashService));
private readonly string _link = string.IsNullOrEmpty(link) == false ? link : throw new ArgumentException(nameof(link));
public string GetPayingLink(Order order) =>
string.Format(_link,order.Amount, _hashService.Hash(order.Id));
}
public class RubAmountPaymentSystem(IHashService hashService, string link) : IPaymentSystem
{
private readonly IHashService _hashService = hashService ?? throw new ArgumentNullException(nameof(hashService));
private readonly string _link = string.IsNullOrEmpty(link) == false ? link : throw new ArgumentException(nameof(link));
public string GetPayingLink(Order order) =>
string.Format(_link, _hashService.Hash(order.Id), order.Amount);
}
public class EurPaymentSystem(IHashService hashService, string link, string secretKey) : IPaymentSystem
{
private readonly IHashService _hashService = hashService ?? throw new ArgumentNullException(nameof(hashService));
private readonly string _link = string.IsNullOrEmpty(link) == false ? link : throw new ArgumentException(nameof(link));
private readonly string _secretKey = string.IsNullOrEmpty(secretKey) == false ? secretKey : throw new ArgumentException(nameof(secretKey));
public string GetPayingLink(Order order) =>
string.Format(_link, order.Amount, _hashService.Hash(string.Concat(order.Amount.ToString(), order.Id, _secretKey)));
}
static class ByteArrayExtension
{
public static string ToHex(this byte[] bytes, bool upperCase)
{
StringBuilder result = new StringBuilder(bytes.Length * 2);
for (int i = 0; i < bytes.Length; i++)
result.Append(bytes[i].ToString(upperCase ? "X2" : "x2"));
return result.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment