/** * 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); }