--- uc_points_payment.inc	2008-02-13 09:44:26.000000000 -0800
+++ uc_points_payment.inc.jhp	2008-02-13 09:46:56.000000000 -0800
@@ -66,7 +66,7 @@ function userpoints_ubercart_order($op, 
 	  //paypal specific hook
   	  ptdiscount_load($op, $arg1, $arg2);
 
-	  if ($paymethod == 'paypal_wps' && strtolower(substr(request_uri(),1,22)) == 'uc_paypal/wps/complete') {
+          if ($paymethod == 'paypal_wps' && ((stripos(request_uri(),'uc_paypal/wps/complete') !== false) || (stripos(request_uri(),'uc_paypal/ipn/') !== false))) {
 		if (variable_get(USERPOINTS_DISCOUNT,0) != 0) {
 		  	ptdiscount_submit($op, $arg1, $arg2);
 	  	}
@@ -156,13 +156,37 @@ function userpoints_ubercart_insert($ord
 
   global $user;
   $curUserId	= $user->uid;
-  $multiplier 	= (int)variable_get(USERPOINTS_UC_EARN, 0);
+  $multiplier 	= variable_get(USERPOINTS_UC_EARN, 0);
   $min 			= variable_get(USERPOINTS_MIN_PURCHASE, '0.00');
   $result 		= db_query("select sum(price) from {uc_order_products} p, {uc_orders} o where p.order_id = o.order_id and o.order_id = %d and o.uid = %d", $order->order_id, $curUserId);
   $total		= db_result($result, $row = 0);
 
+  $refUserId = 0; // default to 0 so if referral module is not installed or no referrer, we skip all referral code
+  $ref_mult = 1; // assume a multiplier of 1, unless $award_to == 2, in which case we retrieve the value below
+  $award_to     = (int) variable_get(USERPOINTS_AWARD_TO, 0);
+
+  // award_to: 0 = award points to referral
+  // award_to: 1 = award points to purchaser
+  // award_to: 2 = award points to referral and purchaser
+
+  if (module_exists('referral')) {
+    if($award_to == 0 || $award_to == 2){
+      // Look up our referrer if we have one
+      $result = db_query("select referral_uid FROM {referral} WHERE uid = %d", $curUserId);
+      if(db_num_rows($result)>0){
+        $ref = db_fetch_object($result);
+        $refUserId = $ref->referral_uid;
+        if($award_to == 2){
+          $ref_mult =  variable_get(USERPOINTS_REF_MULT,1);
+        }
+      }
+    }
+  }
+
+
   // Payment completed
-  if ($curUserId != 0) {
+  // Award points to buyer, if appropriate
+  if ($curUserId != 0 && ($award_to==1 || $award_to==2)) {
   // User id from the transaction
 
 	if ($total < $min) {
@@ -176,7 +200,7 @@ function userpoints_ubercart_insert($ord
 		'uid' => $curUserId,
 		'points' => $points,
 		'operation' => 'insert',
-		'description' => 'User made a purchase, awarding poings (Ubercart Order ID ' . $order->order_id . ')',
+		'description' => 'User made a purchase, awarding points (Ubercart Order ID ' . $order->order_id . ')',
 		'entity_id' => $order->order_id,
 		'entity_type' => 'Ubercart Transaction'
 	  );
@@ -184,6 +208,29 @@ function userpoints_ubercart_insert($ord
 	  db_query("insert into {uc_uplog} (uid, oid, points, referrer) values(%d, %d, %d, 0)",$curUserId, $order->order_id, $points);
 	}
   }
+
+  // Award referral points, if appropriate
+  if ($refUserId != 0 && ($award_to==0 || $award_to==2)) {
+	if ($total < $min) {
+	  $points = 0;
+	}
+	else
+	{
+	  $points = (int)(round($total) * $multiplier * $ref_mult);
+	  $params = array (
+		'tid' => 0,
+		'uid' => $refUserId,
+		'points' => $points,
+		'operation' => 'insert',
+		'description' => 'Purchase made by referral, awarding points to referrer (Ubercart Order ID ' . $order->order_id . ')',
+		'entity_id' => $order->order_id,
+		'entity_type' => 'Ubercart Transaction'
+	  );
+	  userpoints_userpointsapi($params);
+	  db_query("insert into {uc_uplog} (uid, oid, points, referrer) values(%d, %d, %d, %d)",$curUserId, $order->order_id, $points, $refUserId);
+	}
+  }
+
 }
 
 function userpoints_ubercart_delete($order) {
