Give_Payment::insert_payment PHP Method

insert_payment() private method

Create the base of a payment.
Since: 1.5
private insert_payment ( ) : integer | boolean
return integer | boolean False on failure, the payment ID on success.
    private function insert_payment()
    {
        // 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;
                }
            }
        }
        //Set Key
        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('give', true)));
            // Unique key
            $this->pending['key'] = $this->key;
        }
        //Set IP
        if (empty($this->ip)) {
            $this->ip = give_get_ip();
            $this->pending['ip'] = $this->ip;
        }
        $payment_data = array('price' => $this->total, 'date' => $this->date, 'user_email' => $this->email, 'purchase_key' => $this->key, 'form_title' => $this->form_title, 'form_id' => $this->form_id, 'price_id' => $this->price_id, 'currency' => $this->currency, 'user_info' => array('id' => $this->user_id, 'email' => $this->email, 'first_name' => $this->first_name, 'last_name' => $this->last_name, 'address' => $this->address), 'status' => $this->status, 'fees' => $this->fees);
        $args = apply_filters('give_insert_payment_args', array('post_title' => $payment_title, 'post_status' => $this->status, 'post_type' => 'give_payment', 'post_date' => !empty($this->date) ? $this->date : null, 'post_date_gmt' => !empty($this->date) ? get_gmt_from_date($this->date) : null, 'post_parent' => $this->parent_payment), $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('give_pre_process_purchase') && is_user_logged_in()) {
                $customer = new Give_Customer(get_current_user_id(), true);
            }
            if (empty($customer->id)) {
                $customer = new Give_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('give_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('_give_payment_meta', $this->payment_meta);
            $this->new = true;
        }
        return $this->ID;
    }