/** * Add a record of the purchase to the DB * * @param array $products Product Id(s) of Product(s) purchased * @param array $quantity Quantity of products purchases * @param array $paypal_data IPN POST variables * @todo implemente physical item vs. download, reflected in 'status' */ function handlePurchase($products, $quantity, $paypal_data, $product_name) { global $_TABLES, $_CONF, $_PAY_CONF, $LANG_PAYPAL_EMAIL; // initialize file and names arrays $files = array(); $names = array(); $oldids = $products; $products = PAYPAL_realId($products); // for each item purchased, record purchase in purchase table for ($i = 0; $i < count($products); $i++) { if (DEBUG) { COM_errorLog('PAYPAL-IPN: Product id:' . $products[$i]); } // grab relevant product data from product table to insert into purchase table. $sql = "SELECT * FROM {$_TABLES['paypal_products']} " . "WHERE id = '{$products[$i]}'"; $res = DB_query($sql); $A = DB_fetchArray($res); if (DEBUG) { COM_errorLog('PAYPAL-IPN: Type: ' . $A['type']); } if ($A['download'] > 0) { $files[] = $_PAY_CONF['download_path'] . $A['file']; } //TODO + attribute name // Set quantity to one if empty if ($quantity[$i] == '') { $quantity[$i] = 1; } $names[] = $product_name[$i] . ' x ' . $quantity[$i]; // Do record anonymous users in purchase table //TODO record product name + product_id with attribute if (is_numeric((int) $paypal_data['custom']) && (int) $paypal_data['custom'] > 0) { // Add the purchase to the paypal purchase table $sql = "INSERT INTO {$_TABLES['paypal_purchases']} SET product_id = '{$products[$i]}', " . "quantity = '{$quantity[$i]}', user_id = '{$paypal_data['custom']}', " . "txn_id = '{$paypal_data['txn_id']}', " . 'purchase_date = NOW(), status = \'complete\''; /** * @todo implemente physical item vs. download, reflected in 'status' */ // if physical item (aka, must be shipped) status = 'pending', otherwise 'complete' //if ( $physical == 1 ) { // $sql .= ", status = 'pending'"; //} else { // $sql .= ", status = 'complete'"; //} // add an expiration date if appropriate if (is_numeric($A['expiration']) && $A['type'] == 'product') { $sql .= ", expiration = DATE_ADD(NOW(), INTERVAL {$A['expiration']} DAY)"; } if (DEBUG) { COM_errorLog('PAYPAL-IPN: ' . $sql); } DB_query($sql); if (DEBUG) { COM_errorLog('PAYPAL-IPN: Purchase recorded'); } } // stock movement $stock_id = PAYPAL_getStockId($oldids[$i]); $qty = $quantity[$i]; PAYPAL_stockMovement($stock_id, $oldids[$i], -$qty); } // Update user details if empty user_id, user_name, user_contact, user_proid, user_street1, user_street2, user_postal, user_city, user_country, user_phone1, user_phone2, user_fax, status $fields = array('user_name' => $paypal_data['address_name'], 'user_contact' => $paypal_data['first_name'] . ' ' . $paypal_data['last_name'], 'user_street1' => $paypal_data['address_street'], 'user_postal' => $paypal_data['address_zip'], 'user_city' => $paypal_data['address_city'], 'user_country' => $paypal_data['address_country']); if (is_numeric((int) $paypal_data['custom']) && (int) $paypal_data['custom'] != 1) { PAYPAL_updateUserDetails((int) $paypal_data['custom'], $fields, true); } // Send the purchaser a confirmation email (if set to do so in config) if (is_numeric((int) $paypal_data['custom']) && (int) $paypal_data['custom'] != 1 && $_PAY_CONF['purchase_email_user'] || (!is_numeric($paypal_data['custom']) || (int) $paypal_data['custom'] == 1) && $_PAY_CONF['purchase_email_anon']) { // setup templates $message = new Template($_CONF['path'] . 'plugins/paypal/templates'); $message->set_file(array('subject' => 'purchase_email_subject.txt', 'message' => 'purchase_email_message.txt')); // site variables $message->set_var('site_url', $_CONF['site_url']); $message->set_var('site_name', $_CONF['site_name']); //Email subject $message->set_var('purchase_receipt', $LANG_PAYPAL_EMAIL['purchase_receipt']); // list of product names for ($i = 0; $i < count($products); $i++) { $li_products .= '<li>' . $names[$i]; } $message->set_var('products', $li_products); //Email messages $message->set_var('thank_you', $LANG_PAYPAL_EMAIL['thank_you']); $message->set_var('thanks', $LANG_PAYPAL_EMAIL['thanks']); // paypal details $message->set_var('payment_gross', $paypal_data['payment_gross']); $message->set_var('tax', $paypal_data['tax']); $message->set_var('shipping', $paypal_data['mc_shipping']); $message->set_var('handling', $paypal_data['mc_handling']); $message->set_var('payment_date', $paypal_data['payment_date']); $message->set_var('payer_email', $paypal_data['payer_email']); $message->set_var('first_name', $paypal_data['first_name']); $message->set_var('last_name', $paypal_data['last_name']); $subject = trim($message->parse('output', 'subject')); // if specified to mail attachment, do so, otherwise skip attachment if ((is_numeric((int) $paypal_data['custom']) && (int) $paypal_data['custom'] != 1 && $_PAY_CONF['purchase_email_user_attach'] || (!is_numeric((int) $paypal_data['custom']) || (int) $paypal_data['custom'] == 1) && $_PAY_CONF['purchase_email_anon_attach']) && count($files) > 0) { $message->set_var('attached_files', $LANG_PAYPAL_EMAIL['attached_files']); $text = $message->parse('output', 'message'); paypal_mailAttachment($paypal_data['payer_email'], $subject, $text, $files, $_PAY_CONF['receiverEmailAddr']); } else { if (count($files) > 0) { $message->set_var('attached_files', $LANG_PAYPAL_EMAIL['download_files']); } else { $message->set_var('attached_files', ''); } $text = $message->parse('output', 'message'); COM_mail($paypal_data['payer_email'], $subject, $text, $_PAY_CONF['receiverEmailAddr'], true); } if (DEBUG) { COM_errorLog('PAYPAL-IPN: Email was sent'); } } //Send email to receiver COM_mail($_PAY_CONF['receiverEmailAddr'], $subject, $subject . ' >> ' . $text, $_PAY_CONF['receiverEmailAddr'], true); //Subscription if ($A['type'] == 'subscription') { //add subscription to db PAYPAL_addsubscription($A, $paypal_data); if (DEBUG) { COM_errorLog('PAYPAL-IPN: Subscription recorded'); } //add user to group if ($A['add_to_group'] > 1 && (int) $paypal_data['custom'] > 1) { PAYPAL_addToGroup($A['add_to_group'], $paypal_data['custom']); if (DEBUG) { COM_errorLog('PAYPAL-IPN: User with UID ' . $paypal_data['custom'] . ' added to group ID ' . $A['add_to_group']); } } } }
$products .= '<p>' . $LANG_PAYPAL_1['total_row_label'] . ' ' . $ipn['mc_gross'] . ' ' . $_PAY_CONF['currency'] . '</p>'; $message->set_var('products', $products); //Email messages $message->set_var('thank_you', $LANG_PAYPAL_EMAIL['thank_you']); $message->set_var('thanks', $LANG_PAYPAL_EMAIL['thanks']); // paypal details $payment_date = COM_getUserDateTimeFormat($ipn['payment_date']); $message->set_var('payment_date', $payment_date[0]); $message->set_var('payer_email', $ipn['payer_email']); $message->set_var('address_name', $ipn['address_name']); $subject = trim($message->parse('output', 'subject')); // if specified to mail attachment, do so, otherwise skip attachment if ((is_numeric((int) $ipn['custom']) && (int) $ipn['custom'] != 1 && $_PAY_CONF['purchase_email_user_attach'] || (!is_numeric((int) $ipn['custom']) || (int) $ipn['custom'] == 1) && $_PAY_CONF['purchase_email_anon_attach']) && count($files) > 0) { $message->set_var('attached_files', $LANG_PAYPAL_EMAIL['attached_files']); $text = $message->parse('output', 'message'); paypal_mailAttachment($ipn['payer_email'], $subject, $text, $files, $_PAY_CONF['receiverEmailAddr']); } else { $message->set_var('attached_files', $LANG_PAYPAL_EMAIL['download_files']); $text = $message->parse('output', 'message'); COM_mail($ipn['payer_email'], $subject, $text, $_PAY_CONF['receiverEmailAddr'], true); } if ($_PAY_CONF['debug']) { COM_errorLog('Email was sent'); } } //Send email to receiver COM_mail($_PAY_CONF['receiverEmailAddr'], $subject, $subject . ' >> ' . $text, '', true); $_REQUEST['msg'] = $LANG_PAYPAL_1['order_validated']; } //Main $display = COM_siteHeader('none');