Exemple #1
0
 private bool TryUpdateSubscription(MailchimpSubscriptionPart part)
 {
     // When a User is Created a Published is improperly called to early and the part is not Updated
     // So I skip this step
     if (part.Subscription.Audience == null)
     {
         return(false);
     }
     if (part.As <UserPart>() != null && part.As <UserPart>().EmailStatus != UserStatus.Approved)
     {
         // A registered user with an EmailStatus not Approved should not Update Mailchimp member
         return(false);
     }
     if (!part.IsPublished())
     {
         return(false);
     }
     // check if subscriptions is different from orchard to mailchimp
     // if changed it fires an update over Mailchimp servers
     if (part.Subscription.Subscribed != _apiService.IsUserRegistered(part) || part.As <UserPart>() == null)
     {
         var settings = part.Settings.GetModel <MailchimpSubscriptionPartSettings>();
         if (!_apiService.TryUpdateSubscription(part))
         {
             if (settings.NotifySubscriptionResult || AdminFilter.IsApplied(_workContext.GetContext().HttpContext.Request.RequestContext))
             {
                 _notifier.Error(T("Oops! We have experienced a problem during your email subscription. Please, retry later."));
             }
             return(false);
         }
         else
         {
             if (settings.NotifySubscriptionResult || AdminFilter.IsApplied(_workContext.GetContext().HttpContext.Request.RequestContext))
             {
                 if (part.Subscription.Subscribed)
                 {
                     _notifier.Information(T("Nice to meet you! Your subscription has been accepted."));
                 }
                 else
                 {
                     _notifier.Information(T("You have been unsubscribed."));
                 }
             }
             return(true);
         }
     }
     else
     {
         return(false);
     }
 }
Exemple #2
0
        public bool TryUpdateSubscription(MailchimpSubscriptionPart part, bool isUserCreation = false)
        {
            var sub = part.Subscription;

            if (sub.Audience == null || string.IsNullOrWhiteSpace(sub.Audience.Identifier))
            {
                return(false);
            }

            var settings    = part.Settings.GetModel <MailchimpSubscriptionPartSettings>();
            var putPayload  = _tokenizer.Replace(settings.PutPayload, new { Content = part.ContentItem });
            var memberEmail = _tokenizer.Replace(settings.MemberEmail, new { Content = part.ContentItem });

            var syncronized = false;
            var urlTokens   = new Dictionary <string, string> {
                { "{list-id}", sub.Audience.Identifier },
                { "{member-id}", _mailchimpService.ComputeSubscriberHash(memberEmail) }
            };
            string result     = "";
            var    urlApiCall = CalculateUrlByType(RequestTypes.Member, urlTokens);

            if (sub.Subscribed)
            {
                // register member
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                JObject body = JObject.Parse(putPayload ?? "{}");
                syncronized = TryApiCall(HttpVerbs.Put, urlApiCall, body, ErrorHandlerDefault, ref result);
                if (isUserCreation)
                {
                    _workflowManager.TriggerEvent("UserCreatedOnMailchimp",
                                                  part,
                                                  () => new Dictionary <string, object> {
                        { "Syncronized", syncronized },
                        { "APICallEdit", new APICallEdit {
                              Url         = urlApiCall,
                              RequestType = RequestTypes.Member.ToString(),
                              HttpVerb    = HttpVerbs.Put.ToString(),
                              Payload     = putPayload
                          } },
                        { "Email", part.As <UserPart>() == null ? body["email_address"].ToString() :  part.As <UserPart>().Email }
                    });
                }
                else
                {
                    _workflowManager.TriggerEvent("UserUpdatedOnMailchimp",
                                                  part,
                                                  () => new Dictionary <string, object> {
                        { "Syncronized", syncronized },
                        { "APICallEdit", new APICallEdit {
                              Url         = urlApiCall,
                              RequestType = RequestTypes.Member.ToString(),
                              HttpVerb    = HttpVerbs.Put.ToString(),
                              Payload     = putPayload
                          } },
                        { "Email", part.As <UserPart>() == null ? body["email_address"].ToString() :  part.As <UserPart>().Email }
                    });
                }
                IsNewSubscription = syncronized;
                _workContextAccessor.GetContext().HttpContext.Items["newsletterSubscription"] = syncronized;
            }
            else
            {
                // delete the member only if they are being edited
                if (!isUserCreation)
                {
                    syncronized = TryApiCall(HttpVerbs.Delete, urlApiCall, null, ErrorHandlerDelete, ref result);
                    _workflowManager.TriggerEvent("UserDeletedOnMailchimp",
                                                  part,
                                                  () => new Dictionary <string, object> {
                        { "Syncronized", syncronized },
                        { "APICallEdit", new APICallEdit {
                              Url         = urlApiCall,
                              RequestType = RequestTypes.Member.ToString(),
                              HttpVerb    = HttpVerbs.Delete.ToString(),
                              Payload     = putPayload
                          } },
                        { "Email", part.As <UserPart>().Email }
                    });
                }
                else
                {
                    // return operation successfully executed for the next step
                    syncronized = true;
                }
            }
            return(syncronized);
        }