Last active
June 21, 2025 18:58
-
-
Save Kitecy/58b2eadf37a7c121142d2f6571546db8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
namespace PaymentSystems | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
Order order = new(10, 500); | |
string secretKey = "waflyaaa"; | |
MD5HashConverter md5Converter = new(); | |
SHA1HashConverter sha1Converter = new(); | |
System1PaymentProvider md5PaymentSystem = new(md5Converter); | |
System2PaymentProvider amountMd5PaymentSystem = new(md5Converter); | |
System3PaymentProvider sha1SecretKeyPaymentSystem = new(sha1Converter, secretKey); | |
Console.WriteLine(md5PaymentSystem.GetPayingLink(order)); | |
Console.WriteLine(amountMd5PaymentSystem.GetPayingLink(order)); | |
Console.WriteLine(sha1SecretKeyPaymentSystem.GetPayingLink(order)); | |
} | |
} | |
public interface IPaymentSystem | |
{ | |
public string GetPayingLink(Order order); | |
} | |
public interface IHashConverter | |
{ | |
public string ComputeHash(string data); | |
} | |
public sealed class Order | |
{ | |
public readonly int Id; | |
public readonly int Amount; | |
public Order(int id, int amount) | |
{ | |
ArgumentOutOfRangeException.ThrowIfNegative(id); | |
ArgumentOutOfRangeException.ThrowIfNegative(amount); | |
Id = id; | |
Amount = amount; | |
} | |
} | |
public sealed class MD5HashConverter : IHashConverter | |
{ | |
public string ComputeHash(string data) | |
{ | |
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) | |
{ | |
byte[] bytes = Encoding.ASCII.GetBytes(data); | |
byte[] hashBytes = md5.ComputeHash(bytes); | |
return Convert.ToHexString(hashBytes); | |
} | |
} | |
} | |
public sealed class SHA1HashConverter : IHashConverter | |
{ | |
public string ComputeHash(string data) | |
{ | |
using (System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create()) | |
{ | |
byte[] bytes = Encoding.ASCII.GetBytes(data); | |
byte[] hashBytes = sha1.ComputeHash(bytes); | |
return Convert.ToHexString(hashBytes); | |
} | |
} | |
} | |
public abstract class PaymentSystem : IPaymentSystem | |
{ | |
protected IHashConverter Converter; | |
public PaymentSystem(IHashConverter converter) | |
{ | |
Converter = converter ?? throw new ArgumentNullException(nameof(converter)); | |
} | |
public abstract string GetPayingLink(Order order); | |
} | |
public sealed class System1PaymentProvider : PaymentSystem | |
{ | |
private readonly string _link = "pay.system1.ru/order?amount=12000RUB&hash="; | |
public System1PaymentProvider(IHashConverter converter) : base(converter) | |
{ | |
} | |
public override string GetPayingLink(Order order) | |
{ | |
return _link + Converter.ComputeHash(order.Id.ToString()); | |
} | |
} | |
public sealed class System2PaymentProvider : PaymentSystem | |
{ | |
private readonly string _link = "order.system2.ru/pay?hash="; | |
public System2PaymentProvider(IHashConverter converter) : base(converter) | |
{ | |
} | |
public override string GetPayingLink(Order order) | |
{ | |
return _link + Converter.ComputeHash(order.Id.ToString() + order.Amount); | |
} | |
} | |
public sealed class System3PaymentProvider : PaymentSystem | |
{ | |
private readonly string _link = "system3.com/pay?amount=12000&curency=RUB&hash="; | |
private readonly string _secretKey; | |
public System3PaymentProvider(IHashConverter converter, string secretKey) : base(converter) | |
{ | |
if (string.IsNullOrEmpty(secretKey)) | |
throw new ArgumentNullException(nameof(secretKey)); | |
_secretKey = secretKey; | |
} | |
public override string GetPayingLink(Order order) | |
{ | |
return _link + Converter.ComputeHash(order.Id.ToString() + order.Amount + _secretKey); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment