Assigning a File Download Plan after subscription

6 replies [Last post]
Pedro Pablo
Offline
Joined: 12/02/2010
Status: 
Answered

Hi Leighton

Is there a way to automatically assign a File Download Plan (as if it was purchased, to be viewed in the files section of the user page) after a user purchases a specific Subscription Plan?

It seems that it could be done with Rules, but I am not very good ar rules usage, and I have got stuck when trying to define de Action to be accomplished after the condition (subscription type) is met.

Could you give me some hints about how to continue?

Thank you in advance

Leighton Whiting
Offline
Joined: 06/02/2009
I'll need to add a rules

I'll need to add a rules Action to add a file plan so that is can be possible. I'll get it out in the next release.

Sincerely,
Leighton Whiting

Pedro Pablo
Offline
Joined: 12/02/2010
Would this code make the action?

Thank you, Leighton.

Looking at the rules.inc syntax and the code of the modules, it looked like that should not be very difficult, so I've tried to figure out what the code could be in order to patch the module until the next release. Could you please have a look at it and tell me if something (or everything) is wrong?

  1. /**
  2. * Implementation of hook_rules_action_info().
  3. * @ingroup rules
  4. */
  5. function ms_files_rules_action_info() {
  6.   return array(
  7.     'ms_rules_action_add_file' => array(
  8.       'label' => t('Add a File to a User'),
  9.       'arguments' => array(
  10.         'pfid' => array('type' => 'pfid', 'label' => t('File Plan')),
  11.         'account' => array('type' => 'user', 'label' => t('User')),
  12.       ),
  13.       'new variables' => array(
  14.         'file' => array(
  15.           'type' => 'ms_files',
  16.           'label' => t('New file'),
  17.         ),
  18.       ),
  19.       'module' => 'Files Download Suite',
  20.     ),
  21.   );
  22. }
  23.  
  24. /**
  25. * Action: Add a file to a user
  26. */
  27. function ms_files_action_add_file($pfid, $account) {
  28.   return  ms_files_insert_file_user($pfid, $account->uid);
  29. }

Thank you in advance

Leighton Whiting
Offline
Joined: 06/02/2009
It looks like it should work,

It looks like it should work, have you tried it?

Indeed adding Rules Actions and Conditions is fairly simple, you just need to know the API functions and make use of them :)

Pedro Pablo
Offline
Joined: 12/02/2010
that snippet was plenty of errors...

For such a few lines of code, there was no room for more errors... :-) But you are right: it was not such a difficult thing. I guess I will start using rules much more often...

Finally I made it work with this code

  1. function ms_files_rules_action_info() {
  2.   return array(
  3.     'ms_files_action_add_file' => array(
  4.       'label' => t('Add a File to a User'),
  5.       'arguments' => array(
  6.         'pfid' => array('type' => 'pfid', 'label' => t('File Plan')),
  7.         'account' => array('type' => 'user', 'label' => t('User')),
  8.       ),
  9.       'module' => 'Files Download Suite',
  10.     ),
  11.   );
  12. }
  13.  
  14. /**
  15. * Action: Add a file to a user
  16. */
  17. function ms_files_action_add_file($pfid, $account) {
  18.   $f_plan = ms_files_load_file_plan($pfid);
  19.   return ms_files_insert_file_user($f_plan, $account->uid);
  20. }

Some interesting point is that, when the trigger is "purchase membership" if the action rule does not work properly, the payment will return an error (at least when going back to the sellers page from Paypal) which is a very good way to troubleshoot, but somehow a bit tricky for the user, as the message says "there was an error processing the payment", while payment processing went fine and the actual problem was rules handling... Hope once the site is well configured and polished, these kind of errors will be minimal, but I don't know how to approach this error handling situation. Any ideas?

Leighton Whiting
Offline
Joined: 06/02/2009
Thanks, I've added that to

Thanks, I've added that to the file and it will be in the next release.

About the payment failing if there is an error in Rules, what kind of error was rules causing? The only thing I can think of is if it crashed php or produced a fatal error, then the payment wouldn't continue to process.

Sincerely,
Leighton Whiting

Pedro Pablo
Offline
Joined: 12/02/2010
you are rigth, it was a broken php...

... the problem was that as that php was only processed during the rule action, the site was working fine, and the payment had been correctly processed. The user could see after the error message his membership status as granted, and the PayPal payment done. It looks like the action is evaluated after the payment is processed and the memebership assigned (which I think is what it should be, as memebership granted was the trigger event), but that there is no way to catch that error on the fly...

After thinking about it, I see the issue is actually something associated to the trigger event (assign a membership, something that happens after payment) because even though all other moneyscripts actions were perfectly tested, there are many more actions (provided by other modules or even custom actions) that could be used with that trigger, and while the module would have worked fine, users would think that was a payment problem just because of the error message. But don't know if there is some way to handle this (via rules?), other that a big warning when using that kind of trigger events...

Anyway, I think it is a minor problem -at least to me-, but for yourself could become nasty in the long when providing support for many different sites with enabled rules.

Regards

Twitter Feed