Example #1
0
 /**
  *   Processes the purchase, for purchases made without an IPN message.
  *
  *   @param  array   $vals   Submitted values, e.g. $_POST
  */
 public function handlePurchase($vals = array())
 {
     global $_TABLES, $_CONF, $_PP_CONF;
     USES_paypal_functions();
     USES_paypal_class_cart();
     USES_paypal_class_order();
     USES_paypal_class_product();
     if (!empty($vals['cart_id'])) {
         $cart = new ppCart($vals['cart_id']);
         if (!$cart->hasItems()) {
             return;
         }
         // shouldn't be empty
         $items = $cart->Cart();
     } else {
         $cart = new ppCart();
     }
     // Create an order record to get the order ID
     $Order = $this->CreateOrder($vals, $cart);
     $db_order_id = DB_escapeString($Order->order_id);
     $prod_types = 0;
     // For each item purchased, record purchase in purchase table
     foreach ($items as $id => $item) {
         //COM_errorLog("Processing item: $id");
         list($item_number, $item_opts) = PAYPAL_explode_opts($id, true);
         // If the item number is numeric, assume it's an
         // inventory item.  Otherwise, it should be a plugin-supplied
         // item with the item number like pi_name:item_number:options
         if (PAYPAL_is_plugin_item($item_number)) {
             PAYPAL_debug("handlePurchase for Plugin item " . $item_number);
             // Initialize item info array to be used later
             $A = array();
             // Split the item number into component parts.  It could
             // be just a single string, depending on the plugin's needs.
             $pi_info = explode(':', $item['item_number']);
             PAYPAL_debug('Paymentgw::handlePurchase() pi_info: ' . print_r($pi_info, true));
             $status = LGLIB_invokeService($pi_info[0], 'productinfo', array($item_number, $item_opts), $product_info, $svc_msg);
             if ($status != PLG_RET_OK) {
                 $product_info = array();
             }
             if (!empty($product_info)) {
                 $items[$id]['name'] = $product_info['name'];
             }
             PAYPAL_debug("Paymentgw::handlePurchase() Got name " . $items[$id]['name']);
             $vars = array('item' => $item, 'ipn_data' => array());
             $status = LGLIB_invokeService($pi_info[0], 'handlePurchase', $vars, $A, $svc_msg);
             if ($status != PLG_RET_OK) {
                 $A = array();
             }
             // Mark what type of product this is
             $prod_types |= PP_PROD_VIRTUAL;
         } else {
             PAYPAL_debug("Paypal item " . $item_number);
             $P = new Product($item_number);
             $A = array('name' => $P->name, 'short_description' => $P->short_description, 'expiration' => $P->expiration, 'prod_type' => $P->prod_type, 'file' => $P->file, 'price' => $item['price']);
             if (!empty($item_opts)) {
                 $opts = explode(',', $itemopts);
                 $opt_str = $P->getOptionDesc($opts);
                 if (!empty($opt_str)) {
                     $A['short_description'] .= " ({$opt_str})";
                 }
                 $item_number .= '|' . $item_opts;
             }
             // Mark what type of product this is
             $prod_types |= $P->prod_type;
         }
         // An invalid item number, or nothing returned for a plugin
         if (empty($A)) {
             //$this->Error("Item {$item['item_number']} not found");
             continue;
         }
         // If it's a downloadable item, then get the full path to the file.
         // TODO: pp_data isn't available here, should be from $vals?
         if (!empty($A['file'])) {
             $this->items[$id]['file'] = $_PP_CONF['download_path'] . $A['file'];
             $token_base = $this->pp_data['txn_id'] . time() . rand(0, 99);
             $token = md5($token_base);
             $this->items[$id]['token'] = $token;
         } else {
             $token = '';
         }
         $items[$id]['prod_type'] = $A['prod_type'];
         // If a custom name was supplied by the gateway's IPN processor,
         // then use that.  Otherwise, plug in the name from inventory or
         // the plugin, for the notification email.
         if (empty($item['name'])) {
             $items[$id]['name'] = $A['short_description'];
         }
         // Add the purchase to the paypal purchase table
         $uid = isset($vals['uid']) ? (int) $vals['uid'] : $_USER['uid'];
         $sql = "INSERT INTO {$_TABLES['paypal.purchases']} SET \n                        order_id = '{$db_order_id}',\n                        product_id = '{$item_number}',\n                        description = '{$items[$id]['name']}',\n                        quantity = '{$item['quantity']}', \n                        user_id = '{$uid}', \n                        txn_type = '{$this->gw_id}',\n                        txn_id = '', \n                        purchase_date = '{$_PP_CONF['now']->toMySQL()}', \n                        status = 'complete',\n                        token = '{$token}',\n                        price = " . (double) $item['price'] . ",\n                        options = '" . DB_escapeString($item_opts) . "'";
         // add an expiration date if appropriate
         if (is_numeric($A['expiration']) && $A['expiration'] > 0) {
             $sql .= ", expiration = DATE_ADD('{$_PP_CONF['now']->toMySQL()}', INTERVAL {$A['expiration']} DAY)";
         }
         //echo $sql;die;
         PAYPAL_debug($sql);
         DB_query($sql);
     }
     // foreach item
     // If this was a user's cart, then clear that also
     if (isset($vals['cart_id']) && !empty($vals['cart_id'])) {
         DB_delete($_TABLES['paypal.cart'], 'cart_id', $vals['cart_id']);
     }
 }
 /**
  *   Get the form variables for the cart checkout button.
  *
  *   @uses   _addItem()
  *   @uses   _getButton()
  *   @uses   getActionUrl()
  *   @return string      HTML code for the button
  */
 public function CheckoutButton($cart)
 {
     global $_PP_CONF, $_USER, $LANG_PP_authorizenetsim;
     if (!$this->Supports('checkout')) {
         return '';
     }
     $this->cart = $cart;
     $cartItems = $this->cart->Cart();
     $cartID = $this->cart->cartID();
     $this->AddCustom('cart_id', $cartID);
     USES_paypal_class_product();
     foreach ($cartItems as $item_id => $item) {
         list($id, $optstr) = explode('|', $item_id);
         if (is_numeric($id)) {
             $P = new Product($id);
             if ($optstr) {
                 $opts = explode(',', $optstr);
                 $optdesc = $P->getOptionDesc($opts);
                 if (!empty($optdesc)) {
                     $item['descrip'] .= ', ' . $optdesc;
                 }
             }
         }
         $this->_addItem($item_id, $item);
         /*$this->_addItem($item_id, $item['name'] , $item['descrip'],
           $item['price'],
           $item['quantity'], $item['shipping'], $item['taxable']);*/
     }
     $gateway_vars = $this->_getButton('cart');
     $T = new Template(PAYPAL_PI_PATH . '/templates/buttons/' . $this->gw_name);
     $T->set_file(array('btn' => 'btn_checkout.thtml'));
     $T->set_var('action_url', $this->getActionUrl());
     $T->set_var('gw_name', $this->gw_name);
     $T->set_var('gateway_vars', $gateway_vars);
     $T->set_var('btn_text', $LANG_PP_authorizenetsim['buy_now']);
     $retval = $T->parse('', 'btn');
     return $retval;
 }
