From 6b80ab102ac3867a6fa50f776968a014a4d18091 Mon Sep 17 00:00:00 2001 From: Stanislav Smetanin Date: Mon, 7 Oct 2024 18:46:09 +1000 Subject: [PATCH 1/3] Fixed problem with "Prefill customer address" setting and related data. --- ...e.CheckoutHandlers.DibsEasyCheckout.csproj | 2 +- .../CreatePaymentRequest/Consumer/Company.cs | 2 +- .../CreatePaymentRequest/Consumer/Consumer.cs | 2 +- .../CreatePaymentRequest/Consumer/Contact.cs | 4 +- src/Models/ErrorResponse.cs | 11 ++++ src/Service/DibsRequest.cs | 19 +++++- src/Service/DibsService.cs | 59 ++++++++++--------- 7 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 src/Models/ErrorResponse.cs diff --git a/src/Dynamicweb.Ecommerce.CheckoutHandlers.DibsEasyCheckout.csproj b/src/Dynamicweb.Ecommerce.CheckoutHandlers.DibsEasyCheckout.csproj index 0699b8d..16ab632 100644 --- a/src/Dynamicweb.Ecommerce.CheckoutHandlers.DibsEasyCheckout.csproj +++ b/src/Dynamicweb.Ecommerce.CheckoutHandlers.DibsEasyCheckout.csproj @@ -1,6 +1,6 @@  - 10.6.1 + 10.6.2 1.0.0.0 Nets Easy Checkout Nets Easy Checkout diff --git a/src/Models/CreatePaymentRequest/Consumer/Company.cs b/src/Models/CreatePaymentRequest/Consumer/Company.cs index 3f76460..3e55a48 100644 --- a/src/Models/CreatePaymentRequest/Consumer/Company.cs +++ b/src/Models/CreatePaymentRequest/Consumer/Company.cs @@ -8,6 +8,6 @@ internal sealed class Company [DataMember(Name = "name")] public string Name { get; set; } - [DataMember(Name = "contact")] + [DataMember(Name = "contact", EmitDefaultValue = false)] public Contact Contact { get; set; } } diff --git a/src/Models/CreatePaymentRequest/Consumer/Consumer.cs b/src/Models/CreatePaymentRequest/Consumer/Consumer.cs index 7bac235..c63905c 100644 --- a/src/Models/CreatePaymentRequest/Consumer/Consumer.cs +++ b/src/Models/CreatePaymentRequest/Consumer/Consumer.cs @@ -11,7 +11,7 @@ internal sealed class Consumer [DataMember(Name = "shippingAddress")] public ShippingAddress ShippingAddress { get; set; } - [DataMember(Name = "privatePerson")] + [DataMember(Name = "privatePerson", EmitDefaultValue = false)] public Contact PrivatePerson { get; set; } [DataMember(Name = "company")] diff --git a/src/Models/CreatePaymentRequest/Consumer/Contact.cs b/src/Models/CreatePaymentRequest/Consumer/Contact.cs index c74f365..61b4e80 100644 --- a/src/Models/CreatePaymentRequest/Consumer/Contact.cs +++ b/src/Models/CreatePaymentRequest/Consumer/Contact.cs @@ -5,9 +5,9 @@ namespace Dynamicweb.Ecommerce.CheckoutHandlers.DibsEasyCheckout.Models.CreatePa [DataContract] internal sealed class Contact { - [DataMember(Name = "firstName")] + [DataMember(Name = "firstName", EmitDefaultValue = false)] public string FirstName { get; set; } - [DataMember(Name = "lastName")] + [DataMember(Name = "lastName", EmitDefaultValue = false)] public string LastName { get; set; } } diff --git a/src/Models/ErrorResponse.cs b/src/Models/ErrorResponse.cs new file mode 100644 index 0000000..aa31743 --- /dev/null +++ b/src/Models/ErrorResponse.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using System.Runtime.Serialization; + +namespace Dynamicweb.Ecommerce.CheckoutHandlers.DibsEasyCheckout.Models; + +[DataContract] +internal sealed class ErrorResponse +{ + [DataMember(Name = "errors")] + public Dictionary> Errors { get; set; } +} diff --git a/src/Service/DibsRequest.cs b/src/Service/DibsRequest.cs index 3d4c15f..8e4f677 100644 --- a/src/Service/DibsRequest.cs +++ b/src/Service/DibsRequest.cs @@ -1,5 +1,8 @@ using Dynamicweb.Core; +using Dynamicweb.Ecommerce.CheckoutHandlers.DibsEasyCheckout.Models; using System; +using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; @@ -47,9 +50,19 @@ ApiCommand.CreatePayment or if (!response.IsSuccessStatusCode) { - string errorMessage = $"Unhandled exception. Operation failed: {response.ReasonPhrase}"; - //probably add some error handling later - throw new Exception(errorMessage); + var errorResponse = Converter.Deserialize(data); + if (errorResponse?.Errors?.Any() is true) + { + var errorMessage = new StringBuilder(); + foreach ((string propertyName, IEnumerable errors) in errorResponse.Errors) + { + string errorsText = string.Join(", ", errors); + errorMessage.AppendLine($"{propertyName}: {errorsText}"); + } + throw new Exception(errorMessage.ToString()); + } + + throw new Exception($"Unhandled exception. Operation failed: {response.ReasonPhrase}"); } return data; diff --git a/src/Service/DibsService.cs b/src/Service/DibsService.cs index 1357d11..eafdebb 100644 --- a/src/Service/DibsService.cs +++ b/src/Service/DibsService.cs @@ -196,50 +196,53 @@ private static PaymentRequest ConvertOrder(Order order, CreatePaymentParameters private static Consumer GetConsumerData(Order order) { - string firstName = Converter.ToString(order.CustomerFirstName); - string lastName = Converter.ToString(order.CustomerSurname); - var customerName = Converter.ToString(order.CustomerName).Trim(); - var isDeliveryAddressFilled = !string.IsNullOrEmpty(order.DeliveryCountryCode); - var countryCode3 = Services.Countries.GetCountry(isDeliveryAddressFilled ? order.DeliveryCountryCode : order.CustomerCountryCode)?.Code3; - - var delimeterPosition = customerName.IndexOf(' '); - if (string.IsNullOrWhiteSpace(firstName)) - { - firstName = delimeterPosition > -1 ? customerName.Substring(0, delimeterPosition) : customerName; - } - if (string.IsNullOrWhiteSpace(lastName)) - { - lastName = delimeterPosition > -1 ? customerName.Substring(delimeterPosition + 1) : customerName; - } + bool isDeliveryAddressFilled = !string.IsNullOrEmpty(order.DeliveryCountryCode); + bool isCompanyDataFilled = !string.IsNullOrWhiteSpace(order.CustomerCompany); + string countryCode = Services.Countries.GetCountry(isDeliveryAddressFilled ? order.DeliveryCountryCode : order.CustomerCountryCode)?.Code3; return new() { - Email = Converter.ToString(order.CustomerEmail), + Email = order.CustomerEmail, ShippingAddress = new() { AddressLine1 = isDeliveryAddressFilled ? order.DeliveryAddress : order.CustomerAddress, AddressLine2 = isDeliveryAddressFilled ? order.DeliveryAddress2 : order.CustomerAddress2, PostalCode = isDeliveryAddressFilled ? order.DeliveryZip : order.CustomerZip, City = isDeliveryAddressFilled ? order.DeliveryCity : order.CustomerCity, - Country = countryCode3 + Country = countryCode }, - PrivatePerson = string.IsNullOrEmpty(order.CustomerCompany) ? new() - { - FirstName = firstName, - LastName = lastName - } : null, - Company = !string.IsNullOrEmpty(order.CustomerCompany) ? new() + PrivatePerson = !isCompanyDataFilled ? GetContact(order) : null, + Company = isCompanyDataFilled ? new() { Name = order.CustomerCompany, - Contact = new() - { - FirstName = firstName, - LastName = lastName - } + Contact = GetContact(order) } : null }; } + private static Contact GetContact(Order order) + { + string firstName = string.IsNullOrWhiteSpace(order.CustomerFirstName) ? order.CustomerName : order.CustomerFirstName; + string lastName = string.IsNullOrWhiteSpace(order.CustomerSurname) ? order.CustomerMiddleName : order.CustomerSurname; + + if (!string.IsNullOrWhiteSpace(firstName) || !string.IsNullOrWhiteSpace(lastName)) + { + //both first name and last name are required if Contact is serialized + if (string.IsNullOrWhiteSpace(firstName)) + firstName = "-"; + if (string.IsNullOrWhiteSpace(lastName)) + lastName = "-"; + + return new() + { + FirstName = firstName, + LastName = lastName + }; + } + + return null; + } + private static LineItem ConvertOrderLine(OrderLine orderline, ref long linesTotalPIP) { var unitName = "pcs"; From a5f602a0f376f787000dc3034caf4847d0b5049b Mon Sep 17 00:00:00 2001 From: Stanislav Smetanin Date: Mon, 7 Oct 2024 18:57:48 +1000 Subject: [PATCH 2/3] Small cosmetic change --- src/Service/DibsService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service/DibsService.cs b/src/Service/DibsService.cs index eafdebb..0d6b252 100644 --- a/src/Service/DibsService.cs +++ b/src/Service/DibsService.cs @@ -230,7 +230,7 @@ private static Contact GetContact(Order order) //both first name and last name are required if Contact is serialized if (string.IsNullOrWhiteSpace(firstName)) firstName = "-"; - if (string.IsNullOrWhiteSpace(lastName)) + else lastName = "-"; return new() From 48dcb3577afae1c7bf06906ce3d2cfee6e8a8b0e Mon Sep 17 00:00:00 2001 From: Stanislav Smetanin Date: Mon, 7 Oct 2024 19:08:01 +1000 Subject: [PATCH 3/3] Revert stupid cosmetic improve - not an improve at all! --- src/Service/DibsService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service/DibsService.cs b/src/Service/DibsService.cs index 0d6b252..eafdebb 100644 --- a/src/Service/DibsService.cs +++ b/src/Service/DibsService.cs @@ -230,7 +230,7 @@ private static Contact GetContact(Order order) //both first name and last name are required if Contact is serialized if (string.IsNullOrWhiteSpace(firstName)) firstName = "-"; - else + if (string.IsNullOrWhiteSpace(lastName)) lastName = "-"; return new()