Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed global config and static classes #47

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var vippsConfigurationOptions = new VippsConfigurationOptions
UseTestMode = true
};

VippsConfiguration.ConfigureVipps(vippsConfigurationOptions);
var vippsApi = VippsApi.Create(vippsConfigurationOptions)

var request = new InitiateSessionRequest
{
Expand All @@ -57,7 +57,7 @@ var request = new InitiateSessionRequest
}
};

var result = await CheckoutService.InitiateSession(request);
var result = await vippsApi.CheckoutService().InitiateSession(request);

```

Expand Down Expand Up @@ -97,6 +97,6 @@ All response objects have a property called `RawResponse` that contains the resp
**`RawResponse` example:**

```c#
var response = checkoutService.InitiateSession(initiateSessionRequest);
var response = vippsApi.CheckoutService().InitiateSession(initiateSessionRequest);
var cancellationUrl = response.RawResponse["cancellationUrl"].ToString();
```
7 changes: 5 additions & 2 deletions src/Tests/Vipps.net.IntegrationTests/CheckoutServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class CheckoutServiceTests
[TestMethod]
public async Task Can_Create_And_Get_Session()
{
var vippsApi = TestSetup.CreateVippsAPI();
var reference = Guid.NewGuid().ToString();
var sessionInitiationRequest = new Models.Checkout.InitiateSessionRequest
{
Expand All @@ -26,9 +27,11 @@ public async Task Can_Create_And_Get_Session()
}
};

var sessionResponse = await CheckoutService.InitiateSession(sessionInitiationRequest);
var sessionResponse = await vippsApi
.CheckoutService()
.InitiateSession(sessionInitiationRequest);
Assert.IsNotNull(sessionResponse);
var sessionPolledResponse = await CheckoutService.GetSessionInfo(reference);
var sessionPolledResponse = await vippsApi.CheckoutService().GetSessionInfo(reference);
Assert.AreEqual(
Models.Checkout.ExternalSessionState.SessionCreated,
sessionPolledResponse.SessionState
Expand Down
56 changes: 39 additions & 17 deletions src/Tests/Vipps.net.IntegrationTests/EpaymentServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,77 @@ public class EpaymentServiceTests
[TestMethod]
public async Task Can_Create_Get_Cancel_Payment()
{
IVippsApi vippsApi = TestSetup.CreateVippsAPI();
olokken marked this conversation as resolved.
Show resolved Hide resolved
var reference = Guid.NewGuid().ToString();
var createPaymentRequest = GetCreatePaymentRequest(reference);

var createPaymentResponse = await EpaymentService.CreatePayment(createPaymentRequest);
var createPaymentResponse = await vippsApi
.EpaymentService()
.CreatePayment(createPaymentRequest);
Assert.IsNotNull(createPaymentResponse);
Assert.AreEqual(reference, createPaymentResponse.Reference);

var modificationResponse = await EpaymentService.CancelPayment(reference);
var modificationResponse = await vippsApi.EpaymentService().CancelPayment(reference);
Assert.IsNotNull(modificationResponse);
Assert.AreEqual(reference, modificationResponse.Reference);
Assert.AreEqual(State.TERMINATED, modificationResponse.State);

var getPaymentResponse = await EpaymentService.GetPayment(reference);
var getPaymentResponse = await vippsApi.EpaymentService().GetPayment(reference);
Assert.AreEqual(reference, getPaymentResponse.Reference);
Assert.AreEqual(State.TERMINATED, getPaymentResponse.State);
}

[Ignore] //Test is failing because paymentaction has changed variable name
[TestMethod]
public async Task Can_Create_Approve_Capture_Refund_Payment()
{
IVippsApi vippsApi = TestSetup.CreateVippsAPI();
var reference = Guid.NewGuid().ToString();
var createPaymentRequest = GetCreatePaymentRequest(reference);

var createPaymentResponse = await EpaymentService.CreatePayment(createPaymentRequest);
var createPaymentResponse = await vippsApi
.EpaymentService()
.CreatePayment(createPaymentRequest);
Assert.IsNotNull(createPaymentResponse);
Assert.AreEqual(reference, createPaymentResponse.Reference);

await EpaymentService.ForceApprovePayment(
reference,
new ForceApprove { Customer = new Customer { PhoneNumber = CustomerPhoneNumber } }
);
await vippsApi
.EpaymentService()
.ForceApprovePayment(
reference,
new ForceApprove
{
Customer = new Customer { PhoneNumber = CustomerPhoneNumber }
}
);

var captureResponse = await EpaymentService.CapturePayment(
reference,
new CaptureModificationRequest { ModificationAmount = createPaymentRequest.Amount }
);
var captureResponse = await vippsApi
.EpaymentService()
.CapturePayment(
reference,
new CaptureModificationRequest
{
ModificationAmount = createPaymentRequest.Amount
}
);
Assert.IsNotNull(captureResponse);
Assert.AreEqual(reference, captureResponse.Reference);
Assert.AreEqual(State.AUTHORIZED, captureResponse.State);

var refundResponse = await EpaymentService.RefundPayment(
reference,
new RefundModificationRequest { ModificationAmount = createPaymentRequest.Amount }
);
var refundResponse = await vippsApi
.EpaymentService()
.RefundPayment(
reference,
new RefundModificationRequest
{
ModificationAmount = createPaymentRequest.Amount
}
);
Assert.IsNotNull(refundResponse);
Assert.AreEqual(reference, refundResponse.Reference);
Assert.AreEqual(State.AUTHORIZED, refundResponse.State);

var paymentEvents = await EpaymentService.GetPaymentEventLog(reference);
var paymentEvents = await vippsApi.EpaymentService().GetPaymentEventLog(reference);
Assert.IsNotNull(paymentEvents);
AssertOneEvent(paymentEvents, PaymentEventName.CREATED);
AssertOneEvent(paymentEvents, PaymentEventName.CAPTURED);
Expand Down
5 changes: 2 additions & 3 deletions src/Tests/Vipps.net.IntegrationTests/TestSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace Vipps.net.IntegrationTests
[TestClass]
public class TestSetup
{
[AssemblyInitialize]
public static void TestFixtureSetup(TestContext context)
public static IVippsApi CreateVippsAPI()
{
// Called once before any MSTest test method has started (optional)
var configbuilder = new ConfigurationBuilder();
Expand Down Expand Up @@ -40,7 +39,7 @@ public static void TestFixtureSetup(TestContext context)
};

// The following line configures vipps with custom settings
VippsConfiguration.ConfigureVipps(vippsConfigurationOptions);
return VippsApi.Create(vippsConfigurationOptions);
}

private static string GetConfigValue(IConfiguration config, string key)
Expand Down
15 changes: 9 additions & 6 deletions src/Tests/Vipps.net.Tests/AccessTokenCacheServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,33 @@ public class AccessTokenCacheServiceTests
[TestMethod]
public void Can_Retrieve_Saved_Valid()
{
var accessTokenCacheService = new AccessTokenCacheService();
var key = Guid.NewGuid().ToString();
var accessToken = GetToken(DateTime.Now.AddHours(-1), DateTime.Now.AddHours(1));
AccessTokenCacheService.Add(key, accessToken);
var res = AccessTokenCacheService.Get(key);
accessTokenCacheService.Add(key, accessToken);
var res = accessTokenCacheService.Get(key);
Assert.AreEqual(accessToken, res);
}

[TestMethod]
public void Can_Not_Retrieve_Saved_Expired()
{
AccessTokenCacheService accessTokenCacheService = new AccessTokenCacheService();
var key = Guid.NewGuid().ToString();
var accessToken = GetToken(DateTime.Now.AddHours(-2), DateTime.Now.AddHours(-1));
AccessTokenCacheService.Add(key, accessToken);
var res = AccessTokenCacheService.Get(key);
accessTokenCacheService.Add(key, accessToken);
var res = accessTokenCacheService.Get(key);
Assert.IsNull(res);
}

[TestMethod]
public void Can_Not_Retrieve_Saved_NotValidForLongEnough()
{
AccessTokenCacheService accessTokenCacheService = new AccessTokenCacheService();
var key = Guid.NewGuid().ToString();
var accessToken = GetToken(DateTime.Now.AddHours(-2), DateTime.Now.AddMinutes(1));
AccessTokenCacheService.Add(key, accessToken);
var res = AccessTokenCacheService.Get(key);
accessTokenCacheService.Add(key, accessToken);
var res = accessTokenCacheService.Get(key);
Assert.IsNull(res);
}

Expand Down
25 changes: 0 additions & 25 deletions src/Tests/Vipps.net.Tests/VippsConfigurationTests.cs

This file was deleted.

9 changes: 7 additions & 2 deletions src/Vipps.net.AspCoreDemo/Controllers/CheckoutController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ namespace Vipps.net.AspCore31Demo.Controllers
public class CheckoutController : ControllerBase
{
private readonly ILogger<CheckoutController> _logger;
private readonly IVippsCheckoutService _checkoutService;

public CheckoutController(ILogger<CheckoutController> logger)
public CheckoutController(
ILogger<CheckoutController> logger,
IVippsCheckoutService vippsCheckoutService
)
{
_logger = logger;
_checkoutService = vippsCheckoutService;
}

[HttpPost]
Expand All @@ -42,7 +47,7 @@ public async Task<ActionResult<InitiateSessionResponse>> CreateSession()
request.Transaction.Reference
);

var result = await CheckoutService.InitiateSession(request);
var result = await _checkoutService.InitiateSession(request);

_logger.LogInformation(
"Created session with reference {reference}",
Expand Down
39 changes: 39 additions & 0 deletions src/Vipps.net.AspCoreDemo/Controllers/EpaymentController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Vipps.net.Models.Epayment;
using Vipps.net.Services;

namespace Vipps.net.AspCore31Demo.Controllers
{
[ApiController]
[Route("[controller]")]
public class EpaymentController
{
private readonly IVippsEpaymentService _epaymentService;

public EpaymentController(IVippsEpaymentService vippsEpaymentService)
{
_epaymentService = vippsEpaymentService;
}

[HttpPost]
public async Task<string> CreatePayment()
{
var request = new CreatePaymentRequest
{
Amount = new Amount { Value = 1000, Currency = Currency.NOK },
PaymentMethod = new PaymentMethod { Type = PaymentMethodType.WALLET },
Customer = new Customer { PhoneNumber = "4747375750" },
Reference = Guid.NewGuid().ToString(),
UserFlow = CreatePaymentRequestUserFlow.WEB_REDIRECT,
ReturnUrl = $"http://localhost:3000",
PaymentDescription = "paymentDescription",
Profile = new ProfileRequest { Scope = "name phoneNumber address birthDate email" },
};

var result = await _epaymentService.CreatePayment(request);
return result?.RedirectUrl.ToString();
}
}
}
51 changes: 30 additions & 21 deletions src/Vipps.net.AspCoreDemo/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Reflection;
using System;
using dotenv.net;
olokken marked this conversation as resolved.
Show resolved Hide resolved
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Vipps.net.Infrastructure;

namespace Vipps.net.AspCore31Demo
Expand All @@ -21,7 +22,28 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var vippsConfigurationOptions = new VippsConfigurationOptions
{
ClientId = configuration.GetValue<string>("CLIENT-ID")!,
ClientSecret = configuration.GetValue<string>("CLIENT-SECRET")!,
MerchantSerialNumber = configuration.GetValue<string>("MERCHANT-SERIAL-NUMBER")!,
SubscriptionKey = configuration.GetValue<string>("SUBSCRIPTION-KEY")!,
UseTestMode = true,
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
PluginVersion =
Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "1.0.0"
};
olokken marked this conversation as resolved.
Show resolved Hide resolved

var vippsApi = VippsApi.Create(vippsConfigurationOptions);

services.AddSingleton(vippsApi.CheckoutService());
services.AddSingleton(vippsApi.EpaymentService());

services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Your API Name", Version = "v1" });
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -34,32 +56,19 @@ IConfiguration configuration
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API Name v1");
});
}

app.UseHttpsRedirection();
//app.UseHttpsRedirection();
olokken marked this conversation as resolved.
Show resolved Hide resolved

app.UseRouting();

app.UseAuthorization();

var vippsConfigurationOptions = new VippsConfigurationOptions
{
ClientId = configuration.GetValue<string>("CLIENT-ID")!,
ClientSecret = configuration.GetValue<string>("CLIENT-SECRET")!,
MerchantSerialNumber = configuration.GetValue<string>("MERCHANT-SERIAL-NUMBER")!,
SubscriptionKey = configuration.GetValue<string>("SUBSCRIPTION-KEY")!,
UseTestMode = true,
PluginName = Assembly.GetExecutingAssembly().GetName().Name,
PluginVersion =
Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "1.0.0"
};

// The following line configures vipps with custom settings
VippsConfiguration.ConfigureVipps(
vippsConfigurationOptions,
app.ApplicationServices.GetService<ILoggerFactory>()
);

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
Expand Down
4 changes: 3 additions & 1 deletion src/Vipps.net.AspCoreDemo/Vipps.net.AspCoreDemo.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>5fcb55b3-f577-4b73-a82a-f13b33796367</UserSecretsId>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2" />
<PackageReference Include="Azure.Identity" Version="1.8.2" />
<PackageReference Include="dotenv.net" Version="3.1.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading