Userpoints Integration

Contrib type: 
Module
Status: 
Uploaded for testing
Compatibility: 
Ubercart Alpha 8

This module integrates userpoints with ubercart for payment and reward when making nonpoint purchases.

Alpha 2 makes a minor change to what causes the payment method to be shown.
Alpha 3 fixes some changes (or errors on my part) in the hook_order api that caused some problems
Version 1.5 (latest upload) combines the previous feature requests of a discount method and referrers per the discussion on here. This version needs lots of testing and I would someone to just kinda verify everything is working correctly. I think I would also recommend that you uninstall the previous version and then install this one due to some database changes (I learned how to tweak that so the next install will work better after I had changed everything already)(this is the same as the cvs right now, when they change i will remove these.)

This module is now on the drupal cvs system. I will be posting all releases there.
it can be found at http://drupal.org/project/userpoints_ubercart

Thanks, feedback is always welcome.

Related threads: 

Re: Userpoints Integration

Seems to be interesting, I will install a sandbox to test that. Thank you very much for your contrib !

Re: Re: Userpoints Integration

I tester your module a little, it's a very good begining.

However I have an error on the checkout review page :

<?php
user warning
: Table 'XXX.uc_orders' doesn't exist query: uc_payment_method_points select order_total from uc_orders where order_id = 345 in /html/includes/database.mysqli.inc on line 151.
?>

In addition, I have some suggestion for future improvement.

  • The possibility to use point to make a discount, at the moment, you have to pay the total order with your points. It should be good to use point to make a discount and pay the rest with a normal payment method
  • I will post here if I have others ideas, but the first one is very good Eye-wink

Re: Re: Re: Userpoints Integration

what version of ubercart and userpoints are you using?

I would like to allow discounts with the module in the future. At present I didn't have the time to build something like that into the module. I am just not sure how to go about that yet. I am open to suggestions on how to do that, my big problem is that you would need to execute two payments to do that and have it be fully paid for, if i could solve that i'd be ok...

Re: Re: Re: Re: Userpoints Integration

I think the best way it to not consider points as a payment method, but as a discount system.

In a perfect world, it should create a new checkout pane in which user see his point and write how many points he want to spend for his order.

The pane should show to the user the value of a point in money.

When the user choose the number of point it want to put in his order, he have a button "apply" and it apply the discount in line item and in the order.

In administration setting, you shoule be able to choose the maximum of point an user can spend in an order (several options : In money value, in money percentage, in point number, in point percentage).

And you should be able to choose a minimum amount of order for which point discount apply (it's already the case if I remember).

I'm improving the discount coupon system, I will make it compatible with the checkout page and line item system. If you don't know how to do that, you could look through my code to make it, I think there are not lot of differences, your conditions are just more complex to apply the discount and you have to provide more administration settings.

I will edit my post when I will be able to post the discount coupon system.

thank you for your module and your feedback, I think it really could improve the marketing system... I'm really enjoying to use it one day Eye-wink

Re: Re: Re: Re: Re: Userpoints Integration

can you post a link to your code so i could take a look, I will be busy with end of the semester finals and projects for the next two weeks but after that I like the idea you have. I never thought of running it in as a line item discount, but that seems like it would be an easy way to incorporate the ability to use part of your points or only pay a portion of the order.

Let's see the Improved

Let's see the Improved coupon module that give you ideas to make discount system on checkout by checking user points.

Re: Let's see the Improved

I will try and look at this next week after finals are over.

Re: Re: Let's see the Improved

I am going to start looking at this part of it, but just to confirm you know that you need to submit to paypal as a single item since this will be a line item discount? If so can you confirm that that part (paypal + points) is working ok before I go ahead and make these changes, i seem to be having some minor issues on my site when going to the sandbox

discount option

I do not think I will be implementing this at this time. It is a nice idea but I am not sure if it is appropriate. I would like more feedback from the community and their thoughts before something like this get incorporated.

Hi there, I'm not sure to

Hi there,

I'm not sure to understand what do you mean above, but I suggested to use point as a discount instead of a payment method because it allow to use points and pay with another payment system. That is could be very more extensible because you can just use few points to have a discount, you don't have to have the full number of points to pay the total order, which is a very special case and lot of store owner don't want to allow.

If you have some questions or want some help to make it, I will send you private message with my msn to contact me.

3.x API and Userpoints Contrib

Hi

There is a new and improved API (3.x) for Userpoints that has a lot of new features.

There is a development release tarball already here http://drupal.org/project/userpoints and a stable release is around the corner.

Another thing is that there is now a plethora of contribs for Userpoints in a separate project here http://drupal.org/project/userpoints_contrib (including ecommerce). So it is a good idea to create an issue and add your stable version of ubercart integration there, so it is included. Please specify if it is API 2.x or API 3.x so it is committed to the appropriate branch.
--
Drupal performance tuning, development, customization and consulting: 2bits.com
Personal blog: Baheyeldin.com

Hi there, Thank you for your

Hi there,

Thank you for your maintenance, I will try the last version.

I try to update from the last alpha and I get an error when updating the database

<?php
user warning
: Table 'KALYS_SANDBOX.dp_uc_uplog' doesn't exist query: update_sql ALTER TABLE dp_uc_uplog ADD discount INT(4) NOT NULL DEFAULT '0' AFTER referrer in /includes/database.mysqli.inc on line 151.
?>

With a uninstall of the module before install the new one, it works, so I think it's just an error in your update function of the .install file.

Re: Hi there,

I've tested it and everything seems to work great so far! I did get a blank page when trying to add a new Category term to the Userpoints Taxonomy but I haven't narrowed that down to see if it was caused by your module or something else. Will investigate soon.

One request I have, which I think would be amazing, is to add a new Product Feature with this Contrib - that would be a "Number of Points to purchase". For instance on our site, I'd like to provide a $10 product that, when purchased, adds 15 Userpoints to the person's account. This is something I could probably write quickly unless you are already looking to address it.

Many major kudos! This is an awesome module so far! Let me know if I can help in any way. (We'll be using this on our site within the next few months, so I'll be testing heavily until then.)

Since this is a feature I needed, I'm editing my post with the code I'm writing so far. It's quick and dirty at the moment, and I still need to do the "award the user their points after purchase" part, but I thought I'd throw up what I have so far. bmagistro, let me know if this is something I can continue doing or if I should make it another Contrib (but that seems wasteful to me ..).

<?php
/**
* Implementation of hook_product_feature().
*/
function userpoints_ubercart_product_feature() {
 
$features[] = array(
   
'id' => 'userpoints',
   
'title' => t('Userpoints'),
   
'callback' => 'userpoints_ubercart_feature_form',
       
'delete' => 'userpoints_ubercart_feature_delete',
  
'settings' => 'userpoints_ubercart_feature_settings',
  );
   
    return
$features;
}

/**
* Build the userpoints feature form
*/
function userpoints_ubercart_feature_form($node, $feature) {
   
   
$form['nid'] = array(
       
'#type' => 'value',
       
'#value' => $node->nid,
       
    );
   
$form['points'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Number of Userpoints'),       
       
'#description' => t('How many userpoints to give when this product is purchased.'),
       
'#default_values' => $points,
    );
    return
uc_product_feature_form($form);
}


function
userpoints_ubercart_feature_form_validate($form_id, $form_values) {
   
// Not validating anything yet
}


/**
* Submit the form
*/
function userpoints_ubercart_feature_form_submit($form_id, $form_values) {

    if (empty(
$form_values['pfid'])) {
       
$pfid = db_next_id('{uc_product_features}_pfid') + 1;
    }
    else {
       
$pfid = $form_values['pfid'];
       
    }

   
$data = array(
       
'pfid' => $pfid,
       
'nid' => $form_values['nid'],
       
'fid' => 'file',
       
'description' => 'Product is now worth '.$form_values['points'].' points.',
    );
   
   
db_query("INSERT INTO {uc_product_features} (pfid, nid, fid, description) VALUES (%d, %d, '%s', '%s')",$pfid,$form_values['nid'],'userpoints',$data['description']);
   
db_query("INSERT INTO {uc_userpoints_products} (pfid, nid, points) VALUES (%d, %d, %d)",$pfid,$form_values['nid'],$form_values['points']);   
   
  return
uc_product_feature_save($data);
}


/**
* Feature settings
*/
function userpoints_ubercart_feature_settings() {
   
}


/**
* product_feature delete function
*/
function userpoints_ubercart_feature_delete($feature) {
   
db_query("DELETE FROM {uc_userpoints_products} WHERE pfid = %d",$feature['pfid']);
}
?>

You'll notice I created a new Table to keep track of the features<->nodes relationship..

<?php
CREATE TABLE
`uc_userpoints_products` (
  `
upid` int(11) NOT NULL auto_increment,
  `
pfid` mediumint(9) NOT NULL default '0',
  `
nid` mediumint(9) NOT NULL default '0',
  `
points` mediumint(9) NOT NULL default '0',
 
PRIMARY KEY  (`upid`)
)
ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
?>

More to come as I complete it ... (unless someone else does first!)

EDIT: Here's what I added to userpoints_ubercart_order (in uc_points_payment.inc):

<?php
if ($arg2 == 'completed') {
       
            foreach (
$order->products as $product) {
               
$node = node_load($product->nid);
               
$points = db_result(db_query("SELECT points FROM {uc_userpoints_products} WHERE `nid`=%d", $product->nid));
               
//drupal_set_message('<pre>'.print_r($item, TRUE).'</pre>');
                   
               
if ($points > 0) {
                   
drupal_set_message($product->title.' has '.$points.' Points attached'); 
                   
$totalPoints .= $points;
                }    
                                                   
            }   
                   
        }
       
       
userpoints_userpointsapi(array(
         
'uid' => $order->uid,
         
'points' => $totalPoints,
        ));
       
       
uc_order_comment_save($order->order_id, 0, t('User earned '.$totalPoints.' Points!', 'admin'));
?>

I'm sure there's a more elegant solution but this seems to work for now. More testing tomorrow...

Re: Userpoints Integration

that was not something i was planning on doing, but i wouldn't mind merging the code with this one if that would help?

please let me know if you find any problems, glad to hear you like it.

Re: Re: Userpoints Integration

Awesome, glad my work wasn't in vain. I think it's a useful feature, kind of like a gift certificate; but in our case we'll be offering a Subscription Role product, that when purchased gives the customers x number of "Points" as well.

I'll update my code or post back here if I have any updates, I think my code could be a little bit leaner.

Re: Re: Re: Userpoints Integration

just to give you a heads up i will try putting all of your code in its own include file and then link that back in. if you want to try and do that now, that would be great, if not don't worry about it. i'll take care of it later on.

thanks for the contribution.

Re: Re: Re: Re: Userpoints Integration

Okay, I think the one other thing I'll add is an optional block that would show a user how many points they have available. Other than that I think it's good to go.

I'm an idiot, of course there is already a block for this. Nevermind Smiling

Keep in mind that I had to add content to your implementation of hook_order, so I'm not sure how much of it you can use as included content to avoid name conflicts.

Re: Re: Re: Re: Re: Userpoints Integration

Hmm, that was a trick that im not sure i pulled off well. you can look at what i did with the discount method...i don't think my solution was the cleanest, but you're welcome to look at it and comment.

basically what i did was make functions on the include file and then called that function from the primary hook_order().

points awarded for buying (multiplied by price) -- sell price?

firstly, thanks for the great contrib, bmagistro!

I believe that the multiplied price in Points Settings means product "Sell Price".

How to change "Sell Price" to "Total Price"? I want customer got 1 point whenever they spent 1 dollar.

Thanks a lot!

Re: points awarded for buying (multiplied by price) -- sell pric

you would need to edit the code, but without looking at my code again I could not tell you which line to change. I did not do it on total price because I did not want to give people points for tax and shipping.

Re: Re: points awarded for buying (multiplied by price) -- sell

I understand your point. however, now even customers buy 3 pieces of same item, they get 1 piece of item points only.

when trying to change the ubercart options, always get the following errors:

warning: Division by zero in /var/www/xxx.com/htdocs/sites/all/modules/ubercart/contrib/userpoints_ubercart/uc_points_payment.inc on line 13.
warning: Division by zero in /var/www/xxx.com/htdocs/sites/all/modules/ubercart/contrib/userpoints_ubercart/uc_points_discount.inc on line 33.

Re: Re: Re: points awarded for buying (multiplied by price) -- s

I will try and look at it this week, can you open a issue for this on drupal just so that I can reference it?

On line 161 of uc_points_payment it doesn't multiply by the quantity. I will be posting a fix to the dev version in the next few minutes (i think) if not by 3/17 5:00 est

Nice job! I'd love to see

Nice job!

I'd love to see the discount option added to this module.

My main question is: is this module for Ubercart different than the Userpoints module on drupal.org? Or have they been integrated with this module?

Personally I think the Userpoints module on the Drupal site is much more complete and well-rounded, only missing a few features like Ubercart integration and being able to give discounts for a certain amount of points.

http://drupal.org/project/userpoints

It'd only make sense if your efforts were combined! Smiling

Re: Nice job! I'd love to see

uc_userpoints is for integration with the Userpoints module. You need Userpoints to be able to use uc_userpoints.

Re: Re: Nice job! I'd love to see

Yay thank you!

Great Contrib, couple problems

I really like this conribution, great work.

I've found a few little issues.

Firstly I'm counting 1 point as $0.01 so at first I was setting the 'Ubercash used in payment (for every dollar):' to be 0.01, this produced a divide by zero error.

Then i realised that i actually needed to set it to 100, which gets rid of the divide by zero error but now when i spend points for say £1.50 it deducts 200 points. I think this is because it's doing the rounding before it's multiplying by my exchange rate of 100.

The big problem i'm having is that if I don't have enough points then i can still buy stuff and I end up with -100 points, or however many i'm over. That is to say that it's not stopping me making my purchase because i have too ffew points.

________________________________________________

torgosPizza: I like the sound of your Buying Points Feature very much. Has your code been integrated with the main userpoints_ubercart module yet? I would very much like to be able to sell credit in the form of points. eg you buy a product for $10 and you get 1000 points. This is because I'll be selling node access in lots of little bits and don't want people to paypal me lots of tiny payments.

_______________________________________________

Now all i have to do is figure out a better way to restrict node access, roles and TAC Lite is a bit long winded.

Cheers,

matt

Re: Re: Hi there,

this is way cool! exactly what i was looking for. but i need some help. im getting this error:

    * warning: Invalid argument supplied for foreach() in ../includes/common.inc on line 744.
    * warning: strtr() [function.strtr]: The second argument is not an array. in ../includes/common.inc on line 759.

everytime i complete a transaction. it's probably because i inserted the code for userpoints_ubercart_order inside the submit case of the function. is this correct?

also, i think it's these lines causing the error:

userpoints_userpointsapi(array('uid' => $order->uid,'points' => $totalPoints,));
userpoints_userpointsapi($params);

because the moment i commented them out, the error stopped showing. of course, the points weren't awarded either.

im sure this is a really stupid error on my part.. but im lost. Sad any help would be appreciated.

thanks!
-- sarah

[edit - 13.43p the next day]

okay, i found the cause of the error. it was stupid. there was a typo in the code.
this:
uc_order_comment_save($order->order_id, 0, t('User earned '.$totalPoints.' Points!', 'admin'));
should be:
uc_order_comment_save($order->order_id, 0, t('User earned '.$totalPoints.' Points!'), 'admin');

after i fixed that up, it was okay. no more errors.

for the sake of whoever else, this was what worked for me. i edited the code given above to this: (line 54 of uc_points_payment.inc)

  case 'submit':
    // fires when the order is submitted and adds/subtracts thier points
    if ($paymethod == 'points') {
      userpoints_ubercart_payment($order);
    } else {
      // for points assigning - by torgosPizza
      // commented out the $arg2 line as it was not being satisfied. could be a problem on my placement? - sarah20080417
      //if ($arg2 == 'completed') {
        foreach ($order->products as $product) {
          //print_r($product);
          $node = node_load($product->nid);
          //print_r($node);
          $qty = $product->qty;
          $points = db_result(db_query("SELECT points FROM {uc_userpoints_products} WHERE nid=%d", $product->nid));
          //drupal_set_message('<pre>'.print_r($item, TRUE).'</pre>');
          if ($points > 0) {
            //drupal_set_message($product->title.' has '.$points.' Points attached');
            $totalPoints .= ($points * $qty);
          }
        }
      //}
      $params = array(
          'points' => $totalPoints,
          'uid' => $product->uid,
          'operation' => 'insert',
          'description' => 'User made a purchase of points (Order ID ' . $order->order_id . ')',
          'entity_id' => $order->order_id,
          'entity_type' => 'Ubercart Transaction'
      );
      userpoints_userpointsapi($params);
      //uc_order_comment_save($order->order_id, 0, t('User earned '.$totalPoints.' Points!'), 'admin');
      uc_order_comment_save($order->order_id, 0, t('User purchased '.$totalPoints.' Credits!'), 'admin');
      // end points assigning - by torgosPizza

      userpoints_ubercart_insert($order);
      if (variable_get(USERPOINTS_DISCOUNT,0) != 0) {
          ptdiscount_submit($op, $arg1, $arg2);
      }
    }
    break;

Oh, and an observation on roles & userpoints (kinda OT), i noticed that whenever i assign points to a category (using userpoints taxonomy) it doesn't work with the role assigning contribution of userpoints. i haven't been able to figure out why, but in any case, i decided i didn't need userpoints taxonomy, so i just kept the points 'uncategorized' and the roles started being assigned. Smiling

Re: Userpoints Integration

there is a new dev version out there on drupal. It still need some testing and documentation but finishes and fixes a few things. It also changes to workflow for awarding points....patch by korvus.

The ability to purchase points is here :)

I have uploaded a patch to the UC_Userpoints Issue Queue, but here is the version including the patch should you not want to go patching things (or in case my patches don't work...).

This new version adds a Product Feature for "awarding" points based on a purchase. So for instance, if you want to entice users into Subscribing, you can add Points to the Subscription product which are awarded upon order Completion. It also honors the qty of the product in the cart; so if a user wants to buy 2x a monthly subscription, it gives them 2x the number of points that the product is worth.

Tested on Ubercart RC5, so I'm sure it works on 1.0 as well. I'll be checking that out later this week to be sure, but I don't expect any issues.

AttachmentSize
userpoints_ubercart-5.x-dev.tar.gz12.99 KB

Re: The ability to purchase points is here :)

Many thanks to torgosPizza. I have updated the cvs copy on drupal with this and several other fixes. I am currently working on a couple other items that have been requested. I hope to have one of them completed today still.

OOOooo

Great stuff, thanks for that torgosPizza.

I'll have a play around with this at some point soon.

Can users use their points to buy products, or as part payment? I tried to set it all up once before but it seemed a little buggy.

cheers for the update,

matt

Re: OOOooo

Sorry I didn't see this. Yeah, you can use Points as a Discount, but you can't use Points to buy another product containing Points. (I considered making this a configurable option... but I think it would not make sense in most situations.)

So points can be used to by products (except those with points) or as a discount, which can be specified. There are settings to accommodate this configuration under Userpoints config.

Purchasing userpoints?

I'm still figuring out what modules will be needed for my new Drupal website.

Can points be purchased for cash through ubercart using this module? And are there more than one kind of point available?

One to use for currency, one to use for ranking for instance.

Hope this was the right place to ask!

Thanks

Re: Purchasing userpoints?

Yes, using the dev version of the module you can attach a certain number of Userpoints to a product, so that a product can be "worth" those points. For instance you could purchase 1000 points for $10.00 or whatever you choose for your exchange rate.

Right now though there is only one type of points using the Userpoints module. If you wanted to rank someone using points there would probably need to be another module or another set of points. Definitely an interesting concept but that is more of a limitation of the Userpoints module at the moment.

Fatal error:

Fatal error: Call to undefined function intvar() in /Applications/MAMP/htdocs/mysite/sites/all/modules/userpoints_ubercart/uc_points_payment.inc on line 13

Newly upgraded site: Drupal 5.7, Ubercart 5.x-1.0, Userpoints-workflow-integration 5.x-3.7, Workflow-ng 5.x-2.1, Userpoints 5.x-3.7, Userpoints Ubercart 5.x-1.8.

when clicking 'Checkout' from cart.

