private LoyaltyCard GetLoyaltyCardById(string loyaltyCardId)
            {
                var getLoyaltyCardDataRequest  = new GetLoyaltyCardDataRequest(loyaltyCardId);
                var getLoyaltyCardDataResponse = this.Context.Runtime.Execute <SingleEntityDataServiceResponse <LoyaltyCard> >(getLoyaltyCardDataRequest, this.Context);

                return(getLoyaltyCardDataResponse.Entity);
            }
示例#2
0
            /// <summary>
            /// Gets the loyalty card status including the loyalty groups and the reward points status.
            /// </summary>
            /// <param name="request">The request containing the card number.</param>
            /// <returns>The response containing the loyalty card status.</returns>
            private static GetLoyaltyCardStatusServiceResponse GetLoyaltyCardStatus(GetLoyaltyCardStatusServiceRequest request)
            {
                // Get loyalty card basic information
                var         getLoyaltyCardDataRequest = new GetLoyaltyCardDataRequest(request.LoyaltyCardNumber);
                LoyaltyCard loyaltyCard = request.RequestContext.Execute <SingleEntityDataServiceResponse <LoyaltyCard> >(getLoyaltyCardDataRequest).Entity;

                if (loyaltyCard == null)
                {
                    return(new GetLoyaltyCardStatusServiceResponse());
                }

                var validateCustomerAccountRequest = new GetValidatedCustomerAccountNumberServiceRequest(loyaltyCard.CustomerAccount, throwOnValidationFailure: true);

                request.RequestContext.Execute <GetValidatedCustomerAccountNumberServiceResponse>(validateCustomerAccountRequest);

                // Get loyalty groups and loyalty tiers
                DateTimeOffset channelDateTime = request.RequestContext.GetNowInChannelTimeZone();
                var            getLoyaltyGroupsAndTiersDataRequest = new GetLoyaltyGroupsAndTiersDataRequest(request.LoyaltyCardNumber, request.RetrieveRewardPointStatus);

                getLoyaltyGroupsAndTiersDataRequest.QueryResultSettings = QueryResultSettings.AllRecords;
                loyaltyCard.LoyaltyGroups = request.RequestContext.Execute <EntityDataServiceResponse <LoyaltyGroup> >(getLoyaltyGroupsAndTiersDataRequest).PagedEntityCollection.Results;

                // Get reward points status
                if (request.RetrieveRewardPointStatus)
                {
                    var serviceRequest = new GetLoyaltyCardRewardPointsStatusRealtimeRequest(
                        channelDateTime,
                        request.LoyaltyCardNumber,
                        excludeBlocked: false,
                        excludeNoTender: false,
                        includeRelatedCardsForContactTender: false,
                        includeNonRedeemablePoints: false,
                        includeActivePointsOnly: false);

                    EntityDataServiceResponse <LoyaltyCard> serviceResponse = request.RequestContext.Execute <EntityDataServiceResponse <LoyaltyCard> >(serviceRequest);
                    LoyaltyCard loyaltyCardWithPoints = serviceResponse.PagedEntityCollection.FirstOrDefault();

                    if (loyaltyCardWithPoints != null)
                    {
                        loyaltyCard.RewardPoints = loyaltyCardWithPoints.RewardPoints;
                    }
                }

                var response = new GetLoyaltyCardStatusServiceResponse(loyaltyCard);

                return(response);
            }
            private SingleEntityDataServiceResponse <LoyaltyCard> GetLoyaltyCard(GetLoyaltyCardDataRequest request)
            {
                ThrowIf.NullOrWhiteSpace(request.LoyaltyCardNumber, "loyaltyCardNumber");

                ParameterSet parameters = new ParameterSet();

                parameters["@nvc_LoyaltyCardNumber"]  = request.LoyaltyCardNumber;
                parameters["@nvc_CustomerDataAreaId"] = request.RequestContext.GetChannelConfiguration().InventLocationDataAreaId;

                LoyaltyCard loyaltyCard;

                using (SqlServerDatabaseContext sqlServerDatabaseContext = new SqlServerDatabaseContext(request))
                {
                    loyaltyCard = sqlServerDatabaseContext.ExecuteNonPagedStoredProcedure <LoyaltyCard>(LoyaltySqlServerDataService.GetLoyaltyCardSprocName, parameters).SingleOrDefault();
                }

                return(new SingleEntityDataServiceResponse <LoyaltyCard>(loyaltyCard));
            }
            /// <summary>
            /// Authorizes the payment.
            /// This step checks whether the loyalty card has enough reward points to redeem. If yes, it decides the points
            /// to redeem based on redeem ranking.
            /// </summary>
            /// <param name="request">The request.</param>
            /// <returns>A response containing the authorized tender line.</returns>
            private static AuthorizePaymentServiceResponse AuthorizePayment(AuthorizePaymentServiceRequest request)
            {
                if (request == null)
                {
                    throw new ArgumentNullException("request");
                }

                if (request.TenderLine == null)
                {
                    throw new ArgumentException("request.TenderLine cannot be null.");
                }

                if (request.RequestContext == null)
                {
                    throw new ArgumentException("request.RequestContext cannot be null.");
                }

                if (request.Transaction == null)
                {
                    throw new ArgumentException("request.Transaction cannot be null.");
                }

                // Check tender amount.
                if (request.TenderLine.Amount == 0m)
                {
                    throw new PaymentException(PaymentErrors.Microsoft_Dynamics_Commerce_Runtime_InvalidPaymentRequest, "The tender amount must be greater than zero.");
                }

                // Check tender currency.
                if (string.IsNullOrWhiteSpace(request.TenderLine.Currency))
                {
                    throw new PaymentException(PaymentErrors.Microsoft_Dynamics_Commerce_Runtime_InvalidPaymentRequest, "The tender currency is missing.");
                }

                // Check if the transaction already has loyalty payments
                var activeTenderLines = request.Transaction.ActiveTenderLines;

                if (activeTenderLines != null && activeTenderLines.Any(line => !string.IsNullOrWhiteSpace(line.LoyaltyCardId)))
                {
                    throw new PaymentException(PaymentErrors.Microsoft_Dynamics_Commerce_Runtime_NoMoreThanOneLoyaltyTender, "The transaction cannot contain more than one loyalty payment line.");
                }

                SalesOrder salesOrder = request.Transaction as SalesOrder;

                if (salesOrder != null && salesOrder.HasLoyaltyPayment &&
                    (salesOrder.CustomerOrderMode != CustomerOrderMode.Cancellation || salesOrder.AmountDue > decimal.Zero))
                {
                    throw new PaymentException(PaymentErrors.Microsoft_Dynamics_Commerce_Runtime_NoMoreThanOneLoyaltyTender, "The transaction cannot contain more than one loyalty payment line.");
                }

                // Check whether the loyalty card is valid.
                var         getLoyaltyCardDataRequest = new GetLoyaltyCardDataRequest(request.TenderLine.LoyaltyCardId);
                LoyaltyCard loyaltyCard = request.RequestContext.Execute <SingleEntityDataServiceResponse <LoyaltyCard> >(getLoyaltyCardDataRequest).Entity;

                if (loyaltyCard == null || string.IsNullOrWhiteSpace(loyaltyCard.CardNumber))
                {
                    throw new PaymentException(PaymentErrors.Microsoft_Dynamics_Commerce_Runtime_InvalidLoyaltyCardNumber, "The loyalty card number does not exists.");
                }

                // Check whether the loyalty card is blocked.
                if (loyaltyCard.CardTenderType == LoyaltyCardTenderType.Blocked)
                {
                    throw new PaymentException(PaymentErrors.Microsoft_Dynamics_Commerce_Runtime_BlockedLoyaltyCard, "The loyalty card is blocked.");
                }

                if (loyaltyCard.CardTenderType == LoyaltyCardTenderType.NoTender)
                {
                    throw new PaymentException(PaymentErrors.Microsoft_Dynamics_Commerce_Runtime_NoTenderLoyaltyCard, "The loyalty card is not allowed for payment.");
                }

                // Calculate redeem trans and fill in the sales transaction.
                if (request.TenderLine.Amount >= 0)
                {
                    LoyaltyServiceHelper.FillInLoyaltyRewardPointLinesForPayment(
                        request.RequestContext,
                        request.Transaction,
                        loyaltyCard,
                        request.TenderLine.Amount,
                        request.TenderLine.Currency);
                }
                else
                {
                    LoyaltyServiceHelper.FillInLoyaltyRewardPointLinesForRefund(
                        request.RequestContext,
                        request.Transaction,
                        loyaltyCard,
                        request.TenderLine.Amount,
                        request.TenderLine.Currency);
                }

                // Set Card Type Id for Loyalty Card if not set by the client.
                if (request.TenderLine.CardTypeId == null)
                {
                    string tenderTypeId                  = request.TenderLine.TenderTypeId;
                    var    cardTypeDataRequest           = new GetCardTypeDataRequest(QueryResultSettings.AllRecords);
                    var    cardTypeInfoResponse          = request.RequestContext.Execute <EntityDataServiceResponse <CardTypeInfo> >(cardTypeDataRequest);
                    IEnumerable <CardTypeInfo> cardTypes = cardTypeInfoResponse.PagedEntityCollection.Results;
                    CardTypeInfo loyaltyCardTypeInfo     = cardTypes.FirstOrDefault(cardType => cardType.PaymentMethodId == tenderTypeId);
                    if (loyaltyCardTypeInfo == null)
                    {
                        throw new ConfigurationException(ConfigurationErrors.Microsoft_Dynamics_Commerce_Runtime_ConfigurationSettingNotFound, "The loyalty card payment as a tender type card is not configured for the channel.");
                    }

                    request.TenderLine.CardTypeId = loyaltyCardTypeInfo.TypeId;
                }

                // Authorize.
                request.TenderLine.Status     = TenderLineStatus.PendingCommit;
                request.TenderLine.IsVoidable = true;

                return(new AuthorizePaymentServiceResponse(request.TenderLine));
            }