Right now, the integration plugin I generated with Amember works beautifully, it creates the user account in my database, and deletes it if I delete it in amember. However, I would like to only have the user in my database if he has the subscription for a certain product active, but I need to do it from within Amember, without him having to access my website. I thought of using hooks or events in my plugin, but the documentation is not very clear. I have managed to create my own class and use the events I might be interested in, but how do I check the subscription? PHP: class Am_Bandit_Management extends Am_Plugin{ protected $db_bandit; const PLUGIN_STATUS = self::STATUS_PRODUCTION; const PLUGIN_REVISION = '4.5.3'; const UPLOAD_PREFIX = 'avatar'; protected $_configPrefix = 'misc.'; function init() { Am_Di::getInstance()->hook->add(Am_Event::USER_AFTER_INSERT, 'onUserAfterInsert'); Am_Di::getInstance()->hook->add(Am_Event::USER_AFTER_UPDATE, 'onUserAfterUpdate'); Am_Di::getInstance()->hook->add(Am_Event::USER_UNSUBSCRIBED_CHANGED, 'onUserUnsubscribedChanged'); Am_Di::getInstance()->hook->add(Am_Event::PAYMENT_AFTER_INSERT, 'onPaymentAfterInsert'); Am_Di::getInstance()->hook->add(Am_Event::PAYMENT_WITH_ACCESS_AFTER_INSERT, 'onPaymentWithAccessAfterInsert'); Am_Di::getInstance()->hook->add(Am_Event::SUBSCRIPTION_ADDED , 'onSubscriptionAdded'); Am_Di::getInstance()->hook->add(Am_Event::SUBSCRIPTION_DELETED , 'onSubscriptionDeleted'); Am_Di::getInstance()->hook->add(Am_Event::SUBSCRIPTION_CHANGED , 'onSubscriptionChanged'); Am_Di::getInstance()->hook->add(Am_Event::SUBSCRIPTION_UPDATED , 'onSubscriptionChanged'); Am_Di::getInstance()->hook->add(Am_Event::SUBSCRIPTION_REMOVED , 'onSubscriptionChanged'); $db_bandit = get_class($this->getDi()->db); print_r($db_bandit); } function onUserAfterInsert(Am_Event $event) // catches Am_Event::INVOICE_STARTED { $user = $event->getUser(); // User object //print_r($user); } function onUserAfterUpdate(Am_Event $event) { } function onUserUnsubscribedChanged(Am_Event $event) { } function onPaymentAfterInsert(Am_Event $event) { } function onPaymentWithAccessAfterInsert(Am_Event $event) { } function onSubscriptionAdded(Am_Event $event) { } function onSubscriptionDeleted(Am_Event $event) { } function onSubscriptionChanged(Am_Event $event) { } function onSubscriptionUpdated(Am_Event $event) { } function onSubscriptionRemoved(Am_Event $event) { }}
First you don't need to initialize hooks from your init method. This will happen automatically (by function name). In order to add user in to your database you should use onSubscriptionAdded hook. from this hook you can get user record and product which was added to user account: PHP: function onSubscriptionAdded(Am_Event $event) { $user = $event->getUser(); $product = $event->getProduct();}
I understand, it's just what I needed, once I get the user object I can check what products he has access to and execute the according action. My site is running on the same server so it will just be a matter of executing some mysql queries on another database. For removal which event do I need to care about? SUBSCRIPTION_DELETED SUBSCRIPTION_REMOVED Both or just one? Also, updated and changed to me means the same thing, it could be confusing for developers. And it the comments section of the class itself it's not more clear. Thank you
You should use SUBSCRIPTION_DELETED - it will be called when subscription to a product was deleted. SUBSCRIPTION_REMOVED - when user was removed. Also. change will be called only once for all products. updated - for each product. For example if user signup from shopping cart and has more then one product in the basket. CHANGED will have all purchased prtoduct. UPDATED will be executed for each product.
Ok, I used the events, but I have simply no way to see if my event is being executed, I tried to call die(); I tried to log to a file, the events are not being called! The plugin is activated in the Admin area, it's all fine, I tried manually subscribing a user, I tried registering manually via the front end side like a normal user would, subscribed to a product, and paid for it via paypal (sandbox, of course) the subscription is confirmed, everything is happening, but the events are not triggered! I searched all of your documentation and just can't find a way to properly debug those events, I just have to hope they are being executed? Is there a way in the system to dump a log of functions attached to those events? So I can see if at least my plugin has been hooked to those events? I attached my plugin like it is now. PHP: <?php/*** Plugin to manage BANDIT Integration* Users can manage everything from here.**/class Am_Bandit_Management extends Am_Plugin{ protected $db_bandit; const PLUGIN_STATUS = self::STATUS_PRODUCTION; const PLUGIN_REVISION = '4.5.3'; const UPLOAD_PREFIX = 'bandit'; protected $_configPrefix = 'misc.'; function init() { define('APPLICATION_ENV', 'debug'); Am_Di::getInstance()->hook->add(Am_Event::USER_UNSUBSCRIBED_CHANGED, 'onUserUnsubscribedChanged'); Am_Di::getInstance()->hook->add(Am_Event::SUBSCRIPTION_ADDED , 'onSubscriptionAdded'); Am_Di::getInstance()->hook->add(Am_Event::SUBSCRIPTION_DELETED , 'onSubscriptionDeleted'); Am_Di::getInstance()->hook->add(Am_Event::SUBSCRIPTION_CHANGED , 'onSubscriptionChanged'); } function onUserUnsubscribedChanged(Am_Event $event) { } function onSubscriptionAdded(Am_Event $event) { // $myFile = "testFile.txt"; $fh = fopen($myFile, 'a'); $stringData = "New Stuff 1\n"; fwrite($fh, $stringData); $stringData = "New Stuff 2\n"; fwrite($fh, $stringData); fclose($fh); $user = $event->getUser(); $product = $event->getProduct(); print_r($user); print_r($product); die(); } function onSubscriptionDeleted(Am_Event $event) { // $myFile = "testFile.txt"; $fh = fopen($myFile, 'a'); $stringData = "New Stuff 1\n"; fwrite($fh, $stringData); $stringData = "New Stuff 2\n"; fwrite($fh, $stringData); fclose($fh); $user = $event->getUser(); $product = $event->getProduct(); print_r($user); print_r($product); die(); } function onSubscriptionChanged(Am_Event $event) { } function onSubscriptionUpdated(Am_Event $event) { } function onSubscriptionRemoved(Am_Event $event) { $myFile = "testFile.txt"; $fh = fopen($myFile, 'a'); $stringData = "New Stuff 1\n"; fwrite($fh, $stringData); $stringData = "New Stuff 2\n"; fwrite($fh, $stringData); fclose($fh); $user = $event->getUser(); $product = $event->getProduct(); print_r($user); print_r($product); die(); }}
Where do you have that plugin file stored exactly? And what is file name? Also in order to properly debug plugin code use: PHP: $this->getDi()->errorLogTable->log("MESSAGE"); MESSAGE will be stored in error log.