Credits System

Discussion in 'aMember Pro v.4' started by xindexer, Mar 27, 2012.

  1. xindexer

    xindexer Member

    Joined:
    Apr 17, 2011
    Messages:
    52
    How can I refresh the session data?

    Here's my problem, I have a custom variable (add field) that is changed after a payment is made (using a plugin to catch the product being purchased and then updating the variable accordingly). The problem is that the session variable needs to get updated so that I can show the change on the user dashboard.

    In V3 I just unset the $_SESSION['variable'] and it would reload it on page load. How can I force a session refresh without logging the user out and back in?

    I see a "session_needs_refresh" variable in the user object - can I use that somehow?

    Thanks
  2. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    That variable affects only third-party plugins. Currently you can set it and then add hook for onAuthSessionRefresh event.
    In that hook just refresh user's object in session. (Let me know if this require further clarification).
  3. xindexer

    xindexer Member

    Joined:
    Apr 17, 2011
    Messages:
    52
    so something like this?

    Code:
    require_once "amember/library/Am/Lite.php';
    $user = Am_Lite::getInstance()->getUser();
    $user['session_needs_refresh'] = 1;
    
    in my plugin file:

    Code:
    function onAuthSessionRefresh(Am_Event_AuthSessionRefresh $event) {
       how do I call the refresh here?
    }
    
    Also, how does the onAuthSessionRefresh get called - All I'm doing above is setting a variable.

    Thanks
  4. starwizard

    starwizard New Member

    Joined:
    Dec 26, 2011
    Messages:
    10
    I am also interested to know the answer to this.

    Thanks,
  5. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    No. I guess this is even better to update it in session when you update this variable in your plugin code:
    PHP:
    $user Am_Di::getInstance()->auth->getUser();
    $user->updateQuick('YOURFIELDNAME''YOURFIELDVALUE');
    Am_Di::getInstance()->auth->setSessionVar($user->toRow());
  6. xindexer

    xindexer Member

    Joined:
    Apr 17, 2011
    Messages:
    52
    I can just make the "setSessionVar" call after a $user->save() in my plugin to update the variables right?

    So the only thing that I really need to do here is to add the last line in my plugin:

    PHP:
    function onSubscriptionAdded(Am_Event $event) {
      
    $user $event->getUser();
      
    $product $event->getProduct();
        if(
    $product->product_id == 6)
            
    $user->credits += 500;
      if(
    $product->product_id == 7)
            
    $user->credits += 1000;
      
    $user->save();
      
    Am_Di::getInstance()->auth->setSessionVar($user->toRow());
    }
    Right?
  7. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    It could be correct if you know exactly this hook is executed always in user session. But that is not true in case if you are using system like "PayPal". In this case, payment processing happens in IPN handler, and user session cannot be updated. That it what "SESSION_NEED_REFRESH" flag used to.
    Most correct behaviour would be to query user credits count where you need it, instead of relying on session info - it is here only for quick reference, but not for assurance.
  8. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    Another problem in code is usage of subscriptionAdded hook. This hook is only executed when user orders product that he did not have earlier. For second purchase of the same product it will not work.
    I will develop and send you a copy of "credits" plugin to solve your task, OK?
  9. xindexer

    xindexer Member

    Joined:
    Apr 17, 2011
    Messages:
    52
    I see your point and I am using Paypal.

    Yes, I would like to get this one fixed.

    Thanks
  10. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    Plugin file attached.

    Configure plugin and it will add field "Credits Count" into product settings in aMember CP.

    The plugin creates am_credit table where it stores journal of credits and debits operations.
    - credit records will be added by aMember automatically.
    - debit records you must add via API
    - you can check user balance via API
    - you can optionally add more credits to user via API;

    usage examples:
    PHP:
    <?php
    require_once '/home/me/public_html/amember/bootstrap.php'// include aMember API stack
    $plugin Am_Di::getInstance()->plugins_misc->loadGet('credits');

    $balance $plugin->balance(); // get balance for current customer
    echo "balance=$balance <br />\n";
    $plugin->credit(2"Manually added 2 credits"); // add 2 credits to customer manually
    $plugin->debit(1"Debited 1 credit"); // deduct 1 credit from user balance
    echo "balance=$balance <br />\n"// must be +1 to previous

    If you just want to display customer balance on many pages, implement kind of caching in your code, including entire aMember stack and running db queries is not cheap from performance point of view.

    Attached Files:

  11. xindexer

    xindexer Member

    Joined:
    Apr 17, 2011
    Messages:
    52
    WOW - that is a very comprehensive solution.

    Thank you

Share This Page