Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- class Program
- {
- 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&curency=RUB&hash={SHA-1 хеш сумма заказа + ID заказа + секретный ключ от системы}
- PayLinkFinder payLinkFinder1 = new PayLinkFinder(new System1(new MD5HashInt()), new Order(13, 12000));
- PayLinkFinder payLinkFinder2 = new PayLinkFinder(new System2(new MD5HashInt()), new Order(4234, 3000));
- PayLinkFinder payLinkFinder3 = new PayLinkFinder(new System3(new SHA1HashInt()), new Order(5321, 12000));
- PayLinkFinder[] payLinkFinders = new PayLinkFinder[] { payLinkFinder1, payLinkFinder2, payLinkFinder3 };
- foreach (var payLinkFinder in payLinkFinders)
- {
- payLinkFinder.Find();
- }
- }
- }
- public class PayLinkFinder
- {
- private IPaymentSystem _paymentSystem;
- private Order _order;
- public PayLinkFinder(IPaymentSystem paymentSystem, Order order)
- {
- _paymentSystem = paymentSystem;
- _order = order;
- }
- public void Find()
- {
- Console.WriteLine(_paymentSystem.GetPayingLink(_order));
- Console.WriteLine();
- }
- }
- public class MD5HashInt : IHash
- {
- public string GetHash(int inputData)
- {
- MD5 md5 = MD5.Create();
- return String.Concat(md5.ComputeHash(BitConverter.GetBytes(inputData)).Select(x => x.ToString("x2")));
- }
- }
- public class SHA1HashInt : IHash
- {
- public string GetHash(int inputData)
- {
- var hash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(inputData.ToString()));
- return $"{String.Concat(hash.Select(b => b.ToString("x2")))}";
- }
- }
- public class Order
- {
- public readonly int Id;
- public readonly int Amount;
- public Order(int id, int amount)
- {
- Id = id;
- Amount = amount;
- }
- }
- public abstract class PaymentSystem : IPaymentSystem
- {
- protected readonly IHash HashInt;
- protected readonly string PaymentSystemLink;
- protected PaymentSystem(IHash hashInt)
- {
- HashInt = hashInt;
- }
- public abstract string GetPayingLink(Order order);
- }
- public class System1 : PaymentSystem
- {
- public System1(IHash hashInt) : base(hashInt)
- {
- }
- public override string GetPayingLink(Order order)
- {
- return $"pay.system1.ru/order?amount=12000RUB&hash={HashInt.GetHash(order.Id)}";
- }
- }
- public class System2 : PaymentSystem
- {
- public System2(IHash hashInt) : base(hashInt)
- {
- }
- public override string GetPayingLink(Order order)
- {
- return $"order.system2.ru/pay?hash ={HashInt.GetHash(order.Id)}+{HashInt.GetHash(order.Amount)}";
- }
- }
- public class System3 : PaymentSystem
- {
- private int _secretKey = 125623145;
- public System3(IHash hashInt) : base(hashInt)
- {
- }
- public override string GetPayingLink(Order order)
- {
- return $"system3.com/pay?amount=12000&curency=RUB&hash={HashInt.GetHash(order.Id)}+{HashInt.GetHash(order.Amount)}+{HashInt.GetHash(_secretKey)}";
- }
- }
- public interface IPaymentSystem
- {
- public string GetPayingLink(Order order);
- }
- public interface IHash
- {
- public string GetHash(int inputData);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement