public async Task <ActionResult> CheckoutPaymentsApi(decimal amount, string token) { var response = await PaymentsApiClient.ChargeCheckoutTransaction(amount, token); var content = await response.Content.ReadAsStringAsync(); // Return any error to display to the user. if (!response.IsSuccessStatusCode) { var errorMessage = TryGetErrorMessage(content); if (string.IsNullOrWhiteSpace(errorMessage)) { errorMessage = "The attempt to retrieve payment configurations from Payments API did not succeed and has produced an unexpected error."; } HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; return(new JsonResult() { Data = new { Error = errorMessage } }); } return(new HttpStatusCodeResult(HttpStatusCode.NoContent)); }
/// <summary> /// Sets the public key on the view model or any errors that occurred. /// </summary> private async Task SetPublicKey(CheckoutViewModel model) { var response = await PaymentsApiClient.GetPublicKey(); var content = await response.Content.ReadAsStringAsync(); // Check that the request to get the public key was successful. if (!response.IsSuccessStatusCode) { var errorMessage = TryGetErrorMessage(content); if (string.IsNullOrWhiteSpace(errorMessage)) { errorMessage = "The attempt to retrieve the public key from Payments API did not succeed and has produced an unexpected error."; } model.ErrorsViewModel.Errors.Add(new ErrorViewModel { Error = "Public key request error", Description = errorMessage }); return; } var publicKeyData = JsonConvert.DeserializeObject <Models.PaymentsApi.PublicKeyData>(content); model.PublicKey = publicKeyData.Value; }
public async Task GetPayments_Expected_StatusCode_Is_Return(HttpStatusCode statusCode, HttpContent content) { var mockHttpMessageHandler = new Mock <HttpMessageHandler>(); var merchantId = 123; var absoluteURi = $"{Url}/api/payments?merchantId={merchantId}"; mockHttpMessageHandler .Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.Is <HttpRequestMessage>(x => x.RequestUri.AbsoluteUri == absoluteURi && x.Method == HttpMethod.Get), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = statusCode, Content = content }); var httpClient = new HttpClient(mockHttpMessageHandler.Object); var paymentsApiClient = new PaymentsApiClient(httpClient, _configuration); var response = await paymentsApiClient.GetPayments(123); response.StatusCode.Should().Be(statusCode); }
public async Task CreatePayment_Expected_StatusCode_Is_Return(HttpStatusCode statusCode) { var mockHttpMessageHandler = new Mock <HttpMessageHandler>(); var absoluteURi = $"{Url}/api/payments"; mockHttpMessageHandler .Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.Is <HttpRequestMessage>(x => x.RequestUri.AbsoluteUri == absoluteURi && x.Method == HttpMethod.Put), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = statusCode }); var httpClient = new HttpClient(mockHttpMessageHandler.Object); var paymentsApiClient = new PaymentsApiClient(httpClient, _configuration); var response = await paymentsApiClient.CreatePayment(new PaymentRequest()); response.StatusCode.Should().Be(statusCode); }
/// <summary> /// Sets the merchant accounts on the view model or any errors that occurred. /// </summary> private async Task SetPaymentConfigurations(CheckoutViewModel model) { var response = await PaymentsApiClient.GetPaymentConfigurations(); var content = await response.Content.ReadAsStringAsync(); // Check that the request to get payment configurations was successful. if (!response.IsSuccessStatusCode) { var errorMessage = TryGetErrorMessage(content); if (string.IsNullOrWhiteSpace(errorMessage)) { errorMessage = "The attempt to retrieve payment configurations from Payments API did not succeed and has produced an unexpected error."; } model.ErrorsViewModel.Errors.Add(new ErrorViewModel { Error = "Payment configurations request error", Description = errorMessage }); return; } var paymentConfigurationsData = JsonConvert.DeserializeObject <Models.PaymentsApi.PaymentConfigurationsData>(content); // Check if there is at least one payment configuration available. if (!paymentConfigurationsData.Value.Any()) { model.ErrorsViewModel.Errors.Add(new ErrorViewModel { Error = "Payment configurations not found", Description = "There were no payment configurations returned from Payments API. " + "Ensure that there are payment configurations available to the current organization user. " + "For the purposes of this sample application you may not use payment configurations that are currently marked with a 'Live' process mode." }); return; } var validPaymentConfigurations = paymentConfigurationsData.Value.Where(pc => pc.ProcessMode != "Live"); // Check that at least one payment configuration is valid. if (!validPaymentConfigurations.Any()) { model.ErrorsViewModel.Errors.Add(new ErrorViewModel { Error = "Payment configurations not valid ('Live' process mode)", Description = "All payment configurations returned from Payments API are currently marked with a 'Live' process mode. " + "For the purposes of this sample application you may not use payment configurations that are currently marked with a 'Live' process mode. " }); return; } model.PaymentConfigurations = new SelectList(validPaymentConfigurations, "Id", "Name"); }
/// <summary> /// Validate that the authenticated organization user can make a request to Payments API. /// </summary> private async Task <ErrorViewModel> TestPaymentsApiAuthorization() { var response = await PaymentsApiClient.GetPublicKey(); if (response.IsSuccessStatusCode) { return(null); } return(new ErrorViewModel { Error = "Public key request error", Description = "The attempt to test Payments API did not succeed and has produced an unexpected error." }); }