Snippets

EasyPaymentGateway c# integration with EPG

Created by Ruben Fernandez last modified Jesus Cervan
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Net;
using System.IO;
using System.Web;



namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            string merchantId = "MERCHANT-ID";
            string key = "MERCHANT-PASSWORD";
            
            string data = "operationType=debit&merchantId="+merchantId+"&customerId=19&customerEmail=test.staging@mm.com&amount=20.0&description=debit&country=MT&currency=EUR&addressLine1=1, test Str&city=Sliema&postCode=12345&telephone=99999999&firstname=test&lastname=Staging&customerCountry=GB&merchantTransactionId=2015060110fds00163&productId=4354353&language=en&dob=31-12-1970";
            


            string encrypted = AesEncryption(data, key);
            string integrityCheck = sha256_hash(data);
            string URI = "https://checkout-stg.easypaymentgateway.com/EPGCheckout/rest/online/tokenize";

         

            string myParameters = "encrypted=" + UTF8Encoding(encrypted) + "&integrityCheck=" + integrityCheck + "&merchantId="+ "1073";
            
            string HtmlResult = "response";
            using (WebClient wc = new WebClient())
            {
                wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                HtmlResult = wc.UploadString(URI, myParameters);
            }
            Console.Write(HtmlResult);
        }



        public static String UTF8Encoding(String data)
        {
            return System.Net.WebUtility.UrlEncode(data);
        }

        public static string GetSha256Hash(string data)
        {
            System.Security.Cryptography.SHA256Managed crypt = new System.Security.Cryptography.SHA256Managed();
            System.Text.StringBuilder hash = new System.Text.StringBuilder();
            byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(data));

            foreach (byte bit in crypto)
            {
                hash.Append(bit.ToString("x2"));
            }

            return hash.ToString();
        }

        public static String sha256_hash(String value)
        {
            using (SHA256 hash = SHA256Managed.Create())
            {
                return String.Join("", hash
                  .ComputeHash(Encoding.UTF8.GetBytes(value))
                  .Select(item => item.ToString("x2")));
            }
        }
      
        public static string AesEncryption(string data, string key)
        {
            AesManaged aes = new AesManaged();
            aes.Key = Encoding.UTF8.GetBytes(key);
            aes.Mode = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
          //  aes.KeySize = 256;
           // aes.BlockSize = 128;
            //aes.IV = new byte[16];

            ICryptoTransform crypt = aes.CreateEncryptor();
            byte[] encBytes = Encoding.UTF8.GetBytes(data);
            byte[] resultBytes = crypt.TransformFinalBlock(encBytes, 0, encBytes.Length);

            return Convert.ToBase64String(resultBytes);
        }
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.