aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2021-04-04 19:39:29 +0200
committerivarlovlie <git@ivarlovlie.no>2021-04-04 19:39:29 +0200
commit7965562ac3d64fbc1f742a1427e7a9c932eb2d28 (patch)
tree777321de7f0886f6a23f38aaf848a303e75dd02a /src
parentae12f681bad3befacc9d36d85415454f854b3e9d (diff)
downloaddotnet-vipps-ecommerce-7965562ac3d64fbc1f742a1427e7a9c932eb2d28.tar.xz
dotnet-vipps-ecommerce-7965562ac3d64fbc1f742a1427e7a9c932eb2d28.zip
new execution-history for api calls
Diffstat (limited to 'src')
-rw-r--r--src/IOL.VippsEcommerce.Tests/Helpers.cs6
-rw-r--r--src/IOL.VippsEcommerce.Tests/PaymentInitiationTests.cs13
-rw-r--r--src/IOL.VippsEcommerce/Models/Api/VippsRequestException.cs21
-rw-r--r--src/IOL.VippsEcommerce/VippsEcommerceService.cs325
4 files changed, 184 insertions, 181 deletions
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<VippsConfiguration>(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);
}
/// <summary>
@@ -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 (!forceRefresh && _cacheDirectoryPath.IsPresent() && File.Exists(CacheFilePath)) {
+ var fileContents = await File.ReadAllTextAsync(CacheFilePath, ct);
- if (fileContents.IsPresent()) {
- VippsAuthorizationTokenResponse credentials = default;
- try {
- credentials = JsonSerializer.Deserialize<VippsAuthorizationTokenResponse>(fileContents);
- } catch (Exception e) {
- if (e is JsonException && _cacheEncryptionKey.IsPresent()) {
- // most likely encrypted, try to decrypt
+ if (fileContents.IsPresent()) {
+ VippsAuthorizationTokenResponse credentials = default;
+ try {
+ credentials = JsonSerializer.Deserialize<VippsAuthorizationTokenResponse>(fileContents);
+ } catch (Exception e) {
+ if (e is JsonException && _cacheEncryptionKey.IsPresent()) { // most likely encrypted
+ try {
var decryptedContents = fileContents.DecryptWithAes(_cacheEncryptionKey);
credentials =
JsonSerializer.Deserialize<VippsAuthorizationTokenResponse>(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<VippsAuthorizationTokenResponse>(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<VippsErrorResponse>(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<VippsErrorResponse>(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<VippsInitiatePaymentResponse>(cancellationToken: ct);
+ .ReadFromJsonAsync<VippsInitiatePaymentResponse>(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<VippsErrorResponse>(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<VippsErrorResponse>(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<VippsPaymentActionResponse>(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<VippsErrorResponse>(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<VippsErrorResponse>(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<VippsPaymentActionResponse>(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<VippsErrorResponse>(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<VippsErrorResponse>(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<VippsPaymentActionResponse>(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<VippsErrorResponse>(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<VippsErrorResponse>(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<VippsPaymentActionResponse>(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<VippsErrorResponse>(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<VippsErrorResponse>(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<VippsErrorResponse>(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<VippsErrorResponse>(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<VippsGetPaymentDetailsResponse>(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<VippsErrorResponse>(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<VippsErrorResponse>(cancellationToken: ct);
+ _logger.LogError(nameof(GetPaymentDetailsAsync)
+ + " Api error response: "
+ + JsonSerializer.Serialize(response.Content));
+ } catch {
+ // ignored
+ }
- Console.WriteLine(e);
throw exception;
}
}