Beispiel #1
0
            private async void btnActivate_Click(object sender, EventArgs e)
            {
                try
                {
                    string aadToken = AzureActiveDirectoryHelper.GetAADHeaderWithPrompt();

                    this.textBoxRetailServerUrl.Text = this.retailServerUrl;
                    RetailServerContext context        = Helpers.CreateNewRetailServerContext(this.retailServerUrl);
                    ManagerFactory      managerFactory = ManagerFactory.Create(context);

                    managerFactory.Context.SetUserToken(new AADToken(aadToken));
                    managerFactory.Context.SetDeviceToken(null);
                    DeviceActivationResult  result = null;
                    IStoreOperationsManager storeOperationsManager = managerFactory.GetManager <IStoreOperationsManager>();
                    result = await storeOperationsManager.ActivateDevice(this.textBoxDeviceId.Text, this.textBoxRegisterId.Text, "testDevice.DeviceId", forceActivate : true, deviceType : 2 /*testDevice.DeviceType*/);

                    this.AppInfo = new DeviceActivationInformation(this.retailServerUrl, result.Device.TerminalId, result.Device.ChannelName, result.Device.Token, result.Device.DeviceNumber, DateTime.Now);
                    this.mainForm.Log("Activation succeeded.");
                }
                catch (Exception ex)
                {
                    this.mainForm.Log(ex.ToString());
                }

                this.Close();
            }
            /// <summary>
            /// Activates the device.
            /// </summary>
            /// <param name="request">The device activation request.</param>
            /// <returns>The device activation response.</returns>
            private static ActivateDeviceRealtimeResponse ActivateDevice(ActivateDeviceRealtimeRequest request)
            {
                var    getDeviceRequest = new GetDeviceDataRequest(request.DeviceNumber, isActivatedOnly: false);
                Device device           = request.RequestContext.Runtime.Execute <SingleEntityDataServiceResponse <Device> >(getDeviceRequest, request.RequestContext).Entity;

                device.ActivatedDateTime   = device.ActivatedDateTime ?? DateTimeOffset.UtcNow;
                device.DeactivateComments  = device.DeactivateComments ?? string.Empty;
                device.DeactivatedDateTime = device.DeactivatedDateTime ?? DateTimeOffset.MinValue;
                device.TokenIssueTime      = device.TokenIssueTime ?? DateTimeOffset.UtcNow;

                var result = new DeviceActivationResult();

                if (device == null)
                {
                    // Device is not found, throws exception.
                    string message = string.Format("The input device number '{0}' does not exist in demo database.", request.DeviceNumber);
                    throw new ConfigurationException(ConfigurationErrors.Microsoft_Dynamics_Commerce_Runtime_DeviceConfigurationNotFound, message);
                }
                else if (string.IsNullOrWhiteSpace(device.TerminalId))
                {
                    // If device is found but the terminal associated with the device is not found, try to find the terminal using input terminal id.
                    var      columnSet          = new ColumnSet(Terminal.RecordIdColumn, Terminal.TerminalIdColumn, Terminal.ChannelIdColumn);
                    var      settings           = new QueryResultSettings(columnSet, PagingInfo.AllRecords);
                    var      getTerminalRequest = new GetTerminalDataRequest(request.TerminalId, settings);
                    Terminal terminal           = request.RequestContext.Runtime.Execute <SingleEntityDataServiceResponse <Terminal> >(getTerminalRequest, request.RequestContext).Entity;

                    if (terminal == null)
                    {
                        string message = string.Format("The input device number '{0}' and terminal identifier '{1}' do not exist in demo database.", request.DeviceNumber, request.TerminalId);
                        throw new ConfigurationException(ConfigurationErrors.Microsoft_Dynamics_Commerce_Runtime_DeviceConfigurationNotFound, message);
                    }

                    result.Device = new Device
                    {
                        DeviceId            = request.DeviceId,
                        DeviceNumber        = request.DeviceNumber,
                        TerminalRecordId    = terminal.RecordId,
                        TerminalId          = terminal.TerminalId,
                        ChannelId           = terminal.ChannelId,
                        DeviceTypeRecordId  = device.DeviceTypeRecordId,
                        ActivatedDateTime   = device.ActivatedDateTime,
                        TokenIssueTime      = device.TokenIssueTime,
                        DeactivatedDateTime = device.DeactivatedDateTime,
                        DeactivateComments  = device.DeactivateComments,
                        TokenData           = device.TokenData,
                        TokenSalt           = device.TokenSalt
                    };
                }
                else
                {
                    // Both the device and associated terminal are found.
                    device.DeviceNumber = request.DeviceNumber;
                    device.DeviceId     = request.DeviceId;
                    result.Device       = device;
                }

                var response = new ActivateDeviceRealtimeResponse(result);

                return(response);
            }
            /// <summary>
            /// Activate a device in AX.
            /// </summary>
            /// <param name="deviceNumber">The device number.</param>
            /// <param name="terminalId">The terminal identifier.</param>
            /// <param name="staffId">The staff identifier.</param>
            /// <param name="deviceId">The physical device identifier.</param>
            /// <param name="forceActivate">The value indicating whether to force the Activation of a device when the physical device identifiers are different.</param>
            /// <param name="deviceType">The device type (optional).</param>
            /// <returns>The device activation result object.</returns>
            internal DeviceActivationResult ActivateDevice(string deviceNumber, string terminalId, string staffId, string deviceId, bool forceActivate, int?deviceType)
            {
                ThrowIf.Null(deviceNumber, "deviceNumber");
                ThrowIf.Null(staffId, "staffId");

                // transform the customer to parameters
                object[] parameters = new object[] { deviceNumber, terminalId, staffId, deviceId, forceActivate, deviceType.GetValueOrDefault(TransactionServiceClient.DefaultDeviceType) };
                ReadOnlyCollection <object> data = null;

                try
                {
                    data = this.InvokeMethod(ActivateDeviceMethodName, parameters);
                }
                catch (HeadquarterTransactionServiceException exception)
                {
                    string errorCode = (string)exception.HeadquartersErrorData.FirstOrDefault();
                    if (AttemptToActivateFromDifferencePhysicalDeviceErrorCode.Equals(errorCode, StringComparison.OrdinalIgnoreCase))
                    {
                        RetailLogger.Log.CrtServicesAttemptToActivateFromDifferentPhysicalDevice(staffId, deviceNumber, deviceId, terminalId);

                        throw new CommerceException(
                                  SecurityErrors.Microsoft_Dynamics_Commerce_Runtime_AttemptToActivateFromDifferentPhysicalDevice.ToString(),
                                  exception,
                                  string.Format("Attempt to activate an activated device '{0}' from another physical device.", deviceNumber));
                    }
                    else
                    {
                        throw;
                    }
                }

                if (data == null || data.Count < DeviceActivationResponseSize)
                {
                    throw new Microsoft.Dynamics.Commerce.Runtime.CommunicationException(CommunicationErrors.Microsoft_Dynamics_Commerce_Runtime_HeadquarterResponseParsingError);
                }

                // Parse response data
                Microsoft.Dynamics.Commerce.Runtime.DataModel.Device device = this.CreateDevice(data);
                var result = new DeviceActivationResult {
                    Device = device
                };

                return(result);
            }