Am I using the correct versions of everything? Is this the right place to post this?

Re: Fatal error:

Ooof... not sure how that happened. Change it to intval (instead of "intvar").

Also I've heard that the issue hadn't gotten fixed yet where the balance is not being properly checked. It's something I'll be looking into this weekend... but that change should fix it for the time being. I think. Smiling

re: Fatal Error - Phantom Intvar()

Have searched through every document in your module and in none of them does the string 'intvar' appear. Lots of 'intvals, but no 'intvars'. Cleared all caches. Still getting the fatal error. Are there more compatible modules I could be using it with? (July 3rd, 2008 release 5.x-1.9)

Just downloaded and installed the 5.x-1.x-dev and it doesn't have the same problems. What's the difference in functionality? What's stable in the non-dev release that isn't in the dev?

It's great, btw. I love being able to pay with userpoints exclusively. Have a great weekend!

re: Fatal Error - You fixed it!!!!!

Just found the July 4th release and it's awesome!

Re: re: Fatal Error - You fixed it!!!!!

Glad it is working for you Smiling I have some todo's that I'm meaning to address with some of those new features, just haven't been gotten around to them. Maybe I can be made a co-maintainer on the module Smiling

re: re: re: Fatal Error

@newbstah
There should be no differences between the 1.9 release and the 1.x-dev so to hear that the 1.9 is not matching the 1.x-dev is something i will need to look at in the morning. Perhaps the packaging scripts are running at different times...or maybe I just need to learn a few more things about using a CVS.

@torogsPizza
I would love the help and you have already contributed a great deal. Please contact me if you are still interested.

Granular control over moderation?

I posted this on Drupal.org in the userpoints module, but now I am thinking that perhaps this could be a function of the userpoints_ubercart module.

Is it possible to have some points automatically approved while having others require moderation? I would like to use user points for discounts on an Ubercart site (which seems to work). The problem I have is that I need to moderate the points for posting comments etc. but I need the points to be deducted from a user as soon as they use them as a discount. Currently a user could use their points again and again until the deduction has been approved.

Any Ideas?

Re: Granular control over moderation?

that is a setting that can be enabled. Can you open an issue on the drupal site for userpoints_ubercart just so that I have a way of tracking changes and if the worked or not? I should be able to get this one added some time this week.

Re: Re: Granular control over moderation?

I have worked through the past week and have gotten userpoints slit into multiple modules (done mostly for administrative reasons) but have also added moderation settings for all actions. Please see the notes on the drupal.org site. This is only available under the 2.x release. I am still doing some testing on it but it should be safe to run.