/**
* Complete a purchase aka donation
*
* Performs all necessary actions to complete a purchase.
* Triggered by the give_update_payment_status() function.
*
* @since 1.0
*
* @param int $payment_id The ID number of the payment.
* @param string $new_status The status of the payment, probably "publish".
* @param string $old_status The status of the payment prior to being marked as "complete", probably "pending".
*
* @return void
*/
function give_complete_purchase($payment_id, $new_status, $old_status)
{
// Make sure that payments are only completed once
if ($old_status == 'publish' || $old_status == 'complete') {
return;
}
// Make sure the payment completion is only processed when new status is complete
if ($new_status != 'publish' && $new_status != 'complete') {
return;
}
$payment = new Give_Payment($payment_id);
$creation_date = get_post_field('post_date', $payment_id, 'raw');
$payment_meta = $payment->payment_meta;
$completed_date = $payment->completed_date;
$user_info = $payment->user_info;
$customer_id = $payment->customer_id;
$amount = $payment->total;
$price_id = $payment->price_id;
$form_id = $payment->form_id;
do_action('give_pre_complete_purchase', $payment_id);
// Ensure these actions only run once, ever
if (empty($completed_date)) {
give_record_sale_in_log($form_id, $payment_id, $price_id, $creation_date);
do_action('give_complete_form_donation', $form_id, $payment_id, $payment_meta);
}
// Increase the earnings for this form ID
give_increase_earnings($form_id, $amount);
give_increase_purchase_count($form_id);
// Clear the total earnings cache
delete_transient('give_earnings_total');
// Clear the This Month earnings (this_monththis_month is NOT a typo)
delete_transient(md5('give_earnings_this_monththis_month'));
delete_transient(md5('give_earnings_todaytoday'));
// Increase the donor's purchase stats
$customer = new Give_Customer($customer_id);
$customer->increase_purchase_count();
$customer->increase_value($amount);
give_increase_total_earnings($amount);
// Ensure this action only runs once ever
if (empty($completed_date)) {
// Save the completed date
$payment->completed_date = current_time('mysql');
$payment->save();
/**
* Fires after a donation successfully complete.
*
* @since 1.6
*
* @param int $payment_id The ID of the payment.
*/
do_action('give_complete_purchase', $payment_id);
}
}