public CapitaInvokeResponse InvokeRequest(CapitaInvokeRequest request)
        {
            scpSimpleInvokeRequest scpInvokeRequest = CreateCapitaInvokeRequest(request);
            CapitaInvokeResponse   response         = new CapitaInvokeResponse();

            using (scpClient capitaClient = new scpClient("CapitaScpSoap", Shared.Capita.Default.CapitaWebServiceUrl))
            {
                try
                {
                    scpInvokeResponse scpResponse  = capitaClient.scpSimpleInvoke(scpInvokeRequest);
                    string            errorMessage = string.Empty;
                    if (scpResponse?.invokeResult != null)
                    {
                        if (scpResponse.requestId == request.UniqueReference)
                        {
                            if (scpResponse.transactionState == transactionState.INVALID_REFERENCE)
                            {
                                errorMessage = "Transaction aborted! It may be because of session time out or some other technical glitch from Capita";
                            }
                            else
                            {
                                response.ScpReference = scpResponse.scpReference;
                                if (scpResponse.invokeResult.status == status.SUCCESS)
                                {
                                    response.RedirectUrl = (string)scpResponse.invokeResult.Item;
                                }
                                else
                                {
                                    errorDetails item = scpResponse.invokeResult.Item as errorDetails;
                                    errorMessage = item != null ? $"ErrorId: {item.errorId}, Message: {item.errorMessage}" : "Transaction failed for some unknown reason.";
                                }
                            }
                        }
                        else
                        {
                            errorMessage = "Transaction is being invalidated b/c the unique reference returned from Capita is wrong.";
                        }
                    }

                    if (!string.IsNullOrEmpty(errorMessage))
                    {
                        response.Error        = true;
                        response.ErrorMessage = errorMessage;
                    }
                }
                catch (Exception ex)
                {
                    response.Error        = true;
                    response.ErrorMessage = "Fatal error";
                }

                return(response);
            }
        }
        private scpSimpleInvokeRequest CreateCapitaInvokeRequest(CapitaInvokeRequest request)
        {
            scpSimpleInvokeRequest invokeRequest = new scpSimpleInvokeRequest
            {
                credentials = new credentials
                {
                    subject = new subject()
                    {
                        subjectType = subjectType.CapitaPortal,
                        identifier  = request.ScpId,
                        systemCode  = systemCode.SCP
                    },
                    requestIdentification = new requestIdentification()
                    {
                        uniqueReference = Guid.NewGuid().ToString(),
                        timeStamp       = DateTime.UtcNow.ToString("yyyyMMddHHmmss")
                    },
                    signature = new signature()
                    {
                        algorithm = algorithm.Original,
                        hmacKeyID = request.HmacKeyId
                    }
                }
            };

            string credentialsToHash = CapitaApiHelpers.GetCredentialsToHash(invokeRequest.credentials);

            invokeRequest.credentials.signature.digest = CapitaApiHelpers.CalculateDigest(request.HmacKey, credentialsToHash);
            invokeRequest.requestType = request.SaveCard ? requestType.payAndAutoStore : requestType.payOnly;

            //store card details
            if (request.SaveCard)
            {
                invokeRequest.additionalInstructions = new additionalInstructions()
                {
                    cardholderID = request.CardHolderId
                };
            }

            invokeRequest.requestId = request.UniqueReference;

            //ECOM – Indicates that the cardholder will be interacting directly with the web pages displayed by the SCP.
            //CNP – Indicates that the transaction is being processed by a third party(e.g.a telesales operative) on behalf of the cardholder.
            //N.B. In order to comply with bank rules and to ensure that 3 - D Secure authentication is processed correctly it is important to set this element correctly.
            invokeRequest.panEntryMethod = request.IsMediated ? panEntryMethod.CNP : panEntryMethod.ECOM;

            invokeRequest.routing = new routing()
            {
                siteId    = request.SiteId,
                scpId     = request.ScpId,
                returnUrl = request.ReturnUrl
            };

            invokeRequest.sale = new simpleSale()
            {
                saleSummary = new summaryData()
                {
                    description          = request.PurchaseDescription,
                    reference            = request.IntegraCode,
                    amountInMinorUnits   = request.PaymentTotal,
                    displayableReference = $"Booking reference : {request.BookingRef}"
                },
                items = new[]
                {
                    new simpleItem()
                    {
                        itemSummary = new summaryData()
                        {
                            description          = request.PurchaseDescription,
                            amountInMinorUnits   = request.PaymentTotal,
                            reference            = request.IntegraCode,
                            displayableReference = $"Booking reference : {request.BookingRef}"
                        },
                        lgItemDetails = new lgItemDetails()
                        {
                            additionalReference = request.PurchaseId,
                            fundCode            = string.IsNullOrEmpty(request.FundCode) ? null : request.FundCode,
                            isFundItem          = string.IsNullOrEmpty(request.FundCode) ? false : true
                        },
                        lineId = request.PurchaseId,
                        tax    = string.IsNullOrEmpty(request.VatCode) ? null :
                                 new taxItem()
                        {
                            vat = new vatItem()
                            {
                                vatCode = request.VatCode,
                                vatRate = request.VatRate
                            }
                        }
                    }
                }
            };

            return(invokeRequest);
        }