This commit is contained in:
@@ -123,7 +123,44 @@ namespace Sockeye.Biz
|
||||
try
|
||||
{
|
||||
var jData = JObject.Parse(p.VendorData);
|
||||
|
||||
//fundamentally validate the object is a purchase notification
|
||||
if (jData["order_notification"]["purchase"]["purchase_id"] == null)
|
||||
{
|
||||
//this is not the expected format data, stop processing and alert:
|
||||
throw new System.FormatException($"Vendor data unexpected format:{p.VendorData}");
|
||||
}
|
||||
|
||||
//CUSTOMER create or locate
|
||||
var jCustomerName = jData["order_notification"]["purchase"]["customer_data"]["reg_name"].Value<string>() ?? throw new System.FormatException($"Vendor data empty reg_name:{p.VendorData}");
|
||||
var jCustomerEmail = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["email"].Value<string>() ?? throw new System.FormatException($"Vendor data empty email:{p.VendorData}");
|
||||
|
||||
|
||||
var customerBiz = CustomerBiz.GetBiz(ct);
|
||||
|
||||
Customer c = await ct.Customer.FirstOrDefaultAsync(z => z.Name == jCustomerName) ?? await ct.Customer.FirstOrDefaultAsync(z => z.EmailAddress == jCustomerEmail);
|
||||
if (c == null)
|
||||
{
|
||||
//New customer
|
||||
c = new Customer();
|
||||
|
||||
c.Name = jCustomerName;
|
||||
UpdateCustomerFromVendorData(jData, jCustomerEmail, c);
|
||||
c = await customerBiz.CreateAsync(c);
|
||||
if (c == null)
|
||||
throw new System.ApplicationException($"Error creating new Customer: {customerBiz.GetErrorsAsString()} vendor data :{p.VendorData}");
|
||||
}
|
||||
else
|
||||
{
|
||||
//existing customer, refresh it
|
||||
UpdateCustomerFromVendorData(jData, jCustomerEmail, c);
|
||||
c = await customerBiz.PutAsync(c);
|
||||
if (c == null)
|
||||
throw new System.ApplicationException($"Error updating existing Customer: {customerBiz.GetErrorsAsString()} vendor data :{p.VendorData}");
|
||||
}
|
||||
|
||||
var salesOrderNumber = jData["order_notification"]["purchase"]["purchase_id"].Value<string>();
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -133,6 +170,30 @@ namespace Sockeye.Biz
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateCustomerFromVendorData(JObject jData, string jCustomerEmail, Customer c)
|
||||
{
|
||||
c.EmailAddress = jCustomerEmail;
|
||||
c.Active = true;//if they just made a purchase they are active even if they weren't before
|
||||
c.DoNotContact = false;//if they just made a purchase they are contactable even if they weren't before
|
||||
c.AccountNumber = jData["order_notification"]["purchase"]["customer_data"]["shopper_id"].Value<string>();//appears to be mycommerce customer id number hopefully static between orders
|
||||
c.Address = c.PostAddress = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["address"]["street1"].Value<string>();
|
||||
c.City = c.PostCity = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["address"]["city"].Value<string>();
|
||||
c.Region = c.PostRegion = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["address"]["state"].Value<string>();
|
||||
c.Country = c.PostCountry = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["address"]["country"].Value<string>();
|
||||
c.PostCode = c.AddressPostal = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["address"]["postal_code"].Value<string>();
|
||||
var firstName = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["first_name"].Value<string>() ?? "FirstNameEmpty";
|
||||
var lastName = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["last_name"].Value<string>() ?? "LastNameEmpty";
|
||||
var language = jData["order_notification"]["purchase"]["customer_data"]["language"].Value<string>() ?? "LanguageEmpty";
|
||||
if (string.IsNullOrWhiteSpace(c.Notes))
|
||||
c.Notes = string.Empty;
|
||||
if (!c.Notes.Contains(lastName))
|
||||
{
|
||||
if (c.Notes.Length > 0 && !c.Notes.EndsWith('\n'))
|
||||
c.Notes += "\n";
|
||||
c.Notes += $"Purchase contact:{firstName} {lastName}, language: {language}\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region SAMPLE VENDOR PURCHASE NOTIFICATIONS
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user