Epinova's take on a NETS payment gateway API.
No configuration via config files are needed. The MerchantInfo passed in to each method described in IPaymentGatewayService dictates merchant info and environment to be used (test or production).
public class NetsProcessor : IPaymentGatewayProcessor
{
private readonly IPaymentGatewayService _paymentGateway;
public NetsProcessor(IPaymentGatewayService paymentGateway)
{
_paymentGateway = paymentGateway;
}
public async Task<PaymentSetupModel> InitializePaymentAsync(string orderNumber, decimal totalAmount)
{
var request = new RegisterRequest
{
Amount = totalAmount,
CurrencyCode = "NOK",
OrderDescription = "Order description. Line by line.",
OrderNumber = orderNumber,
RedirectUrl = new Uri($"https://absolute.url/to/your/site/NetsCallBack?orderNumber={orderNumber}")
};
//Send in specific payment method, or leave blank to use default payment methods defined in MerchantInfo model.
//request.PaymentMethods.Add("Vipps");
RegisterResponse response = await _paymentGateway.RegisterAsync(GetMerchantInfo(), request);
return new PaymentSetupModel(registerResponse.TransactionId, registerResponse.TerminalUrl);
}
private static MerchantInfo GetMerchantInfo()
{
return new MerchantInfo
{
IsTestEnvironment = Boolean.Parse(ConfigurationManager.AppSettings["Nets.IsTestMode"] ?? "false"),
MerchantId = ConfigurationManager.AppSettings["Nets.MerchantId"],
Token = ConfigurationManager.AppSettings["Nets.Token"],
DefaultPaymentMethods = (ConfigurationManager.AppSettings[$"Nets.DefaultPaymentMethods"] ?? String.Empty)
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.ToArray(),
ValidCallbackIPs = (ConfigurationManager.AppSettings["Nets.ValidCallbackIPs"]?.Split(',') ?? Enumerable.Empty<string>())
.Select(ip => IPAddress.TryParse(ip.Trim(), out IPAddress parsedIp) ? parsedIp : IPAddress.None)
.Where(ip => !ip.Equals(IPAddress.None)).ToArray()
};
}
}
web.config:
<configuration>
<appSettings>
<add key="Nets.IsTestMode" value="true" />
<add key="Nets.MerchantId" value="1337" />
<add key="Nets.Token" value="xyzabc" />
<add key="Nets.DefaultPaymentMethods" value="Visa, MasterCard" />
<add key="Nets.ValidCallbackIPs" value="127.0.0.1" />
<appSettings>
</configuration>
if using Structuremap:
container.Configure(
x =>
{
x.Scan(y =>
{
y.TheCallingAssembly();
y.WithDefaultConventions();
});
x.AddRegistry<Epinova.NetsPaymentGateway.PaymentGatewayRegistry>();
});
If you cannot use the structuremap registry provided with this module, you can manually set up PaymentGatewayService for IPaymentGatewayService
Epinova.NetsPaymentGateway.IPaymentGatewayService describes the NETS payment gateway service.
- Epinova.Infrastructure >= v11.1.0.95.
- EPiServer.Framework >= v11.1 for logging purposes.
- Automapper >= v9.0 for mapping models.
- StructureMap >= v4.7 for registering service contract.
The module is published on nuget.org.
nuget install Epinova.NetsPaymentGateway
- .NET Standard 2.0
- Tests target .NET Core 2.1
- Tarjei Olsen - Initial work - apeneve
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE file for details