PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.11.1
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.11.1
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / square / helpers / class-charitable-donation-data-mapper.php

class-charitable-donation-data-mapper.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.11.1, at includes/square/helpers/class-charitable-donation-data-mapper.php

339 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A class responsible for providing convenient access donation
4 * data, including the ability to structure it in a formatted way
5 * for different payment gateways.
6 *
7 * @package Charitable/Classes/Charitable_Donation_Data_Mapper
8 * @author Eric Daams
9 * @copyright Copyright (c) 2022, Studio 164a
10 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11 * @since 1.0.0
12 * @version 1.0.0
13 */
14
15 // namespace Charitable\Helpers;
16
17 // Exit if accessed directly.
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit;
20 }
21
22 /**
23 * Data Mapper helper.
24 *
25 * @since 1.0.0
26 */
27 class Charitable_Square_Donation_Data_Mapper {
28
29 /**
30 * Donation.
31 *
32 * @since 1.0.0
33 *
34 * @var \Charitable_Donation
35 */
36 private $donation;
37
38 /**
39 * Data map.
40 *
41 * @since 1.0.0
42 *
43 * @var array
44 */
45 private $map;
46
47 /**
48 * The mapped data, with the payment gateway's keys
49 * and the data from the donation.
50 *
51 * @since 1.0.0
52 *
53 * @var array
54 */
55 private $data;
56
57 /**
58 * Create class object.
59 *
60 * @since 1.0.0
61 */
62 public function __construct( \Charitable_Donation $donation ) {
63 $this->donation = $donation;
64 }
65
66 /**
67 * Return donation properties.
68 *
69 * @since 1.0.0
70 *
71 * @param string $prop The property to retrieve.
72 * @return mixed
73 */
74 public function __get( $prop ) {
75 $method = 'get_' . $prop;
76
77 if ( method_exists( $this, $method ) ) {
78 return call_user_func( array( $this, $method ) );
79 }
80
81 return $this->donation->get( $prop );
82 }
83
84 /**
85 * Get the full name of the donor.
86 *
87 * @since 1.0.0
88 *
89 * @return string
90 */
91 public function get_full_name() {
92 return $this->donation->get( 'donor' )->get_name();
93 }
94
95 /**
96 * Get the URL that the donor should be returned to after
97 * completing their donation.
98 *
99 * @since 1.0.0
100 *
101 * @return string
102 */
103 public function get_return_url() {
104 return charitable_get_permalink(
105 'donation_receipt_page',
106 array( 'donation_id' => $this->donation->ID )
107 );
108 }
109
110 /**
111 * Get the URL that the donor should be returned to
112 * if they cancel their donation.
113 *
114 * @since 1.0.0
115 *
116 * @return string
117 */
118 public function get_cancel_url() {
119 return charitable_get_permalink(
120 'donation_cancel_page',
121 array( 'donation_id' => $this->donation->ID )
122 );
123 }
124
125 /**
126 * Return the URL that the gateway should send its
127 * webhook to.
128 *
129 * @since 1.0.0
130 *
131 * @return string
132 */
133 public function get_webhook_url() {
134 return charitable_get_ipn_url( $this->donation->get_gateway() );
135 }
136
137 /**
138 * Return the sanitized donation amount.
139 *
140 * @since 1.0.0
141 *
142 * @return int
143 */
144 public function get_amount() {
145 if ( $this->donation->get( 'cover_fees' ) ) {
146 return \Charitable_Currency::get_instance()->cast_to_decimal_format( $this->donation->get( 'total_donation_with_fees' ) );
147 }
148
149 return $this->donation->get_total_donation_amount( true );
150 }
151
152 /**
153 * Return the donation amount in cents.
154 *
155 * @since 1.0.0
156 *
157 * @return int
158 */
159 public function get_amount_in_cents() {
160 return intval( $this->get_amount() * 100 );
161 }
162
163 /**
164 * Return the donation description.
165 *
166 * @since 1.0.0
167 *
168 * @return string
169 */
170 public function get_description() {
171 return $this->donation->get_campaigns_donated_to();
172 }
173
174 /**
175 * Return the currency used for the donation.
176 *
177 * @since 1.0.0
178 *
179 * @return string
180 */
181 public function get_currency() {
182 return charitable_get_currency();
183 }
184
185 /**
186 * Return the donation key.
187 *
188 * @since 1.0.0
189 *
190 * @return string
191 */
192 public function get_donation_key() {
193 return $this->donation->get_donation_key();
194 }
195
196 /**
197 * Return the donation ID.
198 *
199 * @since 1.0.0
200 *
201 * @return int
202 */
203 public function get_donation_id() {
204 return $this->donation->ID;
205 }
206
207 /**
208 * Return the site locale.
209 *
210 * @since 1.0.0
211 *
212 * @return string
213 */
214 public function get_locale() {
215 return get_locale();
216 }
217
218 /**
219 * Get the session ID.
220 *
221 * @since 1.0.0
222 *
223 * @return string
224 */
225 public function get_session_id() {
226 return charitable_get_session()->get_session_id();
227 }
228
229 /**
230 * Add the map to be used for the mapped data.
231 *
232 * @since 1.0.0
233 *
234 * @param array $map A key value map.
235 * @return void
236 */
237 public function add_map( $map ) {
238 $this->map = $map;
239 }
240
241 /**
242 * Return the mapped data.
243 *
244 * @since 1.0.0
245 *
246 * @return array
247 */
248 public function get_mapped_data() {
249 if ( ! isset( $this->data ) ) {
250 if ( ! isset( $this->map ) ) {
251 $this->map = $this->get_default_map();
252 }
253
254 $this->data = array();
255
256 foreach ( $this->map as $key => $gateway_key ) {
257 if ( false === strpos( $gateway_key, '.' ) ) {
258 $this->data[ $gateway_key ] = $this->$key;
259 } else {
260 $parts = array_reverse( explode( '.', $gateway_key ) );
261 $branch = $this->$key;
262
263 foreach ( $parts as $part ) {
264 $branch = array( $part => $branch );
265 }
266
267 $this->data = array_merge_recursive(
268 $this->data,
269 $branch
270 );
271 }
272 }
273 }
274
275 return $this->data;
276 }
277
278 /**
279 * Return a default map to be used if no map was defined.
280 *
281 * @since 1.0.0
282 *
283 * @return array
284 */
285 public function get_default_map() {
286 $keys = array(
287 'email',
288 'first_name',
289 'last_name',
290 'full_name',
291 'address',
292 'address_2',
293 'city',
294 'country',
295 'postcode',
296 'donation_key',
297 'donation_id',
298 'amount',
299 'currency',
300 'description',
301 'locale',
302 'return_url',
303 'cancel_url',
304 'webhook_url',
305 );
306
307 return array_combine( $keys, $keys );
308 }
309
310 /**
311 * Return some or all of the data.
312 *
313 * @since 1.0.0
314 *
315 * @param string[]|null $keys The keys to get, or null to get all data.
316 * @return array
317 */
318 public function get_data( $keys = null ) {
319 $data = $this->get_mapped_data();
320
321 if ( ! is_array( $keys ) ) {
322 return $data;
323 }
324
325 return array_intersect_key( $data, array_flip( $keys ) );
326 }
327
328 /**
329 * Return the donation object that this map was created with.
330 *
331 * @since 1.0.0
332 *
333 * @return \Charitable_Donation
334 */
335 public function get_donation() {
336 return $this->donation;
337 }
338 }
339