public IGenerateCVVResponse GenerateCvv(string pan, string expiryDate) { #region Refactored /* * if (string.IsNullOrEmpty(pan)) throw new ArgumentNullException("pan"); * if (pan.Length < 16 || pan.Length > 19) throw new ArgumentException("pan is shorter than 16-digits or greater that 19-digits"); * string serviceCode = "000"; // to be printed behind the card * * * //configure Parameters * var parameters = new List<string>() * { * Configuration.ConfigurationManager.HsmConfig.CardVerificationKey, * pan, * ";", * expiryDate, * serviceCode * }; * * //configure Response Format * var responseFormat = new MessageFields(); * responseFormat.Fields.Add(new MessageField() { Name = "cvv", Length = 3 }); * * var hsmResponse = _theHsm.Send("CW", parameters, responseFormat); * * IGenerateCVVResponse response = new GenerateCVVResponse(); * response.Cvv = hsmResponse.Item("cvv"); */ #endregion IGenerateCVVResponse response = GenerateCvv(pan, expiryDate, PinConfigurationManager.HsmConfig.CardVerificationKey, "000"); return(response); }
public string GenerateTrack2DataForMagstripe(string pan, string expiry, string serviceCode) { string response = string.Empty; ThalesHsm hsm = new ThalesHsm(); IGeneratePinOffsetResponse pinOffsset = null; // ChangePINResponse cpResponse = null; new PANE.ERRORLOG.Error().LogInfo("In GenerateTrack2DataForMagstripe"); // obatin the account number string accountNo = ""; try { accountNo = pan.Substring(pan.Length - 13, 12); } catch (Exception) { response = "1:Invalid CardNumber, ensure the card number is minimum of 16 digits"; return(response); } #region Derive PIN Using the IBM Method new PANE.ERRORLOG.Error().LogInfo("In GenerateTrack2DataForMagstripe Step 1"); // Step 1: Generate a new encrypted Random Pin string _encryptedPIN; try { //_encryptedPIN = hsm.PinGenerator().DeriveEncryptedPin(pan).EncryptedPin; _encryptedPIN = hsm.PinGenerator().EncryptClearPin("0000", accountNo).EncryptedPin; new PANE.ERRORLOG.Error().LogInfo("Derived: " + _encryptedPIN); } catch (Exception ex) { new PANE.ERRORLOG.Error().LogToFile(new Exception("Unable to Encrypt clear Pin", ex)); //Exception ex2 = new ApplicationException("System error"); //throw ex2; response = "1:System PIN Error"; return(response); } #endregion #region Generate PVV new PANE.ERRORLOG.Error().LogInfo("In GenerateTrack2DataForMagstripe Step 2"); // Step 2: Generate the Pin offset for the random pin try { //pinOffsset = hsm.PinGenerator().GeneratePVV(_encryptedPIN, accountNo, pan); pinOffsset = hsm.PinGenerator().GeneratePinOffset(_encryptedPIN, accountNo, pan); } catch (Exception ex) { new PANE.ERRORLOG.Error().LogToFile(new Exception("Unable to Generate the PVV", ex)); response = "1:System PVV Generation Error"; return(response); } #endregion //return string.Format("0:{0}", pinOffsset.Offset.Substring(0, 4)); ; #region Generate CVV IGenerateCVVResponse cvv = hsm.CvvGenerator().GenerateCvv(pan, expiry, PinConfigurationManager.HsmConfig.CardVerificationKey, serviceCode); #endregion #region Format Track Data string discretionaryDataLessCvv = serviceCode + "01" + pinOffsset.Offset.Substring(0, 4); string cardHolderName = "CARDHOLDER"; string track1 = string.Format("B{0}^{1}^{2}{3}{4}", pan, cardHolderName, expiry, discretionaryDataLessCvv, cvv.Cvv); string track2 = string.Format("{0}={1}{2}{3}", pan, expiry, discretionaryDataLessCvv, cvv.Cvv); #endregion new PANE.ERRORLOG.Error().LogInfo(track1); new PANE.ERRORLOG.Error().LogInfo(track2); return(string.Format("0:{0}", track2)); }