PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
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 1.7.0.9 All 211 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.13, at includes/square/helpers/class-charitable-donation-data-mapper.php

345 lines 6.2 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 // Skip the ×100 conversion for zero-decimal currencies (JPY, KRW,
161 // VND, etc.) where Square expects the amount in the full unit.
162 if ( Charitable_Square_Gateway_Processor::is_zero_decimal_currency( $this->get_currency() ) ) {
163 return intval( $this->get_amount() );
164 }
165
166 return intval( $this->get_amount() * 100 );
167 }
168
169 /**
170 * Return the donation description.
171 *
172 * @since 1.0.0
173 *
174 * @return string
175 */
176 public function get_description() {
177 return $this->donation->get_campaigns_donated_to();
178 }
179
180 /**
181 * Return the currency used for the donation.
182 *
183 * @since 1.0.0
184 *
185 * @return string
186 */
187 public function get_currency() {
188 return charitable_get_currency();
189 }
190
191 /**
192 * Return the donation key.
193 *
194 * @since 1.0.0
195 *
196 * @return string
197 */
198 public function get_donation_key() {
199 return $this->donation->get_donation_key();
200 }
201
202 /**
203 * Return the donation ID.
204 *
205 * @since 1.0.0
206 *
207 * @return int
208 */
209 public function get_donation_id() {
210 return $this->donation->ID;
211 }
212
213 /**
214 * Return the site locale.
215 *
216 * @since 1.0.0
217 *
218 * @return string
219 */
220 public function get_locale() {
221 return get_locale();
222 }
223
224 /**
225 * Get the session ID.
226 *
227 * @since 1.0.0
228 *
229 * @return string
230 */
231 public function get_session_id() {
232 return charitable_get_session()->get_session_id();
233 }
234
235 /**
236 * Add the map to be used for the mapped data.
237 *
238 * @since 1.0.0
239 *
240 * @param array $map A key value map.
241 * @return void
242 */
243 public function add_map( $map ) {
244 $this->map = $map;
245 }
246
247 /**
248 * Return the mapped data.
249 *
250 * @since 1.0.0
251 *
252 * @return array
253 */
254 public function get_mapped_data() {
255 if ( ! isset( $this->data ) ) {
256 if ( ! isset( $this->map ) ) {
257 $this->map = $this->get_default_map();
258 }
259
260 $this->data = array();
261
262 foreach ( $this->map as $key => $gateway_key ) {
263 if ( false === strpos( $gateway_key, '.' ) ) {
264 $this->data[ $gateway_key ] = $this->$key;
265 } else {
266 $parts = array_reverse( explode( '.', $gateway_key ) );
267 $branch = $this->$key;
268
269 foreach ( $parts as $part ) {
270 $branch = array( $part => $branch );
271 }
272
273 $this->data = array_merge_recursive(
274 $this->data,
275 $branch
276 );
277 }
278 }
279 }
280
281 return $this->data;
282 }
283
284 /**
285 * Return a default map to be used if no map was defined.
286 *
287 * @since 1.0.0
288 *
289 * @return array
290 */
291 public function get_default_map() {
292 $keys = array(
293 'email',
294 'first_name',
295 'last_name',
296 'full_name',
297 'address',
298 'address_2',
299 'city',
300 'country',
301 'postcode',
302 'donation_key',
303 'donation_id',
304 'amount',
305 'currency',
306 'description',
307 'locale',
308 'return_url',
309 'cancel_url',
310 'webhook_url',
311 );
312
313 return array_combine( $keys, $keys );
314 }
315
316 /**
317 * Return some or all of the data.
318 *
319 * @since 1.0.0
320 *
321 * @param string[]|null $keys The keys to get, or null to get all data.
322 * @return array
323 */
324 public function get_data( $keys = null ) {
325 $data = $this->get_mapped_data();
326
327 if ( ! is_array( $keys ) ) {
328 return $data;
329 }
330
331 return array_intersect_key( $data, array_flip( $keys ) );
332 }
333
334 /**
335 * Return the donation object that this map was created with.
336 *
337 * @since 1.0.0
338 *
339 * @return \Charitable_Donation
340 */
341 public function get_donation() {
342 return $this->donation;
343 }
344 }
345