Example #3
0
 /**
  *   Handles the item purchases.
  *   The purchase should already have been validated; this function simply
  *   records the purchases.  Purchased files will be emailed to the
  *   customer by Order::Notify().
  *
  *   @uses   CreateOrder()
  */
 protected function handlePurchase()
 {
     global $_TABLES, $_CONF, $_PP_CONF;
     //USES_paypal_functions();
     // Create an order record to get the order ID
     //list($status, $order_id) = $this->CreateOrder();
     //if ($status != 0) return;
     //$db_order_id = DB_escapeString($order_id);
     $prod_types = 0;
     // For each item purchased, create an order item
     foreach ($this->items as $id => $item) {
         // If the item number is numeric, assume it's an
         // inventory item.  Otherwise, it should be a plugin-supplied
         // item with the item number like pi_name:item_number:options
         if (PAYPAL_is_plugin_item($item['item_number'])) {
             PAYPAL_debug("handlePurchase for Plugin item " . $item['item_number']);
             // Initialize item info array to be used later
             $A = array();
             // Split the item number into component parts.  It could
             // be just a single string, depending on the plugin's needs.
             if (strstr($item['item_number'], ':')) {
                 $pi_info = explode(':', $item['item_number']);
             } else {
                 $pi_info = array($item['item_number']);
             }
             PAYPAL_debug('BaseIPN::handlePurchase() pi_info: ' . print_r($pi_info, true));
             $status = LGLIB_invokeService($pi_info[0], 'productinfo', $pi_info, $A, $svc_msg);
             if ($status != PLG_RET_OK) {
                 $A = array();
             }
             if (!empty($A)) {
                 $this->items[$id]['name'] = $A['name'];
             }
             PAYPAL_debug("BaseIPN::handlePurchase() Got name " . $this->items[$id]['name']);
             $vars = array('item' => $item, 'ipn_data' => $this->pp_data);
             if ($this->pp_data['status'] == 'paid') {
                 $status = LGLIB_invokeService($pi_info[0], 'handlePurchase', $vars, $A, $svc_msg);
                 if ($status != PLG_RET_OK) {
                     $A = array();
                 }
             }
             // Mark what type of product this is
             $prod_types |= PP_PROD_VIRTUAL;
         } else {
             PAYPAL_debug("Paypal item " . $item['item_number']);
             $P = new Product($item['item_number']);
             $A = array('name' => $P->name, 'short_description' => $P->short_description, 'expiration' => $P->expiration, 'prod_type' => $P->prod_type, 'file' => $P->file, 'price' => $item['price']);
             if (!empty($item['options'])) {
                 $opts = explode(',', $item['options']);
                 $opt_str = $P->getOptionDesc($opts);
                 if (!empty($opt_str)) {
                     $A['short_description'] .= " ({$opt_str})";
                 }
                 $this->items[$id]['item_number'] .= '|' . $item['options'];
             }
             // Mark what type of product this is
             $prod_types |= $P->prod_type;
             $P->handlePurchase($item['quantity']);
         }
         // An invalid item number, or nothing returned for a plugin
         if (empty($A)) {
             $this->Error("Item {$item['item_number']} not found - txn " . $this->pp_data['txn_id']);
             continue;
         }
         // If it's a downloadable item, then get the full path to the file.
         if (!empty($A['file'])) {
             $this->items[$id]['file'] = $_PP_CONF['download_path'] . $A['file'];
             $token_base = $this->pp_data['txn_id'] . time() . rand(0, 99);
             $token = md5($token_base);
             $this->items[$id]['token'] = $token;
         } else {
             $token = '';
         }
         $this->items[$id]['prod_type'] = $A['prod_type'];
         if (is_numeric($A['expiration']) && $A['expiration'] > 0) {
             $this->items[$id]['expiration'] = $A['expiration'];
         }
         // If a custom name was supplied by the gateway's IPN processor,
         // then use that.  Otherwise, plug in the name from inventory or
         // the plugin, for the notification email.
         if (empty($item['name'])) {
             $this->items[$id]['name'] = $A['short_description'];
         }
         // Add the purchase to the paypal purchase table
         if (is_numeric($this->pp_data['custom']['uid'])) {
             $uid = $this->pp_data['custom']['uid'];
         } else {
             $uid = 1;
             // Anonymous as a fallback
         }
         /*$sql = "INSERT INTO {$_TABLES['paypal.purchases']} SET 
                                 order_id = '{$db_order_id}',
                                 product_id = '{$item['item_number']}',
                                 description = '{$this->items[$id]['name']}',
                                 quantity = '{$item['quantity']}', 
                                 user_id = '{$this->pp_data['custom']['uid']}', 
                                 txn_type = '{$this->pp_data['custom']['transtype']}',
                                 txn_id = '{$this->pp_data['txn_id']}', 
                                 purchase_date = '{$this->sql_date}', 
                                 status = 'complete',
                                 token = '$token',
                                 price = " . (float)$item['price'] . ",
                                 options = '" . DB_escapeString($item['options']) . "'";
         
                     // add an expiration date if appropriate
                     if (is_numeric($A['expiration']) && $A['expiration'] > 0) {
                         $sql .= ", expiration = DATE_ADD('{$_PP_CONF['now']}', INTERVAL {$A['expiration']} DAY)";
                     }
                     PAYPAL_debug($sql);
                     DB_query($sql);*/
     }
     // foreach item
     $status = $this->CreateOrder();
     if ($status == 0) {
         $this->Order->Notify();
     }
     // Update the order status to Paid
     //ppOrder::UpdateStatus($this->gw->getPaidStatus($prod_types),
     //            $order_id, false);
 }