public static List <RealexComment> CreateCommentList(IEnumerable <string> optionalComments, int maxNumberOfComments = 2) { if (null == optionalComments) { return(null); } List <RealexComment> result = new List <RealexComment>(); foreach (string comment in optionalComments) { if (null != comment) { string sanitizedComment = MessageContentUtility.TruncateAndStripDisallowed(comment, truncateTo: 255, disallowedCharacters: RealexFields.RealexFieldCommentDisallowRegex); result.Add(new RealexComment() { id = result.Count + 1, Value = sanitizedComment }); if (result.Count >= 2) { break; } } } return(result.Any() ? result : null); }
public ActionResult SendToPaymentProvider( PaymentProviderConfiguration configuration, GeneralisedPaymentTransfer transferObject, DateTime nowLocalTime) { var realExXfer = new RealExPaymentTransfer() { MerchantId = configuration.AccountIdentifer, OrderId = transferObject.TransactionId, Amount = (int)(transferObject.Amount * 100), ProviderUrl = Shared.RealEx.Default.RealExPostUrl, ReturnUrl = transferObject.ReturnUrl, Currency = "GBP", Timestamp = nowLocalTime.ToString("yyyyMMddHHmmss"), AutoSettleFlag = true }; realExXfer.Account = transferObject.Account; realExXfer.VariableReference = MessageContentUtility.TruncateAndStripDisallowed(transferObject.VariableReference, 50, RealexFields.RealexFieldVarRefDisallowRegex); realExXfer.CustomerNumber = transferObject.CustomerNumber; realExXfer.ProductId = MessageContentUtility.TruncateAndStripDisallowed(transferObject.ProductId, null, RealexFields.RealexFieldProductIdDisallowRegex); realExXfer.Comment1 = MessageContentUtility.TruncateAndStripDisallowed(transferObject.Comment1, 255, RealexFields.RealexFieldCommentDisallowRegex); realExXfer.Comment2 = MessageContentUtility.TruncateAndStripDisallowed(transferObject.Comment2, 255, RealexFields.RealexFieldCommentDisallowRegex); var fieldsForSignature = new List <string>(); fieldsForSignature.Add(realExXfer.Timestamp); fieldsForSignature.Add(realExXfer.MerchantId); fieldsForSignature.Add(realExXfer.OrderId); fieldsForSignature.Add(realExXfer.Amount.ToString()); fieldsForSignature.Add(realExXfer.Currency); if (transferObject.SaveCard != null) { realExXfer.HasSavedCard = true; realExXfer.CardStorageEnabled = true; realExXfer.OfferSaveCard = transferObject.SaveCard.OfferSaveCard; realExXfer.PaymentReference = MessageContentUtility.TruncateAndStripDisallowed(transferObject.SaveCard.PaymentReference, 30, RealexFields.RealexFieldPaymentRefDisallowRegex); realExXfer.PayerReference = MessageContentUtility.TruncateAndStripDisallowed(transferObject.SaveCard.PayerReference, 50, RealexFields.RealexFieldPayerRefDisallowRegex); realExXfer.PayerExists = transferObject.SaveCard.PayerExists; fieldsForSignature.Add(realExXfer.PayerReference); fieldsForSignature.Add(realExXfer.PaymentReference); } realExXfer.Sha1Hash = CalculateRealexSignature(fieldsForSignature.ToArray(), configuration.SharedSecret); var resultView = PaymentFrameworkUtility.CreateView("~/Views/Payment/SendToRealEx.cshtml", realExXfer); var sendToPaymentLogMessage = PaymentFrameworkUtility.DescribeActionResultForLogging(resultView, true); this.Logger.CreateEntry(typeof(RealExPaymentProvider), LogLevel.Info, sendToPaymentLogMessage); return(resultView); }
public static string CalculateRealexSignature( string[] fieldsToSign, string secretKey) { string concatenatedInput = string.Join(".", fieldsToSign); SHA1 sha = new SHA1CryptoServiceProvider(); ASCIIEncoding encoder = new ASCIIEncoding(); byte[] intermediateHashBytes = sha.ComputeHash(encoder.GetBytes(concatenatedInput)); string intermediateHashString = MessageContentUtility.ByteArrayToHexString(intermediateHashBytes).ToLower(); string finalStringToHash = $"{intermediateHashString}.{secretKey}"; byte[] finalHashBytes = sha.ComputeHash(encoder.GetBytes(finalStringToHash)); string finalHashedString = MessageContentUtility.ByteArrayToHexString(finalHashBytes).ToLower(); return(finalHashedString); }
public ActionResult SendToPaymentProvider( PaymentProviderConfiguration configuration, GeneralisedPaymentTransfer transferObject, Action <PaymentProviderConfiguration, GeneralisedPaymentTransfer, string> saveProviderReference) { int siteId; int.TryParse(configuration.AccountIdentifer, out siteId); int scpId; int.TryParse(transferObject.Account, out scpId); int hmacKeyId; string hmacSecretKey; CapitaApiHelpers.GetHmacIdAndSecretKey(configuration.SharedSecret, out hmacKeyId, out hmacSecretKey); string returnUrl = $"{transferObject.ReturnUrl}?{RoundTripTokenKey}={transferObject.TransactionId}"; CapitaInvokeRequest request = new CapitaInvokeRequest() { SiteId = siteId, ScpId = scpId, HmacKeyId = hmacKeyId, HmacKey = hmacSecretKey, UniqueReference = transferObject.TransactionId, PurchaseId = transferObject.ProductId, BookingRef = transferObject.Comment2, PurchaseDescription = transferObject.Comment1, PaymentTotal = (int)(transferObject.Amount * 100), ReturnUrl = returnUrl, IntegraCode = transferObject.GeneralLedgerCode, IsMediated = transferObject.IsMediated, FundCode = Shared.Capita.Default.FundCode, VatCode = transferObject.VatCode, VatRate = transferObject.VatRate }; if (transferObject.SaveCard != null) { request.SaveCard = true; request.CardHolderId = MessageContentUtility.TruncateAndStripDisallowed(transferObject.SaveCard.PayerReference, 50, null); } //Call Capita web service to set up the payment CapitaInvokeResponse response = InvokeRequest(request); if (response != null && !response.Error) { //call this action method to save scpReference into PendingPayment table saveProviderReference(configuration, transferObject, response.ScpReference); RedirectResult resultView = new RedirectResult(response.RedirectUrl, true); var sendToPaymentLogMessage = PaymentFrameworkUtility.DescribeActionResultForLogging(resultView, true); this.Logger.CreateEntry(typeof(CapitaPaymentProvider), LogLevel.Info, sendToPaymentLogMessage); return(resultView); } else { string errorMessage = "Capita Server returns null response."; if (response != null) { errorMessage += " " + response.ErrorMessage; } this.Logger.CreateEntry(typeof(CapitaPaymentProvider), LogLevel.Error, errorMessage); return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } }
public void TruncateAndStripDisallowed() { this.CustomerNumber = MessageContentUtility.TruncateAndStripDisallowed(this.CustomerNumber, disallowedCharacters: RealexFields.RealexFieldProductIdDisallowRegex); this.VarReference = MessageContentUtility.TruncateAndStripDisallowed(this.VarReference, truncateTo: 50, disallowedCharacters: RealexFields.RealexFieldVarRefDisallowRegex); this.ProductCode = MessageContentUtility.TruncateAndStripDisallowed(this.ProductCode, truncateTo: 50, disallowedCharacters: RealexFields.RealexFieldProductIdDisallowRegex); }