1 <?php
2 /**
3 * Fees
4 *
5 * This class is for adding arbitrary fees to the cart. Fees can be positive or negative (discounts)
6 *
7 * @package EDD
8 * @subpackage Classes/Fees
9 * @copyright Copyright (c) 2012, Pippin Williamson
10 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11 * @since 1.5
12 */
13
14 // Exit if accessed directly
15 if ( ! defined( 'ABSPATH' ) ) exit;
16
17 /**
18 * EDD_Fees Class
19 *
20 * @since 1.5
21 */
22 class EDD_Fees {
23 /**
24 * Setup the EDD Fees
25 *
26 * @since 1.5
27 * @return void
28 */
29 public function __construct() {
30 add_filter( 'edd_payment_meta', array( $this, 'record_fees' ), 10, 2 );
31 }
32
33 /**
34 * Adds a new Fee
35 *
36 * @access public
37 * @since 1.5
38 * @param int $amount Fee Amount
39 * @param string $label Fee label
40 * @param string $id Fee ID
41 * @uses EDD_Fees::get_fees()
42 * @uses EDD_Session::set()
43 * @return array $fees
44 */
45 public function add_fee( $amount = '', $label = '', $id = '' ) {
46 $fees = $this->get_fees();
47
48 $key = empty( $id ) ? sanitize_key( $label ) : sanitize_key( $id );
49
50 $fees[ $key ] = array( 'amount' => $amount, 'label' => $label );
51
52 EDD()->session->set( 'edd_cart_fees', $fees );
53
54 return $fees;
55 }
56
57 /**
58 * Remove an Existing Fee
59 *
60 * @access public
61 * @since 1.5
62 * @param string $id Fee ID
63 * @uses EDD_Fees::get_fees()
64 * @uses EDD_Session::set()
65 * @return array $fees
66 */
67 public function remove_fee( $id = '' ) {
68 $fees = $this->get_fees();
69
70 if ( isset( $fees[ $id ] ) ) {
71 unset( $fees[ $id ] );
72 }
73
74 EDD()->session->set( 'edd_cart_fees', $fees );
75
76 return $fees;
77 }
78
79 /**
80 * Check if any fees are present
81 *
82 * @access public
83 * @since 1.5
84 * @uses EDD_Fees::get_fees()
85 * @return bool
86 */
87 public function has_fees() {
88 $fees = $this->get_fees();
89 return ! empty( $fees ) && is_array( $fees );
90 }
91
92 /**
93 * Retrieve all active fees
94 *
95 * @access public
96 * @since 1.5
97 * @uses EDD_Session::get()
98 * @return mixed array|bool
99 */
100 public function get_fees() {
101 $fees = EDD()->session->get( 'edd_cart_fees' );
102 return ! empty( $fees ) ? $fees : array();
103 }
104
105 /**
106 * Retrieve a specific fee
107 *
108 * @access public
109 * @since 1.5
110 * @uses EDD_Fees::get_fees()
111 * @return mixed array|bool
112 */
113 public function get_fee( $id = '' ) {
114 $fees = $this->get_fees();
115
116 if ( ! isset( $fees[ $id ] ) )
117 return false;
118
119 return $fees[ $id ];
120 }
121
122 /**
123 * Calculate the total fee amount
124 *
125 * Can be negative
126 *
127 * @access public
128 * @since 1.5
129 * @uses EDD_Fees::get_fees()
130 * @uses EDD_Fees::has_fees()
131 * @return float $total Total fee amount
132 */
133 public function total() {
134 $fees = $this->get_fees();
135 $total = (float) 0.00;
136
137 if ( $this->has_fees() ) {
138 foreach ( $fees as $fee ) {
139 $total += $fee['amount'];
140 }
141 }
142
143 return edd_sanitize_amount( $total );
144 }
145
146 /**
147 * Stores the fees in the payment meta
148 *
149 * @access public
150 * @since 1.5
151 * @uses EDD_Session::set()
152 * @param array $payment_meta The meta data to store with the payment
153 * @param array $payment_data The info sent from process-purchase.php
154 * @return array $payment_meta Return the payment meta with the fees added
155 */
156 public function record_fees( $payment_meta, $payment_data ) {
157 if ( $this->has_fees() ) {
158 $payment_meta['fees'] = $this->get_fees();
159 EDD()->session->set( 'edd_cart_fees', null );
160 }
161
162 return $payment_meta;
163 }
164 }