From 7965562ac3d64fbc1f742a1427e7a9c932eb2d28 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sun, 4 Apr 2021 19:39:29 +0200 Subject: new execution-history for api calls --- src/IOL.VippsEcommerce.Tests/Helpers.cs | 6 - .../PaymentInitiationTests.cs | 13 - .../Models/Api/VippsRequestException.cs | 21 +- src/IOL.VippsEcommerce/VippsEcommerceService.cs | 327 +++++++++++---------- 4 files changed, 185 insertions(+), 182 deletions(-) delete mode 100644 src/IOL.VippsEcommerce.Tests/PaymentInitiationTests.cs (limited to 'src') diff --git a/src/IOL.VippsEcommerce.Tests/Helpers.cs b/src/IOL.VippsEcommerce.Tests/Helpers.cs index 0553027..8b82741 100644 --- a/src/IOL.VippsEcommerce.Tests/Helpers.cs +++ b/src/IOL.VippsEcommerce.Tests/Helpers.cs @@ -20,11 +20,5 @@ namespace IOL.VippsEcommerce.Tests return vippsEcommerceService; } - - public static VippsConfiguration GetVippsValidConfiguration() { - var json = System.IO.File.ReadAllText("configuration.json"); - var configuration = JsonSerializer.Deserialize(json); - return configuration; - } } } \ No newline at end of file diff --git a/src/IOL.VippsEcommerce.Tests/PaymentInitiationTests.cs b/src/IOL.VippsEcommerce.Tests/PaymentInitiationTests.cs deleted file mode 100644 index d802fb1..0000000 --- a/src/IOL.VippsEcommerce.Tests/PaymentInitiationTests.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Xunit.Abstractions; - -namespace IOL.VippsEcommerce.Tests -{ - public class PaymentInitiationTests - { - private readonly ITestOutputHelper _helper; - - public PaymentInitiationTests(ITestOutputHelper helper) { - _helper = helper; - } - } -} \ No newline at end of file diff --git a/src/IOL.VippsEcommerce/Models/Api/VippsRequestException.cs b/src/IOL.VippsEcommerce/Models/Api/VippsRequestException.cs index 3a9b62a..f456131 100644 --- a/src/IOL.VippsEcommerce/Models/Api/VippsRequestException.cs +++ b/src/IOL.VippsEcommerce/Models/Api/VippsRequestException.cs @@ -1,18 +1,19 @@ +#nullable enable using System; namespace IOL.VippsEcommerce.Models.Api { - [Serializable] - public class VippsRequestException : Exception - { - public VippsRequestException() { } + [Serializable] + public class VippsRequestException : Exception + { + public VippsRequestException() { } - public VippsRequestException(string message) - : base(message) { } + public VippsRequestException(string message) + : base(message) { } - public VippsRequestException(string message, Exception inner) - : base(message, inner) { } + public VippsRequestException(string message, Exception inner) + : base(message, inner) { } - public VippsErrorResponse ErrorResponse { get; set; } - } + public VippsErrorResponse? ErrorResponse { get; set; } + } } \ No newline at end of file diff --git a/src/IOL.VippsEcommerce/VippsEcommerceService.cs b/src/IOL.VippsEcommerce/VippsEcommerceService.cs index 1611809..291899d 100644 --- a/src/IOL.VippsEcommerce/VippsEcommerceService.cs +++ b/src/IOL.VippsEcommerce/VippsEcommerceService.cs @@ -49,10 +49,10 @@ namespace IOL.VippsEcommerce _vippsClientId = Configuration.GetValue(VippsConfigurationKeyNames.VIPPS_CLIENT_ID); _vippsClientSecret = Configuration.GetValue(VippsConfigurationKeyNames.VIPPS_CLIENT_SECRET); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", - Configuration.GetValue(VippsConfigurationKeyNames - .VIPPS_SUBSCRIPTION_KEY_PRIMARY) - ?? Configuration.GetValue(VippsConfigurationKeyNames - .VIPPS_SUBSCRIPTION_KEY_SECONDARY)); + Configuration.GetValue(VippsConfigurationKeyNames + .VIPPS_SUBSCRIPTION_KEY_PRIMARY) + ?? Configuration.GetValue(VippsConfigurationKeyNames + .VIPPS_SUBSCRIPTION_KEY_SECONDARY)); var msn = Configuration.GetValue(VippsConfigurationKeyNames.VIPPS_MSN); if (msn.IsPresent()) { @@ -85,14 +85,16 @@ namespace IOL.VippsEcommerce if (_cacheDirectoryPath.IsPresent()) { if (!_cacheDirectoryPath.IsDirectoryWritable()) { _logger.LogError("Could not write to cache file directory (" - + _cacheDirectoryPath - + "). Disabling caching."); + + _cacheDirectoryPath + + "). Disabling caching."); _cacheDirectoryPath = default; _cacheEncryptionKey = default; } } - _logger.LogInformation("VippsEcommerceService was successfully initialised with api url: " + vippsApiUrl); + _logger.LogInformation(nameof(VippsEcommerceService) + + " was successfully initialised with api url: " + + vippsApiUrl); } /// @@ -106,31 +108,32 @@ namespace IOL.VippsEcommerce bool forceRefresh = false, CancellationToken ct = default ) { - if (!forceRefresh) { - if (_cacheDirectoryPath.IsPresent() && File.Exists(CacheFilePath)) { - var fileContents = await File.ReadAllTextAsync(CacheFilePath, ct); - - if (fileContents.IsPresent()) { - VippsAuthorizationTokenResponse credentials = default; - try { - credentials = JsonSerializer.Deserialize(fileContents); - } catch (Exception e) { - if (e is JsonException && _cacheEncryptionKey.IsPresent()) { - // most likely encrypted, try to decrypt + if (!forceRefresh && _cacheDirectoryPath.IsPresent() && File.Exists(CacheFilePath)) { + var fileContents = await File.ReadAllTextAsync(CacheFilePath, ct); + + if (fileContents.IsPresent()) { + VippsAuthorizationTokenResponse credentials = default; + try { + credentials = JsonSerializer.Deserialize(fileContents); + } catch (Exception e) { + if (e is JsonException && _cacheEncryptionKey.IsPresent()) { // most likely encrypted + try { var decryptedContents = fileContents.DecryptWithAes(_cacheEncryptionKey); credentials = JsonSerializer.Deserialize(decryptedContents); + } catch { + // ignored } } + } - if (credentials != default) { - var currentEpoch = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); - if (long.TryParse(credentials.ExpiresOn, out var expires) - && credentials.AccessToken.IsPresent()) { - if (expires - 600 > currentEpoch) { - _logger.LogDebug("VippsEcommerceService: Got tokens from cache"); - return credentials; - } + if (credentials != default) { + var currentEpoch = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); + if (long.TryParse(credentials.ExpiresOn, out var expires) + && credentials.AccessToken.IsPresent()) { + if (expires - 600 > currentEpoch) { + _logger.LogDebug(nameof(VippsEcommerceService) + ": Got tokens from cache"); + return credentials; } } } @@ -156,29 +159,30 @@ namespace IOL.VippsEcommerce if (_cacheDirectoryPath.IsPresent()) { await File.WriteAllTextAsync(CacheFilePath, - _cacheEncryptionKey.IsPresent() - ? credentials.EncryptWithAes(_cacheEncryptionKey) - : credentials, - ct); + _cacheEncryptionKey.IsPresent() + ? credentials.EncryptWithAes(_cacheEncryptionKey) + : credentials, + ct); } - _logger.LogDebug("VippsEcommerceService: Got tokens from " + requestMessage.RequestUri); + _logger.LogDebug(nameof(VippsEcommerceService) + ": Got tokens from " + requestMessage.RequestUri); return JsonSerializer.Deserialize(credentials); } catch (Exception e) { - var exception = - new VippsRequestException("Vipps get token request returned unsuccessfully.", e); - if (e is HttpRequestException) { - try { - exception.ErrorResponse = - await response.Content.ReadFromJsonAsync(cancellationToken: ct); - _logger.LogError("ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - } catch (Exception e1) { - _logger.LogError("Unknown ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - Console.WriteLine(e1); - } + var exception = new VippsRequestException(nameof(GetAuthorizationTokenAsync) + " failed.", e); + if (e is not HttpRequestException) { + throw exception; + } + + try { + exception.ErrorResponse = + await response.Content.ReadFromJsonAsync(cancellationToken: ct); + _logger.LogError(nameof(GetAuthorizationTokenAsync) + + " Api error response: " + + JsonSerializer.Serialize(response.Content)); + } catch { + // ignored } - Console.WriteLine(e); throw exception; } } @@ -207,30 +211,33 @@ namespace IOL.VippsEcommerce } var response = await _client.PostAsJsonAsync("ecomm/v2/payments", - payload, - _requestJsonSerializerOptions, - ct); + payload, + _requestJsonSerializerOptions, + ct); try { response.EnsureSuccessStatusCode(); - _logger.LogDebug("VippsEcommerceService: Sent InitiatePaymentRequest"); + _logger.LogDebug(nameof(VippsEcommerceService) + + ": Successfully issued a request to " + + response.RequestMessage?.RequestUri); return await response.Content - .ReadFromJsonAsync(cancellationToken: ct); + .ReadFromJsonAsync(cancellationToken: ct); } catch (Exception e) { - var exception = - new VippsRequestException("Vipps initiate payment request returned unsuccessfully.", e); - if (e is HttpRequestException) { - try { - exception.ErrorResponse = - await response.Content.ReadFromJsonAsync(cancellationToken: ct); - _logger.LogError("ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - } catch (Exception e1) { - _logger.LogError("Unknown ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - Console.WriteLine(e1); - } + var exception = new VippsRequestException(nameof(InitiatePaymentAsync) + " failed.", e); + if (e is not HttpRequestException) { + throw exception; + } + + try { + exception.ErrorResponse = + await response.Content.ReadFromJsonAsync(cancellationToken: ct); + _logger.LogError(nameof(InitiatePaymentAsync) + + " Api error response: " + + JsonSerializer.Serialize(response.Content)); + } catch { + // ignored } - Console.WriteLine(e); throw exception; } } @@ -265,29 +272,32 @@ namespace IOL.VippsEcommerce } var response = await _client.PostAsJsonAsync("ecomm/v2/payments/" + orderId + "/capture", - payload, - _requestJsonSerializerOptions, - ct); + payload, + _requestJsonSerializerOptions, + ct); try { response.EnsureSuccessStatusCode(); - _logger.LogDebug("VippsEcommerceService: Sent CapturePaymentRequest"); + _logger.LogDebug(nameof(VippsEcommerceService) + + ": Successfully issued a request to " + + response.RequestMessage?.RequestUri); return await response.Content.ReadFromJsonAsync(cancellationToken: ct); } catch (Exception e) { - var exception = - new VippsRequestException("Vipps capture payment request returned unsuccessfully.", e); - if (e is HttpRequestException) { - try { - exception.ErrorResponse = - await response.Content.ReadFromJsonAsync(cancellationToken: ct); - _logger.LogError("ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - } catch (Exception e1) { - _logger.LogError("Unknown ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - Console.WriteLine(e1); - } + var exception = new VippsRequestException(nameof(CapturePaymentAsync) + " failed.", e); + if (e is not HttpRequestException) { + throw exception; + } + + try { + exception.ErrorResponse = + await response.Content.ReadFromJsonAsync(cancellationToken: ct); + _logger.LogError(nameof(CapturePaymentAsync) + + " Api error response: " + + JsonSerializer.Serialize(response.Content)); + } catch { + // ignored } - Console.WriteLine(e); throw exception; } } @@ -319,30 +329,32 @@ namespace IOL.VippsEcommerce } var response = await _client.PutAsJsonAsync("ecomm/v2/payments/" + orderId + "/cancel", - payload, - _requestJsonSerializerOptions, - ct); + payload, + _requestJsonSerializerOptions, + ct); try { response.EnsureSuccessStatusCode(); - _logger.LogDebug("VippsEcommerceService: Sent CancelPaymentRequest"); + _logger.LogDebug(nameof(VippsEcommerceService) + + ": Successfully issued a request to " + + response.RequestMessage?.RequestUri); return await response.Content.ReadFromJsonAsync(cancellationToken: ct); } catch (Exception e) { - var exception = - new VippsRequestException("Vipps cancel payment request returned unsuccessfully.", e); - if (e is HttpRequestException) { - try { - exception.ErrorResponse = - await response.Content.ReadFromJsonAsync(cancellationToken: ct); - _logger.LogError("ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - } catch (Exception e1) { - _logger.LogError("Unknown ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - Console.WriteLine(e1); - } + var exception = new VippsRequestException(nameof(CancelPaymentAsync) + " failed.", e); + if (e is not HttpRequestException) { + throw exception; } + try { + exception.ErrorResponse = + await response.Content.ReadFromJsonAsync(cancellationToken: ct); + _logger.LogError(nameof(CancelPaymentAsync) + + " Api error response: " + + JsonSerializer.Serialize(response.Content)); + } catch { + // ignored + } - Console.WriteLine(e); throw exception; } } @@ -372,30 +384,32 @@ namespace IOL.VippsEcommerce } var response = await _client.PutAsJsonAsync("ecomm/v2/payments/" + orderId + "/authorize", - payload, - _requestJsonSerializerOptions, - ct); + payload, + _requestJsonSerializerOptions, + ct); try { response.EnsureSuccessStatusCode(); - _logger.LogDebug("VippsEcommerceService: Sent AuthorizePaymentRequest"); + _logger.LogDebug(nameof(VippsEcommerceService) + + ": Successfully issued a request to " + + response.RequestMessage?.RequestUri); return await response.Content.ReadFromJsonAsync(cancellationToken: ct); } catch (Exception e) { - var exception = - new VippsRequestException("Vipps authorize payment request returned unsuccessfully.", e); - if (e is HttpRequestException) { - try { - exception.ErrorResponse = - await response.Content.ReadFromJsonAsync(cancellationToken: ct); - _logger.LogError("ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - } catch (Exception e1) { - _logger.LogError("Unknown ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - Console.WriteLine(e1); - } + var exception = new VippsRequestException(nameof(AuthorizePaymentAsync) + " failed.", e); + if (e is not HttpRequestException) { + throw exception; } + try { + exception.ErrorResponse = + await response.Content.ReadFromJsonAsync(cancellationToken: ct); + _logger.LogError(nameof(AuthorizePaymentAsync) + + " Api error response: " + + JsonSerializer.Serialize(response.Content)); + } catch { + // ignored + } - Console.WriteLine(e); throw exception; } } @@ -429,29 +443,31 @@ namespace IOL.VippsEcommerce } var response = await _client.PostAsJsonAsync("ecomm/v2/payments/" + orderId + "/refund", - payload, - _requestJsonSerializerOptions, - ct); + payload, + _requestJsonSerializerOptions, + ct); try { response.EnsureSuccessStatusCode(); - _logger.LogDebug("VippsEcommerceService: Sent RefundPaymentRequest"); + _logger.LogDebug(nameof(VippsEcommerceService) + + ": Successfully issued a request to " + + response.RequestMessage?.RequestUri); return await response.Content.ReadFromJsonAsync(cancellationToken: ct); } catch (Exception e) { - var exception = - new VippsRequestException("Vipps refund payment request returned unsuccessfully.", e); - if (e is HttpRequestException) { - try { - exception.ErrorResponse = - await response.Content.ReadFromJsonAsync(cancellationToken: ct); - _logger.LogError("ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - } catch (Exception e1) { - _logger.LogError("Unknown ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - Console.WriteLine(e1); - } + var exception = new VippsRequestException(nameof(RefundPaymentAsync) + " failed.", e); + if (e is not HttpRequestException) { + throw exception; } + try { + exception.ErrorResponse = + await response.Content.ReadFromJsonAsync(cancellationToken: ct); + _logger.LogError(nameof(RefundPaymentAsync) + + " Api error response: " + + JsonSerializer.Serialize(response.Content)); + } catch { + // ignored + } - Console.WriteLine(e); throw exception; } } @@ -480,29 +496,32 @@ namespace IOL.VippsEcommerce var response = await _client.PostAsJsonAsync("ecomm/v2/integration-test/payments/" + orderId + "/approve", - payload, - _requestJsonSerializerOptions, - ct); + payload, + _requestJsonSerializerOptions, + ct); try { response.EnsureSuccessStatusCode(); - _logger.LogDebug("VippsEcommerceService: Sent ForceApprovePaymentRequest"); + _logger.LogDebug(nameof(VippsEcommerceService) + + ": Successfully issued a request to " + + response.RequestMessage?.RequestUri); return true; } catch (Exception e) { - var exception = - new VippsRequestException("Vipps force approve payment request returned unsuccessfully.", e); - if (e is HttpRequestException) { - try { - exception.ErrorResponse = - await response.Content.ReadFromJsonAsync(cancellationToken: ct); - _logger.LogError("ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - } catch (Exception e1) { - _logger.LogError("Unknown ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - Console.WriteLine(e1); - } + var exception = new VippsRequestException(nameof(ForceApprovePaymentAsync) + " failed.", e); + if (e is not HttpRequestException) { + throw exception; + } + + try { + exception.ErrorResponse = + await response.Content.ReadFromJsonAsync(cancellationToken: ct); + _logger.LogError(nameof(ForceApprovePaymentAsync) + + " Api error response: " + + JsonSerializer.Serialize(response.Content)); + } catch { + // ignored } - Console.WriteLine(e); throw exception; } } @@ -527,25 +546,27 @@ namespace IOL.VippsEcommerce try { response.EnsureSuccessStatusCode(); - _logger.LogDebug("VippsEcommerceService: Sent GetPaymentDetailsRequest"); + _logger.LogDebug(nameof(VippsEcommerceService) + + ": Successfully issued a request to " + + response.RequestMessage?.RequestUri); return await response.Content.ReadFromJsonAsync(cancellationToken: ct); } catch (Exception e) { - var exception = - new VippsRequestException("Vipps get payment detailsG request returned unsuccessfully.", e); - if (e is HttpRequestException) { - try { - exception.ErrorResponse = - await response.Content.ReadFromJsonAsync(cancellationToken: ct); - _logger.LogError("ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - } catch (Exception e1) { - _logger.LogError("Unknown ErrorResponse: " + JsonSerializer.Serialize(response.Content)); - Console.WriteLine(e1); - } + var exception = new VippsRequestException(nameof(GetPaymentDetailsAsync) + " failed.", e); + if (e is not HttpRequestException) { + throw exception; } + try { + exception.ErrorResponse = + await response.Content.ReadFromJsonAsync(cancellationToken: ct); + _logger.LogError(nameof(GetPaymentDetailsAsync) + + " Api error response: " + + JsonSerializer.Serialize(response.Content)); + } catch { + // ignored + } - Console.WriteLine(e); throw exception; } } -- cgit v1.3