Last active
December 12, 2022 15:00
-
-
Save harslannet/0e7e1b8e5c252611433a430ca383ab80 to your computer and use it in GitHub Desktop.
Sipay ödeme sistemi için .Net Core ile hazırlanmış, hash doğrulama, oluşturma ve iade hash oluşturma örnekleri
This file contains 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
// See https://aka.ms/new-console-template for more information | |
using System.Globalization; | |
using System.Net; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace ConsoleApp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
} | |
private string _merchantKey = string.Empty; | |
private string _appSecret = string.Empty; | |
private string _invoiceId = string.Empty; | |
private string _amount = string.Empty; | |
#region SİPAY HASH KEY DOĞRULAMA | |
//https://apidocs.sipay.com.tr/#tag/payment/paths/Hash/post | |
[Obsolete("Obsolete")] | |
private object[] ValidateHashKey(string hashKey, string appSecret) | |
{ | |
var status = string.Empty; | |
var currencyCode = string.Empty; | |
var total = string.Empty; | |
var invoiceId = string.Empty; | |
var orderId = string.Empty; | |
// double total = 0, invoiceId = 0, orderId = 0; | |
if (!string.IsNullOrEmpty(hashKey)) | |
{ | |
hashKey = hashKey.Replace("_", "/"); | |
string password = Sha1Hash(appSecret); | |
string[] split = hashKey.Split(":"); | |
if (split.Length > 2) | |
{ | |
string iv = !string.IsNullOrEmpty(split[0]) ? split[0] : ""; | |
string salt = !string.IsNullOrEmpty(split[1]) ? split[1] : ""; | |
salt = Sha256Hash(password + salt); | |
string encryptedMsg = !string.IsNullOrEmpty(split[2]) ? split[2] : ""; | |
string decryptedMsg = AesDecrypt(encryptedMsg, salt, iv); | |
if (decryptedMsg.IndexOf("|", StringComparison.Ordinal) != -1) | |
{ | |
string[] array = decryptedMsg.Split("|"); | |
status = !string.IsNullOrEmpty(array[0]) ? array[0] : string.Empty; | |
total = !string.IsNullOrEmpty(array[1]) ? array[1] : "0"; | |
invoiceId = !string.IsNullOrEmpty(array[2]) ? array[2] : "0"; | |
orderId = !string.IsNullOrEmpty(array[3]) ? array[3] : "0"; | |
currencyCode = !string.IsNullOrEmpty(array[4]) ? array[4] : string.Empty; | |
} | |
} | |
} | |
return new object[] { status, total, invoiceId, orderId, currencyCode }; | |
} | |
#endregion | |
#region SİPAY ÖDEME HASH KEY OLUŞTUR | |
//GenerateHashKey("165.62", 1, "TRY", _merchantKey, _invoiceId, _appSecret); | |
//https://apidocs.sipay.com.tr/#tag/payment/paths/~1api~1paySmart3D/post | |
[Obsolete("Obsolete")] | |
private static string GenerateHashKey(string total, double installment, string currencyCode, string merchantKey, string invoiceId, string appSecret) | |
{ | |
Random rnd = new Random(); | |
string data = $"{total}|{installment}|{currencyCode}|{merchantKey}|{invoiceId}"; | |
string iv = Sha1Hash(Convert.ToString(rnd.Next()))[..16]; | |
string password = Sha1Hash(appSecret); | |
string salt = Sha1Hash(Convert.ToString(rnd.Next()))[..4]; | |
string saltWithPassword = Sha256Hash(password + salt); | |
var encrypted = AesEncrypt(data, saltWithPassword, iv); | |
string msgEncryptedBundle = $"{iv}:{salt}:{encrypted}"; | |
string hashKey = msgEncryptedBundle.Replace("/", "__"); | |
return hashKey; | |
} | |
#endregion | |
#region İADE İÇİN HASH KEY OLUŞTUR | |
//Kullanımı GenerateRefundHashKey(string _amount, string _invoiceId, string _merchantKey, string _appSecret); | |
//https://apidocs.sipay.com.tr/#tag/payment/paths/~1api~1refund/post | |
[Obsolete("Obsolete")] | |
private string GenerateRefundHashKey(string amount, string invoiceId, string merchantKey, string appSecret) | |
{ | |
Random rnd = new Random(); | |
string data = $"{amount}|{invoiceId}|{merchantKey}"; | |
//string data = invoiceId + "|" + merchantKey; | |
string iv = Sha1Hash(Convert.ToString(rnd.Next()))[..16]; | |
string password = Sha1Hash(appSecret); | |
string salt = Sha1Hash(Convert.ToString(rnd.Next()))[..4]; | |
string saltWithPassword = Sha256Hash(password + salt); | |
var encrypted = AesEncrypt(data, saltWithPassword, iv); | |
var msgEncryptedBundle = $"{iv}:{salt}:{encrypted}"; | |
var hashKey = msgEncryptedBundle.Replace("/", "__"); | |
return hashKey; | |
} | |
#endregion | |
#region SHA1 METODU | |
[Obsolete("Obsolete")] | |
private static string Sha1Hash(string input) | |
{ | |
using SHA1Managed sha1 = new SHA1Managed(); | |
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input)); | |
var sb = new StringBuilder(hash.Length * 2); | |
foreach (byte b in hash) | |
{ | |
sb.Append(b.ToString("x2")); | |
} | |
return sb.ToString(); | |
} | |
#endregion | |
#region SHA256 METODU | |
[Obsolete("Obsolete")] | |
private static string Sha256Hash(String value) | |
{ | |
StringBuilder Sb = new StringBuilder(); | |
using (SHA256 hash = SHA256.Create()) | |
{ | |
Encoding enc = Encoding.UTF8; | |
Byte[] result = hash.ComputeHash(enc.GetBytes(value)); | |
foreach (Byte b in result) | |
Sb.Append(b.ToString("x2")); | |
} | |
return Sb.ToString(); | |
} | |
#endregion | |
#region AES SSL 256 Bit CBC ENCRYPT | |
[Obsolete("Obsolete")] | |
private static string AesEncrypt(string plainText, string keystring, string ivString) | |
{ | |
byte[] key = Encoding.UTF8.GetBytes(keystring).Take(32).ToArray(); | |
byte[] iv = Encoding.UTF8.GetBytes(ivString).Take(16).ToArray(); | |
// Create a new AesManaged. | |
using AesManaged aes = new AesManaged(); | |
aes.Mode = CipherMode.CBC; | |
aes.Padding = PaddingMode.PKCS7; | |
aes.KeySize = 256; | |
ICryptoTransform encryptor = aes.CreateEncryptor(key, iv); | |
using MemoryStream ms = new MemoryStream(); | |
using CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write); | |
using (StreamWriter sw = new StreamWriter(cs)) | |
sw.Write(plainText); | |
var encrypted = Convert.ToBase64String(ms.ToArray()); | |
// Return encrypted data | |
return encrypted; | |
} | |
#endregion | |
#region AES SSL 256 Bit CBC DECRYPT | |
private static string AesDecrypt(string cipherText, string saltString, string ivString) | |
{ | |
byte[] key = Encoding.UTF8.GetBytes(saltString).Take(32).ToArray(); | |
byte[] iv = Encoding.UTF8.GetBytes(ivString).Take(16).ToArray(); | |
using Aes aes = Aes.Create(); | |
aes.Mode = CipherMode.CBC; | |
aes.Padding = PaddingMode.PKCS7; | |
aes.KeySize = 256; | |
aes.Key = key; | |
aes.IV = iv; | |
ICryptoTransform decryptor = aes.CreateDecryptor(); | |
byte[] cipher = Convert.FromBase64String(cipherText); | |
using MemoryStream ms = new MemoryStream(cipher); | |
using CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read); | |
using StreamReader sr = new StreamReader(cs); | |
return sr.ReadToEnd(); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment