| 1 |
<?php |
| 2 |
/** |
| 3 |
* Donation model. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Donation |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2022, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
* @version 1.6.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Donation' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Donation Model. |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
class Charitable_Donation extends Charitable_Abstract_Donation { |
| 26 |
|
| 27 |
/** |
| 28 |
* The type of donation. |
| 29 |
* |
| 30 |
* @since 1.4.0 |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
public $donation_type = 'simple'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Process a refund. |
| 38 |
* |
| 39 |
* @since 1.0.0 |
| 40 |
* |
| 41 |
* @param float $refund_amount The amount to be refunded. |
| 42 |
* @param string $message The message to add to the donation log. |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function process_refund( $refund_amount, $message = '' ) { |
| 46 |
$refund_log = get_post_meta( $this->ID, 'donation_refund', true ); |
| 47 |
$total_refund = isset( $refund_log['total_refund'] ) ? $refund_log['total_refund'] : 0; |
| 48 |
$refunds_per_campaign = isset( $refund_log['campaign_refunds'] ) ? $refund_log['campaign_refunds'] : array(); |
| 49 |
|
| 50 |
foreach ( $this->get_campaign_donations() as $campaign_donation ) { |
| 51 |
|
| 52 |
if ( 0 == $refund_amount ) { |
| 53 |
break; |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! isset( $refunds_per_campaign[ $campaign_donation->campaign_id ] ) ) { |
| 57 |
$refunds_per_campaign[ $campaign_donation->campaign_id ] = array(); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Calculate the amount to be refunded out of this particular campaign's amount. |
| 62 |
* |
| 63 |
* This takes into account any amounts that have already been refunded, to find the |
| 64 |
* amount that remains credited towards to the campaign. |
| 65 |
*/ |
| 66 |
$campaign_remaining_amount = $campaign_donation->amount - array_sum( $refunds_per_campaign[ $campaign_donation->campaign_id ] ); |
| 67 |
|
| 68 |
if ( $campaign_remaining_amount > $refund_amount ) { |
| 69 |
$campaign_refund_amount = $refund_amount; |
| 70 |
} else { |
| 71 |
$campaign_refund_amount = $campaign_remaining_amount; |
| 72 |
} |
| 73 |
|
| 74 |
$refunds_per_campaign[ $campaign_donation->campaign_id ][] = $campaign_refund_amount; |
| 75 |
|
| 76 |
/* Reduce the remaining amount to refund. */ |
| 77 |
$refund_amount -= $campaign_refund_amount; |
| 78 |
|
| 79 |
/* Increase the total refund amount. */ |
| 80 |
$total_refund += $campaign_refund_amount; |
| 81 |
} |
| 82 |
|
| 83 |
$refund_log = array( |
| 84 |
'time' => time(), |
| 85 |
'message' => $message, |
| 86 |
'campaign_refunds' => $refunds_per_campaign, |
| 87 |
'total_refund' => $total_refund, |
| 88 |
); |
| 89 |
|
| 90 |
update_post_meta( $this->ID, 'donation_refund', $refund_log ); |
| 91 |
|
| 92 |
$this->update_status( 'charitable-refunded' ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
endif; |
| 97 |
|