Skip to content

Commit

Permalink
AdyenUrlManager:
Browse files Browse the repository at this point in the history
-Updated Adyen api version to v71, and js/css version to 5.59.0.
-Added new template tags: Adyen.JsIntegrityKey, Adyen.CssIntegrityKey. Customer don't need to look at adyen documentation and search these values, since we use the specific js/css adyen version anyway and should provide integrity values also.
AdyenCheckout: Added processing of errors output.
Helper:
- Changed OutputResult of Helper.EndRequest to be StreamOutputResult. ContentOutputResult provides the html markup, and for these methods we need only json, since they are used in js-ajax fetches etc.
- Fixed little bug related with query string.
WebRequestHelper:
- Properly added NoCache header, removed Accept-Charset, since this is processing in StringContent.
- Added async reading to ReadRequestInputStream to fix the error.
  • Loading branch information
StanislavSmetaninSSM committed Mar 21, 2024
1 parent 4f5f5c3 commit e62418b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
12 changes: 10 additions & 2 deletions src/AdyenCheckoutHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ private static class Tags
public static string AmountValue = "Adyen.Price";
public static string AdyenJavaScriptUrl = "Adyen.JavaScriptUrl";
public static string AdyenCssUrl = "Adyen.CssUrl";
public static string AdyenJsIntegrityKey = "Adyen.JsIntegrityKey";
public static string AdyenCssIntegrityKey = "Adyen.CssIntegrityKey";
}

#region Fields
Expand Down Expand Up @@ -165,6 +167,8 @@ public override OutputResult BeginCheckout(Order order, CheckoutParameters param
paymentMethodsTemplate.SetTag(Tags.AmountValue, order.Price.PricePIP);
paymentMethodsTemplate.SetTag(Tags.AdyenCssUrl, ApiUrlManager.GetCssUrl());
paymentMethodsTemplate.SetTag(Tags.AdyenJavaScriptUrl, ApiUrlManager.GetJavaScriptUrl());
paymentMethodsTemplate.SetTag(Tags.AdyenJsIntegrityKey, ApiUrlManager.JsIntegrityKey);
paymentMethodsTemplate.SetTag(Tags.AdyenCssIntegrityKey, ApiUrlManager.CssIntegrityKey);

return new ContentOutputResult
{
Expand Down Expand Up @@ -288,9 +292,13 @@ private OutputResult PaymentMethodSelected(Order order, PaymentMethodData paymen
var paymentRequest = new PaymentRequest(order, paymentMethodData, MerchantName, GetCallbackUrl(order, new NameValueCollection { ["action"] = "GatewayResponse" }), additionalAction);
try
{
var paymentMethodsResponse = WebRequestHelper.Request(ApiUrlManager.GetPaymentUrl(), paymentRequest.ToJson(), GetEnvironmentType(), ApiKey);
var paymentResponse = Converter.Deserialize<PaymentResponse>(paymentMethodsResponse);
string paymentMethodsResponse = WebRequestHelper.Request(ApiUrlManager.GetPaymentUrl(), paymentRequest.ToJson(), GetEnvironmentType(), ApiKey);

var error = Converter.Deserialize<ServiceError>(paymentMethodsResponse);
if (!string.IsNullOrEmpty(error.ErrorCode))
return OnError(order, $"Error code: {error.ErrorCode}. {error.Message}", null, Helper.IsAjaxRequest());

var paymentResponse = Converter.Deserialize<PaymentResponse>(paymentMethodsResponse);
return HandleAdyenPaymentResponse(order, paymentResponse, paymentMethodsResponse, doSaveCard, cardName, paymentMethodData.PaymentMethod.Type);
}
catch (Exception e) when (e is not ThreadAbortException)
Expand Down
8 changes: 6 additions & 2 deletions src/AdyenUrlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ namespace Dynamicweb.Ecommerce.CheckoutHandlers.Adyen
{
internal class AdyenUrlManager
{
private const string ApiVersion = "v70";
private const string JsCssVersion = "5.37.0";
private const string ApiVersion = "v71";
private const string JsCssVersion = "5.59.0";

private EnvironmentType _environment;
private string _liveEndpointUrlPrefix;

private bool IsTest => _environment == EnvironmentType.Test;

public string JsIntegrityKey => "sha384-O8p0CLZyOw1jkmYN7ZwJxWzd+sDYRFGpLEffqc+dKye24gFImbU72did4PC7ysTY";

public string CssIntegrityKey => "sha384-zgFNrGzbwuX5qJLys75cOUIGru/BoEzhGMyC07I3OSdHqXuhUfoDPVG03G+61oF4";

public AdyenUrlManager(EnvironmentType environment, string liveEndpointUrlPrefix)
{
_environment = environment;
Expand Down
16 changes: 8 additions & 8 deletions src/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
Expand All @@ -20,15 +21,13 @@ public static bool IsAjaxRequest()
return "application/json".Equals(Context.Current.Request.Headers["Content-Type"], StringComparison.OrdinalIgnoreCase);
}

public static ContentOutputResult EndRequest<T>(T obj) => EndRequest(Converter.SerializeCompact(obj));
public static StreamOutputResult EndRequest<T>(T obj) => EndRequest(Converter.SerializeCompact(obj));

public static ContentOutputResult EndRequest(string json)
public static StreamOutputResult EndRequest(string json) => new StreamOutputResult
{
if (!string.IsNullOrEmpty(json))
return new() { Content = json };

return ContentOutputResult.Empty;
}
ContentStream = new MemoryStream(Encoding.UTF8.GetBytes(json ?? string.Empty)),
ContentType = "application/json"
};

public static string GetCallbackUrl(string baseUrl, NameValueCollection parameters)
{
Expand All @@ -40,7 +39,8 @@ public static string GetCallbackUrl(string baseUrl, NameValueCollection paramete
{
query[key] = parameters[key];
}
uriBuilder.Query = query.ToString();

uriBuilder.Query = string.Join("&", query.AllKeys.Select(key => $"{key}={query[key]}"));
}

return uriBuilder.Uri.ToString();
Expand Down
9 changes: 6 additions & 3 deletions src/WebRequestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
Expand Down Expand Up @@ -29,9 +30,11 @@ public static string Request(string endpoint, string json, EnvironmentType envir
handler.ServerCertificateCustomValidationCallback = ServerCertificateValidationCallback;
using (var client = new HttpClient(handler))
{
client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
{
NoCache = true
};
var content = new StringContent(json, Encoding.UTF8, "application/json");
content.Headers.Add("Accept-Charset", "UTF-8");
content.Headers.Add("Cache-Control", "no-cache");
content.Headers.Add("x-api-key", apiKey);
using (HttpResponseMessage response = client.PostAsync(endpoint, content).GetAwaiter().GetResult())
{
Expand All @@ -52,7 +55,7 @@ public static string ReadRequestInputStream()
{
using (var readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
return readStream.ReadToEnd();
return readStream.ReadToEndAsync().GetAwaiter().GetResult();
}
}
}
Expand Down

0 comments on commit e62418b

Please sign in to comment.