/** * Insert Payment * * @since 1.0 * @param array $payment_data * @return int|bool Payment ID if payment is inserted, false otherwise */ function edd_insert_payment($payment_data = array()) { if (empty($payment_data)) { return false; } // Make sure the payment is inserted with the correct timezone date_default_timezone_set(edd_get_timezone_id()); // Construct the payment title if (isset($payment_data['user_info']['first_name']) || isset($payment_data['user_info']['last_name'])) { $payment_title = $payment_data['user_info']['first_name'] . ' ' . $payment_data['user_info']['last_name']; } else { $payment_title = $payment_data['user_email']; } // Retrieve the ID of the discount used, if any if ($payment_data['user_info']['discount'] != 'none') { $discount = edd_get_discount_by('code', $payment_data['user_info']['discount']); } // Find the next payment number, if enabled if (edd_get_option('enable_sequential')) { $number = edd_get_next_payment_number(); } $args = apply_filters('edd_insert_payment_args', array('post_title' => $payment_title, 'post_status' => isset($payment_data['status']) ? $payment_data['status'] : 'pending', 'post_type' => 'edd_payment', 'post_parent' => isset($payment_data['parent']) ? $payment_data['parent'] : null, 'post_date' => isset($payment_data['post_date']) ? $payment_data['post_date'] : null, 'post_date_gmt' => isset($payment_data['post_date']) ? get_gmt_from_date($payment_data['post_date']) : null), $payment_data); // Create a blank payment $payment = wp_insert_post($args); if ($payment) { if (isset($payment_data['tax'])) { $cart_tax = $payment_data['tax']; } else { $taxes = $payment_data['cart_details'] ? wp_list_pluck($payment_data['cart_details'], 'tax') : array(); $cart_tax = array_sum($taxes); $cart_tax += edd_get_cart_fee_tax(); } $payment_meta = array('currency' => $payment_data['currency'], 'downloads' => $payment_data['downloads'], 'user_info' => $payment_data['user_info'], 'cart_details' => $payment_data['cart_details']); $mode = edd_is_test_mode() ? 'test' : 'live'; $gateway = !empty($payment_data['gateway']) ? $payment_data['gateway'] : ''; $gateway = empty($gateway) && isset($_POST['edd-gateway']) ? $_POST['edd-gateway'] : $gateway; if (!$payment_data['price']) { // Ensures the _edd_payment_total meta key is created for purchases with an amount of 0 $payment_data['price'] = '0.00'; } // Create or update a customer $customer = new EDD_Customer($payment_data['user_email']); $customer_data = array('name' => $payment_data['user_info']['first_name'] . ' ' . $payment_data['user_info']['last_name'], 'email' => $payment_data['user_email'], 'user_id' => $payment_data['user_info']['id']); if (empty($customer->id)) { $customer->create($customer_data); } else { // Only update the customer if their name or email has changed if ($customer_data['email'] !== $customer->email || $customer_data['name'] !== $customer->name) { // We shouldn't be updating the User ID here, that is an admin task unset($customer_data['user_id']); $customer->update($customer_data); } } $customer->attach_payment($payment, false); // Record the payment details edd_update_payment_meta($payment, '_edd_payment_meta', apply_filters('edd_payment_meta', $payment_meta, $payment_data)); edd_update_payment_meta($payment, '_edd_payment_user_id', $payment_data['user_info']['id']); edd_update_payment_meta($payment, '_edd_payment_customer_id', $customer->id); edd_update_payment_meta($payment, '_edd_payment_user_email', $payment_data['user_email']); edd_update_payment_meta($payment, '_edd_payment_user_ip', edd_get_ip()); edd_update_payment_meta($payment, '_edd_payment_purchase_key', $payment_data['purchase_key']); edd_update_payment_meta($payment, '_edd_payment_total', $payment_data['price']); edd_update_payment_meta($payment, '_edd_payment_mode', $mode); edd_update_payment_meta($payment, '_edd_payment_gateway', $gateway); edd_update_payment_meta($payment, '_edd_payment_tax', $cart_tax); if (!empty($discount)) { edd_update_payment_meta($payment, '_edd_payment_discount_id', $discount->ID); } if (edd_get_option('enable_sequential')) { edd_update_payment_meta($payment, '_edd_payment_number', edd_format_payment_number($number)); update_option('edd_last_payment_number', $number); } // Clear the user's purchased cache delete_transient('edd_user_' . $payment_data['user_info']['id'] . '_purchases'); do_action('edd_insert_payment', $payment, $payment_data); return $payment; // Return the ID } // Return false if no payment was inserted return false; }
/** * Create the base of a payment. * * @since 2.5 * @param array $payment_data Base payment data. * @return int|bool Fale on failure, the payment ID on success. */ private function insert_payment() { // Make sure the payment is inserted with the correct timezone date_default_timezone_set(edd_get_timezone_id()); // Construct the payment title $payment_title = ''; if (!empty($this->first_name) && !empty($this->last_name)) { $payment_title = $this->first_name . ' ' . $this->last_name; } else { if (!empty($this->first_name) && empty($this->last_name)) { $payment_title = $this->first_name; } else { if (!empty($this->email) && is_email($this->email)) { $payment_title = $this->email; } } } if (empty($payment_title)) { return false; } if (empty($this->date)) { $this->date = date('Y-m-d H:i:s', current_time('timestamp')); } if (empty($this->key)) { $auth_key = defined('AUTH_KEY') ? AUTH_KEY : ''; $this->key = strtolower(md5($this->email . date('Y-m-d H:i:s') . $auth_key . uniqid('edd', true))); // Unique key $this->pending['key'] = $this->key; } if (empty($this->ip)) { $this->ip = edd_get_ip(); $this->pending['ip'] = $this->ip; } $payment_data = array('price' => $this->total, 'date' => $this->date, 'user_email' => $this->email, 'purchase_key' => $this->key, 'currency' => $this->currency, 'downloads' => $this->downloads, 'user_info' => array('id' => $this->user_id, 'email' => $this->email, 'first_name' => $this->first_name, 'last_name' => $this->last_name, 'discount' => $this->discounts, 'address' => $this->address), 'cart_details' => $this->cart_details, 'status' => $this->status, 'fees' => $this->fees); $args = apply_filters('edd_insert_payment_args', array('post_title' => $payment_title, 'post_status' => $this->status, 'post_type' => 'edd_payment', 'post_parent' => $this->parent_payment, 'post_date' => $this->date, 'post_date_gmt' => get_gmt_from_date($this->date)), $payment_data); // Create a blank payment $payment_id = wp_insert_post($args); if (!empty($payment_id)) { $this->ID = $payment_id; $this->_ID = $payment_id; $customer = new stdClass(); if (did_action('edd_pre_process_purchase') && is_user_logged_in()) { $customer = new EDD_customer(get_current_user_id(), true); } if (empty($customer->id)) { $customer = new EDD_Customer($this->email); } if (empty($customer->id)) { $customer_data = array('name' => !is_email($payment_title) ? $this->first_name . ' ' . $this->last_name : '', 'email' => $this->email, 'user_id' => $this->user_id); $customer->create($customer_data); } $this->customer_id = $customer->id; $this->pending['customer_id'] = $this->customer_id; $customer->attach_payment($this->ID, false); $this->payment_meta = apply_filters('edd_payment_meta', $this->payment_meta, $payment_data); if (!empty($this->payment_meta['fees'])) { $this->fees = array_merge($this->fees, $this->payment_meta['fees']); foreach ($this->fees as $fee) { $this->increase_fees($fee['amount']); } } $this->update_meta('_edd_payment_meta', $this->payment_meta); $this->new = true; } return $this->ID; }
/** * Process Purchase Form * * Handles the purchase form process. * * @access private * @since 1.0 * @return void */ function edd_process_purchase_form() { do_action('edd_pre_process_purchase'); // Make sure the cart isn't empty if (!edd_get_cart_contents() && !edd_cart_has_fees()) { $valid_data = false; edd_set_error('empty_cart', __('Your cart is empty', 'easy-digital-downloads')); } else { // Validate the form $_POST data $valid_data = edd_purchase_form_validate_fields(); // Allow themes and plugins to hook to errors do_action('edd_checkout_error_checks', $valid_data, $_POST); } $is_ajax = isset($_POST['edd_ajax']); // Process the login form if (isset($_POST['edd_login_submit'])) { edd_process_purchase_login(); } // Validate the user $user = edd_get_purchase_form_user($valid_data); if (false === $valid_data || edd_get_errors() || !$user) { if ($is_ajax) { do_action('edd_ajax_checkout_errors'); edd_die(); } else { return false; } } if ($is_ajax) { echo 'success'; edd_die(); } // Make sure the payment is inserted with the correct timezone date_default_timezone_set(edd_get_timezone_id()); // Setup user information $user_info = array('id' => $user['user_id'], 'email' => $user['user_email'], 'first_name' => $user['user_first'], 'last_name' => $user['user_last'], 'discount' => $valid_data['discount'], 'address' => $user['address']); $auth_key = defined('AUTH_KEY') ? AUTH_KEY : ''; // Setup purchase information $purchase_data = array('downloads' => edd_get_cart_contents(), 'fees' => edd_get_cart_fees(), 'subtotal' => edd_get_cart_subtotal(), 'discount' => edd_get_cart_discounted_amount(), 'tax' => edd_get_cart_tax(), 'price' => edd_get_cart_total(), 'purchase_key' => strtolower(md5($user['user_email'] . date('Y-m-d H:i:s') . $auth_key . uniqid('edd', true))), 'user_email' => $user['user_email'], 'date' => date('Y-m-d H:i:s', current_time('timestamp')), 'user_info' => stripslashes_deep($user_info), 'post_data' => $_POST, 'cart_details' => edd_get_cart_content_details(), 'gateway' => $valid_data['gateway'], 'card_info' => $valid_data['cc_info']); // Add the user data for hooks $valid_data['user'] = $user; // Allow themes and plugins to hook before the gateway do_action('edd_checkout_before_gateway', $_POST, $user_info, $valid_data); // If the total amount in the cart is 0, send to the manual gateway. This emulates a free download purchase if (!$purchase_data['price']) { // Revert to manual $purchase_data['gateway'] = 'manual'; $_POST['edd-gateway'] = 'manual'; } // Allow the purchase data to be modified before it is sent to the gateway $purchase_data = apply_filters('edd_purchase_data_before_gateway', $purchase_data, $valid_data); // Setup the data we're storing in the purchase session $session_data = $purchase_data; // Make sure credit card numbers are never stored in sessions unset($session_data['card_info']['card_number']); // Used for showing download links to non logged-in users after purchase, and for other plugins needing purchase data. edd_set_purchase_session($session_data); // Send info to the gateway for payment processing edd_send_to_gateway($purchase_data['gateway'], $purchase_data); edd_die(); }
/** * Check if an item is date restricted * * @since 1.0.6 * @param int $download_id The download ID to check * @return mixed array if restricted, null otherwise */ function edd_pl_is_date_restricted($download_id = 0) { date_default_timezone_set(edd_get_timezone_id()); $range = null; if (edd_get_option('edd_purchase_limit_restrict_date')) { $range['start'] = explode(' ', get_post_meta($download_id, '_edd_purchase_limit_start_date', true)); $range['end'] = explode(' ', get_post_meta($download_id, '_edd_purchase_limit_end_date', true)); if (edd_get_option('edd_purchase_limit_g_start_date') && empty($range['start'][0])) { $range['start'] = explode(' ', edd_get_option('edd_purchase_limit_g_start_date')); } if (edd_get_option('edd_purchase_limit_g_end_date') && empty($range['end'][0])) { $range['end'] = explode(' ', edd_get_option('edd_purchase_limit_g_end_date')); } foreach ($range as $key => &$value) { if (is_array($value)) { $value = array_filter($value); $range[$key] = $value; } } $range = array_filter($range); if (count($range) == 0) { $range = null; } // Maintain backwards compatibility if (isset($range['start'][0]) && !isset($range['start'][1])) { $range['start'][1] = '00:00'; } if (isset($range['end'][0]) && !isset($range['end'][1])) { $range['end'][1] = '00:00'; } } return $range; }
/** * Insert Payment * * @since 1.0 * @param array $payment_data * @return int|bool Payment ID if payment is inserted, false otherwise */ function edd_insert_payment($payment_data = array()) { if (empty($payment_data)) { return false; } // Make sure the payment is inserted with the correct timezone date_default_timezone_set(edd_get_timezone_id()); $payment = new EDD_Payment(); if (is_array($payment_data['cart_details']) && !empty($payment_data['cart_details'])) { foreach ($payment_data['cart_details'] as $item) { $args = array('quantity' => $item['quantity'], 'price_id' => isset($item['item_number']['options']['price_id']) ? $item['item_number']['options']['price_id'] : null, 'tax' => $item['tax'], 'item_price' => isset($item['item_price']) ? $item['item_price'] : $item['price'], 'fees' => isset($item['fees']) ? $item['fees'] : array(), 'discount' => isset($item['discount']) ? $item['discount'] : 0); $options = isset($item['item_number']['options']) ? $item['item_number']['options'] : array(); $payment->add_download($item['id'], $args, $options); } } $payment->increase_tax(edd_get_cart_fee_tax()); $gateway = !empty($payment_data['gateway']) ? $payment_data['gateway'] : ''; $gateway = empty($gateway) && isset($_POST['edd-gateway']) ? $_POST['edd-gateway'] : $gateway; $payment->status = !empty($payment_data['status']) ? $payment_data['status'] : 'pending'; $payment->currency = !empty($payment_data['currency']) ? $payment_data['currency'] : edd_get_currency(); $payment->user_info = $payment_data['user_info']; $payment->gateway = $gateway; $payment->user_id = $payment_data['user_info']['id']; $payment->email = $payment_data['user_email']; $payment->first_name = $payment_data['user_info']['first_name']; $payment->last_name = $payment_data['user_info']['last_name']; $payment->email = $payment_data['user_info']['email']; $payment->ip = edd_get_ip(); $payment->key = $payment_data['purchase_key']; $payment->mode = edd_is_test_mode() ? 'test' : 'live'; $payment->parent_payment = !empty($payment_data['parent']) ? absint($payment_data['parent']) : ''; $payment->discounts = !empty($payment_data['user_info']['discount']) ? $payment_data['user_info']['discount'] : array(); if (!empty($payment_data['date'])) { $payment->date = $payment_data['date']; } elseif (!empty($payment_data['post_date'])) { $payment->date = $payment_data['post_date']; } else { $payment->date = null; } if (edd_get_option('enable_sequential')) { $number = edd_get_next_payment_number(); $payment->number = edd_format_payment_number($number); update_option('edd_last_payment_number', $number); } // Clear the user's purchased cache delete_transient('edd_user_' . $payment_data['user_info']['id'] . '_purchases'); $payment->save(); do_action('edd_insert_payment', $payment->ID, $payment_data); if (!empty($payment->ID)) { return $payment->ID; } // Return false if no payment was inserted return false; }