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

Feature/298 add new sample shop #313

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ bin
packages
*.user
.idea
**/appsettings.Development.json
/src/Svea.WebPay.SDK.Tests/appsettings.Development.json
src/.DS_Store
.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Sample.Testshop.Server.Configurations
{
public class Credentials
{
public string MarketId { get; set; }
public string MerchantId { get; set; }
public string Secret { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Sample.Testshop.Server.Configurations
{
public class MarketSettings
{
public string Id { get; set; }
public IEnumerable<string> Languages { get; set; }
public IEnumerable<string> Currencies { get; set; }
public IEnumerable<string> Countries { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Sample.Testshop.Server.Configurations
{
public class MerchantSettings
{
public Uri PushUri { get; set; }
public Uri TermsUri { get; set; }
public Uri CheckoutUri { get; set; }
public Uri ConfirmationUri { get; set; }
public Uri CheckoutValidationCallbackUri { get; set; }
public Uri WebhookUri { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Sample.Testshop.Server.Configurations
{
using System;

public class SveaApiUrls
{
public Uri CheckoutApiUri { get; set; }
public Uri PaymentAdminApiUri { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Sample.Testshop.Server.Configurations;
using System.Globalization;
using System.Runtime.ConstrainedExecution;
namespace Sample.Testshop.Server.Extensions
{
public static class StartupExtensions
{
public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder)
{

var sveaApiUrlsSettings = builder.Configuration.GetSection("SveaApiUrls");
builder.Services.Configure<SveaApiUrls>(sveaApiUrlsSettings);
var sveaApiUrls = sveaApiUrlsSettings.Get<SveaApiUrls>();
var checkoutUri = new Uri(builder.Configuration.GetValue<string>("SveaApiUrls:CheckoutApiUri") ?? "");
var paymentAdminUri = new Uri(builder.Configuration.GetValue<string>("SveaApiUrls:PaymentAdminApiUri") ?? "");

// We can later add retry Policy like in the old sample testshop
builder.Services.AddHttpClient("checkoutApi", client => client.BaseAddress = checkoutUri)
.ConfigurePrimaryHttpMessageHandler(() => RedirectHandler);

builder.Services.AddHttpClient("paymentAdminApi", client => client.BaseAddress = paymentAdminUri)
.ConfigurePrimaryHttpMessageHandler(() => RedirectHandler);
var credentialsSettings = builder.Configuration.GetSection("Credentials");
builder.Services.Configure<List<Credentials>>(credentialsSettings);
var credentials = credentialsSettings.Get<List<Credentials>>();
var credential = credentials?.FirstOrDefault();

var merchantSettingsSettings = builder.Configuration.GetSection("MerchantSettings");
builder.Services.Configure<MerchantSettings>(sveaApiUrlsSettings);
var merchantSettings = merchantSettingsSettings.Get<MerchantSettings>();

builder.Services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();


builder.Services.Configure<List<MarketSettings>>(builder.Configuration.GetSection("Markets"));

builder.Services.AddSveaClient(checkoutUri, paymentAdminUri, credential?.MerchantId, credential?.Secret);


return builder;

}
public static IServiceCollection AddSveaClient(this IServiceCollection services, Uri checkoutUri, Uri paymentAdminUri, string merchantId, string secret)
{
services.AddHttpClient("checkoutApi", client => client.BaseAddress = checkoutUri)
.ConfigurePrimaryHttpMessageHandler(() => RedirectHandler);

services.AddHttpClient("paymentAdminApi", client => client.BaseAddress = paymentAdminUri)
.ConfigurePrimaryHttpMessageHandler(() => RedirectHandler);

services.AddTransient(s =>
{

var httpContextAccessor = s.GetService<IHttpContextAccessor>();
var currentMarket = httpContextAccessor.HttpContext.Request.Headers["merchantId"].FirstOrDefault() ?? "SE";
var credentials = s.GetService<IOptions<List<Credentials>>>()?.Value;
var credential = credentials?.FirstOrDefault(x => x.MerchantId.Equals(currentMarket, StringComparison.InvariantCultureIgnoreCase));
var httpClientFactory = s.GetService<IHttpClientFactory>();
var checkoutApiHttpClient = httpClientFactory.CreateClient("checkoutApi");
var paymentAdminApiHttpClient = httpClientFactory.CreateClient("paymentAdminApi");
return new Svea.WebPay.SDK.SveaWebPayClient(checkoutApiHttpClient, paymentAdminApiHttpClient, new Svea.WebPay.SDK.Credentials(credential?.MerchantId ?? merchantId, credential?.Secret ?? secret), s.GetService<ILogger>());
});
//services.AddTransient(s =>
//{

// var httpContextAccessor = s.GetService<IHttpContextAccessor>();
// var currentMarket = httpContextAccessor.HttpContext.Request.Headers["merchantId"].FirstOrDefault() ?? "SE";
// var credentials = s.GetService<IOptions<List<Credentials>>>()?.Value;
// var credential = credentials?.FirstOrDefault(x => x.MerchantId.Equals(currentMarket, StringComparison.InvariantCultureIgnoreCase));
// var httpClientFactory = s.GetService<IHttpClientFactory>();
// return new Svea.WebPay.SDK.SveaHttpClient(httpClientFactory.CreateClient("checkoutApi"), new Svea.WebPay.SDK.Credentials(credential?.MerchantId ?? merchantId, credential?.Secret ?? secret), s.GetService<ILogger>())č
//});

return services;
}

private static HttpClientHandler RedirectHandler => new HttpClientHandler { AllowAutoRedirect = false };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Svea.WebPay.SDK;
using Svea.WebPay.SDK.CheckoutApi.Recurring;

namespace Sample.Testshop.Server.Modules.Checkout
{
public static class ChangePaymentMethodHandler
{
public static async Task<ChangePaymentMethodResponse> Handle(string token, ChangepaymentMethodModel request, SveaWebPayClient sveaWebPayClient)
{
var response = await sveaWebPayClient.Checkout.Recurring.ChangePaymentMethodAsync(request, token);
return response;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
using Svea.WebPay.SDK;
using Svea.WebPay.SDK.CheckoutApi;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Sample.Testshop.Server.Modules.Checkout
{
public static class CreateOrderHandler
{

public static async Task<Data> Handle(CreateOrderRequestModel createOrderModel, SveaWebPayClient sveaClient)
{
if (string.IsNullOrWhiteSpace(createOrderModel?.ClientOrderNumber))
{
createOrderModel.ClientOrderNumber = Guid.NewGuid().ToString().Replace("-", "");
createOrderModel.Currency = "SEK";
}


var response = await sveaClient.Checkout.CreateOrder(new CreateOrderModel(new RegionInfo(createOrderModel.CountryCode), new CurrencyCode(createOrderModel.Currency), new Language(createOrderModel.Locale),
createOrderModel.ClientOrderNumber, createOrderModel.MerchantSettings?.ToSDKModel(), createOrderModel.Cart.ToSDKModel(),
createOrderModel.RequireElectronicIdAuthentication,
createOrderModel.PresetValues?.Select(x => new Presetvalue(x.TypeName, x.Value, x.IsReadonly)).ToList(), null, null, createOrderModel.MerchantData, null));

return response;

}
public class CreateOrderRequestModel
{
public string CountryCode { get; set; } = string.Empty;
public string Currency { get; set; } = string.Empty;
public string Locale { get; set; } = string.Empty;
public string ClientOrderNumber { get; set; } = string.Empty;
public MerchantSettings MerchantSettings { get; set; } = new MerchantSettings()
{

CheckoutUri = "https://loalhost:3000/checkoutPage",
ConfirmationUri = "https://localhost:3000/confirmationPage/clientOrderNumber",
TermsUri = "https://localhost:3000/legalterms",
PushUri = "https://localhost:3000/pushUri"

};
public Cart Cart { get; set; } = new Cart();
public ShippingInformation? ShippingInformation { get; set; }
public List<PresetValue>? PresetValues { get; set; }
public Validation Validation { get; set; } = new Validation();
public IdentityFlags? IdentityFlags { get; set; }
public bool RequireElectronicIdAuthentication { get; set; }
public string? PartnerKey { get; set; }
public string? MerchantData { get; set; }
public bool Recurring { get; set; }
}

public class MerchantSettings
{
public string? CheckoutValidationCallBackUri { get; set; } = string.Empty;
public string PushUri { get; set; } = string.Empty;
public string TermsUri { get; set; } = string.Empty;
public string CheckoutUri { get; set; } = string.Empty;
public string ConfirmationUri { get; set; } = string.Empty;
public List<long>? ActivePartPaymentCampaigns { get; set; }
public long? PromotedPartPaymentCampaign { get; set; }

public Svea.WebPay.SDK.CheckoutApi.MerchantSettings ToSDKModel()
{
return new Svea.WebPay.SDK.CheckoutApi.MerchantSettings(new Uri(PushUri),
new Uri(TermsUri),
new Uri(CheckoutUri),
new Uri(ConfirmationUri),
!string.IsNullOrEmpty(CheckoutValidationCallBackUri) ? new Uri(CheckoutValidationCallBackUri) : null,
null,
ActivePartPaymentCampaigns ?? new List<long>(),
PromotedPartPaymentCampaign);
}
}

public class Cart
{
public List<OrderRow> Items { get; set; } = new List<OrderRow>();
public Svea.WebPay.SDK.CheckoutApi.Cart ToSDKModel()
{
return new Svea.WebPay.SDK.CheckoutApi.Cart(
Items.Select(x => new Svea.WebPay.SDK.CheckoutApi.OrderRow(
x.ArticleNumber,
x.Name,
x.Quantity,
x.UnitPrice,
x.DiscountPercent,
x.DiscountAmount,
x.VatPercent,
x.Unit,
x.TemporaryReference,
x.RowNumber,
x.MerchantData,
x.RowType)).ToList());
}
}

public class OrderRow
{
public string? ArticleNumber { get; set; }
public string Name { get; set; } = string.Empty;
public long Quantity { get; set; }
public long UnitPrice { get; set; }
public long DiscountPercent { get; set; }
public long DiscountAmount { get; set; }
public long VatPercent { get; set; }
public string Unit { get; set; } = string.Empty;
public string? TemporaryReference { get; set; }
public int RowNumber { get; set; }
public string? MerchantData { get; set; }
public string? RowType { get; set; }
}

public class ShippingInformation
{
public bool EnableShipping { get; set; }
public bool EnforceFallback { get; set; }
public double Weight { get; set; }
public Dictionary<string, string>? Tags { get; set; }
public List<FallbackOption>? FallbackOptions { get; set; }
public bool ShouldRejectShippingSession { get; set; }
}

public class FallbackOption
{
public string Id { get; set; } = string.Empty;
public string Carrier { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public long ShippingFee { get; set; }
public List<dynamic>? Addons { get; set; }
public List<dynamic>? Fields { get; set; }
}

public class PresetValue
{
public string TypeName { get; set; } = string.Empty;
public string Value { get; set; } = string.Empty;
public bool IsReadonly { get; set; }
}

public class Validation
{
int? MinAge { get; set; }
}

public class IdentityFlags
{
public bool HideNotYou { get; set; }
public bool HideChangeAddress { get; set; }
public bool HideAnonymous { get; set; }
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Sample.Testshop.Server.Modules.Checkout.Models;
using Svea.WebPay.SDK;
using Svea.WebPay.SDK.CheckoutApi;
using Svea.WebPay.SDK.CheckoutApi.Recurring;
using System.Globalization;
using Cart = Sample.Testshop.Server.Modules.Checkout.Models.Cart;

namespace Sample.Testshop.Server.Modules.Checkout
{
public static class CreateRecurringOrderHandler
{
public static async Task<RecurringOrder> Handle(CreateRecurringOrderRequest createRecurringOrderModel, string token, SveaWebPayClient sveaClient)
{
if (string.IsNullOrWhiteSpace(createRecurringOrderModel?.ClientOrderNumber))
{
createRecurringOrderModel.ClientOrderNumber = Guid.NewGuid().ToString().Replace("-", "");
createRecurringOrderModel.Currency = "SEK";
}


var response = await sveaClient.Checkout.Recurring.CreateRecurringOrderAsync(new CreateRecurringOrderModel(createRecurringOrderModel.ClientOrderNumber, createRecurringOrderModel.Currency,
createRecurringOrderModel.MerchantSettings.ToSDKModel(), createRecurringOrderModel.Cart.ToSDKModel()), token);

return response;

}
public class CreateRecurringOrderRequest
{
public string ClientOrderNumber { get; set; }
public string Currency { get; set; }
public Cart Cart { get; set; }
public RecurringMerchantSettingsRequest MerchantSettings { get; set; }
}
public class RecurringMerchantSettingsRequest
{
public string CheckoutValidationCallBackUri { get; set; }
public string PushUri { get; set; }
public RecurringMerchantSettings ToSDKModel()
{
return new RecurringMerchantSettings()
{
PushUri = new Uri(PushUri),
CheckoutValidationCallBackUri = !string.IsNullOrEmpty(CheckoutValidationCallBackUri) ? new Uri(CheckoutValidationCallBackUri) : null,
};
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Mvc;
using Svea.WebPay.SDK;

namespace Sample.Testshop.Server.Modules.Checkout
{
public static class GetAvailableCampaignsHandler
{
//public static Task<AvailableCampaigns> Handle(SveaWebPayClient sveaClient, [FromQuery] bool isCompany, [FromQuery] int amount)
//{
// //sveaClient.Checkout.
//}
public class AvailableCampaigns
{
public int CampaignCode { get; set; }
public string Description { get; set; }
public int PaymentPlanType { get; set; }
public int ContractLengthInMonths { get; set; }
public double MonthlyAnnuityFactor { get; set; }
public double InitialFee { get; set; }
public double NotificationFee { get; set; }
public double InterestRatePercent { get; set; }
public int NumberOfInterestFreeMonths { get; set; }
public int NumberOfPaymentFreeMonths { get; set; }
public decimal FromAmount { get; set; }
public decimal ToAmount { get; set; }
public decimal MonthlyAmount { get; set; }
}

}
}
Loading
Loading