/**
 * 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;
}
/**
 * Gets the total tax amount for the cart contents
 *
 * @since 1.2.3
 *
 * @return mixed|void Total tax amount
 */
function edd_get_cart_tax()
{
    $cart_tax = 0;
    $items = edd_get_cart_content_details();
    if ($items) {
        $taxes = wp_list_pluck($items, 'tax');
        if (is_array($taxes)) {
            $cart_tax = array_sum($taxes);
        }
    }
    $cart_tax += edd_get_cart_fee_tax();
    return apply_filters('edd_get_cart_tax', edd_sanitize_amount($cart_tax));
}
/**
 * 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;
    }
    $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 (isset($payment_data['post_date'])) {
        $payment->date = $payment_data['post_date'];
    }
    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;
}