コード例 #1
0
        /// <summary>
        ///     Adds the new gift card.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="lineItem">The line item.</param>
        private static void AddNewGiftCard(OrderTaskContext context, LineItem lineItem)
        {
            var paymentManager = new OrderPaymentManager(context.Order, context.HccApp);

            var store    = context.HccApp.CurrentStore;
            var giftCard = new GiftCardData
            {
                LineItemId     = lineItem.Id,
                CardNumber     = context.HccApp.CatalogServices.GenerateGiftCardNumber(),
                Amount         = Money.RoundCurrency(lineItem.AdjustedPricePerItem),
                RecipientEmail = lineItem.CustomPropGiftCardEmail,
                RecipientName  = lineItem.CustomPropGiftCardName,
                ExpirationDate = DateTime.UtcNow.AddMonths(store.Settings.GiftCard.ExpirationPeriodMonths)
            };
            var gcNumber = paymentManager.GiftCardCreate(giftCard);

            if (string.IsNullOrEmpty(gcNumber))
            {
                var message = new WorkflowMessage(
                    "Gift Certificate Insert Failed",
                    "Gift Certificate for line item " + lineItem.Id + " in order " + context.Order.OrderNumber +
                    " failed.", false);
                context.Errors.Add(message);
            }
            else
            {
                if (string.IsNullOrEmpty(lineItem.CustomPropGiftCardNumber))
                {
                    lineItem.CustomPropGiftCardNumber = gcNumber;
                }
                else
                {
                    lineItem.CustomPropGiftCardNumber = lineItem.CustomPropGiftCardNumber + "<br/>" + gcNumber;
                }

                context.HccApp.OrderServices.Orders.Update(context.Order);

                var gc = new GiftCard(giftCard, lineItem.CustomPropGiftCardMessage);

                if (string.IsNullOrEmpty(gc.RecipientEmail))
                {
                    gc.RecipientEmail = context.Order.UserEmail;

                    if (string.IsNullOrEmpty(gc.RecipientName))
                    {
                        gc.RecipientName = context.Order.ShippingAddress.FirstName;
                    }
                }

                try
                {
                    context.HccApp.ContentServices.SendGiftCardNotification(gc, context.Order, lineItem);
                }
                catch (Exception ex)
                {
                    context.Errors.Add(new WorkflowMessage("Exception Sending Gift Card", ex.Message + ex.StackTrace,
                                                           false));
                    var note = new OrderNote();
                    note.IsPublic = false;
                    note.Note     = "EXCEPTION: " + ex.Message + " | " + ex.StackTrace;
                    context.Order.Notes.Add(note);

                    EventLog.LogEvent(ex);
                }
            }
        }