First of all, sorry for my english.

Hi everybody, I have been looking into the code of ubercart marketplace and ubercart, and finally I made a good copy paste (and editing some things) and solved the problem of the masspay made by the admin instead of done automatically.

Here's the file i modified: "uc_paypal.pages.inc" (I converted it into .txt in order I can attach it to this post)

Explanation:
When a user makes an order and pays with PayPal or does ExpressCheckout with PayPal and the system redirects him to "checkout/complete" the added code starts to work and makes masspay for all the orders done before that one. (Newest order not included)

Problems:
I did it for my webpage, I only use PayPal, and so I avoided all the verifications for the manual payment.

Things I will change:
I want to include also the latest order and maybe put the code in another place.

Lines in the new file that are new:
551 to 606
623 to the end of the document.

Version of the modules I used:
Drupal 6.17
Ubercart: 6.x-2.4
Ubercart Market-place:6.x-1.0-beta1

File:
The file is in "modules/ubercart/cart/"
I used the code from ubercart-marketplace.

I hope this will help somebody, I will report the changes I'm going to make.

CommentFileSizeAuthor
#1 uc_cart.pages_.txt25.52 KBrafinskipg
uc_paypal.pages_.txt17.63 KBrafinskipg

Comments

rafinskipg’s picture

StatusFileSize
new25.52 KB

THE CORRECT FILE HERE!!

Somebody can help me, please?!

I don't understand drupal's execution very well, so i can't imagine what the hell is happening.
Where is going when finishing the execution of the function uc_cart_checkout_complete() {...} in the uc_cart-pages.inc ???

I need to call again the function, but i don't know where, i can't call it in the uc_order.ca.inc...

And Sorry I attached a bad file: This is the good one: UC_CART.PAGES.INC

-- I'll continue editing things

rafinskipg’s picture

