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

Fixed problem with "Prefill customer address" setting and related data. #17

Merged
merged 3 commits into from
Oct 9, 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
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>10.6.1</VersionPrefix>
<VersionPrefix>10.6.2</VersionPrefix>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Title>Nets Easy Checkout</Title>
<Description>Nets Easy Checkout</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/Models/CreatePaymentRequest/Consumer/Company.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
2 changes: 1 addition & 1 deletion src/Models/CreatePaymentRequest/Consumer/Consumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
4 changes: 2 additions & 2 deletions src/Models/CreatePaymentRequest/Consumer/Contact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
11 changes: 11 additions & 0 deletions src/Models/ErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -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<string, IEnumerable<string>> Errors { get; set; }
}
19 changes: 16 additions & 3 deletions src/Service/DibsRequest.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<ErrorResponse>(data);
if (errorResponse?.Errors?.Any() is true)
{
var errorMessage = new StringBuilder();
foreach ((string propertyName, IEnumerable<string> 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;
Expand Down
59 changes: 31 additions & 28 deletions src/Service/DibsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading