Exemplo n.º 1
0
        public static TransactionCreateResponse Create(ShoppingCart cart,
                                                       Options options)
        {
            if (cart == null)
            {
                throw new ArgumentNullException(nameof(cart));
            }
            options = Options.Build(options);
            SendTransactionRequest request =
                OnepayRequestBuilder.Instance.BuildSendTransactionRequest(cart, options);
            string output = JsonConvert.SerializeObject(request);
            string input  = RequestAsync($"{ServiceUri}/{SendTransaction}",
                                         HttpMethod.Post, output).Result;
            SendTransactionResponse response =
                JsonConvert.DeserializeObject <SendTransactionResponse>(input);

            if (response == null)
            {
                throw new TransactionCreateException(-1,
                                                     "Could not obtain the service response");
            }
            else if (!response.ResponseCode.Equals("ok",
                                                   StringComparison.OrdinalIgnoreCase))
            {
                throw new TransactionCreateException(-1,
                                                     $"{response.ResponseCode} : {response.Description}");
            }

            if (!OnepaySignUtil.Instance.Validate(response.Result, options.SharedSecret))
            {
                throw new SignatureException("The response signature is not valid");
            }

            return(response.Result);
        }
Exemplo n.º 2
0
 public static TransactionHash ReadSendTransactionResponse(SendTransactionResponse response)
 {
     return(response.Hash == null ? null : new TransactionHash(Convert.FromBase64String(response.Hash)));
 }
        public SendTransactionResponse ProcessTransactionold(SendTransactionRequest request)
        {
            var dataStoreType = "Backup";

            Account account = null;

            if (dataStoreType == "Backup")
            {
                var transactionStore = new BackupTransactionRepository();
                account = transactionStore.GetAccount(request.DebtorAccountNumber);
            }
            else
            {
                var transactionStore = new TransactionRepository();
                account = transactionStore.GetAccount(request.DebtorAccountNumber);
            }

            var result = new SendTransactionResponse();

            if (request.PaymentScheme == PaymentScheme.Bacs)
            {
                if (account == null)
                {
                    result.Success = false;
                }
                else if (account.AllowedPaymentSchemes != AllowedPaymentSchemes.Bacs)
                {
                    result.Success = false;
                }
            }
            else if (request.PaymentScheme == PaymentScheme.FasterPayments)
            {
                if (account == null)
                {
                    result.Success = false;
                }
                else if (account.AllowedPaymentSchemes != AllowedPaymentSchemes.FasterPayments)
                {
                    result.Success = false;
                }
                else if (account.Balance < request.Amount)
                {
                    result.Success = false;
                }
            }
            else if (request.PaymentScheme == PaymentScheme.Chaps)
            {
                if (account == null)
                {
                    result.Success = false;
                }
                else if (account.AllowedPaymentSchemes != AllowedPaymentSchemes.Chaps)
                {
                    result.Success = false;
                }
                else if (account.Status != AccountStatus.Live)
                {
                    result.Success = false;
                }
            }

            if (result.Success)
            {
                account.Balance -= request.Amount;

                if (dataStoreType == "Backup")
                {
                    var transactionStore = new BackupTransactionRepository();
                    transactionStore.UpdateAccount(account);
                }
                else
                {
                    var transactionStore = new TransactionRepository();
                    transactionStore.UpdateAccount(account);
                }
            }

            return(result);
        }
        /// <summary>
        /// Re-write ProcessTransaction() method to make it more testable.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public SendTransactionResponse ProcessTransaction(SendTransactionV2Request request)
        {
            Account account;

            // This are functional service with process execution
            if (request.DataStoreType == DataStoreDefault.BackupType)
            {
                account = _backupTransactionRepository.GetAccount(request.DebtorAccountNumber);
            }
            else
            {
                account = _trasactionRepository.GetAccount(request.DebtorAccountNumber);
            }


            var result = new SendTransactionResponse();

            if (request.PaymentScheme == PaymentScheme.Bacs)
            {
                if (account == null)
                {
                    result.Success = false;
                }
                else if (account.AllowedPaymentSchemes != AllowedPaymentSchemes.Bacs)
                {
                    result.Success = false;
                }
            }
            else if (request.PaymentScheme == PaymentScheme.FasterPayments)
            {
                if (account == null)
                {
                    result.Success = false;
                }
                else if (account.AllowedPaymentSchemes != AllowedPaymentSchemes.FasterPayments)
                {
                    result.Success = false;
                }
                else if (account.Balance < request.Amount)
                {
                    result.Success = false;
                }
            }
            else if (request.PaymentScheme == PaymentScheme.Chaps)
            {
                if (account == null)
                {
                    result.Success = false;
                }
                else if (account.AllowedPaymentSchemes != AllowedPaymentSchemes.Chaps)
                {
                    result.Success = false;
                }
                else if (account.Status != AccountStatus.Live)
                {
                    result.Success = false;
                }
            }
            else
            {
                result.Success = true;
            }

            if (result.Success)
            {
                account.Balance -= request.Amount;

                if (request.DataStoreType == DataStoreDefault.BackupType)
                {
                    _backupTransactionRepository.UpdateAccount(account);
                }
                else
                {
                    _trasactionRepository.UpdateAccount(account);
                }
            }

            return(result);
        }