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

Dynamic payer reference for sample site #631

Merged
merged 5 commits into from
Mar 20, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

new SwedbankBlock().payWithSwedbank(PaymentMethods.card);

cy.get('h2', {timeout: 30000}).then(($h) => {
cy.get('h2', {timeout: 60000}).then(($h) => {

Check notice on line 17 in src/Samples/Sample.AspNetCore.UiTests/cypress/e2e/paymentorder/creditcard/creditcard-e2e.spec.cy.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Samples/Sample.AspNetCore.UiTests/cypress/e2e/paymentorder/creditcard/creditcard-e2e.spec.cy.js#L17

'cy' is not defined.
expect($h).to.contain('Thanks!');

cy.getByAutomation('paymentorderlink').then(($paymentOrderLink) => {
Expand All @@ -39,7 +39,7 @@

new SwedbankBlock().payWithSwedbank(PaymentMethods.card);

cy.get('h2', {timeout: 30000}).then(($h) => {
cy.get('h2', {timeout: 60000}).then(($h) => {
expect($h).to.contain('Thanks!');

cy.getByAutomation('paymentorderlink').then(($paymentOrderLink) => {
Expand Down Expand Up @@ -111,7 +111,7 @@
let swedbankBlock = new SwedbankBlock();
swedbankBlock.payWithSwedbank(PaymentMethods.card);

cy.get('h2', {timeout: 30000}).then(($h) => {
cy.get('h2', {timeout: 60000}).then(($h) => {
expect($h).to.contain('Thanks!');

cy.getByAutomation('paymentorderlink').then(($paymentOrderLink) => {
Expand All @@ -136,7 +136,7 @@

new SwedbankBlock().payWithSwedbank(PaymentMethods.card);

cy.get('h2', {timeout: 30000}).then(($h) => {
cy.get('h2', {timeout: 60000}).then(($h) => {
expect($h).to.contain('Thanks!');

cy.getByAutomation('paymentorderlink').then(($paymentOrderLink) => {
Expand All @@ -161,7 +161,7 @@

new SwedbankBlock().payWithSwedbank(PaymentMethods.card);

cy.get('h2', {timeout: 30000}).then(($h) => {
cy.get('h2', {timeout: 60000}).then(($h) => {
expect($h).to.contain('Thanks!');

cy.getByAutomation('paymentorderlink').then(($paymentOrderLink) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class CheckOutController : Controller
private readonly StoreDbContext _context;
private readonly PayeeInfoConfig _payeeInfoOptions;
private readonly ISwedbankPayClient _swedbankPayClient;
private readonly PayerReference _payerReference;
private readonly UrlsOptions _urls;


Expand All @@ -37,14 +38,16 @@ public CheckOutController(IOptionsSnapshot<PayeeInfoConfig> payeeInfoOptionsAcce
Cart cart,
ILogger<CheckOutController> logger,
StoreDbContext storeDbContext,
ISwedbankPayClient payClient)
ISwedbankPayClient payClient,
PayerReference payerReference)
{
_payeeInfoOptions = payeeInfoOptionsAccessor.Value;
_urls = urlsAccessor.Value;
_cartService = cart;
_logger = logger;
_context = storeDbContext;
_swedbankPayClient = payClient;
_payerReference = payerReference;
}

public void Callback([FromBody] CallbackInfo callbackInfo)
Expand Down Expand Up @@ -172,13 +175,12 @@ public async Task<IPaymentOrderResponse> CreatePaymentOrder(bool? generatePaymen
paymentOrderRequest.Metadata = null;
paymentOrderRequest.GenerateRecurrenceToken = generateRecurrenceToken;
paymentOrderRequest.GenerateUnscheduledToken = generateUnscheduledToken;
;

paymentOrderRequest.Payer = new Payer
{
FirstName = "Olivia",
LastName = "Nyhuus",
PayerReference = "AB1234",
PayerReference = _payerReference.Id,
Email = new EmailAddress("olivia.nyhuus@payex.com"),
Msisdn = new Msisdn("+46739000001"),
WorkPhoneNumber = new Msisdn("+4787654321"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class PaymentController : Controller
private readonly StoreDbContext _context;
private readonly PayeeInfoConfig _payeeInfoOptions;
private readonly ISwedbankPayClient _swedbankPayClient;
private readonly PayerReference _payerReference;
private readonly UrlsOptions _urls;


Expand All @@ -37,12 +38,14 @@ public PaymentController(
Cart cart,
StoreDbContext dbContext,
ISwedbankPayClient payClient,
IOptionsSnapshot<UrlsOptions> urlsAccessor)
IOptionsSnapshot<UrlsOptions> urlsAccessor,
PayerReference payerReference)
{
_payeeInfoOptions = payeeInfoOptionsAccessor.Value;
_cartService = cart;
_context = dbContext;
_swedbankPayClient = payClient;
_payerReference = payerReference;
_urls = urlsAccessor.Value;
}

Expand Down Expand Up @@ -293,7 +296,7 @@ private async Task<PaymentOrderRequest> GetRecurringRequest(string description,
OrderItems = orderItems.ToList(),
Payer = new Payer
{
PayerReference = "AB1234",
PayerReference = _payerReference.Id,
}
};

Expand Down Expand Up @@ -322,7 +325,7 @@ private async Task<PaymentOrderRequest> GetUnscheduledRequest(string description
OrderItems = orderItems.ToList(),
Payer = new Payer
{
PayerReference = "AB1234",
PayerReference = _payerReference.Id,
}
};

Expand Down
7 changes: 5 additions & 2 deletions src/Samples/Sample.AspNetCore/Controllers/TokensController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ public class TokensController : Controller
private readonly ISwedbankPayClient _swedbankPayClient;
private readonly ILogger<TokensController> _logger;
private readonly Cart _cart;
private readonly PayerReference _payerReference;

public TokensController(ISwedbankPayClient swedbankPayClient,
ILogger<TokensController> logger,
Cart cart)
Cart cart,
PayerReference payerReference)
{
_swedbankPayClient = swedbankPayClient;
_logger = logger;
_cart = cart;
_payerReference = payerReference;
}


Expand Down Expand Up @@ -106,7 +109,7 @@ private async Task<TokenViewModel> GetViewModel()

try
{
var tokenResponse = await _swedbankPayClient.PaymentOrders.GetOwnedTokens("AB1234");
var tokenResponse = await _swedbankPayClient.PaymentOrders.GetOwnedTokens(_payerReference.Id);
viewModel.Id = tokenResponse?.Id;
viewModel.PayerReference = tokenResponse?.PayerReference;
viewModel.Tokens = tokenResponse?.Tokens;
Expand Down
14 changes: 14 additions & 0 deletions src/Samples/Sample.AspNetCore/PayerReference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Sample.AspNetCore;

public class PayerReference
{
public PayerReference()
{
Id = Guid.NewGuid().ToString().Replace("-", "");
}

public string Id { get; }

}
3 changes: 3 additions & 0 deletions src/Samples/Sample.AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public void ConfigureServices(IServiceCollection services)

services.Configure<UrlsOptions>(Configuration.GetSection("Urls"));
services.AddScoped(provider => SessionCart.GetCart(provider));

services.AddSingleton<PayerReference>();

services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();
services.AddSwedbankPayClient(swedBankPayOptions.ApiBaseUrl, swedBankPayOptions.Token);
services.AddSession();
Expand Down