public function save()
{
$saved = false;
//Must have an ID
if (empty($this->ID)) {
$payment_id = $this->insert_payment();
if (false === $payment_id) {
$saved = false;
} else {
$this->ID = $payment_id;
}
}
//Set ID if not matching
if ($this->ID !== $this->_ID) {
$this->ID = $this->_ID;
}
// If we have something pending, let's save it
if (!empty($this->pending)) {
$total_increase = 0;
$total_decrease = 0;
foreach ($this->pending as $key => $value) {
switch ($key) {
case 'donations':
// Update totals for pending donations
foreach ($this->pending[$key] as $item) {
$quantity = isset($item['quantity']) ? $item['quantity'] : 1;
$price_id = isset($item['price_id']) ? $item['price_id'] : 0;
switch ($item['action']) {
case 'add':
$price = $item['price'];
if ('publish' === $this->status || 'complete' === $this->status) {
// Add sales logs
$log_date = date_i18n('Y-m-d G:i:s', current_time('timestamp'));
$y = 0;
while ($y < $quantity) {
give_record_sale_in_log($item['id'], $this->ID, $price_id, $log_date);
$y++;
}
$form = new Give_Donate_Form($item['id']);
$form->increase_sales($quantity);
$form->increase_earnings($price);
$total_increase += $price;
}
break;
case 'remove':
$log_args = array('post_type' => 'give_log', 'post_parent' => $item['id'], 'numberposts' => $quantity, 'meta_query' => array(array('key' => '_give_log_payment_id', 'value' => $this->ID, 'compare' => '='), array('key' => '_give_log_price_id', 'value' => $price_id, 'compare' => '=')));
$found_logs = get_posts($log_args);
foreach ($found_logs as $log) {
wp_delete_post($log->ID, true);
}
if ('publish' === $this->status || 'complete' === $this->status) {
$form = new Give_Donate_Form($item['id']);
$form->decrease_sales($quantity);
$form->decrease_earnings($item['amount']);
$total_decrease += $item['amount'];
}
break;
}
}
break;
case 'fees':
if ('publish' !== $this->status && 'complete' !== $this->status) {
break;
}
if (empty($this->pending[$key])) {
break;
}
foreach ($this->pending[$key] as $fee) {
switch ($fee['action']) {
case 'add':
$total_increase += $fee['amount'];
break;
case 'remove':
$total_decrease += $fee['amount'];
break;
}
}
break;
case 'status':
$this->update_status($this->status);
break;
case 'gateway':
$this->update_meta('_give_payment_gateway', $this->gateway);
break;
case 'mode':
$this->update_meta('_give_payment_mode', $this->mode);
break;
case 'transaction_id':
$this->update_meta('_give_payment_transaction_id', $this->transaction_id);
break;
case 'ip':
$this->update_meta('_give_payment_user_ip', $this->ip);
break;
case 'customer_id':
$this->update_meta('_give_payment_customer_id', $this->customer_id);
break;
case 'user_id':
$this->update_meta('_give_payment_user_id', $this->user_id);
break;
case 'form_title':
$this->update_meta('_give_payment_form_title', $this->form_title);
break;
case 'form_id':
$this->update_meta('_give_payment_form_id', $this->form_id);
break;
case 'price_id':
$this->update_meta('_give_payment_price_id', $this->price_id);
break;
case 'first_name':
$this->user_info['first_name'] = $this->first_name;
break;
case 'last_name':
$this->user_info['last_name'] = $this->last_name;
break;
case 'address':
$this->user_info['address'] = $this->address;
break;
case 'email':
$this->update_meta('_give_payment_user_email', $this->email);
break;
case 'key':
$this->update_meta('_give_payment_purchase_key', $this->key);
break;
case 'number':
$this->update_meta('_give_payment_number', $this->number);
break;
case 'date':
$args = array('ID' => $this->ID, 'post_date' => $this->date, 'edit_date' => true);
wp_update_post($args);
break;
case 'completed_date':
$this->update_meta('_give_completed_date', $this->completed_date);
break;
case 'parent_payment':
$args = array('ID' => $this->ID, 'post_parent' => $this->parent_payment);
wp_update_post($args);
break;
default:
do_action('give_payment_save', $this, $key);
break;
}
}
if ('pending' !== $this->status) {
$customer = new Give_Customer($this->customer_id);
$total_change = $total_increase - $total_decrease;
if ($total_change < 0) {
$total_change = -$total_change;
// Decrease the customer's purchase stats
$customer->decrease_value($total_change);
give_decrease_total_earnings($total_change);
} else {
if ($total_change > 0) {
// Increase the customer's purchase stats
$customer->increase_value($total_change);
give_increase_total_earnings($total_change);
}
}
}
$this->update_meta('_give_payment_total', $this->total);
$new_meta = array('form_title' => $this->form_title, 'form_id' => $this->form_id, 'price_id' => $this->price_id, 'fees' => $this->fees, 'currency' => $this->currency, 'user_info' => $this->user_info);
$meta = $this->get_meta();
$merged_meta = array_merge($meta, $new_meta);
// Only save the payment meta if it's changed
if (md5(serialize($meta)) !== md5(serialize($merged_meta))) {
$updated = $this->update_meta('_give_payment_meta', $merged_meta);
if (false !== $updated) {
$saved = true;
}
}
$this->pending = array();
$saved = true;
}
if (true === $saved) {
$this->setup_payment($this->ID);
}
return $saved;
}