If somebody could help me telling me were to put that code instead of "uc_cart.." I'll be very gratefull
(I don't know if it is said like this)

The FUNCTION is called : make_masspayment_customized();

function make_masspayment_customized(){
          $payment_id = db_result(db_query("SELECT MAX(payment_id) FROM {mp_seller_payments}"));
         
         $formu= mp_reports_seller_form();
        $sellers = array();
        foreach ($formu['sellers'] as  $seller) {
         $sid = $seller['uid']['value'];
            $payment_id++;
           
              if (strlen($seller['email']['value']) > 4) {
                $sellers[$sid] = array('email' => $seller['email']['value'], 'commission' => $seller['commission']['value'], 'uid' => $seller['uid']['value'], 'order_list' => $seller['order_list']['value'], 'payment_id' => $payment_id);
              }
           
          
        }
       
       if (sizeof($sellers) != 0) {
          if (module_exists('uc_paypal')) {
           $nvp_request = array(
          'METHOD' => 'MassPay',
          'RECEIVERTYPE' => 'EmailAddress',
        );
        $number = 0;
            
        foreach ($sellers as $seller) {
          $nvp_request += array('L_EMAIL'. $number => $seller['email']);
          $nvp_request += array('L_AMT'. $number => uc_currency_format($seller['commission'], FALSE)); 
          $nvp_request += array('L_UNIQUEID'. $number => $seller['payment_id']);
          $number++;
        }
            
        $nvp_request += array('CURRENCYCODE' => variable_get('uc_paypal_wpp_currency', 'USD'));
        $nvp_request += array('EMAILSUBJECT' => variable_get('mp_reports_masspay_subject', 'Payment for your sales.'));
        
        $nvp_response = uc_paypal_api_request($nvp_request, variable_get('uc_paypal_wpp_server', 'https://api-3t.sandbox.paypal.com/nvp'));
        if ($nvp_response['ACK'] == 'Success') {
          watchdog('mp_reports', 'Masspay responds success.');
          foreach ($sellers as $seller) {
            db_query("INSERT INTO {mp_seller_payments} (uid, email, method) VALUES (%d, '%s', '%s')", $seller['uid'], $seller['email'], t('Paypal'));
            foreach (explode(',', $seller['order_list']) as $order_id) {
              db_query("UPDATE {mp_seller_order_statuses} SET payment_id = %d, seller_paid_status = 1 WHERE uid = %d AND order_id = %d", $seller['payment_id'], $seller['uid'], $order_id);
            }
          }
        }
        else {
          watchdog('mp_reports', 'Failed to make MassPay NVP request.' 
            . 'Error Code: ' . $nvp_response['L_ERRORCODE0'] . ' '
            . 'Error Message: ' . $nvp_response['L_LONGMESSAGE0']);
        }
          }
          else {
            drupal_set_message(t('Please enable uc_paypal module and configure API credentials.'), 'error');
          }
        }
           $_SESSION['payment_sellers'] = $sellers;
          
}
    
    
    /**
 * Display the seller reports in Ubercart admin interface
 */
function mp_reports_seller_form() {
  $timezone_offset = variable_get('date_default_timezone', 0);
  $format = variable_get('uc_date_format_default', 'm/d/Y');

  $args = mp_reports_get_args();

  // Put the order statuses into a SQL friendly array.
  $order_statuses = "('complete')";

  // Get array of all sellers.
  $sellers = array();
  if ($args['rid'] == 2) {
    $result = db_query('SELECT uid FROM {users} WHERE uid <> 0');
    while ($seller = db_fetch_array($result)) {
      $sellers[] = $seller['uid'];
    }
  }
  else {
    $result = db_query("SELECT u.uid FROM {users_roles} AS u WHERE u.rid = %d", $args['rid']);
    while ($seller = db_fetch_array($result)) {
      $sellers[] = $seller['uid'];
    }
    $sellers[] = 1;
  }

  $methods = array();
  if (variable_get('mp_reports_display_email', TRUE)) {
    $methods[] = t('Paypal');
  }
  if (variable_get('mp_reports_enable_check', FALSE)) {
    $methods[] = t('Manual');
  }
  $methods[99] = t('not filled');
  if ($args['paid'] == 1) {
    $paid_statuses = "('0', '1', '2', '3', '4', '5')";
  }
  else {
    $paid_statuses = "('0', '3', '4')";
  }
    
  $commission_total = 0;
  $revenue_total = 0;
  $product_total = 0;
  $order_total = 0;

  // create form
  $form = array();
  $form['#tree'] = TRUE;
  $form['sellers'] = array();

  // Loop through the sellers and build the report table/form.
  foreach ($sellers as $seller) {
    $product_data = '';
    $order_data = '';
    $order_list = '';

    // Create the seller title and link for each seller.
    $user = user_load(array('uid' => $seller));
    $name = $user->name;
    $link = l($name, 'user/'. $seller);
    if ($user->payment_method == NULL) {
      $user->payment_method = 99;
    }

    // Build the order data for the seller.
    $result = db_query("SELECT sos.order_id FROM {uc_orders} AS o LEFT JOIN {mp_seller_order_statuses} AS sos ON o.order_id = sos.order_id WHERE sos.uid = %d AND %d <= o.created AND o.created <= %d AND sos.order_status IN $order_statuses AND sos.seller_paid_status IN $paid_statuses", $seller, $args['start_date'], $args['end_date']);
    while ($order = db_fetch_object($result)) {
      $order_data++;
      $order_list .= $order->order_id .',';
    }
    $order_list = substr($order_list, 0, strlen($order_list) - 1);
    $order_total += $order_data;

    
    
      // Otherwise just display the total number of products sold.
      $product_data = db_result(db_query("SELECT SUM(op.qty) FROM {uc_orders} as o LEFT JOIN {uc_order_products} as op ON o.order_id = op.order_id LEFT JOIN {mp_seller_order_statuses} AS sos ON sos.order_id = o.order_id LEFT JOIN {node} as n ON n.nid = op.nid WHERE sos.uid = %d AND n.uid = %d AND sos.order_status IN $order_statuses AND sos.seller_paid_status IN $paid_statuses AND %d <= o.created AND o.created <= %d", $seller, $seller, $args['start_date'], $args['end_date']));
      $product_total += $product_data;
    

    $counts = db_fetch_array(db_query("SELECT SUM(p.cost * p.qty) AS cost, SUM(p.price * p.qty) AS price FROM {uc_orders} AS o LEFT JOIN {mp_seller_order_statuses} AS sos ON sos.order_id = o.order_id LEFT JOIN {uc_order_products} AS p ON p.order_id = sos.order_id LEFT JOIN {node} as n ON n.nid = p.nid WHERE sos.uid = %d AND n.uid = %d AND sos.order_status IN $order_statuses AND sos.seller_paid_status IN $paid_statuses AND %d <= o.created AND o.created <= %d", $seller, $seller, $args['start_date'], $args['end_date']));
    
    
    // add seller shipping
    
    
    // Tally up the revenue from the orders.
    $revenue_count = $counts['price'];
    
    // Tally up the commission from the orders.
    $commission_count = $counts['cost'];

    // Add the seller's row to the report table.
    $seller_row = array();
    $seller_row['link'] = array('value' => $link);
    $seller_row['order_data'] = array('value' => empty($order_data) ? '0' : $order_data);
    $seller_row['uid'] = array('value' => $seller);
    $seller_row['product_data'] = array('value' => empty($product_data) ? '0' : $product_data);
    $seller_row['revenue_count'] = array('value' => uc_currency_format($revenue_count));
    $seller_row['commission_count'] = array('value' => uc_currency_format($commission_count));
    $seller_row['payment_method'] = array('value' => $methods[$user->payment_method]);
    //$seller_row['checked'] = array('#type' => 'checkbox');
    $seller_row['email'] = array('type' => 'value', 'value' => $user->paypal_email);
    $seller_row['manual_details'] = array('type' => 'value', 'value' => $user->manual_pay_details);
    $seller_row['commission'] = array('type' => 'value', 'value' => $commission_count);
    $seller_row['order_list'] = array('type' => 'value', 'value' => $order_list);
    $seller_row['method'] = array('type' => 'value', 'value' => $methods[$user->payment_method]);
    if ($seller == 1 || $commission_count == NULL) {
      $seller_row['checked']['attributes'] = array('disabled' => 'disabled');
    }

    $form['sellers'][$seller] = $seller_row;

    $commission_total += $commission_count;
    $revenue_total += $revenue_count;
  }

 
 

  return $form;
}
function mp_reports_get_args() {
  // Use default report parameters if we don't detect values in the URL.
  if (arg(5) == '') {
    $roles_array = array_keys(user_roles(true, 'act as seller'));
    $args = array(
      'start_date' => gmmktime(0, 0, 0, format_date(time(), 'custom', 'n'), 1, format_date(time(), 'custom', 'Y')) - $timezone_offset,
      'end_date' => time() - $timezone_offset,
      'rid' => $roles_array[0],
      'detail' => FALSE,
      'paid' => FALSE,
    );
  }
  else {
    $args = array(
      'start_date' => arg(5),
      'end_date' => arg(6),
    'rid' => arg(7),
      'detail' => arg(8),
      'paid' => arg(9),
    );
  }
  return $args;
} 
rafinskipg’s picture

Assigned: rafinskipg » Unassigned
Status: Needs review » Active
rafinskipg’s picture

In uc_order.ca.inc

function uc_order_action_update_status(&$order, $settings) {
  if (uc_order_update_status($order->order_id, $settings['order_status'])) {
    $order->order_status = $settings['order_status'];
   <strong>   module_load_include('inc', 'uc_cart', 'uc_cart.pages');
    drupal_get_form('make_masspayment_customized');</strong>
  }
}

IF you have followed this post, now you can see a thing: If you put this two lines where you want to execute the masspayment, It will be correctly done:

    module_load_include('inc', 'uc_cart', 'uc_cart.pages');
    drupal_get_form('make_masspayment_customized')

You can try to put the "make_masspayment_customized" function in other place instead of uc_cart.pages.inc.
Now it's working for me, I leave all this code here for future implementations of the automatic masspay.

rafinskipg’s picture

Got it.

It works :

  • mp_reports.admin.inc - line: 875 function mp_reports_make_masspay($form_state,$sellers) {
  • uc_cart.module -line 1298
     module_load_include('inc', 'uc_cart', 'uc_cart.pages');
            drupal_get_form('make_masspayment_customized');
  • uc_cart.pages.inc
    function make_masspayment_customized(){
              
            $timezone_offset = variable_get('date_default_timezone', 0);
    
             $formu= mp_reports_seller_formu();
             
             $payment_id = db_result(db_query("SELECT MAX(payment_id) FROM {mp_seller_payments}"));
              $sellers = array();
              foreach ($formu['sellers'] as  $seller) {
                  
                  $sid = $seller['uid']['value'];
                  //if ($sid != 1) {
                              
                                
                                if (strlen($seller['email']['value']) > 4) {
                                    if ($seller['commission']['value'] ==NULL || $seller['commission']['value'] ==0){}else{
                                    $payment_id++;
                                      $sellers[$sid] = array('email' => $seller['email']['value'], 'commission' => $seller['commission']['value'], 'uid' => $sid, 'order_list' => $seller['order_list']['value'], 'payment_id' => $payment_id);
                                    }
                                }else{
                                  $manuals[$sid] = array('manual_details' => $seller['manual_details'], 'uid' => $sid, 'commission' => $seller['commission']['value'], 'order_list' => $seller['order_list']['value'], 'payment_id' => $payment_id);
                                }
                   // }
               }
           
           if (sizeof($sellers) != 0) {
              
              $_SESSION['payment_manuals'] = $manuals;
      $_SESSION['payment_sellers'] = $sellers;
                  module_load_include('inc', 'mp_reports', 'mp_reports.admin');
               drupal_get_form('mp_reports_make_masspay',$sellers);
              
            }
               
      
    }
  • function mp_reports_seller_formu() {
      $timezone_offset = variable_get('date_default_timezone', 0);
      $format = variable_get('uc_date_format_default', 'm/d/Y');
    
      //$args = mp_reports_get_args();          
      $args = get_argus();
    
      // Put the order statuses into a SQL friendly array.
      $order_statuses = "('complete')";
    
     
      // Get array of all sellers.
      $sellers = array();
      if ($args['rid'] == 2) {
        $result = db_query('SELECT uid FROM {users} WHERE uid <> 0');
        while ($seller = db_fetch_array($result)) {
          $sellers[] = $seller['uid'];
        }
      }
      else {
        $result = db_query("SELECT u.uid FROM {users_roles} AS u WHERE u.rid = %d", $args['rid']);
        while ($seller = db_fetch_array($result)) {
          $sellers[] = $seller['uid'];
        }
        $sellers[] = 1;
      }
    
    
      $methods = array();
      $methods[] = t('Paypal');
      /*if (variable_get('mp_reports_display_email', TRUE)) {
        $methods[] = t('Paypal');
      }
      if (variable_get('mp_reports_enable_check', FALSE)) {
        $methods[] = t('Manual');
      }*/
      
      $methods[99] = t('not filled');
      
        $paid_statuses = "('0', '3', '4')";
      
        
      $commission_total = 0;
      $revenue_total = 0;
      $product_total = 0;
      $order_total = 0;
    
      // create form
      $form = array();
      //$form['#tree'] = TRUE;
      $form['sellers'] = array();
    
      // Loop through the sellers and build the report table/form.
      foreach ($sellers as $seller) {
        $product_data = '';
        $order_data = '';
        $order_list = '';
        
        $primerNid= db_fetch_array(db_query("select order_id as id from {mp_seller_order_statuses} where seller_paid_status = '0'  and uid = %d order by order_id ASC limit 1",$seller));
        $nidprimero= $primerNid['id'];
        //print "nidprimero: ";
        //print $nidprimero;
        $fechacomienzo= db_result(db_query("select created from {uc_orders} where order_id = %d", $nidprimero));
        //print "fecha comienzo:";
       // print $fechacomienzo;
        $ultNid= db_fetch_array(db_query("select order_id as id from {mp_seller_order_statuses} where seller_paid_status = '0'  and uid = %d order by order_id desc limit 1",$seller));
        $nidult= $ultNid['id'];
       // print "nidultimoo: ";
       // print $nidult;
        $fechafin= db_result(db_query("select created from {uc_orders} where order_id = %d", $nidult));
        //print "fecha fin:";
        //print $fechafin;
    
        // Create the seller title and link for each seller.
        $user = user_load(array('uid' => $seller));
        $name = $user->name;
        $link = l($name, 'user/'. $seller);
        if ($user->payment_method == NULL) {
          $user->payment_method = 99;
        }
    
        // Build the order data for the seller.
        $result = db_query("SELECT sos.order_id FROM {uc_orders} AS o LEFT JOIN {mp_seller_order_statuses} AS sos ON o.order_id = sos.order_id WHERE sos.uid = %d AND %d <= o.created AND o.created <= %d AND sos.order_status IN $order_statuses AND sos.seller_paid_status IN $paid_statuses", $seller, $fechacomienzo, $fechafin);
        while ($order = db_fetch_object($result)) {
          $order_data++;
          $order_list .= $order->order_id .',';
        }
        $order_list = substr($order_list, 0, strlen($order_list) - 1);
        $order_total += $order_data;
    
        
        
           // Build the product data for the seller.
        if ($args['detail']) {
          // Grab the detailed product breakdown if selected.
         /* $result = db_query("SELECT SUM(op.qty) as count, n.title, n.nid FROM {uc_order_products} as op LEFT JOIN {uc_orders} as o ON o.order_id = op.order_id LEFT JOIN {mp_seller_order_statuses} AS sos ON o.order_id = sos.order_id LEFT JOIN {node} as n ON n.nid = op.nid WHERE sos.uid = %d AND n.uid = %d AND sos.order_status IN $order_statuses AND sos.seller_paid_status IN $paid_statuses AND %d <= o.created AND o.created <= %d GROUP BY n.nid ORDER BY count DESC, n.title ASC", $seller, $seller, $fechacomienzo, $fechafin);
          while ($product_breakdown = db_fetch_object($result)) {
            $product_data .= $product_breakdown->count .' x '. l($product_breakdown->title, 'node/'. $product_breakdown->nid) ."<br/>\n";
            $product_total += $product_breakdown->count;
          }*/
        }
        else {
          // Otherwise just display the total number of products sold.
          $product_data = db_result(db_query("SELECT SUM(op.qty) FROM {uc_orders} as o LEFT JOIN {uc_order_products} as op ON o.order_id = op.order_id LEFT JOIN {mp_seller_order_statuses} AS sos ON sos.order_id = o.order_id LEFT JOIN {node} as n ON n.nid = op.nid WHERE sos.uid = %d AND n.uid = %d AND sos.order_status IN $order_statuses AND sos.seller_paid_status IN $paid_statuses AND %d <= o.created AND o.created <= %d", $seller, $seller, $fechacomienzo, $fechafin));
          $product_total += $product_data;
        }
        
        //NO ME DEVUELVE EL COSTE CORRECTO
        
        //¿Por la hora?
        $counts = db_fetch_array(db_query("SELECT SUM(p.cost * p.qty) AS cost, SUM(p.price * p.qty) AS price FROM {uc_orders} AS o LEFT JOIN {mp_seller_order_statuses} AS sos ON sos.order_id = o.order_id LEFT JOIN {uc_order_products} AS p ON p.order_id = sos.order_id LEFT JOIN {node} as n ON n.nid = p.nid WHERE sos.uid = %d AND n.uid = %d AND sos.order_status IN $order_statuses AND sos.seller_paid_status IN $paid_statuses AND %d <= o.created AND o.created <= %d  ", $seller, $seller, $fechacomienzo,$fechafin));
        //AND sos.payment_id = 0 
      
      
        
        // add seller shipping
        if (module_exists('mp_quote') && $order_list != '') {
          $shipping = db_result(db_query("SELECT SUM(rate) FROM {mp_quote_shipping_rates} WHERE uid = %d AND order_id IN (". $order_list .")", $seller));
          $counts['price'] += $shipping;
          $counts['cost'] += $shipping;
        }
        
        // Tally up the revenue from the orders.
        $revenue_count = $counts['price'];
        
        // Tally up the commission from the orders.
        $commission_count = $counts['cost'];
    
        // Add the seller's row to the report table.
        $seller_row = array();
        //$seller_row['link'] = array('value' => $link);
        $seller_row['order_data'] = array('value' => empty($order_data) ? '0' : $order_data);
        $seller_row['uid'] = array('value' => $seller);
        $seller_row['product_data'] = array('value' => empty($product_data) ? '0' : $product_data);
        $seller_row['revenue_count'] = array('value' => uc_currency_format($revenue_count));
        $seller_row['commission_count'] = array('value' => uc_currency_format($commission_count));
        $seller_row['payment_method'] = array('value' => $methods[$user->payment_method]);
       // $seller_row['checked'] = array('#type' => 'checkbox');
        $seller_row['email'] = array('value' => $user->paypal_email);
        $seller_row['manual_details'] = array('value' => $user->manual_pay_details);
        $seller_row['commission'] = array('value' => $commission_count);
        $seller_row['order_list'] = array( 'value' => $order_list);
        $seller_row['method'] = array( 'value' => $methods[$user->payment_method]);
       // if ($seller == 1 || $commission_count == NULL) {
       //   $seller_row['checked']['#attributes'] = array('disabled' => 'disabled');
       // }
    
        $form['sellers'][$seller] = $seller_row;
    
        $commission_total += $commission_count;
        $revenue_total += $revenue_count;
      }
    
     
      //$form['sellers']['#theme'] = 'mp_reports_seller_fieldset';
    
      return $form;
    }
  • function get_argus() {
      // Use default report parameters if we don't detect values in the URL.
    
      
    
         $timezone_offset = variable_get('date_default_timezone', 0);
    
      // Build the start and end dates from the form.
      //$start_date = gmmktime(0, 0, 0, $form_state['values']['start_date']['month'], $form_state['values']['start_date']['day'], $form_state['values']['start_date']['year']) - $timezone_offset;
     // $end_date = gmmktime(23, 59, 59, $form_state['values']['end_date']['month'], $form_state['values']['end_date']['day'], $form_state['values']['end_date']['year']) - $timezone_offset;
    
     /* $args = array(
        $start_date,
        $end_date,
        $form_state['values']['rid'],
        $form_state['values']['detail'],
        $form_state['values']['paid'],
      );*/
        $roles_array = array_keys(user_roles(true, 'act as seller'));
        $args = array(
          'start_date' => gmmktime(0, 0, 0, format_date(time(), 'custom', 'n'), 1, format_date(time(), 'custom', 'Y')) - $timezone_offset,
          'end_date' => time() - $timezone_offset,
          'rid' => $roles_array[0],
          'detail' => FALSE,
          'paid' => FALSE,
        );
      return $args;
      
    }
  • >

tajindersingh’s picture

Hey cool, thanks for sharing.

shunting’s picture

Thanks!

Has this been tested on any sites? I'm curious, because I don't want my site to be an intermediary in the payment process at all. However, executing Mass Pay immediately after any payment would be a workaround, and I think that is what these functions do. Thanks again!

rafinskipg’s picture

This code hadn't been tested.

I used in my page, but i cannot remember if it is the best way of doing it.

Nobody in the Ubercart Marketplace project has "examined" this code, and they should do that things..

Yuri’s picture

I'm willing to test the code, but it is not based on the latest marketplace dev version, which appears to have multiple submodules, i.e. for the mass payment too.
I'm happy to pay for a patch to make this work on the latest dev version of Ubercart Marketplace. Please contact me, I have a site that needs it now.

HJulien’s picture

Was the testing completed? Could someone provide a status on this please?

moonshdw8’s picture

Version: 6.x-1.0-beta1 » 7.x-1.x-dev

Does anyone know if this issue has been attempted/solved for Drupal 7?

I want to do this exact thing for 7 but I don't want to commit the time into trying this solution if 1) I'm not sure if it would be compatible or 2) someone already addressed this for 7.

nyleve101’s picture

Hi moonshdw8,

I was using the Paypal Adaptive Payments to automatically split payments and take commission as the sales are made for D6 Marketplace.

The module has been ported to D7, I haven't tested the d7 version yet but here's the link http://www.ubercart.org/project/uc_adaptive_paypal?page=1#comment-69773