PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / DonationForms / DataTransferObjects / DonateControllerData.php

DonateControllerData.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/DonationForms/DataTransferObjects/DonateControllerData.php

377 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\DonationForms\DataTransferObjects;
4
5 use Exception;
6 use Give\DonationForms\Actions\GenerateDonationConfirmationReceiptUrl;
7 use Give\DonationForms\Actions\GenerateDonationConfirmationReceiptViewRouteUrl;
8 use Give\DonationForms\Models\DonationForm;
9 use Give\Donations\Models\Donation;
10 use Give\Donations\Properties\BillingAddress;
11 use Give\Donations\ValueObjects\DonationStatus;
12 use Give\Donations\ValueObjects\DonationType;
13 use Give\Framework\PaymentGateways\PaymentGateway;
14 use Give\Framework\PaymentGateways\PaymentGatewayRegister;
15 use Give\Framework\Support\ValueObjects\Money;
16 use Give\Subscriptions\Models\Subscription;
17 use Give\Subscriptions\ValueObjects\SubscriptionMode;
18 use Give\Subscriptions\ValueObjects\SubscriptionPeriod;
19 use Give\Subscriptions\ValueObjects\SubscriptionStatus;
20
21 /**
22 * @since 4.17.0 Allow dynamic properties, since custom field values are assigned by name.
23 */
24 #[\AllowDynamicProperties]
25 class DonateControllerData
26 {
27 /**
28 * @var float
29 */
30 public $amount;
31 /**
32 * @var bool|null
33 */
34 public $anonymous;
35 /**
36 * @var string
37 */
38 public $gatewayId;
39 /**
40 * @var string
41 */
42 public $currency;
43 /**
44 * @var string
45 */
46 public $firstName;
47 /**
48 * @var string
49 */
50 public $lastName;
51 /**
52 * @var string
53 */
54 public $email;
55 /**
56 * @var string|null
57 */
58 public $phone;
59 /**
60 * @var int
61 */
62 public $wpUserId;
63 /**
64 * @var int
65 */
66 public $formId;
67 /**
68 * @var string
69 */
70 public $formTitle;
71 /**
72 * @var string|null
73 */
74 public $company;
75 /**
76 * @var string|null
77 */
78 public $honorific;
79 /**
80 * @var string
81 */
82 public $originUrl;
83 /**
84 * @var string|null
85 */
86 public $embedId;
87 /**
88 * @var bool
89 */
90 public $isEmbed;
91 /**
92 * @var DonationType
93 */
94 public $donationType;
95 /**
96 * @var SubscriptionPeriod|null
97 */
98 public $subscriptionPeriod;
99 /**
100 * @var int|null
101 */
102 public $subscriptionFrequency;
103 /**
104 * @var int|null
105 */
106 public $subscriptionInstallments;
107 /**
108 * @var string|null
109 */
110 public $country;
111 /**
112 * @var string|null
113 */
114 public $address1;
115 /**
116 * @var string|null
117 */
118 public $address2;
119 /**
120 * @var string|null
121 */
122 public $city;
123 /**
124 * @var string|null
125 */
126 public $state;
127 /**
128 * @var string|null
129 */
130 public $zip;
131 /**
132 * @var string|null
133 */
134 public $comment;
135 /**
136 * @var string|null
137 */
138 public $levelId;
139
140 /**
141 * @since 4.16.5 Added levelId property
142 * @since 3.9.0 Added phone property
143 * @since 3.2.0 added honorific property
144 * @since 3.0.0
145 */
146 public function toDonation(int $donorId): Donation
147 {
148 $form = $this->getDonationForm();
149
150 return new Donation([
151 'status' => DonationStatus::PENDING(),
152 'gatewayId' => $this->gatewayId,
153 'amount' => $this->amount(),
154 'anonymous' => $this->anonymous,
155 'donorId' => $donorId,
156 'honorific' => $this->honorific,
157 'firstName' => $this->firstName,
158 'lastName' => $this->lastName,
159 'email' => $this->email,
160 'phone' => $this->phone,
161 'formId' => $this->formId,
162 'formTitle' => $form->title,
163 'company' => $this->company,
164 'comment' => $this->comment,
165 'type' => DonationType::SINGLE(),
166 'billingAddress' => $this->getBillingAddress(),
167 'levelId' => $this->levelId,
168 ]);
169 }
170
171 /**
172 * @since 3.9.0 Added phone property
173 * @since 3.0.0
174 */
175 public function toInitialSubscriptionDonation(int $donorId, int $subscriptionId): Donation
176 {
177 $form = $this->getDonationForm();
178
179 return new Donation([
180 'status' => DonationStatus::PENDING(),
181 'gatewayId' => $this->gatewayId,
182 'amount' => $this->amount(),
183 'anonymous' => $this->anonymous,
184 'donorId' => $donorId,
185 'firstName' => $this->firstName,
186 'lastName' => $this->lastName,
187 'email' => $this->email,
188 'phone' => $this->phone,
189 'formId' => $this->formId,
190 'formTitle' => $form->title,
191 'company' => $this->company,
192 'comment' => $this->comment,
193 'type' => DonationType::SUBSCRIPTION(),
194 'subscriptionId' => $subscriptionId,
195 'billingAddress' => $this->getBillingAddress(),
196 'levelId' => $this->levelId,
197 ]);
198 }
199
200 /**
201 * @since 3.16.0 Added "givewp_donation_confirmation_page_redirect_enabled" filter
202 * @since 3.0.0
203 */
204 public function getSuccessUrl(Donation $donation): string
205 {
206 $form = $this->getDonationForm();
207
208 if (apply_filters('givewp_donation_confirmation_page_redirect_enabled', $form->settings->enableReceiptConfirmationPage, $donation->formId)) {
209 return $this->getDonationConfirmationPageFromSettings($donation);
210 }
211
212 return $this->isEmbed ?
213 $this->getDonationConfirmationReceiptUrl($donation) :
214 $this->getDonationConfirmationReceiptViewRouteUrl($donation);
215 }
216
217 /**
218 * @since 3.0.0
219 *
220 * TODO: add params to route for flash message
221 */
222 public function getCancelUrl(): string
223 {
224 return $this->originUrl;
225 }
226
227 /**
228 * @since 3.0.0
229 */
230 public function getDonationConfirmationReceiptViewRouteUrl(Donation $donation): string
231 {
232 return (new GenerateDonationConfirmationReceiptViewRouteUrl())($donation->purchaseKey);
233 }
234
235 /**
236 * @since 3.0.0
237 */
238 public function getDonationConfirmationReceiptUrl(Donation $donation): string
239 {
240 return (new GenerateDonationConfirmationReceiptUrl())($donation, $this->originUrl, $this->embedId);
241 }
242
243 /**
244 * @since 3.16.0
245 */
246 public function getDonationConfirmationPageFromSettings(Donation $donation): string
247 {
248 $settings = give_get_settings();
249
250 $page = isset($settings['success_page'])
251 ? get_permalink(absint($settings['success_page']))
252 : get_bloginfo('url');
253
254 $page = apply_filters('givewp_donation_confirmation_page_redirect_permalink', $page, $donation->formId);
255
256 return esc_url_raw(add_query_arg(['receipt-id' => $donation->purchaseKey], $page));
257 }
258
259 /**
260 * @since 3.0.0
261 */
262 public function getDonationForm(): DonationForm
263 {
264 return DonationForm::find($this->formId);
265 }
266
267 /**
268 * This is a hard-coded way of filtering through our dynamic properties
269 * and only returning custom fields.
270 *
271 * TODO: figure out a less static way of doing this
272 *
273 * @since 3.0.0
274 */
275 public function getCustomFields(): array
276 {
277 $properties = get_object_vars($this);
278
279 return array_filter($properties, static function ($param) {
280 return !in_array(
281 $param,
282 array_merge(
283 Donation::propertyKeys(),
284 Subscription::propertyKeys(),
285 [
286 'currency',
287 'wpUserId',
288 'honorific',
289 'originUrl',
290 'isEmbed',
291 'embedId',
292 'donationType',
293 'subscriptionPeriod',
294 'subscriptionFrequency',
295 'subscriptionInstallments',
296 'country',
297 'address1',
298 'address2',
299 'city',
300 'state',
301 'zip',
302 ]
303 ),
304 true
305 );
306 }, ARRAY_FILTER_USE_KEY);
307 }
308
309 /**
310 * @since 3.0.0
311 *
312 * @throws Exception
313 */
314 public function toSubscription(int $donorId): Subscription
315 {
316 return new Subscription([
317 'amount' => $this->amount(),
318 'period' => $this->subscriptionPeriod,
319 'frequency' => (int)$this->subscriptionFrequency,
320 'donorId' => $donorId,
321 'installments' => (int)$this->subscriptionInstallments,
322 'status' => SubscriptionStatus::PENDING(),
323 'mode' => give_is_test_mode() ? SubscriptionMode::TEST() : SubscriptionMode::LIVE(),
324 'donationFormId' => $this->formId,
325 ]);
326 }
327
328 /**
329 * @return Money
330 */
331 public function amount(): Money
332 {
333 return Money::fromDecimal($this->amount, $this->currency);
334 }
335
336 /**
337 * @since 3.0.0
338 */
339 public function getGateway(): PaymentGateway
340 {
341 return give(PaymentGatewayRegister::class)->getPaymentGateway($this->gatewayId);
342 }
343
344 /**
345 * @since 3.0.0
346 */
347 public function getBillingAddress(): BillingAddress
348 {
349 return BillingAddress::fromArray([
350 'country' => $this->country,
351 'address1' => $this->address1,
352 'address2' => $this->address2,
353 'city' => $this->city,
354 'state' => $this->state,
355 'zip' => $this->zip,
356 ]);
357 }
358
359 /**
360 * @since 3.0.0
361 */
362 public function has(string $name): bool
363 {
364 return isset($this->{$name});
365 }
366
367 /**
368 * @since 3.0.0
369 *
370 * @return mixed|null
371 */
372 public function get(string $name)
373 {
374 return property_exists($this, $name) ? $this->{$name} : null;
375 }
376 }
377