@@ -12,280 +12,280 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Payment_Form_Submission_Refresh_Prices { |
14 | 14 | |
15 | - /** |
|
16 | - * Contains the response for refreshing prices. |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - public $response = array(); |
|
15 | + /** |
|
16 | + * Contains the response for refreshing prices. |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + public $response = array(); |
|
20 | 20 | |
21 | 21 | /** |
22 | - * Class constructor |
|
23 | - * |
|
24 | - * @param GetPaid_Payment_Form_Submission $submission |
|
25 | - */ |
|
26 | - public function __construct( $submission ) { |
|
27 | - |
|
28 | - $this->response = array( |
|
29 | - 'submission_id' => $submission->id, |
|
22 | + * Class constructor |
|
23 | + * |
|
24 | + * @param GetPaid_Payment_Form_Submission $submission |
|
25 | + */ |
|
26 | + public function __construct( $submission ) { |
|
27 | + |
|
28 | + $this->response = array( |
|
29 | + 'submission_id' => $submission->id, |
|
30 | 30 | 'has_recurring' => $submission->has_recurring, |
31 | 31 | 'is_free' => ! $submission->should_collect_payment_details(), |
32 | - ); |
|
33 | - |
|
34 | - $this->add_totals( $submission ); |
|
35 | - $this->add_texts( $submission ); |
|
36 | - $this->add_items( $submission ); |
|
37 | - $this->add_fees( $submission ); |
|
38 | - $this->add_discounts( $submission ); |
|
39 | - $this->add_taxes( $submission ); |
|
40 | - $this->add_gateways( $submission ); |
|
41 | - $this->add_data( $submission ); |
|
42 | - |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Adds totals to a response for submission refresh prices. |
|
47 | - * |
|
48 | - * @param GetPaid_Payment_Form_Submission $submission |
|
49 | - */ |
|
50 | - public function add_totals( $submission ) { |
|
51 | - |
|
52 | - $this->response = array_merge( |
|
53 | - $this->response, |
|
54 | - array( |
|
55 | - |
|
56 | - 'totals' => array( |
|
57 | - 'subtotal' => $submission->format_amount( $submission->get_subtotal() ), |
|
58 | - 'discount' => $submission->format_amount( $submission->get_discount() ), |
|
59 | - 'fees' => $submission->format_amount( $submission->get_fee() ), |
|
60 | - 'tax' => $submission->format_amount( $submission->get_tax() ), |
|
61 | - 'total' => $submission->format_amount( $submission->get_total() ), |
|
62 | - 'raw_total' => html_entity_decode( sanitize_text_field( $submission->format_amount( $submission->get_total() ) ), ENT_QUOTES ), |
|
63 | - ), |
|
64 | - |
|
65 | - 'recurring' => array( |
|
66 | - 'subtotal' => $submission->format_amount( $submission->get_recurring_subtotal() ), |
|
67 | - 'discount' => $submission->format_amount( $submission->get_recurring_discount() ), |
|
68 | - 'fees' => $submission->format_amount( $submission->get_recurring_fee() ), |
|
69 | - 'tax' => $submission->format_amount( $submission->get_recurring_tax() ), |
|
70 | - 'total' => $submission->format_amount( $submission->get_recurring_total() ), |
|
71 | - ), |
|
72 | - |
|
73 | - 'initial_amt' => wpinv_round_amount( $submission->get_total(), null, true ), |
|
74 | - 'currency' => $submission->get_currency(), |
|
75 | - |
|
76 | - ) |
|
77 | - ); |
|
78 | - |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Adds texts to a response for submission refresh prices. |
|
83 | - * |
|
84 | - * @param GetPaid_Payment_Form_Submission $submission |
|
85 | - */ |
|
86 | - public function add_texts( $submission ) { |
|
87 | - |
|
88 | - $payable = $submission->format_amount( $submission->get_total() ); |
|
89 | - |
|
90 | - if ( $submission->has_recurring != 0 ) { |
|
91 | - |
|
92 | - $recurring = new WPInv_Item( $submission->has_recurring ); |
|
93 | - $period = getpaid_get_subscription_period_label( $recurring->get_recurring_period( true ), $recurring->get_recurring_interval(), '' ); |
|
94 | - |
|
95 | - if ( $submission->get_total() == $submission->get_recurring_total() ) { |
|
96 | - $payable = "$payable / $period"; |
|
97 | - } else { |
|
98 | - $payable = sprintf( |
|
99 | - __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
100 | - $submission->format_amount( $submission->get_total() ), |
|
101 | - $submission->format_amount( $submission->get_recurring_total() ), |
|
102 | - $period |
|
103 | - ); |
|
104 | - } |
|
105 | - |
|
106 | - } |
|
107 | - |
|
108 | - $texts = array( |
|
109 | - '.getpaid-checkout-total-payable' => $payable, |
|
110 | - ); |
|
111 | - |
|
112 | - foreach ( $submission->get_items() as $item ) { |
|
113 | - $item_id = $item->get_id(); |
|
114 | - $initial_price = $submission->format_amount( $this->standardize_price( $item->get_id(), $item->get_sub_total(), $submission->get_discount_code(), false ) ); |
|
115 | - $recurring_price = $submission->format_amount( $this->standardize_price( $item->get_id(), $item->get_recurring_sub_total(), $submission->get_discount_code(), true ) ); |
|
116 | - $texts[".item-$item_id .getpaid-form-item-price-desc"] = getpaid_item_recurring_price_help_text( $item, $submission->get_currency(), $initial_price, $recurring_price ); |
|
117 | - } |
|
118 | - |
|
119 | - $this->response = array_merge( $this->response, array( 'texts' => $texts ) ); |
|
120 | - |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Adds items to a response for submission refresh prices. |
|
125 | - * |
|
126 | - * @param GetPaid_Payment_Form_Submission $submission |
|
127 | - */ |
|
128 | - public function add_items( $submission ) { |
|
129 | - |
|
130 | - // Add items. |
|
131 | - $items = array(); |
|
32 | + ); |
|
33 | + |
|
34 | + $this->add_totals( $submission ); |
|
35 | + $this->add_texts( $submission ); |
|
36 | + $this->add_items( $submission ); |
|
37 | + $this->add_fees( $submission ); |
|
38 | + $this->add_discounts( $submission ); |
|
39 | + $this->add_taxes( $submission ); |
|
40 | + $this->add_gateways( $submission ); |
|
41 | + $this->add_data( $submission ); |
|
42 | + |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Adds totals to a response for submission refresh prices. |
|
47 | + * |
|
48 | + * @param GetPaid_Payment_Form_Submission $submission |
|
49 | + */ |
|
50 | + public function add_totals( $submission ) { |
|
51 | + |
|
52 | + $this->response = array_merge( |
|
53 | + $this->response, |
|
54 | + array( |
|
55 | + |
|
56 | + 'totals' => array( |
|
57 | + 'subtotal' => $submission->format_amount( $submission->get_subtotal() ), |
|
58 | + 'discount' => $submission->format_amount( $submission->get_discount() ), |
|
59 | + 'fees' => $submission->format_amount( $submission->get_fee() ), |
|
60 | + 'tax' => $submission->format_amount( $submission->get_tax() ), |
|
61 | + 'total' => $submission->format_amount( $submission->get_total() ), |
|
62 | + 'raw_total' => html_entity_decode( sanitize_text_field( $submission->format_amount( $submission->get_total() ) ), ENT_QUOTES ), |
|
63 | + ), |
|
64 | + |
|
65 | + 'recurring' => array( |
|
66 | + 'subtotal' => $submission->format_amount( $submission->get_recurring_subtotal() ), |
|
67 | + 'discount' => $submission->format_amount( $submission->get_recurring_discount() ), |
|
68 | + 'fees' => $submission->format_amount( $submission->get_recurring_fee() ), |
|
69 | + 'tax' => $submission->format_amount( $submission->get_recurring_tax() ), |
|
70 | + 'total' => $submission->format_amount( $submission->get_recurring_total() ), |
|
71 | + ), |
|
72 | + |
|
73 | + 'initial_amt' => wpinv_round_amount( $submission->get_total(), null, true ), |
|
74 | + 'currency' => $submission->get_currency(), |
|
75 | + |
|
76 | + ) |
|
77 | + ); |
|
78 | + |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Adds texts to a response for submission refresh prices. |
|
83 | + * |
|
84 | + * @param GetPaid_Payment_Form_Submission $submission |
|
85 | + */ |
|
86 | + public function add_texts( $submission ) { |
|
87 | + |
|
88 | + $payable = $submission->format_amount( $submission->get_total() ); |
|
89 | + |
|
90 | + if ( $submission->has_recurring != 0 ) { |
|
91 | + |
|
92 | + $recurring = new WPInv_Item( $submission->has_recurring ); |
|
93 | + $period = getpaid_get_subscription_period_label( $recurring->get_recurring_period( true ), $recurring->get_recurring_interval(), '' ); |
|
94 | + |
|
95 | + if ( $submission->get_total() == $submission->get_recurring_total() ) { |
|
96 | + $payable = "$payable / $period"; |
|
97 | + } else { |
|
98 | + $payable = sprintf( |
|
99 | + __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
100 | + $submission->format_amount( $submission->get_total() ), |
|
101 | + $submission->format_amount( $submission->get_recurring_total() ), |
|
102 | + $period |
|
103 | + ); |
|
104 | + } |
|
105 | + |
|
106 | + } |
|
107 | + |
|
108 | + $texts = array( |
|
109 | + '.getpaid-checkout-total-payable' => $payable, |
|
110 | + ); |
|
132 | 111 | |
133 | 112 | foreach ( $submission->get_items() as $item ) { |
134 | - $item_id = $item->get_id(); |
|
135 | - $items["$item_id"] = $submission->format_amount( $item->get_sub_total() ); |
|
136 | - } |
|
113 | + $item_id = $item->get_id(); |
|
114 | + $initial_price = $submission->format_amount( $this->standardize_price( $item->get_id(), $item->get_sub_total(), $submission->get_discount_code(), false ) ); |
|
115 | + $recurring_price = $submission->format_amount( $this->standardize_price( $item->get_id(), $item->get_recurring_sub_total(), $submission->get_discount_code(), true ) ); |
|
116 | + $texts[".item-$item_id .getpaid-form-item-price-desc"] = getpaid_item_recurring_price_help_text( $item, $submission->get_currency(), $initial_price, $recurring_price ); |
|
117 | + } |
|
137 | 118 | |
138 | - $this->response = array_merge( |
|
139 | - $this->response, |
|
140 | - array( 'items' => $items ) |
|
141 | - ); |
|
119 | + $this->response = array_merge( $this->response, array( 'texts' => $texts ) ); |
|
142 | 120 | |
143 | - } |
|
121 | + } |
|
144 | 122 | |
145 | - /** |
|
146 | - * Adds fees to a response for submission refresh prices. |
|
147 | - * |
|
148 | - * @param GetPaid_Payment_Form_Submission $submission |
|
149 | - */ |
|
150 | - public function add_fees( $submission ) { |
|
123 | + /** |
|
124 | + * Adds items to a response for submission refresh prices. |
|
125 | + * |
|
126 | + * @param GetPaid_Payment_Form_Submission $submission |
|
127 | + */ |
|
128 | + public function add_items( $submission ) { |
|
151 | 129 | |
152 | - $fees = array(); |
|
130 | + // Add items. |
|
131 | + $items = array(); |
|
153 | 132 | |
154 | - foreach ( $submission->get_fees() as $name => $data ) { |
|
155 | - $fees[$name] = $submission->format_amount( $data['initial_fee'] ); |
|
156 | - } |
|
133 | + foreach ( $submission->get_items() as $item ) { |
|
134 | + $item_id = $item->get_id(); |
|
135 | + $items["$item_id"] = $submission->format_amount( $item->get_sub_total() ); |
|
136 | + } |
|
157 | 137 | |
158 | - $this->response = array_merge( |
|
159 | - $this->response, |
|
160 | - array( 'fees' => $fees ) |
|
161 | - ); |
|
138 | + $this->response = array_merge( |
|
139 | + $this->response, |
|
140 | + array( 'items' => $items ) |
|
141 | + ); |
|
162 | 142 | |
163 | - } |
|
143 | + } |
|
164 | 144 | |
165 | - /** |
|
166 | - * Adds discounts to a response for submission refresh prices. |
|
167 | - * |
|
168 | - * @param GetPaid_Payment_Form_Submission $submission |
|
169 | - */ |
|
170 | - public function add_discounts( $submission ) { |
|
145 | + /** |
|
146 | + * Adds fees to a response for submission refresh prices. |
|
147 | + * |
|
148 | + * @param GetPaid_Payment_Form_Submission $submission |
|
149 | + */ |
|
150 | + public function add_fees( $submission ) { |
|
171 | 151 | |
172 | - $discounts = array(); |
|
152 | + $fees = array(); |
|
173 | 153 | |
174 | - foreach ( $submission->get_discounts() as $name => $data ) { |
|
175 | - $discounts[$name] = $submission->format_amount( $data['initial_discount'] ); |
|
176 | - } |
|
154 | + foreach ( $submission->get_fees() as $name => $data ) { |
|
155 | + $fees[$name] = $submission->format_amount( $data['initial_fee'] ); |
|
156 | + } |
|
177 | 157 | |
178 | - $this->response = array_merge( |
|
179 | - $this->response, |
|
180 | - array( 'discounts' => $discounts ) |
|
181 | - ); |
|
158 | + $this->response = array_merge( |
|
159 | + $this->response, |
|
160 | + array( 'fees' => $fees ) |
|
161 | + ); |
|
182 | 162 | |
183 | - } |
|
163 | + } |
|
184 | 164 | |
185 | - /** |
|
186 | - * Adds taxes to a response for submission refresh prices. |
|
187 | - * |
|
188 | - * @param GetPaid_Payment_Form_Submission $submission |
|
189 | - */ |
|
190 | - public function add_taxes( $submission ) { |
|
165 | + /** |
|
166 | + * Adds discounts to a response for submission refresh prices. |
|
167 | + * |
|
168 | + * @param GetPaid_Payment_Form_Submission $submission |
|
169 | + */ |
|
170 | + public function add_discounts( $submission ) { |
|
191 | 171 | |
192 | - $taxes = array(); |
|
193 | - $markup = ''; |
|
194 | - foreach ( $submission->get_taxes() as $name => $data ) { |
|
195 | - $name = sanitize_text_field( $name ); |
|
196 | - $amount = $submission->format_amount( $data['initial_tax'] ); |
|
197 | - $taxes[$name] = $amount; |
|
198 | - $markup .= "<small class='form-text'>$name : $amount</small>"; |
|
199 | - } |
|
172 | + $discounts = array(); |
|
173 | + |
|
174 | + foreach ( $submission->get_discounts() as $name => $data ) { |
|
175 | + $discounts[$name] = $submission->format_amount( $data['initial_discount'] ); |
|
176 | + } |
|
200 | 177 | |
201 | - if ( wpinv_display_individual_tax_rates() && ! empty( $taxes ) ) { |
|
202 | - $this->response['texts']['.getpaid-form-cart-totals-total-tax'] = $markup; |
|
203 | - } |
|
178 | + $this->response = array_merge( |
|
179 | + $this->response, |
|
180 | + array( 'discounts' => $discounts ) |
|
181 | + ); |
|
204 | 182 | |
205 | - $this->response = array_merge( |
|
206 | - $this->response, |
|
207 | - array( 'taxes' => $taxes ) |
|
208 | - ); |
|
183 | + } |
|
209 | 184 | |
210 | - } |
|
185 | + /** |
|
186 | + * Adds taxes to a response for submission refresh prices. |
|
187 | + * |
|
188 | + * @param GetPaid_Payment_Form_Submission $submission |
|
189 | + */ |
|
190 | + public function add_taxes( $submission ) { |
|
191 | + |
|
192 | + $taxes = array(); |
|
193 | + $markup = ''; |
|
194 | + foreach ( $submission->get_taxes() as $name => $data ) { |
|
195 | + $name = sanitize_text_field( $name ); |
|
196 | + $amount = $submission->format_amount( $data['initial_tax'] ); |
|
197 | + $taxes[$name] = $amount; |
|
198 | + $markup .= "<small class='form-text'>$name : $amount</small>"; |
|
199 | + } |
|
211 | 200 | |
212 | - /** |
|
213 | - * Adds gateways to a response for submission refresh prices. |
|
214 | - * |
|
215 | - * @param GetPaid_Payment_Form_Submission $submission |
|
216 | - */ |
|
217 | - public function add_gateways( $submission ) { |
|
201 | + if ( wpinv_display_individual_tax_rates() && ! empty( $taxes ) ) { |
|
202 | + $this->response['texts']['.getpaid-form-cart-totals-total-tax'] = $markup; |
|
203 | + } |
|
218 | 204 | |
219 | - $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
205 | + $this->response = array_merge( |
|
206 | + $this->response, |
|
207 | + array( 'taxes' => $taxes ) |
|
208 | + ); |
|
220 | 209 | |
221 | - if ( $this->response['has_recurring'] ) { |
|
210 | + } |
|
222 | 211 | |
223 | - foreach ( $gateways as $i => $gateway ) { |
|
212 | + /** |
|
213 | + * Adds gateways to a response for submission refresh prices. |
|
214 | + * |
|
215 | + * @param GetPaid_Payment_Form_Submission $submission |
|
216 | + */ |
|
217 | + public function add_gateways( $submission ) { |
|
224 | 218 | |
225 | - if ( ! wpinv_gateway_support_subscription( $gateway ) ) { |
|
226 | - unset( $gateways[ $i ] ); |
|
227 | - } |
|
219 | + $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
228 | 220 | |
229 | - } |
|
221 | + if ( $this->response['has_recurring'] ) { |
|
230 | 222 | |
231 | - } |
|
223 | + foreach ( $gateways as $i => $gateway ) { |
|
232 | 224 | |
225 | + if ( ! wpinv_gateway_support_subscription( $gateway ) ) { |
|
226 | + unset( $gateways[ $i ] ); |
|
227 | + } |
|
233 | 228 | |
234 | - $gateways = apply_filters( 'getpaid_submission_gateways', $gateways, $submission ); |
|
235 | - $this->response = array_merge( |
|
236 | - $this->response, |
|
237 | - array( 'gateways' => $gateways ) |
|
238 | - ); |
|
229 | + } |
|
239 | 230 | |
240 | - } |
|
231 | + } |
|
241 | 232 | |
242 | - /** |
|
243 | - * Standardizes prices. |
|
244 | - * |
|
245 | - * @param int $item_id |
|
246 | - * @param float $item_total |
|
247 | - * @param string $discount_code |
|
248 | - * @param bool $recurring |
|
249 | - */ |
|
250 | - public function standardize_price( $item_id, $item_total, $discount_code, $recurring = false ) { |
|
251 | 233 | |
252 | - $standardadized_price = $item_total; |
|
234 | + $gateways = apply_filters( 'getpaid_submission_gateways', $gateways, $submission ); |
|
235 | + $this->response = array_merge( |
|
236 | + $this->response, |
|
237 | + array( 'gateways' => $gateways ) |
|
238 | + ); |
|
253 | 239 | |
254 | - // Do we have a $discount_code? |
|
255 | - if ( ! empty( $discount_code ) ) { |
|
240 | + } |
|
256 | 241 | |
257 | - $discount = new WPInv_Discount( $discount_code ); |
|
242 | + /** |
|
243 | + * Standardizes prices. |
|
244 | + * |
|
245 | + * @param int $item_id |
|
246 | + * @param float $item_total |
|
247 | + * @param string $discount_code |
|
248 | + * @param bool $recurring |
|
249 | + */ |
|
250 | + public function standardize_price( $item_id, $item_total, $discount_code, $recurring = false ) { |
|
251 | + |
|
252 | + $standardadized_price = $item_total; |
|
258 | 253 | |
259 | - if ( $discount->exists() && $discount->is_valid_for_items( $item_id ) && ( ! $recurring || $discount->is_recurring() ) ) { |
|
260 | - $standardadized_price = $item_total - $discount->get_discounted_amount( $item_total ); |
|
261 | - } |
|
254 | + // Do we have a $discount_code? |
|
255 | + if ( ! empty( $discount_code ) ) { |
|
262 | 256 | |
263 | - } |
|
257 | + $discount = new WPInv_Discount( $discount_code ); |
|
264 | 258 | |
265 | - return max( 0, $standardadized_price ); |
|
259 | + if ( $discount->exists() && $discount->is_valid_for_items( $item_id ) && ( ! $recurring || $discount->is_recurring() ) ) { |
|
260 | + $standardadized_price = $item_total - $discount->get_discounted_amount( $item_total ); |
|
261 | + } |
|
266 | 262 | |
267 | - } |
|
263 | + } |
|
268 | 264 | |
269 | - /** |
|
270 | - * Adds data to a response for submission refresh prices. |
|
271 | - * |
|
272 | - * @param GetPaid_Payment_Form_Submission $submission |
|
273 | - */ |
|
274 | - public function add_data( $submission ) { |
|
265 | + return max( 0, $standardadized_price ); |
|
275 | 266 | |
276 | - $this->response = array_merge( |
|
277 | - $this->response, |
|
278 | - array( |
|
279 | - 'js_data' => apply_filters( |
|
280 | - 'getpaid_submission_js_data', |
|
281 | - array( |
|
282 | - 'is_recurring' => $this->response['has_recurring'], |
|
283 | - ), |
|
284 | - $submission |
|
285 | - ) |
|
286 | - ) |
|
287 | - ); |
|
267 | + } |
|
288 | 268 | |
289 | - } |
|
269 | + /** |
|
270 | + * Adds data to a response for submission refresh prices. |
|
271 | + * |
|
272 | + * @param GetPaid_Payment_Form_Submission $submission |
|
273 | + */ |
|
274 | + public function add_data( $submission ) { |
|
275 | + |
|
276 | + $this->response = array_merge( |
|
277 | + $this->response, |
|
278 | + array( |
|
279 | + 'js_data' => apply_filters( |
|
280 | + 'getpaid_submission_js_data', |
|
281 | + array( |
|
282 | + 'is_recurring' => $this->response['has_recurring'], |
|
283 | + ), |
|
284 | + $submission |
|
285 | + ) |
|
286 | + ) |
|
287 | + ); |
|
288 | + |
|
289 | + } |
|
290 | 290 | |
291 | 291 | } |