Last active
June 7, 2017 20:53
-
-
Save websterian/eee4d83288752f061c9b33ca40b84778 to your computer and use it in GitHub Desktop.
Example integration helper to send orders and customer to ERP from Sitecore commerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Sitecore.Commerce.Connectors.MYERP.Plugin.Erp | |
{ | |
using System; | |
using System.Threading.Tasks; | |
using Sitecore.Commerce.Connectors.MYERP.Plugin.Components; | |
using Sitecore.Commerce.Connectors.MYERP.Plugin.Policies; | |
using Sitecore.Commerce.Core; | |
using Sitecore.Commerce.Plugin.Customers; | |
using Sitecore.Commerce.Plugin.Orders; | |
/// <summary> | |
/// The integration helper. | |
/// </summary> | |
public class IntegrationHelper | |
{ | |
/// <summary> | |
/// The persistEntityPipeline. | |
/// </summary> | |
private readonly IPersistEntityPipeline persistEntityPipeline; | |
/// <summary> | |
/// The get customer pipeline. | |
/// </summary> | |
private readonly IFindEntityPipeline findEntityPipeline; | |
private readonly IServiceProvider serviceProvider; | |
private readonly IGetCustomerPipeline getCustomerPipeline; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="IntegrationHelper"/> class. | |
/// </summary> | |
/// <param name="persistEntityPipeline"> | |
/// The persist entity pipeline. | |
/// </param> | |
/// <param name="findEntityPipeline1"> | |
/// The get customer pipeline 1. | |
/// </param> | |
/// <param name="serviceProvider"></param> | |
/// <param name="getCustomerPipeline"></param> | |
public IntegrationHelper(IPersistEntityPipeline persistEntityPipeline, IFindEntityPipeline findEntityPipeline1, IServiceProvider serviceProvider, IGetCustomerPipeline getCustomerPipeline) | |
{ | |
this.persistEntityPipeline = persistEntityPipeline; | |
this.findEntityPipeline = findEntityPipeline1; | |
this.serviceProvider = serviceProvider; | |
this.getCustomerPipeline = getCustomerPipeline; | |
} | |
/// <summary> | |
/// The translate order and send. | |
/// </summary> | |
/// <param name="order"> | |
/// The order. | |
/// </param> | |
/// <param name="context"> | |
/// The context. | |
/// </param> | |
/// <returns> | |
/// The <see cref="Task"/>. | |
/// </returns> | |
public async Task<Order> TranslateOrderAndSendToErp(Order order, CommercePipelineExecutionContext context) | |
{ | |
try | |
{ | |
var customer = await this.UpdateCreateCustomer(order, context); | |
var persistOrderArgument = await this.UpdateCreateOrder(order, context, customer); | |
return persistOrderArgument.Entity as Order; | |
} | |
catch (Exception e) | |
{ | |
throw e; | |
} | |
} | |
private async Task<PersistEntityArgument> UpdateCreateOrder( | |
Order order, | |
CommercePipelineExecutionContext context, | |
Customer customer) | |
{ | |
// Get the policy for the ERP connection | |
var erpPolicy = context.GetPolicy<ErpPolicy>(); | |
// Create order >> | |
var orderId = order.OrderConfirmationId; | |
//!!!!!REPLACE THIS WITH YOUR CALL TO THE ERP use erpPolicy to get connection details!!!! | |
var orderIntegrated = true; | |
order.GetComponent<ErpOrderStatusComponent>().StatusDate = DateTime.Now; | |
order.GetComponent<ErpOrderStatusComponent>().Integrated = orderIntegrated; | |
// Update the order status on success | |
if (orderIntegrated) | |
{ | |
order.Status = "Completed - Sent to ERP"; | |
order.GetComponent<ErpOrderStatusComponent>().ErpUrl = erpPolicy.AosUrl; | |
order.GetComponent<ErpOrderStatusComponent>().StatusMessage = "Success"; | |
if (customer == null) | |
{ | |
order.GetComponent<ErpOrderStatusComponent>().StatusMessage = | |
order.GetComponent<ErpOrderStatusComponent>().StatusMessage + " - anonymous customer"; | |
} | |
//!!!!!REPLACE THIS WITH YOUR ERPs ORDER NUMBER!!!!!! | |
order.GetComponent<ErpOrderStatusComponent>().ErpOrderNumber = "Ord-" + order.OrderConfirmationId; | |
} | |
else | |
{ | |
order.Status = "Problem - Not sent to ERP"; | |
order.GetComponent<ErpOrderStatusComponent>().ErpUrl = erpPolicy.AosUrl; | |
order.GetComponent<ErpOrderStatusComponent>().StatusMessage = "There was an error while sending the data to ERP"; | |
} | |
// Update the order | |
var persistOrderArgument = await this.persistEntityPipeline.Run(new PersistEntityArgument(order), context); | |
return persistOrderArgument; | |
} | |
private async Task<Customer> UpdateCreateCustomer(Order order, CommercePipelineExecutionContext context) | |
{ | |
// Get the policy for the D365 connection | |
var erpPolicy = context.GetPolicy<ErpPolicy>(); | |
// *** Create customer >> | |
var contactComponent = order.GetComponent<ContactComponent>(); | |
var getCustomerCommand = new GetCustomerCommand(this.getCustomerPipeline, this.serviceProvider); | |
var customerId = $"{CommerceEntity.IdPrefix<Customer>()}{contactComponent.CustomerId}"; | |
var customer = await getCustomerCommand.Process(context.CommerceContext, customerId); | |
// The order will be created against a default "Anonymous customer" if its not found | |
if (customer != null) | |
{ | |
//!!!!REPLACE THIS WITH YOU CALL TO YOUR ERP, use the erpPolicy variable to retreive the connection details!!!!! | |
var customerIntegrated = true; | |
customer.GetComponent<ErpCustomerStatusComponent>().StatusDate = DateTime.Now; | |
customer.GetComponent<ErpCustomerStatusComponent>().Integrated = customerIntegrated; | |
// Update the order status on success | |
if (customerIntegrated) | |
{ | |
customer.GetComponent<ErpCustomerStatusComponent>().ErpUrl = erpPolicy.AosUrl; | |
customer.GetComponent<ErpCustomerStatusComponent>().StatusMessage = "Success"; | |
//!!!!REPLACE THIS WITH THE CUSTOMER ID OF YOUR ERP!!!!!!! | |
customer.GetComponent<ErpCustomerStatusComponent>().ErpCustomerNo = "Cust-" + contactComponent.CustomerId; | |
} | |
else | |
{ | |
customer.GetComponent<ErpCustomerStatusComponent>().ErpUrl = erpPolicy.AosUrl; | |
customer.GetComponent<ErpCustomerStatusComponent>().StatusMessage = | |
"There was an error while sending the data to ERP"; | |
} | |
// Update the customer | |
var persistCustomerResult = await this.persistEntityPipeline.Run(new PersistEntityArgument(customer), context); | |
} | |
// *** Create customer << | |
return customer; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment