public async Task <TransactionProcessingResponse> CreateTransaction(TransactionRequest transactionRequest)
        {
            // Validate that all the transaction request values are present
            string validationErrors = Validation.ValidateTransactionRequest(transactionRequest);

            if (validationErrors != null)
            {
                throw new XenteMissingRequestException(validationErrors);
            }

            TransactionProcessingResponse transactionProcessingResponse = await
                                                                          this.HttpRequestClient.ExecuteTransactionRequest(transactionRequest);

            return(transactionProcessingResponse);
        }
Пример #2
0
        /// <summary>
        /// This is use to make transaction to the Xente API
        /// </summary>
        /// <param name="transactionRequest"></param>
        ///
        public async Task <TransactionProcessingResponse> ExecuteTransactionRequest(TransactionRequest transactionRequest, bool executeWithANewToken = false)
        {
            //Check if the token is present in the global variable or when executeWithANewToken is true
            if (this.BearerToken == null || executeWithANewToken == true)
            {
                this.BearerToken = await ExecuteBearerTokenRequest();
            }

            try
            {
                SetRequestHeaders();

                // Create a transaction by making a post request to Xente payment API
                HttpResponseMessage response = await HttpClient.PostAsJsonAsync($"{Constant.BaseUrl}/transactions", transactionRequest);

                // Throw an exception when the transaction is not successful
                response.EnsureSuccessStatusCode();

                // Cast the Json response to a custom type
                TransactionProcessingResponse transactionProcessingResponse = await response.Content.ReadAsAsync <TransactionProcessingResponse>();

                return(transactionProcessingResponse);
            }
            catch (Exception ex)
            {
                if (this.BearerToken != null && ex.Message.Contains("401"))
                {
                    // Repeat the execution of this function but with a new bearer token
                    TransactionProcessingResponse transactionProcessingResultsWithNewToken = await ExecuteTransactionRequest(transactionRequest, true);

                    return(transactionProcessingResultsWithNewToken);
                }
                else if (ex.Message.Contains("400"))
                {
                    throw new XenteIncorrectRequestException("Incorrect Transaction Request body Provided");
                }
                else
                {
                    throw new XenteConnectionException(ex.Message);
                }
            }
        }