public void GetCredentialsToHash_CredentialObject_ConcatenatedString()
        {
            credentials capitalCredentials = new credentials()
            {
                subject = new subject()
                {
                    subjectType = subjectType.CapitaPortal,
                    identifier  = 174064579
                },
                requestIdentification = new requestIdentification()
                {
                    uniqueReference = "123456",
                    timeStamp       = "20170131125459",
                },
                signature = new signature()
                {
                    algorithm = algorithm.Original,
                    hmacKeyID = 456
                }
            };

            string stringToHash = CapitaApiHelpers.GetCredentialsToHash(capitalCredentials);

            Assert.AreEqual(stringToHash, "CapitaPortal!174064579!123456!20170131125459!Original!456");
        }
        public void CalculateDigest_ValidInput_HashString()
        {
            credentials capitalCredentials = new credentials()
            {
                subject = new subject()
                {
                    subjectType = subjectType.CapitaPortal,
                    identifier  = 174064579
                },
                requestIdentification = new requestIdentification()
                {
                    uniqueReference = "123456",
                    timeStamp       = "20170131125459",
                },
                signature = new signature()
                {
                    algorithm = algorithm.Original,
                    hmacKeyID = 456
                }
            };

            string hmacKey =
                "zgtQwyBsiFkL7ioGpH9YqiYioYpbkQjMmkBrvA6IXGBmzwx+Q5tFn6qbgVgKl95oIiPPHYpWaLquNRWXesBP3w==";

            string stringToHash = CapitaApiHelpers.GetCredentialsToHash(capitalCredentials);
            string hash         = CapitaApiHelpers.CalculateDigest(hmacKey, stringToHash);

            Assert.AreEqual(hash, "X+MlsmdD5RxMQ6/yPaQ0wzJY146oMD0Sp4g3hbXweTU=");
        }
        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));
            }
        }