Exemplo n.º 1
0
        /// <summary>
        /// Tries to receive external transfer
        /// </summary>
        /// <param name="id"></param>
        /// <param name="externalTransfer"></param>
        /// <returns>String representing succesful receiving</returns>
        public async Task <string> ReceiveExternalTransfer(string id, ExternalTransfer externalTransfer)
        {
            var webContext = WebOperationContext.Current;

            try
            {
                var incomingCredentials = webContext.IncomingRequest.Headers[HttpRequestHeader.Authorization];

                if (_authenticationManager.CheckBankCredentials(incomingCredentials) == false)
                {
                    webContext.OutgoingResponse.Headers[HttpResponseHeader.WwwAuthenticate] = "Basic realm=\"Base64(login:password)\"";

                    throw new WebFaultException <JsonError>(new JsonError("Incorrect credentials. Check login, password or investigate encoding process."), HttpStatusCode.Unauthorized);
                }

                var operation = new Operation(externalTransfer, id);
                await _operationManager.ExecuteOperation(operation);

                webContext.OutgoingResponse.StatusCode = HttpStatusCode.Created;
            }
            catch (FormatException exception)
            {
                throw new WebFaultException <JsonError>(new JsonError(exception.Message), HttpStatusCode.BadRequest);
            }

            return("Success");
        }
Exemplo n.º 2
0
        public void Transfer(BankAccount account, AccountNumber target, Currency amount)
        {
            BankAccount targetAccount;

            if (_accountNumberToAccount.TryGetValue(target, out targetAccount))
            {
                DateTime         now      = DateTime.Now;
                OutgoingTransfer outgoing = new OutgoingTransfer(
                    account, amount, CurrencyProvider.GetCurrency(0.5m, GetCurrency("USD")),    // NOI18N
                    now, _converter);
                IncommingTransfer incomming = new IncommingTransfer(
                    targetAccount, amount, now, _converter);

                outgoing.Apply();
                incomming.Apply();
            }
            else
            {
                DateTime         now      = DateTime.Now;
                OutgoingTransfer outgoing = new OutgoingTransfer(
                    account, amount, CurrencyProvider.GetCurrency(1m, GetCurrency("USD")),      // NOI18N
                    now, _converter);
                ExternalTransfer external = new ExternalTransfer(target, amount, now);
                outgoing.Apply();
                _interBankTransferSystem.EnqueueTransfer(external);
            }
        }
Exemplo n.º 3
0
        private async Task <HttpStatusCode> ExecuteExternalTransfer(Operation operation, string credentials)
        {
            using (var client = new HttpClient())
            {
                var externalOperation = new ExternalTransfer(operation);

                var destinationBankId = _operationAnalyzer.AccountAnalyzer.GetBankIdFromAccountId(operation.DestinationId);
                var externalAddress   = DAL.Instance.BankIdToIpMapping[destinationBankId];
                var url = externalAddress + "/accounts/" + operation.DestinationId;

                var content = new StringContent(externalOperation.ToJson(), Encoding.UTF8, "application/json");

                client.DefaultRequestHeaders.Add("Authorization", credentials);
                var response = await client.PostAsync(url, content);

                return(response.StatusCode);
            }
        }