using System; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using Sockeye.Models; using Sockeye.Util; namespace Sockeye.Biz { /// /// Process purchases that are from vendor notification /// Turn vendordata into fully filled out purchase /// attempt to match to existing customer or create one if necessary /// /// A Separate job will make licenses /// /// internal static class SockBotProcessPurchases { private static ILogger log = Sockeye.Util.ApplicationLogging.CreateLogger("SockBotProcessPurchases"); private static DateTime lastSweep = DateTime.MinValue; private static TimeSpan PROCESS_EVERY_INTERVAL = new TimeSpan(0, 5, 10);//every 5 minutes roughly meaning 15 minutes down is highest fail state //////////////////////////////////////////////////////////////////////////////////////////////// // DoSweep // public static async Task DoWorkAsync() { //This will get triggered roughly every minute, but we don't want to check that frequently if (DateTime.UtcNow - lastSweep < PROCESS_EVERY_INTERVAL) return; if (ServerBootConfig.MIGRATING) return;//don't do this during migration (migration is one time only so can remove after up and running) log.LogDebug("Process purchases starting"); await ProcessVendorDataIntoPurchases(); // log.LogDebug("Process licenses from purchases starting"); // await ProcessPurchasesIntoLicenses(); lastSweep = DateTime.UtcNow; } private static async Task ProcessVendorDataIntoPurchases() { return; using (AyContext ct = Sockeye.Util.ServiceProviderProvider.DBContext) { //get a list of all actionable purchases var ProcessablePurchaseIdList = await ct.Purchase .AsNoTracking() .Where(z => z.Processed == false && z.ProductId == null) .OrderBy(z => z.Id) .Select(z => z.Id) .ToListAsync(); try { foreach (long purchaseId in ProcessablePurchaseIdList) { var biz = PurchaseBiz.GetBiz(ct); var p = await biz.GetAsync(purchaseId, false); if (p == null) { //this is a serious issue log and server ops it var err = $"SockBotProcessPurchases error running job, purchase record id {purchaseId} could not be fetched {biz.GetErrorsAsString}"; await NotifyEventHelper.AddOpsProblemEvent(err); log.LogError(err); } else { log.LogDebug($"Processing purchase {p.Id}-{p.PurchaseDate}"); if (string.IsNullOrWhiteSpace(p.VendorData)) { var err = $"Purchase record with ID {purchaseId} has no vendor data for sales order {p.SalesOrderNumber}"; await NotifyEventHelper.AddOpsProblemEvent("SockBotProcessPurchases: " + err); log.LogError(err); continue; } //Parse json vendordata //Existing customer or create new one? //here maybe need to match by vendor provided customer ID?? //sometimes a user will make a new account for a new purchase but intend it as an addon to v7 //for v8 it should be straightforward, however if pricing chagnes and new product needs to be purchased they may create a new record //same dbid for v8 should do the trick //save changes p.Processed = true; await biz.PutAsync(p); } } } catch (Exception ex) { var err = "SockBotProcessPurchases error running job"; //serious issue requires immediate notification await NotifyEventHelper.AddOpsProblemEvent(err, ex); log.LogError(ex, err); } } } internal static async Task ParseVendorNotificationData(Purchase p, AyContext ct, ILogger log) { if (string.IsNullOrWhiteSpace(p.VendorData)) return; 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() ?? throw new System.FormatException($"Vendor data empty reg_name:{p.VendorData}"); var jCustomerEmail = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["email"].Value() ?? 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(); if (await ct.Purchase.AnyAsync(z => z.SalesOrderNumber == salesOrderNumber)) throw new System.ApplicationException($"Sales order already exists: {salesOrderNumber} will not be processed"); //ok, turn this into a fully realized Purchase record } catch (Exception ex) { var err = $"ParseVendorNotificationData: Purchase record {p.Id}-{p.PurchaseDate} triggered exception, see log"; await NotifyEventHelper.AddOpsProblemEvent(err);//notify, this is serious log.LogError(ex, err); } } 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();//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(); c.City = c.PostCity = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["address"]["city"].Value(); c.Region = c.PostRegion = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["address"]["state"].Value(); c.Country = c.PostCountry = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["address"]["country"].Value(); c.PostCode = c.AddressPostal = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["address"]["postal_code"].Value(); var firstName = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["first_name"].Value() ?? "FirstNameEmpty"; var lastName = jData["order_notification"]["purchase"]["customer_data"]["delivery_contact"]["last_name"].Value() ?? "LastNameEmpty"; var language = jData["order_notification"]["purchase"]["customer_data"]["language"].Value() ?? "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 /* ///////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////v7 export to xls add on///////////////////////////// { "creation_date": "2023-01-20T00:18:33Z", "id": 357520816, "order_notification": { "purchase": { "customer_data": { "billing_contact": { "address": { "city": "Cambridge", "country": "Canada", "country_id": "CA", "postal_code": "N1T 1J3", "state": "Ontario", "state_id": "ON", "street1": "181 Shearson Crescent" }, "company": "Cambridge Elevating Inc", "email": "richard.wright@cambridgeelevating.com", "first_name": "Richard", "last_name": "Wright" }, "customer_payment_data": { "currency": "USD", "payment_method": "MasterCard" }, "delivery_contact": { "address": { "city": "Cambridge", "country": "Canada", "country_id": "CA", "postal_code": "N1T 1J3", "state": "Ontario", "state_id": "ON", "street1": "181 Shearson Crescent" }, "company": "Cambridge Elevating Inc", "email": "richard.wright@cambridgeelevating.com", "first_name": "Richard", "last_name": "Wright" }, "language": "English", "language_iso": "en", "reg_name": "Cambridge Elevating Inc", "shopper_id": "62545677", "subscribe_newsletter": false, "user_id": "richard.wright@cambridgeelevating.com-5" }, "is_test": false, "payment_complete_date": "2023-01-20T00:18:33Z", "payment_status": "complete", "payment_status_id": "PCA", "purchase_date": "2023-01-20T00:18:31Z", "purchase_id": 843101393, "purchase_item": [ { "accounting": { "currency": "USD", "product_net": 6.65, "discount": 0.0, "product_vat": 0.86, "shipping": 0.0, "shipping_vat": 0.0, "eu_vat": -0.86, "margin_net": -2.5, "your_revenue": 4.15 }, "additional_information": [ { "additional_id": "ADDITIONAL1", "additional_value": "YES" } ], "currency": "USD", "delivery_type": "Electronically", "discount": 0.0, "extended_download_price": 0.0, "manual_order_price": 0.0, "notification_no": 7953, "product_id": 300740327, "product_name": "optional add-on plug-in Export to XLS 1 year subscription license", "product_single_price": 6.65, "purchase_item_key": [], "quantity": 1, "running_no": 1, "shipping_price": 0.0, "shipping_vat_pct": 0.0, "subscription": { "expiration_date": "2024-01-20T18:36:22Z", "id": "771160083-3", "interval": "Yearly without end", "original_notification_no": "7779", "original_purchase_id": "771160083", "original_running_no": "3", "renewal_discount_count": "", "renewal_discount_start": "", "renewal_type": "auto", "retention_discount_count": "", "retention_discount_percent": "", "start_date": "2022-01-20T00:00:00", "status": "ToProcess", "status_id": "TOP" }, "vat_pct": 13.0 } ], "purchase_origin": "Subscription", "sequential_invoice_no": "e5-DE-2023-00000163106" } } } ///////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////// V7 Scheduleable user ////////////////////// { "creation_date": "2023-01-20T00:18:32Z", "id": 357520809, "order_notification": { "purchase": { "customer_data": { "billing_contact": { "address": { "city": "Cambridge", "country": "Canada", "country_id": "CA", "postal_code": "N1T 1J3", "state": "Ontario", "state_id": "ON", "street1": "181 Shearson Crescent" }, "company": "Cambridge Elevating Inc", "email": "richard.wright@cambridgeelevating.com", "first_name": "Richard", "last_name": "Wright" }, "customer_payment_data": { "currency": "USD", "payment_method": "MasterCard" }, "delivery_contact": { "address": { "city": "Cambridge", "country": "Canada", "country_id": "CA", "postal_code": "N1T 1J3", "state": "Ontario", "state_id": "ON", "street1": "181 Shearson Crescent" }, "company": "Cambridge Elevating Inc", "email": "richard.wright@cambridgeelevating.com", "first_name": "Richard", "last_name": "Wright" }, "language": "English", "language_iso": "en", "reg_name": "Cambridge Elevating Inc", "shopper_id": "62545677", "subscribe_newsletter": false, "user_id": "richard.wright@cambridgeelevating.com-5" }, "is_test": false, "payment_complete_date": "2023-01-20T00:18:32Z", "payment_status": "complete", "payment_status_id": "PCA", "purchase_date": "2023-01-20T00:18:31Z", "purchase_id": 843101383, "purchase_item": [ { "accounting": { "currency": "USD", "product_net": 577.5, "discount": 0.0, "product_vat": 75.08, "shipping": 0.0, "shipping_vat": 0.0, "eu_vat": -75.08, "margin_net": -29.05, "your_revenue": 548.45 }, "additional_information": [ { "additional_id": "ADDITIONAL1", "additional_value": "YES" }, { "additional_id": "ADDITIONAL2", "additional_value": "YES" }, { "additional_id": "ADDITIONAL3", "additional_value": "YES" } ], "currency": "USD", "delivery_type": "Electronically", "discount": 0.0, "extended_download_price": 0.0, "manual_order_price": 0.0, "notification_no": 7952, "product_id": 300807973, "product_name": "Up to 15 AyaNova schedulable resource 1 year subscription license", "product_single_price": 577.5, "purchase_item_key": [], "quantity": 1, "running_no": 1, "shipping_price": 0.0, "shipping_vat_pct": 0.0, "subscription": { "expiration_date": "2024-01-20T18:36:22Z", "id": "771160083-1", "interval": "Yearly without end", "original_notification_no": "7777", "original_purchase_id": "771160083", "original_running_no": "1", "renewal_discount_count": "", "renewal_discount_start": "", "renewal_type": "auto", "retention_discount_count": "", "retention_discount_percent": "", "start_date": "2022-01-20T00:00:00", "status": "ToProcess", "status_id": "TOP" }, "vat_pct": 13.0 } ], "purchase_origin": "Subscription", "sequential_invoice_no": "e5-DE-2023-00000163105" } } } ///////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////// NOT A LICENSE PRODUCT - CUSTOMIZATION SERVICES FOR REPORT TEMPLATE { "creation_date": "2023-01-18T15:45:50Z", "id": 357446088, "order_notification": { "purchase": { "customer_data": { "billing_contact": { "address": { "city": "Brunswick", "country": "USA", "country_id": "US", "postal_code": "38014", "state": "Tennessee", "state_id": "TN", "street1": "PO Box 374" }, "company": "Tri-Star Medical Technologies LLC", "email": "stan@tri-starmedical.com", "first_name": "Stan", "last_name": "Hilton" }, "customer_payment_data": { "currency": "USD", "payment_method": "Visa" }, "delivery_contact": { "address": { "city": "Brunswick", "country": "USA", "country_id": "US", "postal_code": "38014", "state": "Tennessee", "state_id": "TN", "street1": "PO Box 374" }, "company": "Tri-Star Medical Technologies LLC", "email": "stan@tri-starmedical.com", "first_name": "Stan", "last_name": "Hilton" }, "language": "English", "language_iso": "en", "reg_name": "Tri-Star Medical Technologies LLC", "shopper_id": "65821033", "subscribe_newsletter": false, "user_id": "stan@tri-starmedical.com-1" }, "is_test": false, "payment_complete_date": "2023-01-18T15:45:49Z", "payment_status": "complete", "payment_status_id": "PCA", "purchase_date": "2023-01-18T15:45:48Z", "purchase_id": 842819563, "purchase_item": [ { "accounting": { "currency": "USD", "product_net": 50.0, "discount": 0.0, "product_vat": 4.63, "shipping": 0.0, "shipping_vat": 0.0, "eu_vat": -4.63, "margin_net": -5.14, "your_revenue": 44.86 }, "additional_information": [], "currency": "USD", "delivery_type": "Electronically", "discount": 0.0, "extended_download_price": 0.0, "manual_order_price": 0.0, "notification_no": 7945, "product_id": 300525428, "product_name": "AyaNova customization services", "product_single_price": 50.0, "purchase_item_key": [], "quantity": 1, "running_no": 1, "shipping_price": 0.0, "shipping_vat_pct": 0.0, "vat_pct": 9.25 } ], "purchase_origin": "online", "sequential_invoice_no": "e5-US-2023-00000083100" } } } ///////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////// TEST PURCHASE USING SPECIAL TEST CREDIT CARD NUMBER PROVIDED BY MYCOMMERCE { "creation_date": "2023-01-18T04:24:28Z", "id": 357424814, "order_notification": { "purchase": { "customer_data": { "billing_contact": { "address": { "city": "Courtenay", "country": "Canada", "country_id": "CA", "postal_code": "V9J9T6", "state": "British Columbia", "state_id": "BC", "street1": "05-3610 Christie Parkway" }, "company": "GZTestCo", "email": "gzmailadmin@gmail.com", "first_name": "Test", "last_name": "Testerson" }, "customer_payment_data": { "currency": "USD", "payment_method": "Other" }, "delivery_contact": { "address": { "city": "Courtenay", "country": "Canada", "country_id": "CA", "postal_code": "V9J9T6", "state": "British Columbia", "state_id": "BC", "street1": "05-3610 Christie Parkway" }, "company": "GZTestCo", "email": "gzmailadmin@gmail.com", "first_name": "Test", "last_name": "Testerson" }, "language": "English", "language_iso": "en", "reg_name": "GZTestCo", "shopper_id": "65817245", "subscribe_newsletter": false, "user_id": "gzmailadmin@gmail.com-36" }, "is_test": true, "payment_complete_date": "2023-01-18T04:24:28Z", "payment_status": "testpaymentarrived", "payment_status_id": "TCA", "purchase_date": "2023-01-18T04:24:28Z", "purchase_id": 842769483, "purchase_item": [ { "additional_information": [], "currency": "USD", "delivery_type": "Electronically", "discount": 0.0, "extended_download_price": 0.0, "manual_order_price": 0.0, "notification_no": 0, "product_id": 300525428, "product_name": "AyaNova customization services", "product_single_price": 50.0, "purchase_item_key": [], "quantity": 1, "running_no": 1, "shipping_price": 0.0, "shipping_vat_pct": 0.0, "vat_pct": 12.0 } ], "purchase_origin": "online" } } } ///////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////// RAVEN PERPETUAL { "creation_date": "2023-01-14T06:17:59Z", "id": 357214426, "order_notification": { "purchase": { "customer_data": { "billing_contact": { "address": { "city": "Frankfort", "country": "USA", "country_id": "US", "postal_code": "46041", "state": "Indiana", "state_id": "IN", "street1": "47 N Jackson St." }, "company": "ACCS", "email": "internet@accs.net", "first_name": "Marcus", "last_name": "Hodges" }, "customer_payment_data": { "currency": "USD", "payment_method": "Visa" }, "delivery_contact": { "address": { "city": "Frankfort", "country": "USA", "country_id": "US", "postal_code": "46041", "state": "Indiana", "state_id": "IN", "street1": "47 N Jackson St." }, "company": "ACCS", "email": "internet@accs.net", "first_name": "Marcus", "last_name": "Hodges" }, "language": "English", "language_iso": "en", "reg_name": "ACCS", "shopper_id": "65783365", "subscribe_newsletter": false, "user_id": "internet@accs.net" }, "is_test": false, "payment_complete_date": "2023-01-14T06:17:59Z", "payment_status": "complete", "payment_status_id": "PCA", "purchase_date": "2023-01-14T06:17:58Z", "purchase_id": 841935113, "purchase_item": [ { "accounting": { "currency": "USD", "product_net": 72.0, "discount": 0.0, "product_vat": 5.04, "shipping": 0.0, "shipping_vat": 0.0, "eu_vat": -5.04, "margin_net": -6.03, "your_revenue": 65.97 }, "additional_information": [ { "additional_id": "AGREENOREFUNDS", "additional_value": "YES" }, { "additional_id": "AGREEPAYMETHODVALIDCANCEL", "additional_value": "YES" }, { "additional_id": "AGREEEXPIRESIFNOTPAID", "additional_value": "YES" }, { "additional_id": "DATABASEID", "additional_value": "7ktPA+Eaq+LM4vNMkQdbwBkUMDzzFYXmH+m21n3w5Rk=" } ], "currency": "USD", "delivery_type": "Electronically", "discount": 0.0, "extended_download_price": 0.0, "manual_order_price": 0.0, "notification_no": 7944, "product_id": 301028317, "product_name": "AyaNova perpetual single user license includes one year maintenance plan ", "product_single_price": 24.0, "purchase_item_key": [], "quantity": 3, "running_no": 1, "shipping_price": 0.0, "shipping_vat_pct": 0.0, "subscription": { "expiration_date": "2024-01-14T06:17:59Z", "id": "841935113-1", "interval": "Yearly without end", "renewal_discount_count": "", "renewal_discount_start": "", "renewal_type": "auto", "retention_discount_count": "", "retention_discount_percent": "", "start_date": "2023-01-14T00:00:00", "status": "ToProcess", "status_id": "TOP" }, "vat_pct": 7.0, "your_product_id": "perpetual" } ], "purchase_origin": "online", "sequential_invoice_no": "e5-US-2023-00000064245" } } } ///////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////// RAVEN MONTHLY SUBSCRIPTION { "creation_date": "2023-01-07T01:54:11Z", "id": 356798113, "order_notification": { "purchase": { "customer_data": { "billing_contact": { "address": { "city": "UNIONVILLE", "country": "Canada", "country_id": "CA", "postal_code": "L3R 9W6", "state": "Ontario", "state_id": "ON", "street1": "4261 HWY 7 E, Unit A14 #399" }, "company": "sportseffect", "email": "service@sportseffect.com", "first_name": "Mark", "last_name": "Hopkins" }, "customer_payment_data": { "currency": "CAD", "payment_method": "Visa" }, "delivery_contact": { "address": { "city": "UNIONVILLE", "country": "Canada", "country_id": "CA", "postal_code": "L3R 9W6", "state": "Ontario", "state_id": "ON", "street1": "4261 HWY 7 E, Unit A14 #399" }, "company": "sportseffect", "email": "service@sportseffect.com", "first_name": "Mark", "last_name": "Hopkins" }, "language": "English", "language_iso": "en", "reg_name": "sportseffect", "shopper_id": "65387076", "subscribe_newsletter": false, "user_id": "service@sportseffect.com" }, "is_test": false, "payment_complete_date": "2023-01-07T01:54:11Z", "payment_status": "complete", "payment_status_id": "PCA", "purchase_date": "2023-01-07T01:54:09Z", "purchase_id": 840549123, "purchase_item": [ { "accounting": { "currency": "USD", "product_net": 23.8, "discount": 0.0, "product_vat": 3.09, "shipping": 0.0, "shipping_vat": 0.0, "eu_vat": -3.09, "margin_net": -4.03, "your_revenue": 19.77 }, "additional_information": [ { "additional_id": "AGREENOREFUNDS", "additional_value": "YES" }, { "additional_id": "AGREEPAYMETHODVALIDCANCEL", "additional_value": "YES" }, { "additional_id": "AGREEEXPIRESIFNOTPAID", "additional_value": "YES" }, { "additional_id": "DATABASEID", "additional_value": "New subscription" } ], "currency": "USD", "delivery_type": "Electronically", "discount": 0.0, "extended_download_price": 0.0, "manual_order_price": 0.0, "notification_no": 7937, "product_id": 301028467, "product_name": "AyaNova subscription one user monthly", "product_single_price": 11.9, "purchase_item_key": [], "quantity": 2, "running_no": 1, "shipping_price": 0.0, "shipping_vat_pct": 0.0, "subscription": { "expiration_date": "2023-02-07T00:15:13Z", "id": "833674953-1", "interval": "Monthly without end", "original_notification_no": "7925", "original_purchase_id": "833674953", "original_running_no": "1", "renewal_discount_count": "", "renewal_discount_start": "", "renewal_type": "auto", "retention_discount_count": "", "retention_discount_percent": "", "start_date": "2022-12-07T00:00:00", "status": "ToProcess", "status_id": "TOP" }, "vat_pct": 13.0, "your_product_id": "subscriptionmonthly" } ], "purchase_origin": "Subscription", "sequential_invoice_no": "e5-DE-2023-00000055949" } } } */ #endregion sample vendor notifications //order notification json schema on this page down //https://account.mycommerce.com/home/wiki/7479997 //json format //https://account.mycommerce.com/home/wiki/7479805 //overall info ///////////////////////////////////////////////////////////////////// }//eoc }//eons