Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Amanda Earlam 1 post 21 karma points
    Feb 01, 2012 @ 09:04
    Amanda Earlam
    0

    When should I create a new user?

    I am working on a cart checkout based on the TeaCommerce Starter kit. I have added a membership group and all the necessary configuration tags so that a user can log in, buy things and then go to a page only logged-in users can see to download the PDFs they have purchased.

    I now want to create a user automatically during the checkout process. I have created a class implementing the ITeaCommerceExtension interface and included the <profile> tags in the config file. My problem is I don't know which event to handle. I have tried "AfterOrderFinalized", which gives me a new member with the correct group. Sadly, he is not associated with his purchases, and is also not logged in.

    This is my code for the handler and the SaveMember function:

    void MembershipCreator_AfterOrderFinalized(Order order, CallbackInfo callbackInfo)
            {
                // if the user is logged in
                if (Membership.GetUser() == null)
                {
                    SaveMember(order);
                }
                Member m = Member.GetCurrentMember();
                order.MemberId = m.Id.ToString();
                order.Save();
            }
            
    
            void SaveMember(Order order)
            {
                if (string.IsNullOrEmpty(order.Email)) return;
                var chosenGroup = MemberGroup.GetByName("Customer");
                var username = order.Email;
                var password = "password";
                
                var member = Membership.CreateUser(username, password, order.Email);
                Roles.AddUserToRole(username, chosenGroup.Text);
    
                // Log them in
                FormsAuthentication.Authenticate(username, password); // not working  
            }
    A second question is, do I have to save the parameters in this handler (for example, the order)?

  • Anders Burla 2560 posts 8256 karma points
    Feb 01, 2012 @ 20:01
    Anders Burla
    0

    Hi Amanda

    Isnt the member id saved to the order? Could you check the DB and see if the order has the member id?

    One reason this will not work is that AfterOrderFinalized is properly executed from a server to server call from the payment gateway server. This means its not the users session and that is why your member isnt logged in and you cant log them in. So what you have to do is to check if the order.MemberId is != null. If thats the case then your had a member that was logged in. If no member is logged in - you can create the member and set the order.MemberId = newMemberId. Then in your confirmation page you will have to use that id and login that user. Because then you are back in the users own session. Makes sense?

    Kind regards
    Anders

Please Sign in or register to post replies

Write your reply to:

Draft