/** * Constructor. * Set up the pp_data array. */ function __construct($A = array()) { $this->gw_id = 'amazon'; parent::__construct($A); list($ccode, $amount) = preg_split('/\\ +/', $A['transactionAmount']); $this->pp_data['txn_id'] = $A['transactionId']; $this->pp_data['payer_email'] = $A['buyerEmail']; $this->pp_data['payer_name'] = $A['buyerName']; $this->pp_data['pmt_date'] = strftime('%d %b %Y %H:%M:%S', $A['transactionDate']); $this->pp_data['pmt_gross'] = (double) $amount; $this->pp_data['gw_name'] = $this->gw->Description(); $this->pp_data['pmt_status'] = $A['status']; // Check a couple of vars to see if a shipping address was supplied if (isset($A['addressLine1']) || isset($A['city'])) { $this->pp_data['shipto'] = array('name' => $A['addressName'], 'address1' => $A['addressLine1'], 'address2' => $A['addressLine2'], 'city' => $A['city'], 'state' => $A['state'], 'country' => $A['country'], 'zip' => $A['zip'], 'phone' => $A['phoneNumber']); } $custom = explode(';', $A['referenceId']); foreach ($custom as $name => $temp) { list($name, $value) = explode(':', $temp); $this->pp_data['custom'][$name] = $value; } if ($this->pp_data['custom']['transtype'] == 'cart') { USES_paypal_class_cart(); $cart = new ppCart($this->pp_data['custom']['cart_id']); foreach ($cart->Cart() as $itm_id => $data) { $this->AddItem($itm_id, $data['quantity'], $data['price']); } } else { $items = explode('::', $A['paymentReason']); foreach ($items as $item) { list($itm_id, $price, $qty) = explode(';', $item); $this->AddItem($itm_id, $qty, $price); } } }
/** * Process an incoming IPN transaction * Do the following: * 1. Verify IPN * 2. Log IPN * 3. Check that transaction is complete * 4. Check that transaction is unique * 5. Check for valid receiver email address * 6. Process IPN * * @uses BaseIPN::AddItem() * @uses BaseIPN::handleFailure() * @uses BaseIPN::handlePurchase() * @uses BaseIPN::isUniqueTxnId() * @uses BaseIPN::isSufficientFunds() * @uses BaseIPN::Log() * @uses Verify() * @uses isStatusCompleted() * @param array $in POST variables of transaction * @return boolean true if processing valid and completed, false otherwise */ public function Process() { // If no data has been received, then there's nothing to do. if (empty($this->ipn_data)) { return false; } if (!$this->Verify()) { $logId = $this->Log(false); $this->handleFailure(PAYPAL_FAILURE_VERIFY, "({$logId}) Verification failed"); return false; } else { $logId = $this->Log(true); } // Set the custom data field to the exploded value. This has to // be done after Verify() or the Paypal verification will fail. $this->pp_data['custom'] = $this->custom; switch ($this->ipn_data['txn_type']) { case 'web_accept': //usually buy now //usually buy now case 'send_money': //usually donation/send money // Process Buy Now & Send Money $fees_paid = $this->ipn_data['tax'] + $this->pp_data['pmt_shipping'] + $this->pp_data['pmt_handling']; if (!empty($this->ipn_data['item_number'])) { if (!isset($this->ipn_data['quantity']) || (double) $this->ipn_data['quantity'] == 0) { $this->ipn_data['quantity'] = 1; } $payment_gross = $this->pp_data['pmt_gross'] - $fees_paid; $unit_price = $payment_gross / $this->ipn_data['quantity']; $this->AddItem($this->ipn_data['item_number'], $this->ipn_data['quantity'], $unit_price, $this->ipn_data['item_name'], $this->pp_data['pmt_shipping'], $this->pp_data['pmt_handling']); $currency = $this->pp_data['currency']; PAYPAL_debug("Net Settled: {$payment_gross} {$currency}"); if ($this->isSufficientFunds()) { $this->handlePurchase(); } else { $this->handleFailure(PAYPAL_FAILURE_FUNDS, "({$logId}) Insufficient funds for purchase"); return false; } } break; case 'cart': // shopping cart $fees_paid = $this->pp_data['pmt_tax'] + $this->pp_data['pmt_shipping'] + $this->pp_data['pmt_handling']; USES_paypal_class_cart(); if (empty($this->pp_data['custom']['cart_id'])) { $this->handleFailure(NULL, 'Missing Cart ID'); return false; } // Create a cart and read the info from the cart table. // Actual items purchased and prices will come from the IPN. $ppCart = new ppCart($this->pp_data['custom']['cart_id']); $Cart = $ppCart->Cart(); $items = array(); for ($i = 1; $i <= $this->ipn_data['num_cart_items']; $i++) { // PayPal returns the total price as mc_gross_X, so divide // by the quantity to get back to a unit price. if (!isset($this->ipn_data["quantity{$i}"]) || (double) $this->ipn_data["quantity{$i}"] == 0) { $this->ipn_data["quantity{$i}"] = 1; } $item_gross = $this->ipn_data["mc_gross_{$i}"]; if (isset($this->ipn_data["mc_shipping{$i}"])) { $item_shipping = (double) $this->ipn_data["mc_shipping{$i}"]; $item_gross -= $item_shipping; } else { $item_shipping = 0; } if (isset($this->ipn_data["tax{$i}"])) { $item_tax = (double) $this->ipn_data["tax{$i}"]; $item_gross -= $item_tax; } else { $item_tax = 0; } if (isset($this->ipn_data["mc_handling{$i}"])) { $item_handling = (double) $this->ipn_data["mc_handling{$i}"]; $item_gross -= $item_handling; } else { $item_handling = 0; } $unit_price = $item_gross / (double) $this->ipn_data["quantity{$i}"]; // Add the item to the array for the order creation. // IPN item numbers are indexes into the cart, so get the // actual product ID from the cart $this->AddItem($Cart[$this->ipn_data["item_number{$i}"]]['item_id'], $this->ipn_data["quantity{$i}"], $unit_price, $this->ipn_data["item_name{$i}"], $item_shipping, $item_handling, $item_tax, $Cart[$this->ipn_data["item_number{$i}"]]['extras']); } $payment_gross = $this->ipn_data['mc_gross'] - $fees_paid; PAYPAL_debug("Received {$payment_gross} gross payment"); //$currency = $this->ipn_data['mc_currency']; if ($this->isSufficientFunds()) { $this->handlePurchase(); } else { $this->handleFailure(PAYPAL_FAILURE_FUNDS, "({$logId}) Insufficient/incorrect funds for purchase"); return false; } break; // other, unknown, unsupported // other, unknown, unsupported default: switch ($this->ipn_data['reason_code']) { case 'refund': $this->handleRefund(); break; default: $this->handleFailure(PAYPAL_FAILURE_UNKNOWN, "({$logId}) Unknown transaction type"); return false; break; } break; } return true; }
/** * Allow a plugin to push an item into the cart * * @param array $args Array of item information * @param mixed &$output Output data * @param mixed &$svc_msg Service message * @return integer Status code */ function service_addCartItem_paypal($args, &$output, &$svc_msg) { global $ppGCart; USES_paypal_class_cart(); $ppGCart = new ppCart(); $qty = isset($args['quantity']) ? (double) $args['quantity'] : 1; if (!isset($args['item_number']) || empty($args['item_number'])) { return PLG_RET_ERROR; } $item_name = isset($args['item_name']) ? $args['item_name'] : ''; $amount = isset($args['amount']) ? (double) $args['amount'] : 0; $descrip = isset($args['short_description']) ? $args['short_description'] : ''; $options = isset($args['options']) ? $args['options'] : array(); $extras = isset($args['extras']) ? $args['extras'] : ''; // Option to add an item only if it is not currently in the cart. // Cart::addItem will be called to increment the quantity if the // unique flag is not set. if (isset($args['unique']) && $args['unique']) { if ($ppGCart->Contains($args['item_number'])) { return PLG_RET_OK; } } $ppGCart->addItem($args['item_number'], $item_name, $descrip, $qty, $amount, $options, $extras); return PLG_RET_OK; }
/** * 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']); } }
*/ /** Require core glFusion code */ require_once '../lib-common.php'; // If plugin is installed but not enabled, display an error and exit gracefully if (!in_array('paypal', $_PLUGINS)) { COM_404(); } // Ensure sufficient privs and dependencies to read this page PAYPAL_access_check(); // Import plugin-specific functions USES_paypal_functions(); // Create a global shopping cart for our use. This allows the cart to be // manipulated in an action and then displayed in a view, without necessarily // having to revisit the database or create a new cart. USES_paypal_class_cart(); $ppGCart = new ppCart(); // First try to get the SEO-friendly arguments. A single "action" and "id" // will probably be the most common anyway. If that fails, go through all // the possibilies for actions that might come from submit buttons, etc. COM_setArgNames(array('action', 'id')); $action = COM_getArgument('action'); $actionval = ''; if (!empty($action)) { $id = COM_sanitizeID(COM_getArgument('id')); } else { $expected = array('updatecart', 'checkout', 'searchcat', 'savebillto', 'saveshipto', 'updatecart', 'emptycart', 'addcartitem', 'addcartitem_x', 'checkoutcart', 'processorder', 'thanks', 'action', 'order', 'view', 'detail', 'printorder', 'orderhist'); $action = 'view'; foreach ($expected as $provided) { if (isset($_POST[$provided])) { $action = $provided; $actionval = $_POST[$provided];
/** * Create and populate an Order record for this purchase. * Gets the billto and shipto addresses from the cart, if any. * Items are saved in the purchases table by handlePurchase(). * * This function is called only by our own handlePurchase() function, * but is made "protected" so a derived class can use it if necessary. * * @return string Order ID, to link to the purchases table */ protected function CreateOrder() { global $_TABLES, $_PP_CONF; // See if an order already exists for this transaction. // If so, load it and update the status. If not, continue on // and create a new order $order_id = DB_getItem($_TABLES['paypal.orders'], 'order_id', "pmt_txn_id='" . DB_escapeString($this->pp_data['txn_id']) . "'"); if (!empty($order_id)) { $this->Order = new ppOrder($order_id); if ($this->Order->order_id != '') { $this->Order->log_user = $this->gw->Description(); $this->Order->UpdateStatus($this->pp_data['status']); } return 2; } $this->Order = new ppOrder(); USES_paypal_class_cart(); if (isset($this->pp_data['custom']['cart_id'])) { $cart = new ppCart($this->pp_data['custom']['cart_id']); if (!$_PP_CONF['sys_test_ipn'] && !$cart->hasItems()) { return 1; // shouldn't normally be empty except during testing } } else { $cart = NULL; } $uid = (int) $this->pp_data['custom']['uid']; $this->Order->uid = $uid; $this->Order->status = !empty($this->pp_data['status']) ? $this->pp_data['status'] : 'pending'; if ($uid > 1) { USES_paypal_class_userinfo(); $U = new ppUserInfo($uid); } // Get the billing and shipping addresses from the cart record, // if any. There may not be a cart in the database if it was // removed by a previous IPN, e.g. this is the 'completed' message // and we already processed a 'pending' message if ($cart) { $BillTo = $cart->getAddress('billto'); } if (empty($BillTo) && $uid > 1) { $BillTo = $U->getDefaultAddress('billto'); } if (is_array($BillTo)) { $this->Order->setBilling($BillTo); } $ShipTo = $this->pp_data['shipto']; if (empty($ShipTo)) { if ($cart) { $ShipTo = $cart->getAddress('shipto'); } if (empty($ShipTo) && $uid > 1) { $ShipTo = $U->getDefaultAddress('shipto'); } } if (is_array($ShipTo)) { $this->Order->setShipping($ShipTo); } if (isset($this->pp_data['shipto']['phone'])) { $this->Order->phone = $this->pp_data['shipto']['phone']; } $this->Order->pmt_method = $this->gw_id; $this->Order->pmt_txn_id = $this->pp_data['txn_id']; $this->Order->tax = $this->pp_data['pmt_tax']; $this->Order->shipping = $this->pp_data['pmt_shipping']; $this->Order->handling = $this->pp_data['pmt_handling']; $this->Order->buyer_email = $this->pp_data['payer_email']; $this->Order->log_user = $this->gw->Description(); $order_id = $this->Order->Save(); $db_order_id = DB_escapeString($order_id); $this->Order->items = array(); foreach ($this->items as $id => $item) { $options = DB_escapeString($item['options']); list($item_number, $options) = explode('|', $item['item_number']); //if (is_numeric($item['item_number'])) { if (is_numeric($item_number)) { // For Paypal catalog options, check for options and append // to the description. Update quantity on hand if tracking // is enabled. These actions don't apply to items from // other plugins. if (!empty($options)) { // options is expected as CSV $sql = "SELECT attr_value\n FROM {$_TABLES['paypal.prod_attr']}\n WHERE attr_id IN ({$options})"; $optres = DB_query($sql); $opt_str = ''; while ($O = DB_fetchArray($optres, false)) { $opt_str .= ', ' . $O['attr_value']; } $item['name'] .= $opt_str; } /*$sql = "UPDATE {$_TABLES['paypal.products']} SET onhand = GREATEST(0, onhand - " . (int)$item['quantity'] . ") WHERE id = '" . (int)$item['item_number'] . "' AND track_onhand > 0";*/ //COM_errorLog($sql); DB_query($sql, 1); } $sql = "INSERT INTO {$_TABLES['paypal.purchases']} SET \n order_id = '{$db_order_id}',\n product_id = '{$item['item_number']}',\n description = '" . DB_escapeString($item['name']) . "',\n quantity = '{$item['quantity']}', \n user_id = '{$this->pp_data['custom']['uid']}', \n txn_type = '{$this->pp_data['custom']['transtype']}',\n txn_id = '{$this->pp_data['txn_id']}', \n purchase_date = '{$this->sql_date}', \n status = 'pending',\n token = '" . md5(time()) . "',\n price = " . (double) $item['price'] . ",\n options = '{$options}'"; // add an expiration date if appropriate if (is_numeric($item['expiration']) && $item['expiration'] > 0) { $sql .= ", expiration = DATE_ADD('{$_PP_CONF['now']}', INTERVAL {$item['expiration']} DAY)"; } PAYPAL_debug($sql); DB_query($sql); } // foreach item // Reload the order to get the items $this->Order->Load(); // If this was a user's cart, then clear that also if (isset($this->pp_data['custom']['cart_id']) && !empty($this->pp_data['custom']['cart_id'])) { if (!$_PP_CONF['sys_test_ipn']) { DB_delete($_TABLES['paypal.cart'], 'cart_id', $this->pp_data['custom']['cart_id']); PAYPAL_debug('Cart ' . $this->pp_data['custom']['cart_id'] . ' deleted'); } } else { PAYPAL_debug('no cart to delete'); } return 0; }